HomeHelpTrac

Changeset 15749 for trunk/inc


Ignore:
Timestamp:
12/12/11 01:37:22 (6 months ago)
Author:
Alexander Trofimov
Message:

Menu

Location:
trunk/inc/classes
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/inc/classes/BxDolMenu.php

    r15395 r15749  
    1 <?php 
     1<?php defined('BX_DOL') or die('hack attempt'); 
    22/** 
    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 * @{ 
    68 */ 
    7 defined('BX_DOL') or die('hack attempt'); 
    89 
    9 bx_import('BxDol'); 
     10bx_import('BxDolMenuQuery'); 
     11bx_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 */ 
    1024 
    1125class BxDolMenu extends BxDol { 
    1226 
    13     var $isInitialized = false; 
     27    protected static $SEL_MODULE = ''; 
     28    protected static $SEL_NAME = ''; 
    1429 
    15     var $sCacheFile; 
     30    protected $_sObject; 
     31    protected $_aObject; 
     32    protected $_oQuery; 
     33    protected $_oPermalinks; 
     34    protected $_aMarkers = array (); 
    1635 
    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(); 
    1942 
    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(); 
    3447    } 
    3548 
    3649    /** 
    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 
    3853     */ 
    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); 
    4276    } 
    4377 
    4478    /** 
    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 
    4682     */ 
    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; 
    5486    } 
    5587 
    5688    /** 
    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 
    6492     */ 
    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; 
    67102    } 
    68103 
    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;         
    83107    } 
    84108 
    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; 
    89118    } 
    90119 
    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 Name 
    104  
    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 again 
    201                 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             SELECT 
    232                 `ID`, 
    233                 `" . implode('`,`', $aFields ) . "` 
    234             FROM `sys_menu_top` 
    235             WHERE 
    236                 `Active` = 1 AND 
    237                 ( `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 children 
    265             $sQuery = $oDb->prepare(" 
    266                 SELECT 
    267                     `ID`, 
    268                     `" . implode('`,`', $aFields ) . "` 
    269                 FROM `sys_menu_top` 
    270                 WHERE 
    271                     `Active` = 1 AND 
    272                     `Type` = 'custom' AND 
    273                     `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 settings 
    310     */ 
    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     } 
    318120} 
    319121 
     122/** @} */ 
  • trunk/inc/classes/BxDolTemplate.php

    r15733 r15749  
    830830            case 'top_menu': 
    831831                bx_import('BxTemplMenu'); 
    832                 $sRet = BxTemplMenu::getInstance() -> getTopMenu(); 
     832                $sRet = BxTemplMenu::getObjectInstance('sys_main')->getCode (); 
    833833                break; 
    834834            case 'top_submenu': 
    835835                bx_import('BxTemplMenu'); 
    836                 $sRet = BxTemplMenu::getInstance() -> getSubMenu(); 
     836                $sRet = '';//BxTemplMenu::getInstance() -> getSubMenu(); 
    837837                break; 
    838838            case 'top_menu_breadcrumb': 
    839839                bx_import('BxTemplMenu'); 
    840                 $sRet = BxTemplMenu::getInstance() -> getBreadcrumb(); 
     840                $sRet = '';//BxTemplMenu::getInstance() -> getBreadcrumb(); 
    841841                break; 
    842842            case 'extra_top_menu': 
     
    850850                break; 
    851851            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'; 
    854855                break; 
    855856            case 'switch_skin_block': 
Note: See TracChangeset for help on using the changeset viewer.