BxBaseMenu.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. $a['addon'] = $this->_getMenuAddon($a);
  78. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  79. if (!isset($a[$sName]))
  80. $a[$sName] = $sDefaultValue;
  81. $aRet[] = $a;
  82. }
  83. return $aRet;
  84. }
  85. protected function _getMenuIcon ($a) {
  86. $sIcon = false;
  87. $sIconUrl = false;
  88. if (!empty($a['icon'])) {
  89. if ((int)$a['icon'] > 0 ) {
  90. bx_import('BxDolStorage');
  91. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  92. $sIconUrl = $oStorage ? $oStorage->getFileUrlById((int)$a['icon']) : false;
  93. } else {
  94. if (false === strpos($a['icon'], '.')) // font icons
  95. $sIcon = $a['icon'];
  96. else
  97. $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 ?!
  98. }
  99. }
  100. return array ($sIcon, $sIconUrl);
  101. }
  102. protected function _getMenuAddon ($aMenuItem) {
  103. if (empty($aMenuItem['addon']))
  104. return '';
  105. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  106. }
  107. /**
  108. * Add css/js files which are needed for menu display and functionality.
  109. */
  110. protected function _addJsCss() {
  111. $this->_oTemplate->addCss('menu.css');
  112. }
  113. }
  114. /** @} */