Created
October 12, 2016 03:11
-
-
Save DykiSA/70caa9d111bf4af62efbe29c00540f2d to your computer and use it in GitHub Desktop.
Codeigniter General Helper I Always Used
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment