BxBaseMenu.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 = $this->_replaceMarkers($a);
  59. list ($sIcon, $sIconUrl) = $this->_getMenuIcon($a);
  60. $a['class_add'] = $this->_isSelected($a) ? 'bx-menu-tab-active' : '';
  61. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  62. $a['title'] = _t($a['title']);
  63. $a['bx_if:image'] = array (
  64. 'condition' => (bool)$sIconUrl,
  65. 'content' => array('icon_url' => $sIconUrl),
  66. );
  67. $a['bx_if:icon'] = array (
  68. 'condition' => (bool)$sIcon,
  69. 'content' => array('icon' => $sIcon),
  70. );
  71. $aRet[] = $a;
  72. }
  73. return $aRet;
  74. }
  75. protected function _getMenuIcon ($a) {
  76. $sIcon = false;
  77. $sIconUrl = false;
  78. if (!empty($a['icon'])) {
  79. if ((int)$a['icon'] > 0 ) {
  80. bx_import('BxDolStorage');
  81. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  82. $sIconUrl = $oStorage ? $oStorage->getFileUrlById((int)$a['icon']) : false;
  83. } else {
  84. if (false === strpos($a['icon'], '.')) // font icons
  85. $sIcon = $a['icon'];
  86. else
  87. $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 ?!
  88. }
  89. }
  90. return array ($sIcon, $sIconUrl);
  91. }
  92. /**
  93. * Add css/js files which are needed for menu display and functionality.
  94. */
  95. protected function _addJsCss() {
  96. $this->_oTemplate->addCss('menu.css');
  97. }
  98. }
  99. /** @} */