Understanding Fuel Module Hooks
I have a 'clients' table I want to hook into the fuel_users table by using an 'after_create_user' module hook. There is limited information on this so I bet answering this will be useful for someone else as well.
The clients table has the following fields: id, fuel_user_id, company_name and contact_phone. I'll be adding clients to the Fuel CMS users one at a time manually. When that happens I want to automatically create a record in the clients table saving the id from fuel_users into the fuel_users_id of the clients table. I'll then go into the clients module and enter the company name and phone.
In /fuel/application/config/hooks.php I have the following:
$hook['after_create_user'] = array(
'class' => 'Clients',
'function' => 'create_client',
'filename' => 'clients_model.php',
'filepath' => 'models'
);
// include hooks specific to FUEL
include(FUEL_PATH.'config/fuel_hooks.php');
In /fuel/application/models/clients_model.php I have the following function:
function create_client($data)
{
$CI =& get_instance();
$values['fuel_user_id'] = $data['id'];
$this->db->insert($values);
}
I'm sure I've got the module hook in the wrong place or whatever. When I create a new fuel user in the CMS nothing happens. No errors are thrown but it's obviously not seeing the hook.
What am I doing wrong?
Thanks.
Comments
$hook['after_create_users'] = array(...)
function create_client($data) { $CI =& get_instance(); $db['default']['db_debug'] = false; $values['fuel_user_id'] = $data['id']; $this->db->insert($values); }
The error I'm getting is:
You must use the "set" method to update an entry.
It's referring to the line:
$this->db->insert($values);
I'm trying to insert a new record into the clients table, not update an existing record. I checked the clients table and the only field that's not null is fuel_user_id which is the one field I'm saving the new fuel_users id value to. Stumped again.
function create_client($data) { $CI =& get_instance(); $db['default']['db_debug'] = false; $values['fuel_user_id'] = $data['id']; $this->db->insert('my_table', $values); }
OR
function create_client($data) { $CI =& get_instance(); $db['default']['db_debug'] = false; $CI->load->model('my_model'); $CI->my_model->insert($values); }
Is there a way to conditionally hide the item_actions buttons (save, delete, duplicate, etc) from anyone who is not a super admin in a specific module?
It seemed logical to do that in MY_fuel_modules.php where I'd do something like:
$CI =& get_instance(); $user = $CI->fuel_auth->user_data(); if(($user['super_admin']== 'no')) { $clients_array = array( 'module_name'=>'Client Info', 'item_actions' => array('save', 'view') ); } else { $clients_array = array( 'module_name'=>'Client Info' ); } $config['modules']['clients'] = $clients_array;
But that doesn't work as it seems the $CI instance is not accessible from there. Is there a way to set these values conditionally in my clients_module.php file? I saw where I could include the following as an override for the get_others method and it does work, but I want that to be done conditionally as well:
function get_others($display_field, $id, $val_field = NULL) { return array(); }
if(function_exists('get_instance')) { $CI =& get_instance(); $user = $CI->fuel_auth->user_data(); if(($user['super_admin']== 'no')) { $clients_array = array( 'module_name'=>'Client Info', 'item_actions' => array('save', 'view') ); } else { $clients_array = array( 'module_name'=>'Client Info' ); } $config['modules']['clients'] = $clients_array; } else { $config['modules']['clients'] = array( 'module_name'=>'Client Info', 'item_actions' => array('save', 'view'), 'table_actions' => array('edit', 'view') ); }
The above doesn't work. I get an empty page. Here's what is in the php error log.
[01-Nov-2012 10:02:24] PHP Fatal error: Call to a member function view() on a non-object in /Applications/MAMP/htdocs/mpg/fuel/application/views/_blocks/header.php on line 40
Any ideas?<?php $this->load->view('_blocks/main_nav'); ?>
Changed them to:
<?php $CI->load->view('_blocks/main_nav'); ?>
There's no error in the php err log and I can navigate the site just fine but when I nav to /fuel to log in to the admin it jumps straight to /fuel/dashboard (even if I'm logged out).If blocks have to be loaded using $CI in this case could that also be affecting the fuel module? For example, the fule block - fuel_header.php loads a view like this:
<?=$this->load->module_view(FUEL_FOLDER, '_blocks/fuel_header_jqx', array(), TRUE)?>
If you are still having troubles logging out, try killing the browser cookie and see if you can replicate the problem.