Skip to content

Instantly share code, notes, and snippets.

@codersatx
Created May 20, 2011 02:16
Show Gist options
  • Save codersatx/982231 to your computer and use it in GitHub Desktop.
Save codersatx/982231 to your computer and use it in GitHub Desktop.
My base admin controller for my custom cms.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Admin Controller
*
* Provides methods and properties for all admin related objects.
*
*/
class Admin_Controller extends CI_Controller{
//--------------------------------------------------------------------------
/**
* An array of resources we want to exclude from authentication.
* This is needed for the login and other lower level modules.
* These are all defined in the app config file in the app_exclude_resources array.
*
* @var array
*/
protected $exclude;
//--------------------------------------------------------------------------
/**
* Initialize the modules array.
*
* @var array
*/
protected $modules = array();
//--------------------------------------------------------------------------
/**
* The name of the current model table we will be working with.
*
* @var string
*/
protected $db_table;
//--------------------------------------------------------------------------
/**
* An array of models required by the current model.
* The prototype should follow
* $this->db_requires = array('module_type'=>array('model_name','model_name2'));
* The module type is the same name as the folder where the models reside.
*
* $this->db_requires = array('module_name'=>array('model_name','model_name2'));
* @var array
*/
protected $db_requires;
//--------------------------------------------------------------------------
/**
* The fields property holds an array of table column names.
* This is defined by each model and is initialized in the
* CI_Model class.
*
* @var array
*/
protected $db_fields;
//--------------------------------------------------------------------------
/**
* The module segment in our uri: http://site.com/module/class/method/id
*
* @var string
*/
protected $module;
//--------------------------------------------------------------------------
/**
* The name of admin class in our uri: http://site.com/module/class/method/id
* @var string
*/
protected $admin;
//--------------------------------------------------------------------------
/**
* The method segment in our uri: http://site.com/module/class/method/id
*
* @var string
*/
protected $method;
//--------------------------------------------------------------------------
/**
* The id segment in our uri: http://site.com/module/class/method/id
*
* @var string
*/
protected $id;
//--------------------------------------------------------------------------
/**
* Constructor
*
* Creates our admin controller object
*
* @return object Admin_Controller
*/
public function __construct()
{
//Call the parent constructor
parent::__construct();
//Run this code before anyting else runs.
$this->_before();
//Check if this user has access to the resource being requested.
$this->_authenticate($this->module, $this->admin, $this->method, $this->exclude);
//Load Models
$this->_load_models();
//Set the page level variables.
$this->_set_vars();
//Run the system usage tracker
$this->_track_usage();
//Once our bootstrapping and checking is done run this code.
$this->_after();
}
//--------------------------------------------------------------------------
private function _before()
{
write_log('Running before function.');
//If the enable profiler is on let's print the results
if (app('app_enable_profiler') == TRUE)
{
$this->output->enable_profiler(TRUE);
}
$this->exclude = app('app_exclude_resources');
$this->module = (segment(1)) ? segment(1) : 'dashboard';
$this->admin = 'admin';
$this->method = (segment(3)) ? segment(3) : 'index';
$this->id = (segment(4)) ? segment(4) : '';
// if(is_post())
// {
// write_log($this->method .'_post');
// $this->_load_models();
// $m = $this->method .'_post';
// $this->$m();
// }
}
//--------------------------------------------------------------------------
private function _after()
{
write_log('Running after function.');
}
//--------------------------------------------------------------------------
/**
* Authenticate
*
* Checks to see of the current user has has access to the current resources.
*
* @param string $module The name of the module we are checking for.
* @param string $admin The name of the admin controller.
* @param string $method The name of the method in the admin controller we are checking for.
* @param array $exclude_resources An array of resources to exclude from authentication.
* Used for the login and other controllers that would cause an infinite loop since we are
* extending the admin_controller.
* @return mixed If a user has access return true if not redirect to the denied message.
*/
private function _authenticate($module, $admin, $method, $exclude)
{
$group_id = $this->session->userdata('group_id');
$resource = $this->module .'/'. $this->admin .'/'. $this->method;
//If the user is not logged in let's redirect them.
if ($group_id == '')
{
if ( ! in_array($resource, $exclude))
{
redirect('login/admin');
}
}
if( ! in_array($resource, $exclude))
{
if ($this->permission_model->is_allowed($group_id, $resource) === TRUE)
{
return TRUE;
}
else
{
if (segment(1) == 'admin')
{
redirect(app('app_default_admin_controller') . '/' . app('app_default_admin_method'));
}
else
{
redirect('denied/admin/index/'. $this->module .'/'. $this->admin .'/'. $this->method);
}
}
}
}
//--------------------------------------------------------------------------
private function _load_models()
{
//Load a model for this controller if available
$model_name = $this->module .'_model';
if (file_exists(APPPATH . 'modules/'. $this->module .'/models/'. $model_name .'.php' ))
{
$this->load->model($this->module .'/'. $model_name);
$this->db_fields = $this->$model_name->db_fields;
$this->db_table = $this->$model_name->db_table;
$this->db_requires = $this->$model_name->db_requires;
$this->$model_name->module = $this->module;
$this->$model_name->method = $this->method;
$this->$model_name->id = $this->id;
}
//If the db_requires array is not empty let's load the models declared in the array.
if (count($this->db_requires) != 0)
{
foreach($this->db_requires as $module=>$model_names)
{
foreach($model_names as $model_name)
{
if (file_exists(APPPATH . 'modules/'. $module .'/models/'. $model_name .'.php'))
{
$this->load->model($module .'/'. $model_name);
}
}
}
}
}
//--------------------------------------------------------------------------
private function _set_vars()
{
//Define our template for all tables in the admin section
$this->table->set_template(app('app_table_template'));
//Gray bar title above the content in the right column of the template.
if ($this->method == 'index')
{
$data['action_title'] = readable($this->module) .' Home';
}
else
{
$data['action_title'] = readable($this->method) .' '. readable($this->module);
}
//Set the default module title so we can use it in our template.
$data['module_title'] = readable($this->module);
//Set the default form action based on the current action
$data['form_action'] = $this->uri->uri_string();
//Set the default submit button title based on the method.
$data['submit'] = ($this->method == 'create') ? 'Add' : 'Save';
//Set the breadcrumb string to empty
$data['breadcrumb_string'] = NULL;
//Let's load all these data variables into the global scope.
$this->load->vars($data);
}
//--------------------------------------------------------------------------
private function _track_usage()
{
if (app('app_enable_log') == TRUE)
{
$log_data['user_id'] = $this->session->userdata('user_id');
$log_data['resource'] = $this->uri->uri_string();
$log_data['action_timestamp'] = date('Y:m:d h:i:s');
$log_data['message_type'] = 'log';
$log_data['message'] = 'Automatic tracking of activity in application.';
$log_data['ip'] = $_SERVER['REMOTE_ADDR'];
$this->log_model->save($log_data);
}
}
//--------------------------------------------------------------------------
/**
* Renders a view using our convention-based approach
* Get the current uri from the uri_string ci method and explode into
* an array so we can manipulate the various parts.
*
* @param array $data An array of data items for our view. $view_data['item'] = value
* @return object $view Returns a view with our content
*/
public function render($data = array(), $template = NULL, $view = NULL)
{
$theme = app('app_system_theme');
$module = $this->module;
$admin = $this->admin;
$method = $this->method;
$id = $this->id;
//The name of main admin template to load.
if ($template == NULL)
{
$template = 'template';
}
//If the data array is not empty lets loop thru it and create our variables for our view
if (count($data) > 0)
{
foreach($data as $item=>$value)
{
$view_data[$item] = $value;
}
}
else
{
$view_data[] = '';
}
//The name of the view file, the sub template
if ($view == NULL)
{
$view = $this->load->view($method, $view_data , TRUE);
}
else
{
$view = $this->load->view($view, $view_data, TRUE);
}
$data['page_id'] = $module;
$data['page_class'] = $method;
$data['content'] = $view;
//echo 'themes/admin/'. $theme . $template;
$this->load->external_view('themes/admin/'. $theme .'/', $template, $data);
}
//--------------------------------------------------------------------------
/**
* Handles the batch actions for all our table views.
*
* @param string $action
* @return void
* @author Alex Garcia
*/
public function batch($action = NULL)
{
$batch_action = $this->input->post('batch_action');
$model = $this->module .'_model';
$this->load->model($this->module .'/'. $model);
$model = $this->module .'_model';
switch($batch_action)
{
case 'batch_delete':
foreach($_POST as $key=>$value)
{
if($key != 'batch_action')
{
if($key != 'batch_submit')
{
if($value == 1)
{
if ($this->input->post('src') == 'module_resources')
{
$this->load->model('module_resource/module_resource_model');
$this->module_resource_model->delete($key);
$module_id = $this->input->post('module_id');
$redirect = $this->module .'/'. $this->admin .'/update/'. $module_id;
}
else
{
$this->$model->delete($key);
$redirect = $this->module .'/'. $this->admin;
}
}
}
}
}
status('success','The selected items were deleted successfully.');
redirect($redirect);
break;
case 'batch_transfer_owner':
if($action == NULL)
{
foreach($_POST as $key=>$value)
{
if($key != 'batch_action')
{
if($key != 'batch_submit')
{
if($value == 1)
{
$data['data'][$key] = $key;
}
}
}
}
$data['action_title'] = 'Transfer Content Ownership';
$data['users'] = $this->user_model->find();
$this->render($data, '', 'core/layout/batch');
}
else
{
$module_type = $this->input->post('module_type');
$class = $this->input->post('model');
$model = $class .'_model';
$this->load->model($module_type .'/'. $model);
$data['owner_id'] = $this->input->post('owner_id');
foreach($_POST as $key=>$value)
{
if(is_int($key))
{
$this->$model->transfer_content_owner($data, $key);
}
}
status('success', message('owner_transfer_success'));
redirect($module_type .'/'. $class);
}
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment