Source of file FlashMessenger.php
Size: 8,454 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/View/Helper/FlashMessenger.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\View\Helper; use Zend\Mvc\Controller\Plugin\FlashMessenger as PluginFlashMessenger; use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\ServiceManager\ServiceLocatorInterface; use Zend\I18n\View\Helper\AbstractTranslatorHelper; /** * Helper to proxy the plugin flash messenger */ class FlashMessenger extends AbstractTranslatorHelper implements ServiceLocatorAwareInterface { /** * Default attributes for the open format tag * * @var array */ protected $classMessages = array( PluginFlashMessenger::NAMESPACE_INFO => 'info', PluginFlashMessenger::NAMESPACE_ERROR => 'error', PluginFlashMessenger::NAMESPACE_SUCCESS => 'success', PluginFlashMessenger::NAMESPACE_DEFAULT => 'default', PluginFlashMessenger::NAMESPACE_WARNING => 'warning', ); /** * Templates for the open/close/separators for message tags * * @var string */ protected $messageCloseString = '</li></ul>'; protected $messageOpenFormat = '<ul%s><li>'; protected $messageSeparatorString = '</li><li>'; /** * Html escape helper * * @var EscapeHtml */ protected $escapeHtmlHelper; /** * Flash messenger plugin * * @var PluginFlashMessenger */ protected $pluginFlashMessenger; /** * Service locator * * @var ServiceLocatorInterface */ protected $serviceLocator; /** * Returns the flash messenger plugin controller * * @param string|null $namespace * @return FlashMessenger|PluginFlashMessenger */ public function __invoke($namespace = null) { if (null === $namespace) { return $this; } $flashMessenger = $this->getPluginFlashMessenger(); return $flashMessenger->getMessagesFromNamespace($namespace); } /** * Proxy the flash messenger plugin controller * * @param string $method * @param array $argv * @return mixed */ public function __call($method, $argv) { $flashMessenger = $this->getPluginFlashMessenger(); return call_user_func_array(array($flashMessenger, $method), $argv); } /** * Render Messages * * @param string $namespace * @param array $classes * @return string */ public function render($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $classes = array()) { $flashMessenger = $this->getPluginFlashMessenger(); $messages = $flashMessenger->getMessagesFromNamespace($namespace); return $this->renderMessages($namespace, $messages, $classes); } /** * Render Current Messages * * @param string $namespace * @param array $classes * @return string */ public function renderCurrent($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $classes = array()) { $flashMessenger = $this->getPluginFlashMessenger(); $messages = $flashMessenger->getCurrentMessagesFromNamespace($namespace); return $this->renderMessages($namespace, $messages, $classes); } /** * Render Messages * * @param array $messages * @param array $classes * @return string */ protected function renderMessages($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $messages = array(), array $classes = array()) { // Prepare classes for opening tag if (empty($classes)) { if (isset($this->classMessages[$namespace])) { $classes = $this->classMessages[$namespace]; } else { $classes = $this->classMessages[PluginFlashMessenger::NAMESPACE_DEFAULT]; } $classes = array($classes); } // Flatten message array $escapeHtml = $this->getEscapeHtmlHelper(); $messagesToPrint = array(); $translator = $this->getTranslator(); $translatorTextDomain = $this->getTranslatorTextDomain(); array_walk_recursive($messages, function ($item) use (&$messagesToPrint, $escapeHtml, $translator, $translatorTextDomain) { if ($translator !== null) { $item = $translator->translate( $item, $translatorTextDomain ); } $messagesToPrint[] = $escapeHtml($item); }); if (empty($messagesToPrint)) { return ''; } // Generate markup $markup = sprintf($this->getMessageOpenFormat(), ' class="' . implode(' ', $classes) . '"'); $markup .= implode(sprintf($this->getMessageSeparatorString(), ' class="' . implode(' ', $classes) . '"'), $messagesToPrint); $markup .= $this->getMessageCloseString(); return $markup; } /** * Set the string used to close message representation * * @param string $messageCloseString * @return FlashMessenger */ public function setMessageCloseString($messageCloseString) { $this->messageCloseString = (string) $messageCloseString; return $this; } /** * Get the string used to close message representation * * @return string */ public function getMessageCloseString() { return $this->messageCloseString; } /** * Set the formatted string used to open message representation * * @param string $messageOpenFormat * @return FlashMessenger */ public function setMessageOpenFormat($messageOpenFormat) { $this->messageOpenFormat = (string) $messageOpenFormat; return $this; } /** * Get the formatted string used to open message representation * * @return string */ public function getMessageOpenFormat() { return $this->messageOpenFormat; } /** * Set the string used to separate messages * * @param string $messageSeparatorString * @return FlashMessenger */ public function setMessageSeparatorString($messageSeparatorString) { $this->messageSeparatorString = (string) $messageSeparatorString; return $this; } /** * Get the string used to separate messages * * @return string */ public function getMessageSeparatorString() { return $this->messageSeparatorString; } /** * Set the flash messenger plugin * * @param PluginFlashMessenger $pluginFlashMessenger * @return FlashMessenger */ public function setPluginFlashMessenger(PluginFlashMessenger $pluginFlashMessenger) { $this->pluginFlashMessenger = $pluginFlashMessenger; return $this; } /** * Get the flash messenger plugin * * @return PluginFlashMessenger */ public function getPluginFlashMessenger() { if (null === $this->pluginFlashMessenger) { $this->setPluginFlashMessenger(new PluginFlashMessenger()); } return $this->pluginFlashMessenger; } /** * Set the service locator. * * @param ServiceLocatorInterface $serviceLocator * @return AbstractHelper */ public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { $this->serviceLocator = $serviceLocator; return $this; } /** * Get the service locator. * * @return ServiceLocatorInterface */ public function getServiceLocator() { return $this->serviceLocator; } /** * Retrieve the escapeHtml helper * * @return EscapeHtml */ protected function getEscapeHtmlHelper() { if ($this->escapeHtmlHelper) { return $this->escapeHtmlHelper; } if (method_exists($this->getView(), 'plugin')) { $this->escapeHtmlHelper = $this->view->plugin('escapehtml'); } if (!$this->escapeHtmlHelper instanceof EscapeHtml) { $this->escapeHtmlHelper = new EscapeHtml(); } return $this->escapeHtmlHelper; } } |