BxBaseMenu.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. protected 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. list ($sIcon, $sIconUrl) = $this->_getMenuIcon($a);
  62. $a['class_add'] = $this->_isSelected($a) ? 'bx-menu-tab-active' : '';
  63. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  64. $a['title_attr'] = bx_html_attribute($a['title']);
  65. $a['bx_if:image'] = array (
  66. 'condition' => (bool)$sIconUrl,
  67. 'content' => array('icon_url' => $sIconUrl),
  68. );
  69. $a['bx_if:icon'] = array (
  70. 'condition' => (bool)$sIcon,
  71. 'content' => array('icon' => $sIcon),
  72. );
  73. $a['bx_if:title'] = array (
  74. 'condition' => (bool)$a['title'],
  75. 'content' => array('title' => $a['title']),
  76. );
  77. $mixedAddon = $this->_getMenuAddon($a);
  78. $a['bx_if:addon'] = array (
  79. 'condition' => (bool)$mixedAddon,
  80. 'content' => array('addon' => $mixedAddon),
  81. );
  82. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  83. if (!isset($a[$sName]))
  84. $a[$sName] = $sDefaultValue;
  85. $aRet[] = $a;
  86. }
  87. return $aRet;
  88. }
  89. protected function _getMenuIcon ($a) {
  90. $sIcon = false;
  91. $sIconUrl = false;
  92. if (!empty($a['icon'])) {
  93. if ((int)$a['icon'] > 0 ) {
  94. bx_import('BxDolStorage');
  95. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  96. $sIconUrl = $oStorage ? $oStorage->getFileUrlById((int)$a['icon']) : false;
  97. } else {
  98. if (false === strpos($a['icon'], '.')) // font icons
  99. $sIcon = $a['icon'];
  100. else
  101. $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 ?!
  102. }
  103. }
  104. return array ($sIcon, $sIconUrl);
  105. }
  106. protected function _getMenuAddon ($aMenuItem) {
  107. if (empty($aMenuItem['addon']))
  108. return '';
  109. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  110. }
  111. /**
  112. * Add css/js files which are needed for menu display and functionality.
  113. */
  114. protected function _addJsCss() {
  115. $this->_oTemplate->addCss('menu.css');
  116. }
  117. }
  118. /** @} */