-
Notifications
You must be signed in to change notification settings - Fork 0
admin_helper.class.php
For me one of the most painful things ever is to create boring admin interfaces with boring forms and tons of lines of validation. With No-BS' admin_helper.class.php this becomes really easy.
The repo's /admin directory contains an example on how to use admin_helper but here is a step-by-step explanation. Again, No-BS is built in a modular way so if you don't want this just get rid of the /admin directory and the admin_helper.class.php from the /helpers directory
admin_helper uses the following open source tools: Bootstrap, FontAwesome and AJAX Upload by Andris Valums
After including admin_helper.class.php in your admin's index.php you can define an $admin_menu associative array. Here is an example
$admin_menu = array();
$admin_menu['dashboard'] = array("menu" => "dashboard", "url" => "index.php?menu=dashboard", "title" => "Dashboard", "icon" => "fa fa-home",
"submenus" => array(
));
$admin_menu['example'] = array("menu" => false, "url" => false, "title" => "Examples", "icon" => "fa fa-user",
"submenus" => array(
"example_form" => array("title" => "Forms example", "url" => "index.php?menu=example_form", "type" => "normal", "icon" => ""),
"example_sub2" => array("title" => "Example menu #2", "url" => "index.php?menu=example_sub2", "type" => "normal", "icon" => "")
));
Once this array is defined you can call
$admin_helper->renderMenu($admin_menu, "menu_you_are_on");
To render the menu in your admin area. Magic. At the moment it only supports two levels deep. Sorry
Rendering forms is a pain. Not with No-BS. Remember that the schema in model.class.php had all those validation rules? They are not just useful for validation but for rendering a form too
You can render a form of you Product class (stored in $product) like this
$admin_helper->renderForm($product,
array("thumbnail", "title", "category_id", "description_short", "description_long", "price", "recurring"),
"form-horizontal",
$buttons,
$params,
$errors,
"");
The 1st parameter is your class. The 2nd is the fields (in order) you wish to render. Obviously you don't want to render an input field for "id" for example. The 3rd is the form's class. Since its a bootstrap admin i'm using "form-horizontal" in this example The 4th is a string containing html buttons. Sometimes you want to add hidden parameters here. The 5th is the parameters passed in. It is useful when your form is already submitted but there was an error. This way admin_helper can pre-fill the submitted data. It should be an array keyed by the field names The 6th is list of errors keyed by the field names The 7th is a string of html you wish to include in the form before the buttons
As you can see with admin_helper you can build forms with validation in literally minutes. And yes, it does support file/image upload and passwords.
There are other useful utils in this class but I'm being lazy