Hi there,
I'm discovering Grocery CRUD, and I find it really helpful and useful. But I'm already working with Datamapper ORM. So, I was looking for a way to make it work together...
So I created a simple Datamapper extension that I called "GroceryCrud", and that it adds the ->to_grocery() method. It needs some additional coding rules, but it can be really more helpful ;-)
I try to really make a Grocery_Crud object from a Datamapper object. It's note easy but it sounds possible.
Grocery CRUD needs :
- Table name : OK
- Subject : Can be deducted from table name
- Fields configuration : need a special attribute in your Datamapper object
- Relationships : can be deducted from Datamapper object's "has_one" and "has_many" attributes
I'm not able yet to import Datamapper's relationships data into Grocery CRUD object. But soon...
Here the "DMZ_GroceryCrud" class, that is the Datamapper extension :
<?php /** * File : application/datamapper/grocerycrud.php */ class DMZ_GroceryCrud { /** * This method converts Datamapper object to Grocery Crud object */ function to_grocery($object) { // Basic properties $table_name = $object->table; $grocery_subject = (property_exists($object, 'grocery_subject')) ? $object->grocery_subject : ucfirst(str_replace('_', ' ', get_class($object))); $grocery_fields = array(); // Fields loading if ( property_exists($object, 'grocery_fields' )) { $object->grocery_fields; } else { foreach($object->fields as $field_name) { $grocery_fields[$field_name] = array(); } } // Building Grocery Crud Object $crud = new grocery_CRUD(); $crud->set_table($table_name); $crud->set_subject($grocery_subject); // Crud fields settings $crud_add_fields = array(); $crud_edit_fields = array(); $crud_columns = array(); $crud_required = array(); foreach ($grocery_fields as $field_name => $field_config) { foreach ($field_config as $config => $value) { switch ( $config ) { case 'display_as': $crud->display_as($field_name, $field_config['display_as']); break; case 'show_column': if ($field_config['show_column']==true) { $crud_columns[] = $field_name; } break; case 'show_add_field': if ($field_config['show_add_field']==true) { $crud_add_fields[] = $field_name; } break; case 'show_edit_field': if ($field_config['show_edit_field']==true) { $crud_edit_fields[] = $field_name; } break; case 'required': if ($field_config['required']==true) { $crud_required[] = $field_name; } break; } } } $crud->edit_fields($crud_edit_fields); $crud->add_fields($crud_add_fields); $crud->columns($crud_columns); $crud->required_fields($crud_required); // ----- return $crud; } }
You just need to activate this extension in Datamapper's configuration.
And here an example of Datamapper object's additional properties :
public $grocery_subject = 'Serveur'; public $grocery_fields = array( 'ip' => array( 'display_as' => 'IP address', 'show_column' => true, 'show_edit_field' => true, 'show_add_field' => true, 'required' => false ), 'domain' => array( 'show_column' => true, 'show_edit_field' => true, 'show_add_field' => true, 'required' => true ), 'provider' => array( 'show_column' => true, 'show_edit_field' => true, 'show_add_field' => true, 'required' => true ), 'priority_level' => array( 'show_column' => true, 'show_edit_field' => true, 'show_add_field' => true, 'required' => true ), 'driver' => array( 'show_column' => true, 'show_edit_field' => true, 'show_add_field' => true, 'required' => true ), 'driver_parameters' => array( 'show_column' => false, 'show_edit_field' => true, 'show_add_field' => true, 'required' => false ), 'status' => array( 'show_column' => true, 'show_edit_field' => true, 'show_add_field' => true, 'required' => true ), 'total_sent' => array( 'show_column' => true, 'show_edit_field' => false, 'show_add_field' => false, 'required' => false ) );
Feel free to improve this class ;-)
Joffrey.