1. unable to make callback_before_insert and callback_before_update work for me. I need to setup the $post_array with some additional info before the actual insert/update operation is initiated - Code is currently commented out in the controller.
2. callback_escape_delete functions works otherwise [record in the table gets updated] but 1. no info messages are displayed upon the completion, 2. the list grid is not updated either.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
function __construct()
{
parent::__construct();
/* Standard Libraries */
$this->load->database();
$this->load->helper('url', 'date');
$this->load->library('grocery_CRUD');
}
function _users_output($output = null)
{
$this->load->view('users/users.php',$output); //Template ..
}
function index()
{
$this->_users_output((object)array('output' => '' , 'js_files' => array() , 'css_files' => array()));
}
function users_management()
{
$this->session->set_flashdata('pagetitle', 'Manage User Accounts');
try
{
/* This is only for the autocompletion */
$crud = new grocery_CRUD();
$CompID = (int) $this->session->userdata('comp_id');
$crud->set_theme('cosmic');
$crud->set_table('users')
->where('users.active',1,TRUE);
if ($CompID > 0)
$crud->where('users.comp_id',$CompID,TRUE);
// Set relationships
$crud->set_relation('comp_id','companies','company_name')
->set_relation('role_id','roles','role_name');
$crud->set_subject('Users')
->columns('username','first_name','last_name','email')
->required_fields('username', 'email', 'password');
$crud->add_fields('comp_id', 'first_name', 'username', 'last_name', 'password', 'phone', 'email', 'role_id')
->edit_fields('comp_id', 'first_name', 'email', 'last_name', 'phone', 'role_id');
$crud ->display_as('comp_id', 'Comp ID')
->display_as('first_name', 'First Name')
->display_as('username', 'User Name')
->display_as('last_name', 'Last Name')
->display_as('password', 'Password')
->display_as('phone', 'Phone')
->display_as('email', 'Email');
$crud->callback_add_field('comp_id', array($this, 'add_field_callback_comp_id'))
->callback_add_field('password', array($this, 'add_field_callback_password'))
->callback_edit_field('comp_id', array($this, 'edit_field_callback_comp_id'))
->callback_escape_delete(array($this, 'escape_delete'));
/*
$crud->callback_before_insert(array($this,'insert_user_info'));
*/
$crud->callback_before_update(array($this,'update_user_info'));
$output = $crud->render();
$this->_users_output($output);
}
catch(Exception $e)
{
show_error($e->getMessage().' --- '.$e->getTraceAsString());
}
}
function add_field_callback_comp_id()
{
$comp_id = $this->session->userdata('comp_id'); //This is incorrect
if ($comp_id > 0)
{
$outp = '<input type="text" name="comp_id" value="'.set_value('comp_id', $comp_id).'" size="50" disabled="disabled" />';
}
else
{
$comp_id=0;
$outp = '<select name="compid" width="140">';
$outp .= 'selected="selected"' ;
$outp .= '<option value="0">Taxi Despatch System</option>';
$companies=$this->db->query('SELECT * FROM companies WHERE active=1');
// Iterate through the companies table and build the control
for ($i=0;$companies->num_rows()>$i;$i++)
$comp=$companies->row();
$outp .= "<option value='". $comp->id . "' >" . $comp->company_name . "</option>";
$outp .= '</select>';
}
return $outp;
}
function add_field_callback_password()
{
$outp = '<input type="password" name="password" value="" size="50" />';
return $outp;
}
function edit_field_callback_comp_id($Val, $PKey)
{
//echo 'Hello from edit_field_callback';
$comp_id = (int) $Val;
if ($comp_id > 0)
{
$outp = '<input type="text" name="comp_id" value="'.set_value('comp_id', $comp_id).'" size="50" disabled="disabled" />';
}
else
{
$comp_id=0;
$outp = '<select name="compid" width="140">';
$outp .= 'selected="selected"' ;
$outp .= '<option value="0">Taxi Despatch System</option>';
$companies=$this->db->query('select * from companies where active=1');
// Iterate through the companies table and build the control
for ($i=0;$companies->num_rows()>$i;$i++)
$comp=$companies->row();
$outp .= "<option value='". $comp->id . "' >" . $comp->company_name . "</option>";
$outp .= '</select>';
}
return $outp;
}
function insert_user_info($post_array)
{
echo 'Yes, in insert_user_info'; // Never reaches here, no error
/*
$post_array['user_created'] = $this->session->userdata('username');
$post_array['password'] = md5($_POST['password']);
*/
return $post_array;
}
function update_user_info($post, $PKey)
{
echo 'Yes, in update_user_info';
/*
$post['last_edit_user'] = $this->session->userdata('last_edit_user');
$post['last_edit_date_time'] = date("yyyy-dd-mm",time());
*/
return $post;
}
function escape_delete($PKeyValue)
{
//echo 'In Escape Delete';
echo 'The PKey Value is .. '.$PKeyValue;
$this->db->update('users', array('active' => 0,
'last_edit_user' => $this->session->userdata('username'),
'last_edit_date_time' => standard_date('DATE_ISO8601', time()),
'comments' => 'Deleted'), array('id' => $PKeyValue));
return TRUE;
}
}
I hope it will help you understand the problem, I am using CRUD v1.1.4 with CI v2.1
TIA