123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- /**
- *
- * Cube Framework $Id$ 33tNb6gPF5z1q1WGuwD/267TbrBfalBhsX5eDc9csp0=
- *
- * @link http://codecu.be/framework
- * @copyright Copyright (c) 2014 CodeCube SRL
- * @license http://codecu.be/framework/license Commercial License
- *
- * @version 1.0
- */
- /**
- * flash messenger action helper
- */
- namespace Cube\Controller\Action\Helper;
- use Cube\Controller\Front,
- Cube\Session;
- class FlashMessenger extends AbstractHelper
- {
- const SESSION_NAMESPACE = 'FlashMessenger';
- const MESSAGES_NAMESPACE = 'Messages';
- /**
- *
- * name of session variable
- *
- * @var string
- */
- protected $_name = null;
- /**
- *
- * array of messages held by the flash messenger
- *
- * @var array
- */
- protected $_messages = array();
- /**
- *
- * session object
- *
- * @var \Cube\Session
- */
- protected $_session;
- /**
- *
- * class constructor
- *
- * @param string $name
- */
- public function __construct($name = null)
- {
- $this->setName($name);
- $this->setSession();
- }
- /**
- *
- * get session variable name
- *
- * @return string
- */
- public function getName()
- {
- return $this->_name;
- }
- /**
- *
- * set session variable name
- *
- * @param string $name
- * @return \Cube\Controller\Action\Helper\FlashMessenger
- */
- public function setName($name = null)
- {
- if ($name === null) {
- $name = self::MESSAGES_NAMESPACE;
- }
- $this->_name = $name;
- return $this;
- }
- /**
- *
- * get session object
- *
- * @return \Cube\Session
- */
- public function getSession()
- {
- if (!($this->_session instanceof Session)) {
- $this->setSession();
- }
- return $this->_session;
- }
- /**
- *
- * set session object
- *
- * @param \Cube\Session $session
- * @return \Cube\Controller\Action\Helper\FlashMessenger
- */
- public function setSession(Session $session = null)
- {
- if ($session === null) {
- $session = Front::getInstance()->getBootstrap()->getResource('session');
- }
- if (!$session instanceof Session) {
- $session = new Session();
- $session->setNamespace(self::SESSION_NAMESPACE);
- }
- $this->_session = $session;
- return $this;
- }
- /**
- *
- * add a new message to the flash messenger action helper
- *
- * @param mixed $message
- * @return \Cube\Controller\Action\Helper\FlashMessenger
- */
- public function setMessage($message)
- {
- $this->_messages = $this->getMessages();
- array_push($this->_messages, $message);
- $this->_session->set($this->_name, $this->_messages);
- return $this;
- }
- /**
- *
- * get all messages saved in the current flash messenger helper
- *
- * @return array
- */
- public function getMessages()
- {
- $this->_messages = (array) $this->_session->get($this->_name);
- $this->_session->unregister($this->_name);
- return $this->_messages;
- }
- }
|