<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class App_lib
{
/**
* @var string $notifications Array of notifications
*/
private static $notifications = array();
private static $CI;
public static $instance;
// ------------------------------------------------------------------------
public function __construct()
{
self::$CI =& get_instance();
static::$instance = $this;
$this->_check_flash_notifications();
}
// ------------------------------------------------------------------------
public static function keep_flash_notifications()
{
self::$CI->session->keep_flashdata('notifications');
}
// ------------------------------------------------------------------------
private static function set_flash_notifications()
{
self::$CI->session->set_flashdata('notifications', self::$notifications);
}
// ------------------------------------------------------------------------
private function _check_flash_notifications()
{
$flash = self::$CI->session->flashdata('notifications');
if($flash)
{
if(empty(self::$notifications))
{
self::$notifications = $flash;
}
else
{
self::$notifications = array_merge(self::$notifications, $flash);
}
}
}
// ------------------------------------------------------------------------
/**
* Add Notification
*
* Add a notification to the private $notifications array
*
* @param string $type Type of notification (error, success, info, warning)
* @param string $message Notification message value
* @param bool $log If TRUE, then log_message($type, $message) will ensue
*
*/
public static function add_notification($type = 'info', $title = NULL, $message, $log = FALSE)
{
if( ! in_array($type, ['error','info','success','warning']))
{
log_message('error',"Unknown notification type '{$type}' for message: {$message}; Changing type to info");
$type = 'info';
}
self::$notifications[$type][] = array($title, $message);
if($log) log_message($type, $message);
self::set_flash_notifications();
}
// ------------------------------------------------------------------------
/**
* Clear Notifications
*
* Clears private $notifications array
*
*/
public static function clear_notifications()
{
self::$notifications = array();
}
// ------------------------------------------------------------------------
/**
* Get Notifications
*
* Returns local private $notifications array
*
* @since 0.1.0
* @return arr Array of notifications
*
*/
public static function get_notifications()
{
log_message('error','Executed: get_notifications');
$return = self::$notifications;
self::clear_notifications();
return $return;
}
// ------------------------------------------------------------------------
/**
* Get Latest Error
*
* This will retrieve all errors using the self::get_notifications function, meaning it will
* clear out all notifications, but return only one...
*
* @since 0.1.0
* @return str
*/
public static function get_latest_errors()
{
log_message('error','Executed: get_latest_errors');
if( ! $notifications = self::get_notifications())
{
return FALSE;
}
//die('<pre>' . print_r($notifications, TRUE));
if(@count($notifications['error']) > 1)
{
$return = array();
foreach($notifications['error'] as $n)
{
// Push just the error string to the array
array_push($return, $n[1]);
}
return implode('; ',$return);
}
elseif(@count($notifications['error']) === 1)
{
// Return just the error string
return $notifications['error'][0][1];
}
elseif( ! @isset($notifications['error'][0]))
{
return FALSE;
}
// Just incase
return FALSE;
}
// ------------------------------------------------------------------------
/**
* Check Errors
*
* Checks for any errors in private $notifications array
*
* @since 0.1.0
* @return bool TRUE of self::$notifications['error']
*
*/
public static function check_errors()
{
if(isset(self::$notifications['error']))
{
return TRUE;
}
return FALSE;
}
// ------------------------------------------------------------------------
/**
* Check Notifications
*
* Check for any created notifications have been added to $notifications
*
* @since 0.1.0
* @return bool TRUE if self::$notifications isnt empty
*
*/
public static function check_notifications()
{
return (self::$notifications ? TRUE : FALSE);
}
// ------------------------------------------------------------------------
/**
* Generate Notifications
*
* Uses functions in the template helper to generate the output for any
* notifications
*
* @since 0.1.0
* @return string String containing HTML for notifications
*
*/
public static function generate_notifications()
{
$return = '';
foreach(self::get_notifications() as $type => $n)
{
if(function_exists('notification_' . $type))
{
$f = 'notification_' . $type;
if(is_array($n))
{
foreach($n as $n2)
{
$return .= $f($n2[0], $n2[1]);
}
}
else
{
$return .= $f($n[0], $n[1]);
}
}
}
return (empty($return) ? NULL : $return);
}
}