All pastes #332685 Raw Edit

Stuff

public text v1 · immutable
#332685 ·published 2007-01-30 05:22 UTC
rendered paste body
<?php
class MessageStack{
	var $_messages;
	var $_messageTypes;
	
	function MessageStack() {
		$this->resetVars();
	} # end constructor
	
	function resetVars(){
		$this->_messages = array();
		$this->_messageTypes = array('error', 'warning', 'notice', 'success');
		$this->getMessagesFromSession();
		return;
	} # end method
	
	function getMessagesFromSession(){
		if ( SESSION_STARTED === true ){
			if ( array_key_exists('messageToSession', $_SESSION) ){
				$this->_messages = array_merge($this->_messages, $_SESSION['messageToSession']);
				unset($_SESSION['messageToSession']);
				return;
			} else {
				return;
			}
		} else {
			return;
		}
	} # end method
	
	function addMessage($message, $type = 'Error'){
		$this->_messages[] = array('type' => $this->checkType($type), 
		                           'message' => $message
								   );
		return;
	} # end method
	
	function checkType($type){
		if (!in_array(strtolower($type), $this->_messageTypes)){
			$type = 'Error';
		}
		return ucfirst($type);
	} # end method
	
	function addMessageToSession($message, $type = 'Error'){
		if ( SESSION_STARTED === true ){
			$_message = array('type' => $this->checkType($type),
			                  'message' => $message);
			if ( !array_key_exists('messageToSession', $_SESSION) ){
				$_SESSION['messageToSession'] = array();
			}
			$_SESSION['messageToSession'][] = $_message;
			return;
		} else {
			return;
		}
	} # end method
	
	function countMessages(){
		return count($this->_messages);
	} # end method
	
	function formatMessage($data){
		return '<div class="MessageStack' . $this->checkType($data['type']).'">' . $data['message'] . '</div>' . "\n";
	} # end method
	
	function outputStack($type = NULL){
		$output = '';
		if ($this->countMessages() > 0){
			if (notNull($type)){
				foreach($this->_messages as $index => $message){
					if (ucfirst($message['type']) == ucfirst($type)){
						$output .= $this->formatMessage($message);
					} # end if
				} # end foreach
			} else {
				foreach($this->_messages as $index => $message){
					$output .= $this->formatMessage($message);
				} # end foreach			
			} # end if/else
		} # end countMessages
		return $output;
	} # end method
	
} # end class
?>