| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <?php defined('BX_DOL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaBaseView UNA Base Representation Classes
- * @{
- */
- require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
- /**
- * Menu representation.
- * @see BxDolMenu
- */
- class BxBaseMenu extends BxDolMenu
- {
- protected $_oTemplate;
- protected $_aOptionalParams = array('target' => '', 'onclick' => '');
- protected $_bDisplayAddons = false;
- public function __construct ($aObject, $oTemplate)
- {
- parent::__construct ($aObject);
- if ($oTemplate)
- $this->_oTemplate = $oTemplate;
- else
- $this->_oTemplate = BxDolTemplate::getInstance();
- }
- public function getDisplayAddons()
- {
- return $this->_bDisplayAddons;
- }
- public function setDisplayAddons($b)
- {
- $bRet = $this->_bDisplayAddons;
- $this->_bDisplayAddons = $b;
- return $bRet;
- }
- /**
- * Get menu code.
- * @return string
- */
- public function getCode ()
- {
- $sMenuTitle = isset($this->_aObject['title']) ? _t($this->_aObject['title']) : 'Menu-' . rand(0, PHP_INT_MAX);
- if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginMenu($sMenuTitle);
- $s = '';
- $aVars = $this->_getTemplateVars ();
- if (!empty($aVars['bx_repeat:menu_items'])) {
- $this->_addJsCss();
- $s = $this->_getCode($this->_aObject['template'], $aVars);
- }
- if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endMenu($sMenuTitle);
- return $s;
- }
- protected function _getCode($sTmplName, $aTmplVars)
- {
- return $this->_oTemplate->parseHtmlByName($this->getTemplateName($sTmplName), $aTmplVars);
- }
- public function getCodeItem ($sName)
- {
- if(empty($sName))
- return '';
- $sCode = $this->_oTemplate->getHtml(str_replace('.html', '_item.html', $this->_aObject['template']));
- if(empty($sCode))
- return '';
- $mixedTmplVars = $this->getMenuItem($sName);
- if($mixedTmplVars === false)
- return '';
- return $this->_oTemplate->parseHtmlByContent($sCode, $mixedTmplVars);
- }
- /**
- * Get template variables array
- * @return array
- */
- protected function _getTemplateVars ()
- {
- return array (
- 'object' => $this->_sObject,
- 'bx_repeat:menu_items' => $this->getMenuItems (),
- );
- }
- /**
- * Get menu items array, which is ready to pass to menu template.
- * @return array or false
- */
- public function getMenuItems ()
- {
- if (!isset($this->_aObject['menu_items']))
- $this->_aObject['menu_items'] = $this->getMenuItemsRaw ();
- $aItems = array();
- foreach ($this->_aObject['menu_items'] as $aItem) {
- $aItem = $this->_getMenuItem ($aItem);
- if($aItem !== false)
- $aItems[] = $aItem;
- }
- return $aItems;
- }
-
- /**
- * Get menu item array, which is ready to pass to whole menu or
- * single menu item template. May return false if single menu item
- * is requested but cannot be shown by circumstances.
- * @return array or false
- */
- public function getMenuItem ($sName)
- {
- if (!isset($this->_aObject['menu_items']))
- $this->_aObject['menu_items'] = $this->getMenuItemsRaw ();
- if(!empty($sName))
- return $this->_getMenuItem($this->_aObject['menu_items'][$sName]);
- }
- /**
- * Get menu items array, this is just a wrapper for DB function for make it easier to override.
- * It is used in @see BxBaseMenu::getMenuItems
- * @return array
- */
- protected function getMenuItemsRaw ()
- {
- if($this->_bMultilevel)
- return $this->_oQuery->getMenuItemsHierarchy();
- else
- return $this->_oQuery->getMenuItems();
- }
- protected function _getMenuItem ($a)
- {
- $aHiddenOn = array(
- pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
- pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
- pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
- pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
- );
- if (isset($a['active']) && !$a['active'])
- return false;
- if (isset($a['visible_for_levels']) && !$this->_isVisible($a))
- return false;
- $a['title'] = _t($a['title']);
- $a['bx_if:title'] = array(
- 'condition' => !empty($a['title']),
- 'content' => array(
- 'title' => $a['title']
- )
- );
- $this->removeMarker('addon');
- $a = $this->_replaceMarkers($a);
- if ($this->_bDisplayAddons) {
- $mixedAddon = $this->_getMenuAddon($a);
- if (!is_array($mixedAddon)) {
- $this->addMarkers(array('addon' => $mixedAddon));
- $a = $this->_replaceMarkers($a);
- }
- }
- $aMarkers = $this->_getMenuMarkers($a);
- if ($aMarkers && is_array($aMarkers)) {
- $this->addMarkers($aMarkers);
- $a = $this->_replaceMarkers($a);
- }
- list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getMenuIcon($a);
- $a['class_add'] = $this->_isSelected($a) ? 'bx-menu-tab-active' : '';
- $sHiddenOnCssClasses = '';
- if(!empty($a['hidden_on']))
- foreach($aHiddenOn as $iHiddenOn => $sClass)
- if((int)$a['hidden_on'] & $iHiddenOn)
- $sHiddenOnCssClasses .= ' ' . $sClass;
- $a['class_add'] .= $sHiddenOnCssClasses;
- $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
- $a['title_attr'] = bx_html_attribute(strip_tags($a['title']));
- $a['attrs'] = $this->_getMenuAttrs($a);
- $a['bx_if:image'] = array (
- 'condition' => (bool)$sIconUrl,
- 'content' => array('icon_url' => $sIconUrl),
- );
- $a['bx_if:image_inline'] = array (
- 'condition' => false,
- 'content' => array('image' => ''),
- );
- $a['bx_if:icon'] = array (
- 'condition' => (bool)$sIcon,
- 'content' => array('icon' => $sIcon),
- );
- $a['bx_if:icon-html'] = array (
- 'condition' => (bool)$sIconHtml,
- 'content' => array('icon' => $sIconHtml),
- );
- $a['bx_if:icon-a'] = array (
- 'condition' => (bool)$sIconA,
- 'content' => array('icon-a' => $sIconA),
- );
- $a['bx_if:title'] = array (
- 'condition' => (bool)$a['title'],
- 'content' => array(
- 'title' => $a['title'],
- 'title_attr' => $a['title_attr']
- ),
- );
- $aTmplVarsAddon = $this->_bDisplayAddons ? $this->_getTmplVarsAddon($mixedAddon, $a) : array('addon' => '', 'addonf' => '');
- $a['bx_if:addon'] = array (
- 'condition' => $this->_bDisplayAddons && !empty($aTmplVarsAddon['addon']),
- 'content' => $aTmplVarsAddon
- );
- $aTmplVarsSubitems = array('subitems' => '');
- $bTmplVarsSubitems = $this->_bMultilevel && !empty($a['subitems']);
- if($bTmplVarsSubitems) {
- $aSubitems = array();
- foreach($a['subitems'] as $aSubitem) {
- $aSubitem = $this->_getMenuItem($aSubitem);
- if($aSubitem !== false)
- $aSubitems[] = $aSubitem;
- }
- $aTmplVarsSubitems['subitems'] = $this->_oTemplate->parseHtmlByName(str_replace('.html', '_subitems.html', $this->_aObject['template']), array(
- 'bx_repeat:menu_items' => $aSubitems,
- ));
- }
- $a['bx_if:show_subitems'] = array (
- 'condition' => $bTmplVarsSubitems,
- 'content' => $aTmplVarsSubitems
- );
- unset($a['subitems']);
- foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
- if (!isset($a[$sName]))
- $a[$sName] = $sDefaultValue;
- return $a;
- }
- protected function _getMenuIcon ($a)
- {
- $sIcon = false;
- $sIconA = false;
- $sIconUrl = false;
- $sIconHtml = false;
-
- if (!empty($a['icon'])) {
- if ((int)$a['icon'] > 0 ) {
- $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
- $sIconUrl = $oStorage ? $oStorage->getFileUrlById((int)$a['icon']) : false;
- } else {
- //svg
- if (strpos($a['icon'], '<svg') !== false){
- $sIconHtml = $a['icon'];
- $sClass = 'sys-icon sys-icon-svg-inline ';
- if ($sClass != '' && strpos($sIconHtml, 'class="') !== false)
- $sIconHtml = str_replace('class="', 'class="' . $sClass, $sIconHtml);
- else
- $sIconHtml = str_replace('<svg', '<svg class="' . $sClass . '" ', $sIconHtml);
- }
- else{
- //emoji
- if(preg_match('/([0-9#][\x{20E3}])|[\x{00ae}\x{00a9}\x{203C}\x{2047}\x{2048}\x{2049}\x{3030}\x{303D}\x{2139}\x{2122}\x{3297}\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?/u', $a['icon'], $aTmp)){
- $sIconHtml = $this->_oTemplate->parseHtmlByName('icon_emoji.html', array('icon' => $a['icon']));
- }
- else{
- if (false === strpos($a['icon'], '.')) {
- if (0 === strncmp($a['icon'], 'a:', 2))
- $sIconA = substr($a['icon'], 2); // animated icon
- else
- $sIcon = $a['icon']; // font icons
- } else {
- $sIconUrl = $this->_oTemplate->getIconUrl($a['icon']);
- }
- }
- }
- }
- }
- return array ($sIcon, $sIconUrl, $sIconA, $sIconHtml);
- }
- protected function _getMenuAddon ($aMenuItem)
- {
- if (empty($aMenuItem['addon']))
- return '';
- return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
- }
-
- protected function _getMenuMarkers ($aMenuItem)
- {
- if (empty($aMenuItem['markers']))
- return '';
- return BxDolService::callSerialized($aMenuItem['markers'], $this->_aMarkers);
- }
- protected function _getMenuAttrs ($aMenuItem)
- {
- $sAttrs = '';
- if(!empty($aMenuItem['target']))
- $sAttrs .= ' target="' . $aMenuItem['target'] . '"';
- if($this->_bAddNoFollow && !empty($aMenuItem['link']) && preg_match('@^https?://@', $aMenuItem['link']) && strncmp($aMenuItem['link'], BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)) !== 0)
- $sAttrs .= ' rel="noreferrer"';
- return $sAttrs;
- }
- /**
- * Add css/js files which are needed for menu display and functionality.
- */
- protected function _addJsCss()
- {
- $this->_oTemplate->addCss('menu.css');
- }
- protected function _getTmplVarsAddon($mixedAddon, $aMenuItem)
- {
- $sAddon = '';
- if(!is_array($mixedAddon))
- $sAddon = $mixedAddon;
- else if(!empty($mixedAddon['addon']))
- $sAddon = $mixedAddon['addon'];
- return array(
- 'addon' => $sAddon,
- 'addonf' => ''
- );
- }
- }
- /** @} */
|