/src/DluTwBootstrap/View/Helper/Navigation/AbstractHelper.php

https://bitbucket.org/dominicwatson/dlutwbootstrap-active · PHP · 320 lines · 249 code · 30 blank · 41 comment · 31 complexity · 96c958c3ae69eabae6b853795890e25c MD5 · raw file

  1. <?php
  2. namespace DluTwBootstrap\View\Helper\Navigation;
  3. use Zend\View\Helper\Navigation\AbstractHelper as AbstractZfNavigationHelper;
  4. /**
  5. * Abstract Helper
  6. * @package DluTwBootstrap
  7. * @copyright David Lukas (c) - http://www.zfdaily.com
  8. * @license http://www.zfdaily.com/code/license New BSD License
  9. * @link http://www.zfdaily.com
  10. * @link https://bitbucket.org/dlu/dlutwbootstrap
  11. */
  12. abstract class AbstractHelper extends AbstractZfNavigationHelper
  13. {
  14. const ALIGN_LEFT = 'left';
  15. const ALIGN_RIGHT = 'right';
  16. /* *********************** METHODS *************************** */
  17. protected function renderContainer(\Zend\Navigation\Navigation $container,
  18. $renderIcons = true,
  19. $activeIconInverse = true,
  20. array $options = array()) {
  21. $pages = $container->getPages();
  22. $html = '';
  23. foreach ($pages as $page) {
  24. /* @var $page \Zend\Navigation\Page\AbstractPage */
  25. if($page->hasPages()) {
  26. //Dropdown menu
  27. $html .= "\n" . $this->renderDropdown($page, $renderIcons, $activeIconInverse, $options);
  28. } else {
  29. $html .= "\n" . $this->renderItem($page, $renderIcons, $activeIconInverse, false, $options);
  30. }
  31. }
  32. $html = $this->decorateContainer($html, $container, $renderIcons, $activeIconInverse, $options);
  33. return $html;
  34. }
  35. abstract protected function decorateContainer($content,
  36. \Zend\Navigation\Navigation $container,
  37. $renderIcons = true,
  38. $activeIconInverse = true,
  39. array $options = array());
  40. protected function renderItem(\Zend\Navigation\Page\AbstractPage $page,
  41. $renderIcons = true,
  42. $activeIconInverse = true,
  43. $renderInDropdown = false,
  44. array $options = array()) {
  45. if(!$this->accept($page)) {
  46. return '';
  47. }
  48. if($page->navHeader) {
  49. //Nav Header
  50. if($renderInDropdown) {
  51. $itemHtml = $this->renderNavHeaderInDropdown($page, $renderIcons, $activeIconInverse, $options);
  52. $html = $this->decorateNavHeaderInDropdown($itemHtml, $page, $renderIcons, $activeIconInverse, $options);
  53. } else {
  54. $itemHtml = $this->renderNavHeader($page, $renderIcons, $activeIconInverse, $options);
  55. $html = $this->decorateNavHeader($itemHtml, $page, $renderIcons, $activeIconInverse, $options);
  56. }
  57. } elseif($page->divider) {
  58. //Divider
  59. if($renderInDropdown) {
  60. $itemHtml = $this->renderDividerInDropdown($page, $options);
  61. $html = $this->decorateDividerInDropdown($itemHtml, $page, $options);
  62. } else {
  63. $itemHtml = $this->renderDivider($page, $options);
  64. $html = $this->decorateDivider($itemHtml, $page, $options);
  65. }
  66. } else {
  67. //Nav link
  68. if($renderInDropdown) {
  69. $itemHtml = $this->renderLinkInDropdown($page, $renderIcons, $activeIconInverse, $options);
  70. $html = $this->decorateLinkInDropdown($itemHtml, $page, $renderIcons, $activeIconInverse, $options);
  71. } else {
  72. $itemHtml = $this->renderLink($page, $renderIcons, $activeIconInverse, $options);
  73. $html = $this->decorateLink($itemHtml, $page, $renderIcons, $activeIconInverse, $options);
  74. }
  75. }
  76. return $html;
  77. }
  78. protected function renderNavHeader(\Zend\Navigation\Page\AbstractPage $item,
  79. $renderIcons = true,
  80. $activeIconInverse = true,
  81. array $options = array()) {
  82. $icon = $this->htmlifyIcon($item, $renderIcons, $activeIconInverse);
  83. $label = $this->translate($item->getLabel());
  84. $html = $icon . $this->getView()->escapeHtml($label);
  85. return $html;
  86. }
  87. protected function renderNavHeaderInDropdown(\Zend\Navigation\Page\AbstractPage $item,
  88. $renderIcons = true,
  89. $activeIconInverse = true,
  90. array $options = array()) {
  91. $html = $this->renderNavHeader($item, $renderIcons, $activeIconInverse, $options);
  92. return $html;
  93. }
  94. abstract protected function decorateNavHeader($content,
  95. \Zend\Navigation\Page\AbstractPage $item,
  96. $renderIcons = true,
  97. $activeIconInverse = true,
  98. array $options = array());
  99. protected function decorateNavHeaderInDropdown($content,
  100. \Zend\Navigation\Page\AbstractPage $item,
  101. $renderIcons = true,
  102. $activeIconInverse = true,
  103. array $options = array()) {
  104. $html = '<li class="nav-header">' . $content . '</li>';
  105. return $html;
  106. }
  107. protected function renderDivider(\Zend\Navigation\Page\AbstractPage $item,
  108. array $options = array()) {
  109. return '';
  110. }
  111. protected function renderDividerInDropdown(\Zend\Navigation\Page\AbstractPage $item,
  112. array $options = array()) {
  113. $html = $this->renderDivider($item, $options);
  114. return $html;
  115. }
  116. abstract protected function decorateDivider($content,
  117. \Zend\Navigation\Page\AbstractPage $item,
  118. array $options = array());
  119. protected function decorateDividerInDropdown($content,
  120. \Zend\Navigation\Page\AbstractPage $item,
  121. array $options = array()) {
  122. $html = '<li class="divider">' . $content . '</li>';
  123. return $html;
  124. }
  125. protected function renderLink(\Zend\Navigation\Page\AbstractPage $page,
  126. $renderIcons = true,
  127. $activeIconInverse = true,
  128. array $options = array()) {
  129. //Assemble html
  130. $html = $this->htmlifyA($page, $renderIcons, $activeIconInverse);
  131. return $html;
  132. }
  133. protected function renderLinkInDropdown(\Zend\Navigation\Page\AbstractPage $page,
  134. $renderIcons = true,
  135. $activeIconInverse = true,
  136. array $options = array()) {
  137. $html = $this->renderLink($page, $renderIcons, $activeIconInverse, $options);
  138. return $html;
  139. }
  140. abstract protected function decorateLink($content,
  141. \Zend\Navigation\Page\AbstractPage $page,
  142. $renderIcons = true,
  143. $activeIconInverse = true,
  144. array $options = array());
  145. protected function decorateLinkInDropdown($content,
  146. \Zend\Navigation\Page\AbstractPage $page,
  147. $renderIcons = true,
  148. $activeIconInverse = true,
  149. array $options = array()) {
  150. //Active
  151. if($page->isActive(true)) {
  152. $liClass = ' class="active"';
  153. } else {
  154. $liClass = '';
  155. }
  156. $html = '<li' . $liClass . '>' . $content . '</li>';
  157. return $html;
  158. }
  159. protected function renderDropdown(\Zend\Navigation\Page\AbstractPage $page,
  160. $renderIcons = true,
  161. $activeIconInverse = true,
  162. array $options = array()) {
  163. //Get label and title
  164. $label = $this->translate($page->getLabel());
  165. $title = $this->translate($page->getTitle());
  166. $escaper = $this->view->plugin('escapeHtml');
  167. //Get attribs
  168. $class = $page->getClass();
  169. $this->addWord('dropdown-toggle', $class);
  170. $aAttribs = array(
  171. 'title' => $title,
  172. 'class' => $class,
  173. 'data-toggle' => 'dropdown',
  174. 'href' => '#',
  175. );
  176. if($renderIcons) {
  177. $iconHtml = $this->htmlifyIcon($page, $activeIconInverse);
  178. } else {
  179. $iconHtml = '';
  180. }
  181. $html = '<a' . $this->htmlAttribs($aAttribs) . '>'
  182. . $iconHtml . $escaper($label) . ' <b class="caret"></b></a>';
  183. $html .= "\n" . '<ul class="dropdown-menu">';
  184. $pages = $page->getPages();
  185. foreach($pages as $dropdownPage) {
  186. /* @var $dropdownPage \Zend\Navigation\Page\AbstractPage */
  187. $html .= "\n" . $this->renderItem($dropdownPage, $renderIcons, $activeIconInverse, true, $options);
  188. }
  189. $html .= "\n</ul>";
  190. $html = $this->decorateDropdown($html, $page, $renderIcons, $activeIconInverse, $options);
  191. return $html;
  192. }
  193. abstract protected function decorateDropdown($content,
  194. \Zend\Navigation\Page\AbstractPage $page,
  195. $renderIcons = true,
  196. $activeIconInverse = true,
  197. array $options = array());
  198. /**
  199. * Returns an HTML string containing an 'a' element for the given page
  200. * @param \Zend\Navigation\Page\AbstractPage $page
  201. * @param bool $renderIcons
  202. * @param bool $activeIconInverse
  203. * @return string
  204. */
  205. public function htmlifyA(\Zend\Navigation\Page\AbstractPage $page, $renderIcons = true, $activeIconInverse = true) {
  206. // get label and title for translating
  207. $label = $this->translate($page->getLabel());
  208. $title = $this->translate($page->getTitle());
  209. $escaper = $this->view->plugin('escapeHtml');
  210. //Get attribs for anchor element
  211. $attribs = array(
  212. 'id' => $page->getId(),
  213. 'title' => $title,
  214. 'class' => $page->getClass(),
  215. 'href' => $page->getHref(),
  216. 'target' => $page->getTarget()
  217. );
  218. if($renderIcons) {
  219. $iconHtml = $this->htmlifyIcon($page, $activeIconInverse);
  220. } else {
  221. $iconHtml = '';
  222. }
  223. $html = '<a' . $this->htmlAttribs($attribs) . '>'
  224. . $iconHtml . $escaper($label)
  225. . '</a>';
  226. return $html;
  227. }
  228. protected function htmlifyIcon(\Zend\Navigation\Page\AbstractPage $item, $activeIconInverse = true) {
  229. if($item->icon) {
  230. $iClass = $item->icon;
  231. if($activeIconInverse && $item->isActive(true)) {
  232. $classes = explode(' ', $iClass);
  233. $iconWhiteClassKey = array_search('icon-white', $classes);
  234. if($iconWhiteClassKey === false) {
  235. //icon-white class not found
  236. $iClass .= ' icon-white';
  237. } else {
  238. //icon-white class found
  239. unset($classes[$iconWhiteClassKey]);
  240. $iClass = implode(' ', $classes);
  241. }
  242. }
  243. $icon = '<i class="' . $iClass . '"></i> ';
  244. } else {
  245. $icon = '';
  246. }
  247. return $icon;
  248. }
  249. /**
  250. * View helper entry point:
  251. * Retrieves helper and optionally sets container to operate on
  252. * @param \Zend\Navigation\Navigation $container [optional] container to operate on
  253. * @return TwbNavbar fluent interface, returns self
  254. */
  255. public function __invoke(\Zend\Navigation\Navigation $container = null) {
  256. if (null !== $container) {
  257. $this->setContainer($container);
  258. }
  259. return $this;
  260. }
  261. protected function translate($text) {
  262. $t = $this->getTranslator();
  263. if ($this->isTranslatorEnabled()
  264. && $t
  265. && is_string($text)
  266. && !empty($text)) {
  267. $text = $t->translate($text);
  268. }
  269. return $text;
  270. }
  271. /**
  272. * If missing in the text, adds the space separated word to the text
  273. * @param string $word
  274. * @param string $text
  275. */
  276. protected function addWord($word, &$text) {
  277. $text = trim($text);
  278. if(!$text) {
  279. $wordsLower = array();
  280. $words = array();
  281. } else {
  282. $wordsLower = explode(' ', strtolower($text));
  283. $words = explode(' ', $text);
  284. }
  285. if(!in_array(strtolower($word), $wordsLower)) {
  286. $words[] = $word;
  287. $text = implode(' ', $words);
  288. }
  289. }
  290. }