Page 1 of 2

Best Way To Do Admin Sections on Sites

Posted: Wed Jun 03, 2009 11:55 pm
by volomike
I wanted to kick off a roundtable here on what people think would be the best way to do an admin section on a custom site they have built, and especially in an MVC way. I implemented something using MVC and a generator batch script, turning out CRUD pages on domain objects, but what was disappointing was that there was a lot of common code that could use some refactoring, yet not refactor it such that I can't make a domain object do things a little bit more special.

Minimum considerations for these admin systems are:

* a login form/logout page
* a main dashboard
* a sidebar menu (on the left) split out by main groups, followed by domain objects, followed by CRUD, followed by special CRUD (like Reset Password). For instance:

Employers (main grouping)
- Employer Profiles (a domain object)
- New Employer Profile
- Search/View Employer Profile...
- Search/Edit Employer Profile...
- Search/Delete Employer Profile...
- Search/Activate/Deactivate Employer Profile... (custom CRUD that copies Edit Employer Profile and only focuses on one field)

(For me, I just use a control on the left sidebar menu at the top like "< Employers >" where you click the left and right buttons to switch between main groups like Employers, Customers, etc. Then, it displays a jQuery Accordion Menu below that per the main group context, so I might see Employer Accounts, Employer Settings, and Employer Profiles. When you expand that, it shows the CRUD menus beneath it. You click a given CRUD menu item and on the right pane you see either a search form, a locked view form, or some kind of edit form, and usually never anything else.)

* a paginated search form and result grid
* CRUD forms
* an inline DIV dialog in the right pane that lets you search for something and match it to a CRUD form that's shown in the right pane. For instance, let's say I want to select job categories desired by a candidate for their job preference, such as Jack Mason wanting jobs dealing with Aviation or Cooking.

I guess what the discussion really is would be what the best design patterns you think would be from the Gang of Four and/or EAI patterns. I mean, ultimately I want to be able to implement the admin system just as fast as I can, without needing to use a batch generation script (that basically speeds up my cut and paste but makes it hard to extend and doesn't solve the problem of using too much cut and paste).

As well, I think the idea of the "View" may get the axe here in this case because admin systems really don't have a heavy focus on a template that you can revise like the main site might have. I mean, clients would be inclined to stick with a page template that wasn't the most exciting thing in the world, but at least worked and seemed intuitive. So for instance, you might have interaction in your model objects just start emitting XHTML, bypassing the view stuff, just to make it easier to knock out an admin system and with far less cut and paste. So, without having to deal with page templates, it potentially means a faster implementation? Although, this is another talking point on whether that's a good idea or not.

Then again, perhaps I'm asking for way too much here.

Re: Best Way To Do Admin Sections on Sites

Posted: Thu Jun 04, 2009 12:43 am
by allspiritseve
volomike wrote:what was disappointing was that there was a lot of common code that could use some refactoring
I'm sort of in the same situation, so this should be interesting.
volomike wrote:* a paginated search form and result grid
* CRUD forms
Do you automate your CRUD forms? I currently code them by hand. I typically don't mind, but the most annoying thing is needing to append an error class to a certain field. That usually ends up with a bunch of ugly if statements, and I would love to find something more elegant. Selecting the current item in a dropdown list is another annoying one that begs to be automated.
I'd also like to add Image/File handling to the list... I posted a thread a couple of weeks ago with ideas about how to shorten the amount of code needed to add image upload capabilities to any CRUD form, but haven't had a chance to look at it in depth with school and all.

Re: Best Way To Do Admin Sections on Sites

Posted: Thu Jun 04, 2009 1:38 am
by volomike
Thinking this through a little more, I think I might would want views instead of models emitting XHTML, but not in a 1:1 context of a given route to a given view like I might want on a main site. Instead, I might use views in a more basic context that handles these views:

- Login Page
- Logout Page
- Main Dashboard Frameset*
- Menu Frame on the left (jQuery Accordion based with a main grouping switcher at the top)
- Content Frame on the right
- Search Form with Results Grid (fits inside the content frame)
- CRUD --> New Record form base (again, content frame)
- CRUD --> Edit Record form base (content frame)
- CRUD --> View Record form base (content)
- CRUD --> Delete Record form base (content)
- Report Form with Results Report (content)
- Inline Lookup DIV (content frame -- used for a [...] button on a field to provide a search/results grid for adding data to a field)

(*Frameset -- I like the idea of a frameset because (a) it's not on the main site, so it's not uncool; (b) it works well for this context; (c) many sites are implemented on shared hosting arrangements and need fast response times (especially in emergencies), so they could stand to benefit from an admin system using frames so that the menus don't have to be redrawn with every click.)

As far as views, that's all I think I would need for any admin system on any website unless you can think of any more.

Then, for form drawing, I might use something vaguely like:

Code: Select all

$oForm = /* some sort of view or model might handle this */;
$oForm->addField($sLabel, $sName, $sType, $sValue, $sSize, $sMaxSize, $sColumn, $sColumnWidth, $bRequired);
$oForm->addField($sLabel, $sName, $sType, $sValue, $sSize, $sMaxSize, $sColumn, $sColumnWidth, $bRequired);
$oForm->addField($sLabel, $sName, $sType, $sValue, $sSize, $sMaxSize, $sColumn, $sColumnWidth, $bRequired);
...
View::addVar('FORM',$oForm->renderView());
View::displayView('Admin/EditForm');
[/size]

Re: Best Way To Do Admin Sections on Sites

Posted: Thu Jun 04, 2009 4:27 am
by Paul Arnold
http://vailo.wordpress.com/2008/07/23/php-form-class-with-jquery/
This forms class is pretty nice. Uses a bit of jQuery for some client side functionality but can easliy be extended for server side validation too.

Re: Best Way To Do Admin Sections on Sites

Posted: Thu Jun 04, 2009 9:28 am
by volomike
Paul Arnold wrote:http://vailo.wordpress.com/2008/07/23/php-form-class-with-jquery/
This forms class is pretty nice. Uses a bit of jQuery for some client side functionality but can easliy be extended for server side validation too.
Hey, thanks! :)

Re: Best Way To Do Admin Sections on Sites

Posted: Thu Jun 04, 2009 10:53 am
by volomike
So currently thus far, I would be inclined to use MVC, and not drop the V in that ( such as using XHTML in the models). But the views (except for login/logout/dashboard/menu system) would be generic in this case and not have fields in them. Instead, the fields would be generated I guess by a forms class and then inserted into the view template. So getting the screens drawn would be fairly a snap.

Then, there is the posting. This usually means:

- captcha check if that was used
- non-captcha check if that was used instead (a non-captcha is just a means of tricks to prevent typical hackers submitting a valid form)
- field read into variables
- file upload pull if that was used
- smart image resizing/cropping if an image upload was performed, along with other image size checks, image validity checks, etc.
- scrubbing them for XSS and SQL injection attacks
- validating the data in those variables (min length, max length, data type, etc.)
- perhaps checking values against a database to ensure there's a match
- perhaps translating a value in the database with an ID
- posting an error response or posting something to the database (or dropping an auth session var or auth session cookie and redirecting, such as in a login)

Lately the way I've been handling things is that I have gotten away from the typical form post. I now do all my form posting with jQuery and AJAX, and I use the Malsup jQuery Form Plugin. What I like about this tool is that I can handle AJAX without needing JSON creation/parsing. Instead, I handle it via the typical $_POST iteration. Sweet!

So, I validate on the server and fade in an error response without the user leaving the page. (BTW, if you ever use AJAX for form posting, note that it has a special rule when using http vs. https. If you are posting to something that is https, then the form you loaded must also be https. Otherwise, you won't get a response back and you'll timeout in the AJAX.) The cool thing about this is that validation runs fast, doesn't refresh the page, and I have more validation power (via server-side checks) than I would with mere client-side checks. And no JSON.

So, if you look at your page controller for this AJAX form posting and validation, you're looking at a lot of these that will look very similar but have some changes. And you'll end up with a lot of cut, paste, and edit. What is needed here is a set of tool functions to remove the repetitiveness out of it. One avenue is to create a generic form validation class so that you can do something like this:

Code: Select all

 
$oVForm = /* some sort of model class or helper class to create this */
$oVForm->addType('Login');
$oVForm->addFields($_POST);
$sErr = $oVForm->validateForm();
if ($sErr) {
  die($sErr);
}
//...otherwise, proceed...
This might make your page controllers thinner, and might reduce at least some slight amount of the cut and paste on form validation and response.

Re: Best Way To Do Admin Sections on Sites

Posted: Thu Jun 04, 2009 12:49 pm
by volomike
Then comes getting data to/from the database. To me, two simple classes can handle database interaction. One is a DB class that extends PDO, like so:

Code: Select all

class DB extends PDO {
//extra stuff goes here
}
 
Two is where I might add a getRecord() class method in this DB class and this generates an object from an ActiveRecord-style class so that I can set properties on it for fields and then do .insertRecord(), .updateRecord(), or .saveRecord() (the latter of which figures out whether it should insert or update) on that object.

You can handle pagination either with a separate class or right there in your DB class, as well as keyword lookup, or removing noise words from a set of text so that you can post keywords, and other common methods you might need.

But even with this in place -- is there a way to optimize not having to do so much typing or cut/paste? I've thought this through and I just don't think there is any other more optimal way.

Re: Best Way To Do Admin Sections on Sites

Posted: Thu Jun 04, 2009 12:59 pm
by volomike
I think ultimately you'll have to get all this working with like one table. Then you'll stand back a bit and look at a way to optimize that code so it's the least amount of code possible and uses generic classes to help speed up the repeatability. Next, you could write a script that analyzes a table and generates all the code for you for a domain object, but with the exception that all foreign keys would simply not be traversed and would simply be an ID field. It's kind of a 60% effort.

At that point you just point it at your tables, let it do its thing, and then you come back and finish the remaining 40% by switching foreign key ID fields on the forms with a kind of "chooser" field (where one can search for a record with a modal inline DIV that uses jQuery and AJAX), or a popdown listbox, whatever suits the need best.

Re: Best Way To Do Admin Sections on Sites

Posted: Thu Jun 04, 2009 1:57 pm
by Christopher
I'm a little confused. Is this about a whole Admin section or a specific CRUD page?

Re: Best Way To Do Admin Sections on Sites

Posted: Thu Jun 04, 2009 9:20 pm
by volomike
A whole admin section.

Re: Best Way To Do Admin Sections on Sites

Posted: Thu Jun 04, 2009 9:41 pm
by alex.barylski
Mike: Do you mean something like phpMyAdmin or something more polished?

Obviously the more polished you make it the more manual labour it will require. Generating a FORM from a database isn't rocket science so why not do somehting like PMA but instead of dynamically do it in a build script to generate the MVC components for admin then tewak them as required?

Use a FORM generator and explain to clients that design is limited.

You could alternatively use SugarCRM and it's modular backend to quickly hack somehting out and strip extraneous modules...same thing with Joomla backend.

Warning! Both are crazy detailed and very complex and tons of bloat but they get the job done much quicker once you have the learning curve behind you. Besides if clients are to cheap to pay full price give them what money will buy them...

Fast, Good, Cheap....pick two :P

Re: Best Way To Do Admin Sections on Sites

Posted: Thu Jun 04, 2009 9:47 pm
by allspiritseve
PCSpectra wrote:Fast, Good, Cheap....pick two :P
I saw this explained somewhere as a triangle: time, quality, and cost. If you need a site quicker, either quality will decrease or cost will increase. If you need a cheap site, it might take less time but quality will be low. Finally, if you want a quality site, it may take more time and/or cost more.

Re: Best Way To Do Admin Sections on Sites

Posted: Fri Jun 05, 2009 2:22 am
by Christopher
volomike wrote:A whole admin section.
It seem like there are a number of parts to this, some common to many PHP apps, others admin section specific.

- There are the Authentication page(s) (and the implied datasources behind it).

- There is the Access Control system that controls which pages authenticated users can see (and the implied datasources behind it).

- There is the interface itself. It might be nice to have a functional layout with places for everything an Admin section needs. It should be easy to customize the CSS to make it look generally like the main site it is administrating. I usually make them look different so people know where they are.

- Some system to do single table CRUD, which is the most common thing needed.

- Some system to do multi-table CRUD (or any other complex admin thingy), with at least a template for implementing solutions.

- Some basic reporting system that have a form to select criteria and then pops-up a HTML report. PDF versions of the reports would be nice.

Re: Best Way To Do Admin Sections on Sites

Posted: Fri Jun 05, 2009 5:15 am
by volomike
Mike: Do you mean something like phpMyAdmin or something more polished?
Something more polished, but not too polished. See attached images.
New Employer
New Employer
new_employer.jpg (31.01 KiB) Viewed 3332 times
Search Employer
Search Employer
search_employer.jpg (45.69 KiB) Viewed 3332 times

Re: Best Way To Do Admin Sections on Sites

Posted: Fri Jun 05, 2009 8:40 pm
by alex.barylski
Nice...is that your own design?