i dont think its the same,
i think is an incompatibility between
custom_grocery_crud
and the new version of grocery crud so maybe
custom_grocery_crud
have to be fixed.
I will report it here so maybe someone can help
To use it - drop the subclass into the application/libraries folder
right beside grocery_crud.php
call the new file custom_grocery_crud.php
Edit application/controllers/examples.php
class Examples extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
//$this->load->library('grocery_Crud');
$this->load->library('custom_grocery_crud');
}
...
function offices_management()
{
try{
//$crud = new grocery_CRUD();
$crud = new custom_grocery_crud();
$crud->set_theme('datatables');
$crud->set_table('offices');
$crud->set_subject('Office');
$crud->required_fields('city');
$crud->columns('city','country','phone','addressLine1','postalCode');
//our demo
$crud->field_set_defaults('city' , 'readonly', 'Hobart,Tasmania');
$output = $crud->render();
$this->_example_output($output);
}catch(Exception $e){
show_error($e->getMessage().' --- '.$e->getTraceAsString());
}
}
use field_set_defaults exactly the same way you would use field_type
Here's the subclass custom_grocery_crud.php
and here it is inline for you to peruse:
<?php
/**
* PHP grocery CRUD
*
* A Codeigniter library that creates a CRUD automatically with just few lines of code.
*
* Copyright © 2010 - 2012 John Skoumbourdis.
*
* LICENSE
*
* Grocery CRUD is released with dual licensing, using the GPL v3 (license-gpl3.txt) and the MIT license (license-mit.txt).
* You don't have to do anything special to choose one license or the other and you don't have to notify anyone which license you are using.
* Please see the corresponding license file for details of these licenses.
* You are free to use, modify and distribute this software, but all copyright information must remain.
*
* @package grocery CRUD
* @copyright Copyright (c) 2010 through 2012, John Skoumbourdis
* @license https://github.com/scoumbourdis/grocery-crud/blob/master/license-grocery-crud.txt
* @version 1.3
* @author John Skoumbourdis <scoumbourdisj@gmail.com>
*/
// ------------------------------------------------------------------------
include_once(dirname(__FILE__)."/grocery_crud.php");
/**
* PHP grocery CRUD - Customized
*
* Creates a full functional CRUD with few lines of code.
*
* @package grocery CRUD
* @author John Skoumbourdis <scoumbourdisj@gmail.com>
* @license https://github.com/scoumbourdis/grocery-crud/blob/master/license-grocery-crud.txt
* @link http://www.grocerycrud.com/documentation
*
* @hacker Ez (ezgoen@gmail.com) - email me for my real identity
*/
class custom_grocery_crud extends grocery_CRUD{
protected $field_default_values = array();
function __construct()
{
parent::__construct();
}
/**
* Helper Function
*
* Created by Ez (ezgoen@gmail.com) - email me for my real identity
*
*/
protected function get_field_default_values($field){
return array_key_exists($field,$this->field_default_values) ? $this->field_default_values[$field] : null;
}
/**
* Use this function exactly the same way you would use field_type()
* see : http://www.grocerycrud.com/documentation/options_functions/field_type
*
* Created by Ez (ezgoen@gmail.com) - email me for my real identity
*
* @explanation:
* in the case of an ADD data form I want to provide default values to the fields.
* I may also want to make the fields with default values readonly or completely hidden
* The parent object does not allow/facilitate addidtion of default values to visible
* fields other than:
* hidden
* invisible
* password
* enum
* set
* dropdown
* multiselect
*
* I havent fooled around with invisible - so I cant comment on anything to do with that.
* however I want my other fields - most commonly string and integer to have default values
* additionally I may also want thos values to either editable OR readonly and visible at
* the same time AND I want my default values (if they are readonly) to go into the database
*
*/
public function field_set_defaults($field,$type,$value){
$this->field_default_values[$field]=array('type'=>$type,'value'=>$value);
}
/**
* Work out what to do with input fields for add record
*
*
* Modified/Hacked by Ez (ezgoen@gmail.com) - email me for my real identity
*
* @explanation:
* in the case of an ADD data form I want to provide default values to the fields.
* I may also want to make the fields with default values readonly or completely hidden
*/
protected function get_add_input_fields($field_values = null)
{
$fields = $this->get_add_fields();
$types = $this->get_field_types();
$input_fields = array();
foreach($fields as $field_num => $field)
{
$field_info = $types[$field->field_name];
$field_value = !empty($field_values) && isset($field_values->{$field->field_name}) ? $field_values->{$field->field_name} : null;
$field_default_values=$this->get_field_default_values($field->field_name);
if (! empty($field_default_values) and empty($field_value) ){
//if $field_info is not set at this point we need to construct one
if (!isset($field_info)){
$field_info = (object)array();
}
$field_info->custom_default=True; //this is what we look for to ensure we only change the behaviour we want to
$field_info->crud_type=$field_default_values['type'];
$field_info->type=$field_default_values['type'];
$field_info->default=$field_default_values['value'];
$field_info->extras=$field_default_values['value'];
$field_value=$field_default_values['value'];
}
//no changes below here
if(!isset($this->callback_add_field[$field->field_name]))
{
$field_input = $this->get_field_input($field_info, $field_value);
}
else
{
$field_input = $field_info;
$field_input->input = call_user_func($this->callback_add_field[$field->field_name], $field_value, null, $field_info);
}
switch ($field_info->crud_type) {
case 'invisible':
unset($this->add_fields[$field_num]);
unset($fields[$field_num]);
continue;
break;
case 'hidden':
$this->add_hidden_fields[] = $field_input;
unset($this->add_fields[$field_num]);
unset($fields[$field_num]);
continue;
break;
}
$input_fields[$field->field_name] = $field_input;
}
return $input_fields;
}
/**
* Get the html input for the specific field with the
* current value
*
* @param object $field_info
* @param string $value
* @return object
*
* Modified/Hacked by Ez (ezgoen@gmail.com) - email me for my real identity
*
* @explanation:
* in the case of an ADD data form I want my readonly default values to go into
* the database.
* The default behaviour of 'readonly' fields is to not actually "be a form field"
* they are instead just text in a div like this :
* <div id="field-YOUR_FIELD" class="readonly_label">Your Default Value</div>
* this modification adds a hidden form field that will put the default values you
* want into the database like this :
* <div id="field-YOUR_FIELD" class="readonly_label">Your Default Value</div>
* <input id='field-YOUR_FIELD' type='hidden' name='YOUR_FIELD' value='Your Default Value' />
*
*/
protected function get_field_input($field_info, $value = null)
{
$real_type = $field_info->crud_type;
$types_array = array(
'integer',
'text',
'true_false',
'string',
'date',
'datetime',
'enum',
'set',
'relation',
'relation_n_n',
'upload_file',
'hidden',
'password',
'readonly',
'dropdown',
'multiselect'
);
if (in_array($real_type,$types_array)) {
/* A quick way to go to an internal method of type $this->get_{type}_input .
* For example if the real type is integer then we will use the method
* $this->get_integer_input
* */
$field_info->input = $this->{"get_".$real_type."_input"}($field_info,$value);
//my addition here...
if ((property_exists($field_info,'custom_default')) and ( $real_type=="readonly" )) {
$field_info->crud_type='hidden';
$field_info->type='hidden';
$field_info->input .= $this->{"get_hidden_input"}($field_info,$value);
$field_info->crud_type=$real_type;
$field_info->type=$real_type;
}
//no changes below here
}
else
{
$field_info->input = $this->get_string_input($field_info,$value);
}
return $field_info;
}
}
?>