<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class backend_users extends CI_Controller {
public $template = array();
function __construct()
{
parent::__construct();
if($this->staff_model->check_login_status() == false)
{
$message = array(
'result' => false
, 'type' => "error"
, 'message' => $this->lang->line('session_timeout_login_again')
);
if($this->input->get('json') == true) { echo json_encode($message); } else { $this->session->set_flashdata('message', $message); redirect(site_url('backend', true)."?r=".urlencode($_SERVER['REQUEST_URI'])); }
}
if($this->staff_model->check_permission('backend_users') == false)
{
$message = array(
'result' => false
, 'type' => "error"
, 'message' => $this->lang->line('dont_have_permission_access_page')
);
if($this->input->get('json') == true) { echo json_encode($message); } else { $this->session->set_flashdata('message', $message); redirect(site_url('backend/dashboard', true));
}
}
$params = array(
'page_name' => 'Users'
, 'page_name_singular' => 'User'
);
$this->template['preload'] = $this->staff_model->get_prerequisite($params);
}
public function index()
{
$is_json = $this->input->get("json");
$breadcrumbs = array(
array( 'link' => site_url('backend/dashboard', true), 'text' => 'Dashboard' )
, array( 'link' => '', 'text' => 'Users' )
);
$this->template['breadcrumbs'] = $breadcrumbs;
$message = array();
if($this->session->flashdata('message'))
{
$message = $this->session->flashdata('message');
}
$search_options = array(
'status' => 1
);
if(isset($_GET) && !empty($_GET))
{
array_walk($_GET, 'trim');
array_walk($_GET, 'xss_clean');
}
$paging_url = site_url('backend/users', true)."?search=true";
$paging_url_params = "";
$filter_str = "";
//FIXME: Please check validation of query
if($this->input->get('user_type') != "")
{
$search_options['user_type'] = $this->input->get('user_type');
$paging_url .= "&user_type=".$search_options['user_type'];
$filter_str .= "<div class='col-lg-4'><strong>User Type:</strong> ".$search_options['user_type']."</div>";
}
if($this->input->get('first_name') != "")
{
$search_options['first_name_like'] = $this->input->get('first_name');
$paging_url_params .= "&first_name=".$search_options['first_name_like'];
$filter_str .= "<div class='col-lg-4'><strong>First Name</strong> ".$search_options['first_name_like']."</div>";
}
if($this->input->get('last_name') != "")
{
$search_options['last_name_like'] = $this->input->get('last_name');
$paging_url_params .= "&last_name=".$search_options['last_name_like'];
$filter_str .= "<div class='col-lg-4'><strong>Last Name</strong> ".$search_options['last_name_like']."</div>";
}
if($this->input->get('email') != "")
{
$search_options['user_email_like'] = $this->input->get('email');
$paging_url_params .= "&email=".$search_options['user_email_like'];
$filter_str .= "<div class='col-lg-4'><strong>Email</strong> ".$search_options['user_email_like']."</div>";
}
if($this->input->get('reg_date') != "")
{
$date = $this->input->get('reg_date');
$date = au_date_to_mysql($date);
if(strtotime($date) != -1)
{
$search_options['created_at'] = $date;
$paging_url .= "®_date=".$search_options['created_at'];
$filter_str .= "<div class='col-lg-4'><strong>Registration Date:</strong> ".date("d/m/Y", strtotime($search_options['created_at']))."</div>";
}
unset($date);
}
$sort_order_fields = array(
array(
'label' => 'ID DESC'
, 'value' => 'user_id_desc'
, 'order_by' => 'usr.user_id DESC'
)
, array(
'label' => 'ID ASC'
, 'value' => 'user_id_asc'
, 'order_by' => 'usr.user_id ASC'
)
, array(
'label' => 'First Name ASC'
, 'value' => 'first_name_asc'
, 'order_by' => 'usr.first_name ASC'
)
, array(
'label' => 'First Name DESC'
, 'value' => 'first_name_desc'
, 'order_by' => 'usr.first_name DESC'
)
, array(
'label' => 'Last Name ASC'
, 'value' => 'last_name_asc'
, 'order_by' => 'usr.last_name ASC'
)
, array(
'label' => 'Last Name DESC'
, 'value' => 'last_name_desc'
, 'order_by' => 'usr.last_name DESC'
)
, array(
'label' => 'Email ASC'
, 'value' => 'user_email_asc'
, 'order_by' => 'usr.user_email ASC'
)
, array(
'label' => 'Email DESC'
, 'value' => 'user_email_desc'
, 'order_by' => 'usr.user_email DESC'
)
, array(
'label' => 'Registration Date ASC'
, 'value' => 'created_at_asc'
, 'order_by' => 'usr.created_at ASC'
)
, array(
'label' => 'Registration Date DESC'
, 'value' => 'created_at_desc'
, 'order_by' => 'usr.created_at DESC'
)
//FIXME: Expand here to have more order by
);
$this->template['sort_order_fields'] = $sort_order_fields;
$search_options['order_by'] = "usr.user_id DESC";
$search_options['search_order_by'] = "user_id_desc";
if($this->input->get('order_by'))
{
$selected_sort_order_field = array_search_r($sort_order_fields, 'value', $this->input->get('order_by'));
if(!empty($selected_sort_order_field))
{
$selected_sort_order_field = $selected_sort_order_field[0];
$search_options['search_order_by'] = $selected_sort_order_field['value'];
$paging_url .= "&order_by=".$search_options['search_order_by'];
$search_options['order_by'] = $selected_sort_order_field['order_by'];
}
}
$this->template['filter_str'] = $filter_str;
$total_rows = $this->user_model->get_users_result($search_options)->num_rows();
$this->template['total_rows'] = $total_rows;
$per_page = $this->input->get('per_page');
$per_page = (int)$per_page;
if($per_page == 0){ $per_page = 10; }
$paging_config = array(
'base_url' => $paging_url
, 'total_rows' => $total_rows
, 'per_page' => $per_page
, 'num_links' => 5
, 'page_query_string' => true
, 'query_string_segment' => 'page'
, 'full_tag_open' => '<ul class="pagination">'
, 'full_tag_close' => '</ul>'
, 'first_link' => 'First'
, 'first_tag_open' => '<li class="prev">'
, 'first_tag_close' => '</li>'
, 'last_link' => 'Last'
, 'last_tag_open' => '<li class="next">'
, 'last_tag_close' => '</li>'
, 'next_link' => 'Next'
, 'next_tag_open' => '<li class="next">'
, 'next_tag_close' => '</li>'
, 'prev_link' => 'Prev'
, 'prev_tag_open' => '<li class="prev">'
, 'prev_tag_close' => '</li>'
, 'cur_tag_open' => '<li class="active"><span>'
, 'cur_tag_close' => '</span></li>'
, 'num_tag_open' => '<li class="paging_num_{::page}">'
, 'num_tag_close' => '</li>'
);
$this->pagination->initialize($paging_config);
$page = $this->input->get('page');
$paging_url_params .= "&page=".(int)$page;
$page_offset = ($page > 1? $page - 1: 0) * $paging_config['per_page'];
$search_options['limit'] = $paging_config['per_page'];
$search_options['offset'] = $page_offset;
$users = $this->user_model->get_users($search_options, $paging_config['per_page'], $page_offset, $total_rows);
$this->template['users'] = $users;
$this->template['search_options'] = $search_options;
$this->template['paging_url_params'] = $paging_url_params;
$this->session->set_userdata('backend_users_paging_url_params', $paging_url_params);
$this->template['main_content'] = 'users/users_list_view';
$this->template['ssl'] = true;
$this->template['message'] = $message;
$this->load->view($this->config->item('template').'/template/main_template', $this->template);
}
function view_user()
{
$message = array();
if($this->session->flashdata('message'))
{
$message = $this->session->flashdata('message');
}
$user_id = $this->uri->segment(4);
if($user_id == false)
{
$message = array(
'result' => false
, 'type' => "error"
, 'message' => $this->lang->line('sorry_system_couldnt_find_requested_information')
);
if($this->input->get('json') == true) { echo json_encode($message); } else { $this->session->set_flashdata('message', $message); redirect(site_url('backend/users', true)); }
}
$view_mode = "view";
$search_options = array(
'user_id' => $user_id
, 'status' => 1
);
$user = $this->user_model->get_users($search_options);
if(empty($user))
{
$message = array(
'result' => false
, 'type' => "error"
, 'message' => $this->lang->line('sorry_system_couldnt_find_requested_information')
);
if($this->input->get('json') == true) { echo json_encode($message); } else { $this->session->set_flashdata('message', $message); redirect(site_url('backend/users', true)); }
}
$user = $user[0];
$this->template['user'] = $user;
//##################################################################
// START: Breadcrumbs
$breadcrumbs = array(
array( 'link' => site_url('backend/dashboard', true), 'text' => 'Dashboard' )
, array( 'link' => site_url('backend/users', true) , 'text' => 'Users' )
, array( 'link' => '', 'text' => 'View User' )
);
$this->template['breadcrumbs'] = $breadcrumbs;
// FINISH: Breadcrumbs
//##################################################################
$this->template['view_mode'] = $view_mode;
$this->template['message'] = $message;
$this->template['main_content'] = 'users/user_detail_view';
$this->template['ssl'] = true;
$this->load->view($this->config->item('template').'/template/main_template', $this->template);
}
public function user_form()
{
$is_json = $this->input->get("json");
$message = array();
if($this->session->flashdata('message'))
{
$message = $this->session->flashdata('message');
}
$user_id = $this->uri->segment(4);
$view_mode = "create";
if($this->uri->segment(3) == "update" && $user_id != "")
{
$view_mode = "update";
$search_options = array(
'user_id' => $user_id
, 'status' => 1
);
$user_info = $this->user_model->get_users($search_options);
if(empty($user_info))
{
$message = array(
'result' => false
, 'type' => "error"
, 'message' => $this->lang->line('sorry_system_couldnt_find_requested_information')
);
if($this->input->get('json') == true) { echo json_encode($message); } else { $this->session->set_flashdata('message', $message); redirect(site_url('backend/users', true)); }
}
$user_info = $user_info[0];
$this->template['user_info'] = $user_info;
}
if($this->input->post('submitted') == true)
{
if($this->_validate_user($view_mode) == false)
{
$message = array(
'result' => false
, 'type' => "error"
, 'message' => validation_errors()
);
if($is_json == true) { echo json_encode($message);die; }
}
else
{
$user_type = $this->input->post('user_type');
$first_name = $this->input->post('first_name');
$last_name = $this->input->post('last_name');
$user_email = $this->input->post('user_email');
$user_password = $this->input->post('user_password');
$last_login_at = null;
$enabled = $this->input->post('enabled');
$status = 1;
$created_at = date("Y/m/d H:i:s");
$modified_at = date("Y/m/d H:i:s");
if($view_mode == "create")
{
$new_user_info = array(
'user_type' => $user_type
, 'first_name' => $first_name
, 'last_name' => $last_name
, 'user_email' => $user_email
, 'user_password' => $user_password
, 'last_login_at' => $last_login_at
, 'enabled' => $enabled
, 'status' => $status
, 'created_at' => $created_at
, 'modified_at' => $modified_at
);
$result = $this->user_model->store_user($new_user_info);
if($result == true)
{
$new_user_info['user_id'] = $this->db->insert_id();
$message = array(
'result' => true
, 'type' => "success"
, 'message' => $this->lang->line('requested_information_created_successfully')
);
if($is_json == true) { echo json_encode($message);die; }
$this->session->set_flashdata('message', $message);
redirect(site_url('backend/users/view/'.$new_user_info['user_id'], true));
}
else
{
$message = array(
'result' => false
, 'type' => "error"
, 'message' => $this->lang->line('sorry_was_error_occurred_try_again')
);
if($is_json == true) { echo json_encode($message);die; }
}
}
elseif($view_mode == "update")
{
$new_user_info = array(
'user_id' => $user_id
, 'user_type' => $user_type
, 'first_name' => $first_name
, 'last_name' => $last_name
, 'user_email' => $user_email
, 'user_password' => $user_password
, 'enabled' => $enabled
, 'modified_at' => $modified_at
);
$result = $this->user_model->update_user($new_user_info);
if($result == true)
{
$message = array(
'result' => true
, 'type' => "success"
, 'message' => $this->lang->line('requested_information_updated_successfully')
);
if($is_json == true) { echo json_encode($message);die; }
$this->session->set_flashdata('message', $message);
redirect(site_url('backend/users/view/'.$new_user_info['user_id'], true));
}
else
{
$message = array(
'result' => false
, 'type' => "error"
, 'message' => $this->lang->line('sorry_was_error_occurred_try_again')
);
if($is_json == true) { echo json_encode($message);die; }
}
}
}
}
//##################################################################
// START: Breadcrumbs
$breadcrumbs = array(
array( 'link' => site_url('backend/dashboard', true), 'text' => 'Dashboard')
, array( 'link' => site_url('backend/users', true) , 'text' => 'Users' )
);
if($view_mode == "create") { $breadcrumbs[] = array( 'link' => '', 'text' => 'New User' ); }
elseif($view_mode == "update") { $breadcrumbs[] = array( 'link' => '', 'text' => 'Update User' ); }
$this->template['breadcrumbs'] = $breadcrumbs;
// FINISH: Breadcrumbs
//##################################################################
$this->template['view_mode'] = $view_mode;
$this->template['message'] = $message;
$this->template['main_content'] = 'users/user_form_view';
$this->template['ssl'] = true;
$this->load->view($this->config->item('template').'/template/main_template', $this->template);
}
private function _validate_user($view_mode)
{
$this->form_validation->set_rules('user_type', 'User Type', 'trim|required|callback__validate_user_type');
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required|max_length[250]');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|max_length[250]');
$this->form_validation->set_rules('user_email', 'Email', 'trim|required|max_length[250]|valid_email');
$this->form_validation->set_rules('user_password', 'Password', 'trim|required|min_length[6]|max_length[200]');
$this->form_validation->set_rules('enabled', 'Enabled', 'trim|required|numeric');
if($this->form_validation->run() == false)
{
return false;
}
else
{
return true;
}
}
//FIXME: Change validation code and message.
function _validate_user_type($value)
{
$validate_result = false;
if($validate_result == false)
{
$this->form_validation->set_message('_validate_user_type', "Entered User Type is not correct. Please check and try again" );
return false;
}
return true;
}
function delete_user()
{
$is_json = $this->input->get("json");
$message = array();
if($this->session->flashdata('message'))
{
$message = $this->session->flashdata('message');
}
$user_id = $this->uri->segment(4);
$user_info = array();
$view_mode = "delete";
if($user_id != "")
{
$search_options = array(
'user_id' => $user_id
, 'status' => 1
);
$user_info = $this->user_model->get_users($search_options);
}
if(empty($user_info))
{
$message = array(
'result' => false
, 'type' => "error"
, 'message' => $this->lang->line('sorry_system_couldnt_find_requested_information')
);
if($this->input->get('json') == true) { echo json_encode($message); } else { $this->session->set_flashdata('message', $message); redirect(site_url('backend/users', true)); }
}
$user_info = $user_info[0];
$new_user_info = array(
'user_id' => $user_id
, 'status' => 0
);
$result = $this->user_model->update_user($new_user_info);
if($result == true)
{
$message = array(
'result' => true
, 'type' => "success"
, 'message' => $this->lang->line('requested_information_deleted_successfully')
);
if($this->input->get('json') == true) { echo json_encode($message); } else { $this->session->set_flashdata('message', $message); redirect(site_url('backend/users', true)); }
}
else
{
$message = array(
'result' => false
, 'type' => "error"
, 'message' => $this->lang->line('sorry_system_couldnt_delete_requested_information')
);
if($this->input->get('json') == true) { echo json_encode($message); } else { $this->session->set_flashdata('message', $message); redirect(site_url('backend/users', true)); }
}
die;
}
}