BxBaseMenu.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolMenu');
  10. bx_import('BxDolPaginate');
  11. /**
  12. * Menu representation.
  13. * @see BxDolMenu
  14. */
  15. class BxBaseMenu extends BxDolMenu {
  16. protected $_oTemplate;
  17. public function __construct ($aObject, $oTemplate) {
  18. parent::__construct ($aObject);
  19. if ($oTemplate)
  20. $this->_oTemplate = $oTemplate;
  21. else
  22. $this->_oTemplate = BxDolTemplate::getInstance();
  23. }
  24. /**
  25. * Get menu code.
  26. * @return string
  27. */
  28. public function getCode () {
  29. $aVars = array (
  30. 'object' => $this->_sObject,
  31. 'bx_repeat:menu_items' => $this->_getMenuItems (),
  32. );
  33. if (empty($aVars['bx_repeat:menu_items']))
  34. return false;
  35. $this->_addJsCss();
  36. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  37. }
  38. /**
  39. * Get menu items array, which are ready to pass to template.
  40. * @return array
  41. */
  42. protected function _getMenuItems () {
  43. $aRet = array();
  44. if (!isset($this->_aObject['menu_items']))
  45. $this->_aObject['menu_items'] = $this->_oQuery->getMenuItems();
  46. foreach ($this->_aObject['menu_items'] as $a) {
  47. if (isset($a['active']) && !$a['active'])
  48. continue;
  49. if (isset($a['visible_for_levels']) && !$this->_isVisible($a))
  50. continue;
  51. $a = $this->_replaceMarkers($a);
  52. list ($sIcon, $sIconUrl) = $this->_getMenuIcon($a);
  53. $a['class_add'] = $this->_isSelected($a) ? 'bx-menu-tab-active' : '';
  54. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  55. $a['title'] = _t($a['title']);
  56. $a['bx_if:image'] = array (
  57. 'condition' => (bool)$sIconUrl,
  58. 'content' => array('icon_url' => $sIconUrl),
  59. );
  60. $a['bx_if:icon'] = array (
  61. 'condition' => (bool)$sIcon,
  62. 'content' => array('icon' => $sIcon),
  63. );
  64. $aRet[] = $a;
  65. }
  66. return $aRet;
  67. }
  68. protected function _getMenuIcon ($a) {
  69. $sIcon = false;
  70. $sIconUrl = false;
  71. if (!empty($a['icon'])) {
  72. if ((int)$a['icon'] > 0 ) {
  73. bx_import('BxDolStorage');
  74. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  75. $sIconUrl = $oStorage ? $oStorage->getFileUrlById((int)$a['icon']) : false;
  76. } else {
  77. if (false === strpos($a['icon'], '.')) // font icons
  78. $sIcon = $a['icon'];
  79. else
  80. $sIconUrl = $this->_oTemplate->getIconUrl($a['icon']); // TODO: it is used in studio only, so move this line of code to studio or maybe all studio icons will use font icons too ?!
  81. }
  82. }
  83. return array ($sIcon, $sIconUrl);
  84. }
  85. /**
  86. * Add css/js files which are needed for menu display and functionality.
  87. */
  88. protected function _addJsCss() {
  89. $this->_oTemplate->addCss('menu.css');
  90. }
  91. }
  92. /** @} */