logo live demo | class source | download zip | email me
current version: 2012-01-06

What is Lazy Mofo (LM)?

LM is a class similar to PHPGrid. LM is a meta driven datagrid and form generator for PHP 5 and MySQL 5.
What does LM do?

What's Different & Better From other Grids

What Lazy Mofo is useful for

Given that LM is meta driven and dynamic, it's not particularly efficient or secure. LM is best suited for admin/staff area of websites where there is less traffic and users are more trusted. Caution, LM uses the "experimental" PDO method getColumnMeta. It's not clear if this will be supported in future versions of PHP.

Requirements

PHP 5 and MySQL 5
Magic Quotes should be turned off
PDO MySQL module installed for PHP
Database table must have a primary key identity

Built-In Controller

This class has a built-in controller method to call its methods. Usage of the contoller is optional. form(), grid(), sql_insert(), sql_update(), sql_update_grid(), sql_delete() can all be called manually.

 Example: $lm->run();

Example 1 - Basic Usage

// create LM object, pass in PDO connection
$lm = new lazy_mofo($dbh); 

// table name for updates, inserts and deletes
$lm->table = 'market';

// identity / primary key column name
$lm->identity_name = 'market_id';

// use the lm controller 
$lm->run();

Example 2 - Define Forms and Grid with SQL

// create LM object, pass in PDO connection
$lm = new lazy_mofo($dbh); 


// table name for updates, inserts and deletes
$lm->table = 'market';


// identity / primary key column name
$lm->identity_name = 'market_id';


// make friendly names
$lm->rename = array('country_id' => 'Country');


// define input controls on the form
$lm->form_input_control = array('photo' => '--image', 'is_active' => '--checkbox', 'country_id' => 'select country_id as val, country_name as opt from country; --select');


// define editable input controls on the grid
$lm->grid_input_control = array('is_active' => '--checkbox');


// define output control on the grid; make email clickable and the photo a clickable link
$lm->grid_output_control = array('contact_email' => '--email', 'photo' => '--image');


// query for grid(). if the last column selected is the primary key identity, then the [edit] and [delete] links are displayed
$lm->grid_sql = "select m.market_name, m.photo, m.contact_email, c.country_name, m.is_active, m.create_date, m.notes, market_id from market m left join country c on m.country_id = c.country_id";


// query for form()
$lm->form_sql = 'select * from market where market_id = :market_id';
$lm->form_sql_param = array(':market_id' => intval($_REQUEST['market_id']));


// use the lm controller
$lm->run();

Input and Output Controls - define how a field in rendered

Input and Output Controls are associative arrays used to define how to render input or output for a field. Inputs render form inputs like: text, checkbox, radio, etc. Outputs render: text, links, and images.

Example: $lm->form_input_control = array('client_pic' => '--image','pdf' => '--document', 'weird_data' => '--my_user_function', 'will_you_attend' => "select 1 as key, 'Yes' as val union select 0, 'No' union select 3, 'Maybe'; --radio");

Auto Populated Controls

If auto_populate_controls = true, get_columns() will populate form_input_control and grid_output_control with --date, --datetime, --number and --textarea according to meta data.

Input Commands Preceeded by SQL to Populate Data: --select, --checkbox, --radio

--select, --checkbox, --radio can be preceeded by a two column sql statement to popuplate the control. Note: a space required after the sql statement.
Example: $lm->form_input_control = array('country_id' => 'select country_id, country_name from country; --select');

Commands for Input Controls - form_input_control and grid_input_control arrays

Command Description
--my_input_controldefine your own function and return any HTML. example: function my_input_control($column_name, $value, $len, $command, $called_from)
--texttext input (default)
--passwordpassword input
--numbertext input for number, when cast numbers are filtered through restricted_numeric_input pattern.
--datetext input, date is formatted according to public $date_format variable
--datetimetext input, date is formatted according to public $date_format variable
--textareatextarea input
--readonlyplain text (not an input, just displays field)
--imagefile input for uploading, if image exists then image is displayed with 'delete' checkbox.
This input is not available for use on the grid() at this time.
--documentfile input for uploading, if document exists then display link with 'delete' checkbox.
This input is not available for use on the grid() at this time.
[sql ]--selectselect dropdown
[sql ]--selectmultipleselect dropdown with multiple options. values are stored in a delimited list.
[sql ]--checkboxinput checkboxes. values are stored in a delimited list.
[sql ]--radioradio buttons

Commands for Output Control - grid_output_control arrays

Command Description
--my_output_controldefine your own function and return any HTML. example: function my_output_control($column_name, $value, $len, $command, $called_from)
--textoutputs plain text (default)
--dateoutputs date according to date_out setting
--datetimeoutputs datetime according to datetime_out setting
--emailoutputs a clickable email link
--imageoutputs a clickable link to the image, or display image if grid_show_images = true
--documentoutputs a clickable link to the document
--htmloutputs html without tags or formatting

Adding server-side validation

When using the built-in controller, server-side validation can be added by defining user function names in insert_user_function, and update_user_function, update_grid_user_function.
Example: 

$lm->insert_user_function = 'my_validate';
$lm->update_user_function = 'my_validate';

function my_validate(){

    if($_POST['email'] == '')
        return 'Email is Required';
}

Cross Site Request Forgery - csrf

This script does not validate csrf itself but has a placeholder csrf variable from loaded from $_SESSION['_csrf']. To protect from csrf, validate the csrf on POST commands.



Project Sponsored By:
Computer Animation Schools | Graphic Design Degrees | Web Design School | Criminal Justice and Paralegal Schools
Accounting Schools | Massage Therapy Schools | Pharmacy Tech Schools | Auto Mechanic Schools
Dental Hygienist Schools | Medical Tech Training | Phlebotomy Schools | Technician Schools