BxBaseMenu.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. protected $_aOptionalParams = array('target' => '', 'onclick' => '');
  18. public function __construct ($aObject, $oTemplate) {
  19. parent::__construct ($aObject);
  20. if ($oTemplate)
  21. $this->_oTemplate = $oTemplate;
  22. else
  23. $this->_oTemplate = BxDolTemplate::getInstance();
  24. }
  25. /**
  26. * Get menu code.
  27. * @return string
  28. */
  29. public function getCode () {
  30. $aVars = $this->_getTemplateVars ();
  31. if (empty($aVars['bx_repeat:menu_items']))
  32. return false;
  33. $this->_addJsCss();
  34. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  35. }
  36. /**
  37. * Get template variables array
  38. * @return array
  39. */
  40. protected function _getTemplateVars () {
  41. return array (
  42. 'object' => $this->_sObject,
  43. 'bx_repeat:menu_items' => $this->getMenuItems (),
  44. );
  45. }
  46. /**
  47. * Get menu items array, which are ready to pass to template.
  48. * @return array
  49. */
  50. public function getMenuItems () {
  51. $aRet = array();
  52. if (!isset($this->_aObject['menu_items']))
  53. $this->_aObject['menu_items'] = $this->_oQuery->getMenuItems();
  54. foreach ($this->_aObject['menu_items'] as $a) {
  55. if (isset($a['active']) && !$a['active'])
  56. continue;
  57. if (isset($a['visible_for_levels']) && !$this->_isVisible($a))
  58. continue;
  59. $a['title'] = _t($a['title']);
  60. $a = $this->_replaceMarkers($a);
  61. $mixedAddon = $this->_getMenuAddon($a);
  62. $this->addMarkers(array('addon' => $mixedAddon));
  63. $a = $this->_replaceMarkers($a);
  64. list ($sIcon, $sIconUrl) = $this->_getMenuIcon($a);
  65. $a['class_add'] = $this->_isSelected($a) ? 'bx-menu-tab-active' : '';
  66. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  67. $a['title_attr'] = bx_html_attribute($a['title']);
  68. $a['bx_if:image'] = array (
  69. 'condition' => (bool)$sIconUrl,
  70. 'content' => array('icon_url' => $sIconUrl),
  71. );
  72. $a['bx_if:icon'] = array (
  73. 'condition' => (bool)$sIcon,
  74. 'content' => array('icon' => $sIcon),
  75. );
  76. $a['bx_if:title'] = array (
  77. 'condition' => (bool)$a['title'],
  78. 'content' => array('title' => $a['title']),
  79. );
  80. $a['bx_if:addon'] = array (
  81. 'condition' => (bool)$mixedAddon,
  82. 'content' => array('addon' => $mixedAddon),
  83. );
  84. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  85. if (!isset($a[$sName]))
  86. $a[$sName] = $sDefaultValue;
  87. $aRet[] = $a;
  88. }
  89. return $aRet;
  90. }
  91. protected function _getMenuIcon ($a) {
  92. $sIcon = false;
  93. $sIconUrl = false;
  94. if (!empty($a['icon'])) {
  95. if ((int)$a['icon'] > 0 ) {
  96. bx_import('BxDolStorage');
  97. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  98. $sIconUrl = $oStorage ? $oStorage->getFileUrlById((int)$a['icon']) : false;
  99. } else {
  100. if (false === strpos($a['icon'], '.')) // font icons
  101. $sIcon = $a['icon'];
  102. else
  103. $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 ?!
  104. }
  105. }
  106. return array ($sIcon, $sIconUrl);
  107. }
  108. protected function _getMenuAddon ($aMenuItem) {
  109. if (empty($aMenuItem['addon']))
  110. return '';
  111. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  112. }
  113. /**
  114. * Add css/js files which are needed for menu display and functionality.
  115. */
  116. protected function _addJsCss() {
  117. $this->_oTemplate->addCss('menu.css');
  118. }
  119. }
  120. /** @} */