Class naming conventions

This commit is contained in:
Eike Foken
2011-08-08 20:29:05 +02:00
parent 67210327c0
commit 4c8e0b9980

View File

@@ -15,89 +15,89 @@
class Messages class Messages
{ {
var $_ci; var $_ci;
var $_types = array('success', 'error', 'notice'); var $_types = array('success', 'error', 'notice');
function Messages($params = array()) function Messages($params = array())
{ {
$this->_ci =& get_instance(); $this->_ci =& get_instance();
$this->_ci->load->library('session'); $this->_ci->load->library('session');
// check if theres already messages, if not, initialise the messages array in the 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)) { if (empty($messages)) {
$this->clear(); $this->clear();
} }
} }
// clear all messages // clear all messages
function clear() function clear()
{ {
$messages = array(); $messages = array();
foreach ($this->_types as $type) { foreach ($this->_types as $type) {
$messages[$type] = array(); $messages[$type] = array();
} }
$this->_ci->session->set_userdata('messages', $messages); $this->_ci->session->set_userdata('messages', $messages);
} }
// add a message, default type is message // add a message, default type is message
function add($message, $type = 'message') function add($message, $type = 'message')
{ {
$messages = $this->_ci->session->userdata('messages'); $messages = $this->_ci->session->userdata('messages');
// handle PEAR errors gracefully // handle PEAR errors gracefully
if (is_a($message, 'PEAR_Error')) { if (is_a($message, 'PEAR_Error')) {
$message = $message->getMessage(); $message = $message->getMessage();
$type = 'error'; $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 // set the type to message if the user specified a type that's unknown
$type = 'message'; $type = 'message';
} }
// don't repeat messages! // don't repeat messages!
if (!in_array($message, $messages[$type]) && is_string($message)) { if (!in_array($message, $messages[$type]) && is_string($message)) {
$messages[$type][] = $message; $messages[$type][] = $message;
} }
$messages = $this->_ci->session->set_userdata('messages', $messages); $messages = $this->_ci->session->set_userdata('messages', $messages);
} }
// return messages of given type or all types, return false if none // return messages of given type or all types, return false if none
function sum($type = null) function sum($type = null)
{ {
$messages = $this->_ci->session->userdata('messages'); $messages = $this->_ci->session->userdata('messages');
if (!empty($type)) { if (!empty($type)) {
$i = count($messages[$type]); $i = count($messages[$type]);
return $i; return $i;
} }
$i = 0; $i = 0;
foreach ($this->_types as $type) { foreach ($this->_types as $type) {
$i += count($messages[$type]); $i += count($messages[$type]);
} }
return $i; return $i;
} }
// return messages of given type or all types, return false if none, clearing stack // return messages of given type or all types, return false if none, clearing stack
function get($type = null) function get($type = null)
{ {
$messages = $this->_ci->session->userdata('messages'); $messages = $this->_ci->session->userdata('messages');
if (!empty($type)) { if (!empty($type)) {
if (count($messages[$type]) == 0) { if (count($messages[$type]) == 0) {
return false; return false;
} }
return $messages[$type]; return $messages[$type];
} }
// return false if there actually are no messages in the session // return false if there actually are no messages in the session
$i = 0; $i = 0;
foreach ($this->_types as $type) { foreach ($this->_types as $type) {
$i += count($messages[$type]); $i += count($messages[$type]);
} }
if ($i == 0) { if ($i == 0) {
return false; return false;
} }
// order return by order of type array above // order return by order of type array above
// i.e. success, error, warning and then informational messages last // i.e. success, error, warning and then informational messages last
foreach ($this->_types as $type) { foreach ($this->_types as $type) {
$return[$type] = $messages[$type]; $return[$type] = $messages[$type];
} }
$this->clear(); $this->clear();
return $return; return $return;
} }
} }