Clean-up libraries
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
* Message:: a class for writing feedback message information to the session
|
||||
* Messages.php - A class for writing feedback message information to the session
|
||||
*
|
||||
* Copyright 2006 Vijay Mahrra & Sheikh Ahmed <webmaster@designbyfail.com>
|
||||
* Copyright (c) 2006 Vijay Mahrra & Sheikh Ahmed <webmaster@designbyfail.com>
|
||||
*
|
||||
* See the enclosed file COPYING for license information (LGPL). If you
|
||||
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
|
||||
*
|
||||
* @package ScattPort
|
||||
* @author Vijay Mahrra & Sheikh Ahmed <webmaster@designbyfail.com>
|
||||
* @author Eike Foken <kontakt@eikefoken>
|
||||
* @link http://www.designbyfail.com/
|
||||
@@ -15,8 +16,19 @@
|
||||
*/
|
||||
class Messages {
|
||||
|
||||
var $_ci;
|
||||
var $_types = array('success', 'error', 'notice');
|
||||
/**
|
||||
* Contains the CI instance.
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
private $CI;
|
||||
|
||||
/**
|
||||
* Contains the available message types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $types = array('success', 'error', 'notice');
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -24,10 +36,10 @@ class Messages {
|
||||
* @param array $params
|
||||
*/
|
||||
public function __construct($params = array()) {
|
||||
$this->_ci =& get_instance();
|
||||
$this->_ci->load->library('session');
|
||||
$this->CI =& get_instance();
|
||||
$this->CI->load->library('session');
|
||||
// check if theres already messages, if not, initialise the messages array in the session
|
||||
$messages = $this->_ci->session->userdata('messages');
|
||||
$messages = $this->CI->session->userdata('messages');
|
||||
if (empty($messages)) {
|
||||
$this->clear();
|
||||
}
|
||||
@@ -38,22 +50,22 @@ class Messages {
|
||||
*/
|
||||
public function clear() {
|
||||
$messages = array();
|
||||
foreach ($this->_types as $type) {
|
||||
foreach ($this->types as $type) {
|
||||
$messages[$type] = array();
|
||||
}
|
||||
$this->_ci->session->set_userdata('messages', $messages);
|
||||
$this->CI->session->set_userdata('messages', $messages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a message (default type is 'notice').
|
||||
*/
|
||||
public function add($message, $type = 'notice') {
|
||||
$messages = $this->_ci->session->userdata('messages');
|
||||
$messages = $this->CI->session->userdata('messages');
|
||||
// handle PEAR errors gracefully
|
||||
if (is_a($message, 'PEAR_Error')) {
|
||||
$message = $message->getMessage();
|
||||
$type = 'error';
|
||||
} else if (!in_array($type, $this->_types)) {
|
||||
} else if (!in_array($type, $this->types)) {
|
||||
// set the type to message if the user specified a type that's unknown
|
||||
$type = 'notice';
|
||||
}
|
||||
@@ -61,7 +73,7 @@ class Messages {
|
||||
if (!in_array($message, $messages[$type]) && is_string($message)) {
|
||||
$messages[$type][] = $message;
|
||||
}
|
||||
$messages = $this->_ci->session->set_userdata('messages', $messages);
|
||||
$messages = $this->CI->session->set_userdata('messages', $messages);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,13 +83,13 @@ class Messages {
|
||||
* @return boolean|integer
|
||||
*/
|
||||
public function sum($type = null) {
|
||||
$messages = $this->_ci->session->userdata('messages');
|
||||
$messages = $this->CI->session->userdata('messages');
|
||||
if (!empty($type)) {
|
||||
$i = count($messages[$type]);
|
||||
return $i;
|
||||
}
|
||||
$i = 0;
|
||||
foreach ($this->_types as $type) {
|
||||
foreach ($this->types as $type) {
|
||||
$i += count($messages[$type]);
|
||||
}
|
||||
return $i > 0 ? $i : false;
|
||||
@@ -90,7 +102,7 @@ class Messages {
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($type = null) {
|
||||
$messages = $this->_ci->session->userdata('messages');
|
||||
$messages = $this->CI->session->userdata('messages');
|
||||
if (!empty($type)) {
|
||||
if (count($messages[$type]) == 0) {
|
||||
return false;
|
||||
@@ -99,7 +111,7 @@ class Messages {
|
||||
}
|
||||
// return false if there actually are no messages in the session
|
||||
$i = 0;
|
||||
foreach ($this->_types as $type) {
|
||||
foreach ($this->types as $type) {
|
||||
$i += count($messages[$type]);
|
||||
}
|
||||
if ($i == 0) {
|
||||
@@ -108,7 +120,7 @@ class Messages {
|
||||
|
||||
// order return by order of type array above
|
||||
// i.e. success, error, warning and then informational messages last
|
||||
foreach ($this->_types as $type) {
|
||||
foreach ($this->types as $type) {
|
||||
$return[$type] = $messages[$type];
|
||||
}
|
||||
$this->clear();
|
||||
|
||||
Reference in New Issue
Block a user