BxBaseMenu.php 4.2 KB

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