PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/app/classes/Zend/View/Helper/FlashMessenger.php

https://gitlab.com/jalon/doadoronline
PHP | 281 lines | 130 code | 33 blank | 118 comment | 10 complexity | 99cd150a11b3bfda001670d7c057994d MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\View\Helper;
  10. use Zend\Mvc\Controller\Plugin\FlashMessenger as PluginFlashMessenger;
  11. use Zend\ServiceManager\ServiceLocatorAwareInterface;
  12. use Zend\ServiceManager\ServiceLocatorInterface;
  13. use Zend\View\Helper\AbstractHelper;
  14. use Zend\View\Helper\EscapeHtml;
  15. use Zend\I18n\View\Helper\AbstractTranslatorHelper;
  16. /**
  17. * Helper to proxy the plugin flash messenger
  18. */
  19. class FlashMessenger extends AbstractTranslatorHelper implements ServiceLocatorAwareInterface
  20. {
  21. /**
  22. * Default attributes for the open format tag
  23. *
  24. * @var array
  25. */
  26. protected $classMessages = array(
  27. PluginFlashMessenger::NAMESPACE_INFO => 'info',
  28. PluginFlashMessenger::NAMESPACE_ERROR => 'error',
  29. PluginFlashMessenger::NAMESPACE_SUCCESS => 'success',
  30. PluginFlashMessenger::NAMESPACE_DEFAULT => 'default',
  31. );
  32. /**
  33. * Templates for the open/close/separators for message tags
  34. *
  35. * @var string
  36. */
  37. protected $messageCloseString = '</li></ul>';
  38. protected $messageOpenFormat = '<ul%s><li>';
  39. protected $messageSeparatorString = '</li><li>';
  40. /**
  41. * Html escape helper
  42. *
  43. * @var EscapeHtml
  44. */
  45. protected $escapeHtmlHelper;
  46. /**
  47. * Flash messenger plugin
  48. *
  49. * @var PluginFlashMessenger
  50. */
  51. protected $pluginFlashMessenger;
  52. /**
  53. * Service locator
  54. *
  55. * @var ServiceLocatorInterface
  56. */
  57. protected $serviceLocator;
  58. /**
  59. * Returns the flash messenger plugin controller
  60. *
  61. * @param string|null $namespace
  62. * @return FlashMessenger|PluginFlashMessenger
  63. */
  64. public function __invoke($namespace = null)
  65. {
  66. if (null === $namespace) {
  67. return $this;
  68. }
  69. $flashMessenger = $this->getPluginFlashMessenger();
  70. return $flashMessenger->getMessagesFromNamespace($namespace);
  71. }
  72. /**
  73. * Proxy the flash messenger plugin controller
  74. *
  75. * @param string $method
  76. * @param array $argv
  77. * @return mixed
  78. */
  79. public function __call($method, $argv)
  80. {
  81. $flashMessenger = $this->getPluginFlashMessenger();
  82. return call_user_func_array(array($flashMessenger, $method), $argv);
  83. }
  84. /**
  85. * Render Messages
  86. *
  87. * @param string $namespace
  88. * @param array $classes
  89. * @return string
  90. */
  91. public function render($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $classes = array())
  92. {
  93. $flashMessenger = $this->getPluginFlashMessenger();
  94. $messages = $flashMessenger->getMessagesFromNamespace($namespace);
  95. // Prepare classes for opening tag
  96. if (empty($classes)) {
  97. if (isset($this->classMessages[$namespace])) {
  98. $classes = $this->classMessages[$namespace];
  99. } else {
  100. $classes = $this->classMessages[PluginFlashMessenger::NAMESPACE_DEFAULT];
  101. }
  102. $classes = array($classes);
  103. }
  104. // Flatten message array
  105. $escapeHtml = $this->getEscapeHtmlHelper();
  106. $messagesToPrint = array();
  107. $translator = $this->getTranslator();
  108. $translatorTextDomain = $this->getTranslatorTextDomain();
  109. array_walk_recursive($messages, function ($item) use (&$messagesToPrint, $escapeHtml, $translator, $translatorTextDomain) {
  110. if ($translator !== null) {
  111. $item = $translator->translate(
  112. $item,
  113. $translatorTextDomain
  114. );
  115. }
  116. $messagesToPrint[] = $escapeHtml($item);
  117. });
  118. if (empty($messagesToPrint)) {
  119. return '';
  120. }
  121. // Generate markup
  122. $markup = sprintf($this->getMessageOpenFormat(), ' class="' . implode(' ', $classes) . '"');
  123. $markup .= implode(sprintf($this->getMessageSeparatorString(), ' class="' . implode(' ', $classes) . '"'), $messagesToPrint);
  124. $markup .= $this->getMessageCloseString();
  125. return $markup;
  126. }
  127. /**
  128. * Set the string used to close message representation
  129. *
  130. * @param string $messageCloseString
  131. * @return FlashMessenger
  132. */
  133. public function setMessageCloseString($messageCloseString)
  134. {
  135. $this->messageCloseString = (string) $messageCloseString;
  136. return $this;
  137. }
  138. /**
  139. * Get the string used to close message representation
  140. *
  141. * @return string
  142. */
  143. public function getMessageCloseString()
  144. {
  145. return $this->messageCloseString;
  146. }
  147. /**
  148. * Set the formatted string used to open message representation
  149. *
  150. * @param string $messageOpenFormat
  151. * @return FlashMessenger
  152. */
  153. public function setMessageOpenFormat($messageOpenFormat)
  154. {
  155. $this->messageOpenFormat = (string) $messageOpenFormat;
  156. return $this;
  157. }
  158. /**
  159. * Get the formatted string used to open message representation
  160. *
  161. * @return string
  162. */
  163. public function getMessageOpenFormat()
  164. {
  165. return $this->messageOpenFormat;
  166. }
  167. /**
  168. * Set the string used to separate messages
  169. *
  170. * @param string $messageSeparatorString
  171. * @return FlashMessenger
  172. */
  173. public function setMessageSeparatorString($messageSeparatorString)
  174. {
  175. $this->messageSeparatorString = (string) $messageSeparatorString;
  176. return $this;
  177. }
  178. /**
  179. * Get the string used to separate messages
  180. *
  181. * @return string
  182. */
  183. public function getMessageSeparatorString()
  184. {
  185. return $this->messageSeparatorString;
  186. }
  187. /**
  188. * Set the flash messenger plugin
  189. *
  190. * @param PluginFlashMessenger $pluginFlashMessenger
  191. * @return FlashMessenger
  192. */
  193. public function setPluginFlashMessenger(PluginFlashMessenger $pluginFlashMessenger)
  194. {
  195. $this->pluginFlashMessenger = $pluginFlashMessenger;
  196. return $this;
  197. }
  198. /**
  199. * Get the flash messenger plugin
  200. *
  201. * @return PluginFlashMessenger
  202. */
  203. public function getPluginFlashMessenger()
  204. {
  205. if (null === $this->pluginFlashMessenger) {
  206. $this->setPluginFlashMessenger(new PluginFlashMessenger());
  207. }
  208. return $this->pluginFlashMessenger;
  209. }
  210. /**
  211. * Set the service locator.
  212. *
  213. * @param ServiceLocatorInterface $serviceLocator
  214. * @return AbstractHelper
  215. */
  216. public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
  217. {
  218. $this->serviceLocator = $serviceLocator;
  219. return $this;
  220. }
  221. /**
  222. * Get the service locator.
  223. *
  224. * @return ServiceLocatorInterface
  225. */
  226. public function getServiceLocator()
  227. {
  228. return $this->serviceLocator;
  229. }
  230. /**
  231. * Retrieve the escapeHtml helper
  232. *
  233. * @return EscapeHtml
  234. */
  235. protected function getEscapeHtmlHelper()
  236. {
  237. if ($this->escapeHtmlHelper) {
  238. return $this->escapeHtmlHelper;
  239. }
  240. if (method_exists($this->getView(), 'plugin')) {
  241. $this->escapeHtmlHelper = $this->view->plugin('escapehtml');
  242. }
  243. if (!$this->escapeHtmlHelper instanceof EscapeHtml) {
  244. $this->escapeHtmlHelper = new EscapeHtml();
  245. }
  246. return $this->escapeHtmlHelper;
  247. }
  248. }