Last active
October 12, 2016 03:13
-
-
Save DykiSA/dc72dcc50d107ec60be58a5256212fba to your computer and use it in GitHub Desktop.
Base controller class for initialize codeigniter projcet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Global function | |
* place for all global function | |
*/ | |
$GLOBALS['CI'] =& get_instance(); | |
if (! function_exists('flash_message')) | |
{ | |
function flash_message($can_hide = false) { | |
global $CI; | |
$success_message = $CI->session->flashdata('success'); | |
$status = empty($success_message) ? 'error' : 'success'; | |
$message = $CI->session->flashdata($status); | |
if (!empty($message)) { | |
return create_alert($status, $message, $can_hide); | |
} | |
unset($flash); | |
} | |
} | |
if (! function_exists('create_alert')) | |
{ | |
function create_alert($status, $message, $can_hide = false) { | |
global $CI; | |
$class = ($status == 'error' ? 'alert-danger' : 'alert-success'); | |
$title = ($status == 'error' ? 'Error' : 'Sukses'); | |
$icon = ($status == 'error' ? 'fa-ban' : 'fa-check'); | |
$alert = "<div class=\"alert $class alert-dismissible\">"; | |
$alert .= $can_hide ? '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">x</button>' : ''; | |
$alert .= "<h4><i class=\"icon fa $icon\"></i> $title </h4>$message</div>"; | |
return $alert; | |
} | |
} | |
if (! function_exists('do_login')) | |
{ | |
function do_login($userid, $username, $additional_information = array()) | |
{ | |
global $CI; | |
$arr = array( | |
'user_id' => $userid, | |
'user_name' => $username, | |
'time_login' => date('Y-m-d H:i:s'), | |
'additional_information' => $additional_information | |
); | |
if ($CI->session->set_userdata('logged_in', $arr)) | |
return true; | |
return false; | |
} | |
} | |
if (! function_exists('is_login')) | |
{ | |
function is_login() | |
{ | |
global $CI; | |
$CI->load->library('session'); | |
if ($CI->session->userdata('logged_in')) { | |
return true; | |
} | |
return false; | |
} | |
} | |
if (! function_exists('do_logout')) | |
{ | |
function do_logout() | |
{ | |
global $CI; | |
if ($CI->session->unset_userdata('logged_in')) { | |
return true; | |
} | |
return false; | |
} | |
} | |
if (! function_exists('get_logged_in_user')) | |
{ | |
function get_logged_in_user() | |
{ | |
global $CI; | |
return $CI->session->userdata['logged_in']; | |
} | |
} | |
if (! function_exists('is_home')) | |
{ | |
function is_home() { | |
global $CI; | |
$segment = $CI->uri->segment(2, ''); | |
if(empty($segment)) { | |
return true; | |
} | |
return false; | |
} | |
} | |
if (! function_exists('create_help_block')) | |
{ | |
function create_help_block($text) | |
{ | |
$text = str_replace("<p>", "", $text); | |
$text = str_replace("</p>", "", $text); | |
return '<p class="help-block">'.$text.'</p>'; | |
} | |
} | |
if (! function_exists('create_breadcrumb')) | |
{ | |
function create_breadcrumb() { | |
global $CI; | |
if(is_home()) { | |
return '<li class="active"><i class="fa fa-dashboard"></i> Home</li>'; | |
} | |
else { | |
$link = '<li><a href='.base_url().'><i class="fa fa-dashboard"></i> Home</a></li>'; | |
$url = $CI->uri->segment_array(); | |
$item = count($url); | |
unset($url[1]); | |
$num = 1; | |
foreach($url as $segment) { | |
++$num; | |
$path = ($num == $item) ? str_replace('-', ' ', $segment) : anchor(base_url('admin/'.$segment), str_replace('-', ' ', $segment)); | |
$class = ($num == $item) ? null : 'active'; | |
$link .= '<li class='.$class.'>'.ucwords($path).'</li>'; | |
} | |
return $link; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
/** | |
* MY_Controller | |
* Base controller class for initialize codeigniter projcet | |
* | |
* created by acing at 01/11/2015 12:01 WIB | |
*/ | |
class MY_Controller extends CI_Controller { | |
/** | |
* Template directory, by default is empty | |
* that mean is located in root of views directory | |
* @var string | |
*/ | |
var $_template_path = ''; | |
/** | |
* Default data that will sent to view | |
* this used to avoid you to define the data in every function | |
* @var array | |
*/ | |
var $_data = array(); | |
/** | |
* Constructor of this class | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Load page including the template | |
* @param string $page view file | |
* @param string $title title of page | |
* @param array $data (optional) data will be sent to view | |
* @return view load 'view' file | |
* the $page is view file that stored under /contents/ directory | |
* so make sure you create the template structure like this | |
* views/ | |
* contents/ | |
* ncludes/ | |
* template.php | |
* you can also create other template under view which structured like this | |
* views/ | |
* template_name/ | |
* contents/ | |
* includes/ | |
* template.php | |
* other_template_name/ | |
* contents/ | |
* includes/ | |
* template.php | |
* | |
* if do so, you must spesify the $_template_path in constructor of your controller | |
* NOTE: for more detail see template.php script in /application/views directory | |
*/ | |
public function _load_page($page, $title = NULL, $data = NULL) { | |
$data['page'] = "contents/$page"; | |
$data['page_title'] = $title; | |
$data['head_title'] = is_null($title) || empty($title) ? constant("SITE_TITLE") : constant("SITE_TITLE") . " - $title"; | |
$data['template_path'] = $this->_template_path; | |
// get data from $this->_create_validation_error_result() | |
$data['form_val'] = $this->session->flashdata('form_val'); | |
$data['form_error'] = $this->session->flashdata('form_error'); | |
$send_data = array_merge($this->_data, $data); | |
return $this->load->view('template', $send_data); | |
} | |
/** | |
* keep flashdata to next redirect | |
*/ | |
public function _keep_flash_data($key) { | |
$value = $this->session->flashdata($key); | |
return $this->session->set_flashdata($key, $value); | |
} | |
/** | |
* generate error message and get value for each field | |
* @param array $rules form validation rules | |
* @return array error which will saved in flashdata | |
* the function will generate 'form_val' and 'form_error' variable | |
* which will saved in flashdata | |
* the 'form_val' stored the value of each field that was filled | |
* the 'form_error' stored the error message that caused the error | |
* both of their contents has keys that reffered to the name of each fields | |
* by default both of them has sended to view when call '_load_page' function | |
* | |
* NOTE: this feature is used when the form validation is return false | |
*/ | |
public function _create_validation_error_result($rules) | |
{ | |
$this->session->set_flashdata('form_val', $this->input->post()); | |
$form_error = array(); | |
foreach ($rules as $config) { | |
// get error message every each field | |
$field_error = form_error($config['field']); | |
if ($field_error != '') { | |
$form_error[$config['field']] = $field_error; | |
} | |
} | |
// save error message to flashdata | |
$this->session->set_flashdata('form_error', $form_error); | |
return $form_error; | |
} | |
/** | |
* Prevent user to access the function from where this function is called | |
* usually used for deny user to access the method from direct linking | |
* @return error 404 page when is not post request | |
*/ | |
public function _only_post_request() | |
{ | |
if ($this->input->server('REQUEST_METHOD') !== 'POST') { | |
$this->_error404(); | |
} | |
} | |
/** | |
* Prevent user to access the function from where this function is called | |
* usually used for deny user to access the method from direct linking | |
* @return error 404 page when is not ajax request | |
*/ | |
public function _only_ajax_request() | |
{ | |
if (!$this->input->is_ajax_request()) { | |
$this->_error404(); | |
} | |
} | |
/** | |
* Prevent anonymous user to access function | |
* @return error 404 page when is anonymous user | |
*/ | |
public function _only_logged_in_user() | |
{ | |
if (!is_login()) { | |
$this->_error404(); | |
} | |
} | |
/** | |
* delete file under project folder | |
* @param string $path path of the file which will be deleted | |
* @return execute file deletion when the given file's path is exists | |
*/ | |
public function _delete_file($path = NULL) | |
{ | |
if (!is_null($path)) { | |
if (file_exists($path)) { | |
return unlink(APPPATH.'../'.$path); | |
} | |
} | |
return false; | |
} | |
/** | |
* Show error 404 not found page | |
* @return view load error404.php under template folder | |
*/ | |
public function _error404() | |
{ | |
$data['page'] = 'error404'; | |
$data['page_title'] = 'Error'; | |
$data['heading'] = 'Error 404 Halaman Tidak Ada'; | |
$data['head_title'] = constant("SITE_TITLE") . ' - ' . $data['heading']; | |
$data['message'] = 'Halaman yang anda minta tidak tersedia'; | |
$data['template_path'] = $this->_template_path; | |
$this->output->set_status_header('404'); | |
$send_data = array_merge($this->_data, $data); | |
echo $this->load->view('template', $send_data, TRUE); | |
die; | |
} | |
} | |
/* End of file MY_Controller.php */ | |
/* Location: ./application/core/MY_Controller.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment