⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

No notification displayed after callback_escape_delete operation?



tanver

tanver
  • profile picture
  • Member

Posted 10 December 2011 - 11:42 AM

Below please find the complete code of the controller, I am facing various difficulties in getting the callback functions work the way is intended.
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

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 10 December 2011 - 12:20 PM

OK first of all I have already send you at : /topic/42-problem-invoking-before-insertupdate-callbacks/page__view__findpost__p__120 but you didn't understand what I meant. You have problem with your callbacks. I don't know why something happens to your controller and you are unable to work with the function call_user_func . All the callbacks works with this function of php. You have to try something like this in your Controller. So in your case please try:

<?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 just_a_test()
{
call_user_func(array($this, 'update_user_info_test'));
}

function update_user_info_test()
{
echo "Callbacks works fine!";
}
....


If this not works well it will not work ANY callback. If there is an error on this please send me the error.

tanver

tanver
  • profile picture
  • Member

Posted 10 December 2011 - 12:34 PM

I did invoke 'just_a_test' from the index and it did work .. but will need your help to know how can this be used in place of callback_before_insert or callback_before_update?

tanver

tanver
  • profile picture
  • Member

Posted 10 December 2011 - 14:14 PM

Hi Johnny, I was a little disappointed after having learned [quote]If this not works well it will not work ANY callback.[/quote], but kept my faith in this tool and am happy that I made it to work for me at least for this controller and am sure will work for all controllers offering similar functionality - the complete code of the controller is given below, which might help someone else to understand some of the functions and there work around as in my case some of the callbacks did work while few failed to work ..

<?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'));

$crud->callback_after_insert(array($this,'insert_user_info'));
$crud->callback_after_update(array($this,'update_user_info'));
$crud->callback_escape_delete(array($this, 'escape_delete'));

$output = $crud->render();
$this->_users_output($output);
}
catch(Exception $e)
{
show_error($e->getMessage().' --- '.$e->getTraceAsString());
}
}
// Add/Edit Field Callbacks .. for Custom Controls
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;
}
// Insert, Update and Delete Functions
function insert_user_info($post, $insert_id)
{
if (!empty($post['password']))
$post['password'] = md5($post['password']);
$this->db->update('users', array('user_created' => $this->session->userdata('username'),
'password' => md5($post['password']),
'comments' => 'User Created'), array('id' => $insert_id));
}
function update_user_info($post, $PKeyValue)
{
$this->db->update('users', array('last_edit_user' => $this->session->userdata('username'),
'last_edit_date_time' => standard_date('DATE_ISO8601', time()),
'comments' => 'User Info Updated'), array('id' => $PKeyValue));
}
function escape_delete($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;
}
}


Now am able to receive the deletion notification as well .. wonder if there is a way to change the notification message as well. :)

narogg

narogg
  • profile picture
  • Member

Posted 22 December 2011 - 20:38 PM

@tanver
Thnkz very much tanver! I had the same problem with callbacks and your code worked for me!
And btw I would also like to know a way to change the nofication messages as well (if there is any straightforward way to do it) :)

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 15 January 2012 - 17:53 PM

[quote name='narogg' timestamp='1324586339' post='155']
@tanver
Thnkz very much tanver! I had the same problem with callbacks and your code worked for me!
And btw I would also like to know a way to change the nofication messages as well (if there is any straightforward way to do it) :)
[/quote]

If you use grocery CRUD version 1.1.6 or later you simply change the lang_string. So for example in your case the delete message could change like this:


....
this->grocery_crud->set_lang_string('delete_success_message','Your custom delete message');
....


the lang_strings you can find them at : your_project/assets/grocery_crud/languages/english.php (or your chosen language)

For more about the set_lang_string you can see http://www.grocerycrud.com/crud/function_name/set_lang_string

goFrendiAsgard

goFrendiAsgard
  • profile picture
  • Member

Posted 07 February 2012 - 05:16 AM

Firstly, I think I don't need it yet, but when I make custom error message (eg: a user cannot delete his/her own user account), this help me a lot to tell user what is wrong.

Thanks to this new feature