- Timestamp:
- 12/12/11 01:37:22 (6 months ago)
- Location:
- trunk/inc/classes
- Files:
-
- 1 added
- 2 edited
-
BxDolMenu.php (modified) (1 diff)
-
BxDolMenuQuery.php (added)
-
BxDolTemplate.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/inc/classes/BxDolMenu.php
r15395 r15749 1 <?php 1 <?php defined('BX_DOL') or die('hack attempt'); 2 2 /** 3 * @package Dolphin Core 4 * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/ 5 * @license CC-BY - http://creativecommons.org/licenses/by/3.0/ 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 * @{ 6 8 */ 7 defined('BX_DOL') or die('hack attempt');8 9 9 bx_import('BxDol'); 10 bx_import('BxDolMenuQuery'); 11 bx_import('BxDolPermalinks'); 12 13 /** 14 * @page objects 15 * @section menu Menu 16 * @ref BxDolMenu 17 */ 18 19 /** 20 * Menus. 21 * 22 * It allows to display menu using different templates. 23 */ 10 24 11 25 class BxDolMenu extends BxDol { 12 26 13 var $isInitialized = false; 27 protected static $SEL_MODULE = ''; 28 protected static $SEL_NAME = ''; 14 29 15 var $sCacheFile; 30 protected $_sObject; 31 protected $_aObject; 32 protected $_oQuery; 33 protected $_oPermalinks; 34 protected $_aMarkers = array (); 16 35 17 var $aTopMenu = array(); 18 var $aMenuInfo = array('currentCustom' => -1, 'currentTop' => -1); 36 /** 37 * Constructor 38 * @param $aObject array of menu options 39 */ 40 public function __construct($aObject) { 41 parent::__construct(); 19 42 20 var $oPermalinks; 21 var $aCustomBreadcrumbs = array (); 22 23 function BxDolMenu() { 24 25 if (isset($GLOBALS['bxDolClasses'][get_class($this)])) 26 trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR); 27 28 parent::BxDol(); 29 30 bx_import('BxDolPermalinks'); 31 $this->oPermalinks = new BxDolPermalinks(); 32 33 $this->sCacheFile = BX_DIRECTORY_PATH_CACHE . 'sys_menu_top.inc'; 43 $this->_sObject = $aObject['object']; 44 $this->_aObject = $aObject; 45 $this->_oQuery = new BxDolMenuQuery($this->_aObject); 46 $this->_oPermalinks = new BxDolPermalinks(); 34 47 } 35 48 36 49 /** 37 * Prevent cloning the instance 50 * Get menu object instance by object name 51 * @param $sObject object name 52 * @return object instance or false on error 38 53 */ 39 public function __clone() { 40 if (isset($GLOBALS['bxDolClasses'][get_class($this)])) 41 trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR); 54 static public function getObjectInstance($sObject) { 55 56 if (isset($GLOBALS['bxDolClasses']['BxDolMenu!'.$sObject])) 57 return $GLOBALS['bxDolClasses']['BxDolMenu!'.$sObject]; 58 59 $aObject = BxDolMenuQuery::getMenuObject($sObject); 60 if (!$aObject || !is_array($aObject)) 61 return false; 62 63 bx_import('BxTemplMenu'); 64 $sClass = 'BxTemplMenu'; 65 if (!empty($aObject['override_class_name'])) { 66 $sClass = $aObject['override_class_name']; 67 if (!empty($aObject['override_class_file'])) 68 require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']); 69 else 70 bx_import($sClass); 71 } 72 73 $o = new $sClass($aObject); 74 75 return ($GLOBALS['bxDolClasses']['BxDolMenu!'.$sObject] = $o); 42 76 } 43 77 44 78 /** 45 * Get singleton instance of the class 79 * Set selected menu item. 80 * @param $sModule menu item module to set as selected 81 * @param $sName menu item name to set as selected 46 82 */ 47 function getInstance() { 48 if(!isset($GLOBALS['bxDolClasses'][__CLASS__])) { 49 bx_import('BxTemplMenu'); 50 $GLOBALS['bxDolClasses'][__CLASS__] = new BxTemplMenu(); 51 } 52 53 return $GLOBALS['bxDolClasses'][__CLASS__]; 83 static public function setSelected ($sModule, $sName) { 84 self::$SEL_MODULE = $sModule; 85 self::$SEL_NAME = $sName; 54 86 } 55 87 56 88 /** 57 * set custom breadcrumbs 58 * @param $a breadcrumbs array, array keys are titles and array values are links, for example: 59 * array( 60 * _t('Item1') => 'http://item1.com/link', 61 * _t('Item2') => 'http://item2.com/link', 62 * ) 63 * NOTE: first element in breadcrumb is always 'Home', it is added automatically, so you don't need to add in this array 89 * Add replace markers. 90 * @param $a array of markers as key => value 91 * @return true on success or false on error 64 92 */ 65 function setCustomBreadcrumbs ($a) { 66 $this->aCustomBreadcrumbs = $a; 93 public function addMarkers ($a) { 94 if (empty($a) || !is_array($a)) 95 return false; 96 $this->_aMarkers = array_merge ($this->_aMarkers, $a); 97 return true; 98 } 99 100 protected function _isSelected ($a) { 101 return $a['module'] == self::$SEL_MODULE && $a['name'] == self::$SEL_NAME ? true : false; 67 102 } 68 103 69 function setCurrentProfileID($iProfileID = 0) { 70 71 $iProfileID = (int)$iProfileID; 72 $sProfileNickName = getNickName($iProfileID); 73 74 if ($iProfileID > 0 and !empty($sProfileNickName)) { 75 $this->aMenuInfo['profileID'] = $iProfileID; 76 $this->aMenuInfo['profileNick'] = $sProfileNickName; 77 $this->aMenuInfo['profileLink'] = getProfileLink($iProfileID); 78 } else { 79 $this->aMenuInfo['profileID'] = 0; 80 $this->aMenuInfo['profileNick'] = ''; 81 $this->aMenuInfo['profileLink'] = ''; 82 } 104 protected function _isVisible ($a) { 105 // TODO: 106 return true; 83 107 } 84 108 85 function setCurrentProfileNickName($sNickName = '') { 86 $sNickName = trim($sNickName); 87 $iProfileID = getID($sNickName); 88 $this->setCurrentProfileID($iProfileID); 109 protected function _replaceMarkers ($a) { 110 if (empty($this->_aMarkers)) 111 return $a; 112 $aReplacebleFields = array ('title', 'link', 'onclick'); 113 foreach ($aReplacebleFields as $sField) { 114 foreach ($this->_aMarkers as $sKey => $sValue) 115 $a[$sField] = str_replace('{' . $sKey . '}', $sValue, $a[$sField]); 116 } 117 return $a; 89 118 } 90 119 91 function setCustomVar($sVar, $sVal) {92 $this->aMenuInfo[$sVar] = $sVal;93 }94 95 function unsetCustomVar($sVar, $sVal) {96 $this->aMenuInfo[$sVar] = $sVal;97 }98 99 function setCurrentMenu ($sName) {100 101 $this->_init();102 103 // TODO: store indexes for 'Name' in cache to get menu item directly by Name104 105 $this->aMenuInfo['currentCustom'] = -1;106 $this->aMenuInfo['currentTop'] = -1;107 108 foreach ($this->aTopMenu as $iItemID => $aItem ) {109 110 if ($aItem['Name'] != $sName)111 continue;112 113 if ($aItem['Type'] == 'top') {114 $this->aMenuInfo['currentTop'] = $iItemID;115 } else {116 $this->aMenuInfo['currentCustom'] = $iItemID;117 $this->aMenuInfo['currentTop'] = $aItem['Parent'];118 }119 120 break;121 }122 }123 124 function _init () {125 126 if ($this->isInitialized)127 return;128 129 if (!$this->_load())130 $this->aTopMenu = array ();131 132 $this->_initMenuInfo();133 134 $this->isInitialized = true;135 }136 137 function _initMenuInfo() {138 139 if (isMember()) {140 141 $this->aMenuInfo['memberID'] = (int)$_COOKIE['memberID'];142 $this->aMenuInfo['memberNick'] = getNickName ($this->aMenuInfo['memberID']);143 $this->aMenuInfo['memberLink'] = getProfileLink ($this->aMenuInfo['memberID']);144 $this->aMenuInfo['memberPass'] = getPassword ($this->aMenuInfo['memberID']);145 $this->aMenuInfo['visible'] = 'memb';146 147 } else {148 149 $this->aMenuInfo['memberID'] = 0;150 $this->aMenuInfo['memberNick'] = '';151 $this->aMenuInfo['memberLink'] = '';152 $this->aMenuInfo['visible'] = 'non';153 154 }155 }156 157 function _checkToShow ($aItem) {158 159 if (!$this->_checkVisible ($aItem['Visible']))160 return false;161 162 if (!$this->_checkCond($aItem['Check']))163 return false;164 165 return true;166 }167 168 function _checkVisible ($sVisible) {169 return ( strpos( $sVisible, $this->aMenuInfo['visible'] ) !== false );170 }171 172 function _checkCond ($sCheck) {173 174 if( !$sCheck )175 return true;176 177 $sCheck = str_replace ('\$', '$', $sCheck);178 $fFunc = create_function('', $sCheck);179 180 return $fFunc();181 }182 183 function _replaceMetas($sLink) {184 185 foreach ($this->aMenuInfo as $k => $v)186 $sLink = str_replace('{'.$k.'}', $v, $sLink);187 188 return $sLink;189 }190 191 function _load() {192 193 if( !file_exists( $this->sCacheFile ) && !$this->_compile() ) {194 echo '<b>Warning!</b> Cannot find Menu Cache file (' . $this->sCacheFile . ').';195 return false;196 }197 198 $sCache = @file_get_contents( $this->sCacheFile );199 if( !$sCache ) {200 if (!$this->_compile() || !$sCache = @file_get_contents( $this->sCacheFile )) { // compile and try again201 echo '<b>Warning!</b> Cannot read Menu Cache file (' . $this->sCacheFile . ').';202 return false;203 }204 }205 206 $this->aTopMenu = @eval( $sCache );207 if( !$this->aTopMenu or !is_array( $this->aTopMenu ) ) {208 echo '<b>Warning!</b> Cannot evaluate Menu Cache file (' . $this->sCacheFile . ').';209 return false;210 }211 212 if (BxDolRequest::serviceExists('pageac', 'menu_items_filter')) {213 BxDolService::call('pageac', 'menu_items_filter', array('top', &$this->aTopMenu));214 }215 216 return true;217 }218 219 function _compile() {220 221 $oDb = BxDolDb::getInstance();222 223 $fMenu = @fopen( $this->sCacheFile , 'w' );224 if( !$fMenu )225 return false;226 227 fwrite( $fMenu, "return array(\n" );228 $aFields = array( 'Type','Caption','Link','Visible','Target','Onclick','Check','Parent','Picture','Icon','BQuickLink', 'Statistics', 'Name' );229 230 $sQuery = "231 SELECT232 `ID`,233 `" . implode('`,`', $aFields ) . "`234 FROM `sys_menu_top`235 WHERE236 `Active` = 1 AND237 ( `Type` = 'system' OR `Type` = 'top' )238 ORDER BY `Type`,`Order`239 ";240 241 $aMenu = $oDb->getAll($sQuery);242 foreach ($aMenu as $aMenuItem) {243 fwrite( $fMenu, " " . str_pad( $aMenuItem['ID'], 2 ) . " => array(\n" );244 245 foreach( $aFields as $sKey => $sField ) {246 $sCont = $aMenuItem[$sField];247 248 if( $sField == 'Link' )249 $sCont = $this->getCurrLink($sCont);250 251 $sCont = str_replace( '\\', '\\\\', $sCont );252 $sCont = str_replace( '"', '\\"', $sCont );253 $sCont = str_replace( '$', '\\$', $sCont );254 255 $sCont = str_replace( "\n", '', $sCont );256 $sCont = str_replace( "\r", '', $sCont );257 $sCont = str_replace( "\t", '', $sCont );258 259 fwrite( $fMenu, " " . str_pad( "'$sField'", 11 ) . " => \"$sCont\",\n" );260 }261 262 fwrite( $fMenu, " ),\n" );263 264 // write it's children265 $sQuery = $oDb->prepare("266 SELECT267 `ID`,268 `" . implode('`,`', $aFields ) . "`269 FROM `sys_menu_top`270 WHERE271 `Active` = 1 AND272 `Type` = 'custom' AND273 `Parent` = ?274 ORDER BY `Order`275 ", $aMenuItem['ID']);276 277 $aCMenu = $oDb->getAll($sQuery);278 foreach ($aCMenu as $aMenuItem) {279 fwrite( $fMenu, " " . str_pad( $aMenuItem['ID'], 2 ) . " => array(\n" );280 281 foreach( $aFields as $sKey => $sField ) {282 $sCont = $aMenuItem[$sField];283 284 if( $sField == 'Link' )285 $sCont = $this->getCurrLink($sCont);286 287 $sCont = str_replace( '\\', '\\\\', $sCont );288 $sCont = str_replace( '"', '\\"', $sCont );289 $sCont = str_replace( '$', '\\$', $sCont );290 291 $sCont = str_replace( "\n", '', $sCont );292 $sCont = str_replace( "\r", '', $sCont );293 $sCont = str_replace( "\t", '', $sCont );294 295 fwrite( $fMenu, " " . str_pad( "'$sField'", 11 ) . " => \"$sCont\",\n" );296 }297 298 fwrite( $fMenu, " ),\n" );299 }300 }301 302 fwrite( $fMenu, ");\n" );303 fclose( $fMenu );304 @chmod ($this->sCacheFile, 0666);305 return true;306 }307 308 /**309 * Returns link in accordance with permalink settings310 */311 function getCurrLink($sCont) {312 $aCurrLink = explode('|', $sCont);313 $aCurrLink[0] = $this->oPermalinks->permalink($aCurrLink[0]);314 $sCont = implode( '|', $aCurrLink );315 316 return htmlspecialchars_adv($sCont);317 }318 120 } 319 121 122 /** @} */ -
trunk/inc/classes/BxDolTemplate.php
r15733 r15749 830 830 case 'top_menu': 831 831 bx_import('BxTemplMenu'); 832 $sRet = BxTemplMenu::get Instance() -> getTopMenu();832 $sRet = BxTemplMenu::getObjectInstance('sys_main')->getCode (); 833 833 break; 834 834 case 'top_submenu': 835 835 bx_import('BxTemplMenu'); 836 $sRet = BxTemplMenu::getInstance() -> getSubMenu();836 $sRet = '';//BxTemplMenu::getInstance() -> getSubMenu(); 837 837 break; 838 838 case 'top_menu_breadcrumb': 839 839 bx_import('BxTemplMenu'); 840 $sRet = BxTemplMenu::getInstance() -> getBreadcrumb();840 $sRet = '';//BxTemplMenu::getInstance() -> getBreadcrumb(); 841 841 break; 842 842 case 'extra_top_menu': … … 850 850 break; 851 851 case 'bottom_links': 852 bx_import('BxTemplFunctions'); 853 $sRet = BxTemplFunctions::getInstance()->genSiteBottomMenu(); 852 bx_import('BxTemplMenu'); 853 $oMenuFooter = BxTemplMenu::getObjectInstance('sys_footer'); 854 $sRet = $oMenuFooter ? $oMenuFooter->getCode () : 'TODO: footer menu'; 854 855 break; 855 856 case 'switch_skin_block':
Note: See TracChangeset
for help on using the changeset viewer.