Menu.php

Go to the documentation of this file.
00001 <?php
00003 // {{{ license
00004 
00005 // +----------------------------------------------------------------------+
00006 // | FastFrame Application Framework                                      |
00007 // +----------------------------------------------------------------------+
00008 // | Copyright (c) 2002-2006 The Codejanitor Group                        |
00009 // +----------------------------------------------------------------------+
00010 // | This source file is subject to the GNU Lesser Public License (LGPL), |
00011 // | that is bundled with this package in the file LICENSE, and is        |
00012 // | available at through the world-wide-web at                           |
00013 // | http://www.fsf.org/copyleft/lesser.html                              |
00014 // | If you did not receive a copy of the LGPL and are unable to          |
00015 // | obtain it through the world-wide-web, you can get it by writing the  |
00016 // | Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
00017 // | MA 02111-1307, USA.                                                  |
00018 // +----------------------------------------------------------------------+
00019 // | Authors: Jason Rust <jrust@codejanitor.com>                          |
00020 // +----------------------------------------------------------------------+
00021 
00022 // }}}
00023 // {{{ requires 
00024 
00025 require_once dirname(__FILE__) . '/Output.php';
00026 
00027 // }}}
00028 // {{{ class FF_Menu
00029 
00089 // }}}
00090 class FF_Menu {
00091     // {{{ properties
00092 
00097     var $o_registry;
00098 
00103     var $o_output;
00104 
00109     var $o_fileCache;
00110 
00115     var $cacheFile;
00116 
00121     var $menuVariables; 
00122 
00127     var $menuType;
00128 
00133     var $currentAppPlaceholder = '%currentApp%';
00134 
00140     var $debug = false;
00141 
00142     // }}}
00143     // {{{ constructor()
00144 
00153     function FF_Menu($in_type)
00154     {
00155         $this->o_registry =& FF_Registry::singleton();
00156         $this->o_output =& FF_Output::factory();
00157         $this->o_fileCache =& FF_FileCache::singleton();
00158         $this->menuType = $in_type;
00159         $this->_setCacheFile();
00160     }
00161 
00162     // }}}
00163     // {{{ factory()
00164 
00175     function &factory($in_type)
00176     {
00177         static $a_instances;
00178         if (!isset($a_instances)) {
00179             $a_instances = array();
00180         }
00181 
00182         $s_class = 'FF_Menu_' . $in_type;
00183         if (!isset($a_instances[$s_class])) {
00184             $pth_menu = dirname(__FILE__) . '/Menu/' . $in_type . '.php';
00185             if (file_exists($pth_menu)) { 
00186                 require_once $pth_menu;
00187                 $a_instances[$s_class] = new $s_class($in_type);
00188             } 
00189             else {
00190                 trigger_error("Invalid menu type: $in_type", E_USER_ERROR); 
00191             }
00192         }
00193 
00194         return $a_instances[$s_class];
00195     }
00196 
00197     // }}}
00198     // {{{ renderMenu()
00199 
00206     function renderMenu()
00207     {
00208         // interface
00209     }
00210 
00211     // }}}
00212     // {{{ _isMenuCached()
00213 
00221     function _isMenuCached()
00222     {
00223         list($s_subdir, $s_name) = explode('/', $this->cacheFile);
00224         if (!file_exists(($pth_file = $this->o_fileCache->getPath(array('subdir' => $s_subdir, 'name' => $s_name))))) {
00225             return false;
00226         }
00227         else {
00228             $s_cacheMTime = filemtime($pth_file);
00229         }
00230 
00231         // Changing an app setting, such as whether it is enabled can change the menu
00232         if (filemtime($this->o_registry->getRootFile('apps.php', 'config')) > $s_cacheMTime) {
00233             return false;
00234         }
00235 
00236         // See if any of the menu files have been updated
00237         foreach ($this->o_registry->getApps() as $s_app) {
00238             // Make sure this app is enabled 
00239             if ($this->o_registry->getAppParam('status', 'disabled', $s_app) != 'enabled') {
00240                 continue;
00241             }
00242                 
00243             $pth_menu = $this->o_registry->getAppFile($this->_getMenuFilename($s_app), $s_app, 'config');
00244             if (file_exists($pth_menu) && (filemtime($pth_menu) > $s_cacheMTime)) {
00245                 return false;
00246             }
00247         }
00248 
00249         return true;
00250     }
00251 
00252     // }}}
00253     // {{{ _getCachedMenu()
00254 
00262     function _getCachedMenu()
00263     {
00264         require_once dirname(__FILE__) . '/Perms.php';
00265         $o_perms =& FF_Perms::factory();
00266         $o_registry =& FF_Registry::singleton();
00267         $o_output =& FF_Output::factory();
00268         ob_start();
00269         list($s_subdir, $s_name) = explode('/', $this->cacheFile);
00270         require_once $this->o_fileCache->getPath(array('subdir' => $s_subdir, 'name' => $s_name));
00271         $s_data = ob_get_contents();
00272         ob_end_clean();
00273         return $s_data;
00274     }
00275 
00276     // }}}
00277     // {{{ _getMenuFilename()
00278 
00288     function _getMenuFilename($in_app)
00289     {
00290         // Get the profile specific application menu
00291         if ($this->o_registry->getAppParam('profile', false, $in_app)) {
00292             return sprintf('%s/menu.php', 
00293                     $this->o_registry->getAppParam('profile', null, $in_app));
00294         }
00295         else {
00296             return 'menu.php';
00297         }
00298     }
00299 
00300     // }}}
00301     // {{{ _importMenuVars()
00302 
00310     function _importMenuVars()
00311     {
00312         // First just loop and include all actions so all apps can link to other apps
00313         foreach ($this->o_registry->getApps() as $s_app) {
00314             // Include any additional action IDs this app has
00315             $pth_actions = $this->o_registry->getAppFile('ActionHandler/actions.php', $s_app, 'libs');
00316             if (file_exists($pth_actions)) {
00317                 require_once $pth_actions;
00318             }
00319         }
00320 
00321         if ($this->o_registry->getConfigParam('display/home_link')) {
00322             // Add the home link which takes the user to their default page
00323             $a_appMenu = array();
00324             $a_appMenu[] = array(
00325                 'contents' => _('Home'),
00326                 'icon' => 'mini/home.gif',
00327                 'urlParams' => array('actionId' => ACTION_HOME, 'module' => ''));
00328             $this->menuVariables = array(array('app' => '', 'vars' => $a_appMenu));
00329         }
00330         else {
00331             $this->menuVariables = array();
00332         }
00333 
00334         // Now loop through enabled apps and get the menu variables
00335         foreach ($this->o_registry->getApps() as $s_app) {
00336             // Make sure this app is enabled 
00337             if ($this->o_registry->getAppParam('status', 'disabled', $s_app) != 'enabled') {
00338                 continue;
00339             }
00340 
00341             $pth_menu = $this->o_registry->getAppFile($this->_getMenuFilename($s_app), $s_app, 'config');
00342             if (file_exists($pth_menu)) {
00343                 $a_appMenu = null; 
00344                 require $pth_menu;
00345                 if (is_array($a_appMenu) && isset($a_appMenu[0])) {
00346                     $this->menuVariables[] = array(
00347                         'app' => $s_app,
00348                         'vars' => $a_appMenu,
00349                     );
00350                 }
00351                 else {
00352                     return trigger_error('The menu file ' . basename($pth_menu) . ' for the ' . $s_app . ' application is not configured correctly.', E_USER_WARNING);
00353                 }
00354             }
00355         }
00356     }
00357 
00358     // }}}
00359     // {{{ _processPerms()
00360 
00373     function _processPerms($in_data, $in_node)
00374     {
00375         $a_ifStatements = array();
00376         // Possible options for visibility are guest_only, logged_in, and always
00377         $in_data['visibility'] = isset($in_data['visibility']) ? $in_data['visibility'] : 'logged_in';
00378         if ($in_data['visibility'] == 'guest_only') {
00379             $a_ifStatements[] = 'FF_Auth::isGuest()';
00380         }
00381         elseif ($in_data['visibility'] == 'logged_in') {
00382             $a_ifStatements[] = '!FF_Auth::isGuest()';
00383         }
00384 
00385         if (isset($in_data['perms'])) {
00386             $s_permsCall = '$o_perms->hasPerm(';
00387             if (is_array($in_data['perms'])) {
00388                 $s_app = isset($in_data['perms']['app']) ? $in_data['perms']['app'] : $this->currentAppPlaceholder;
00389                 unset($in_data['perms']['app']);
00390                 $s_permsList = '\'' . implode($in_data['perms'], '\',\'') . '\'';
00391                 // first argument is list of perms to check, second is the app
00392                 $s_permsCall .= 'array(' . $s_permsList . '),\'' . $s_app . '\'';
00393             }
00394             else {
00395                 // If a string then that is the perm to check
00396                 $s_permsCall .= '\'' . $in_data['perms'] . '\',\'' . $this->currentAppPlaceholder . '\'';
00397             }
00398 
00399             $s_permsCall .= ')';
00400             $a_ifStatements[] = $s_permsCall;
00401         }
00402 
00403         if (count($a_ifStatements) != 0) {
00404             $tmp_nl = $this->debug ? "\n" : ' '; 
00405             $in_node = "$tmp_nl<?php if (" . implode(' && ', $a_ifStatements) . ") { ?>$tmp_nl$in_node$tmp_nl<?php } ?>$tmp_nl";
00406         }
00407 
00408         return $in_node;
00409     }
00410 
00411     // }}}
00412     // {{{ _processApps()
00413 
00424     function _processApps($in_data, $in_node)
00425     {
00426 
00427         if (isset($in_data['app_private']) && $in_data['app_private'] == true) {
00428             $tmp_nl = $this->debug ? "\n" : ' '; 
00429             $s_appCheck = '$o_registry->getCurrentApp() == \'' . $this->currentAppPlaceholder .  '\'';
00430             $in_node = "$tmp_nl<?php if ($s_appCheck) { ?>$tmp_nl$in_node$tmp_nl<?php } ?>$tmp_nl";
00431         }
00432 
00433         return $in_node;
00434     }
00435 
00436     // }}}
00437     // {{{ _replaceAppPlaceholder()
00438 
00447     function _replaceAppPlaceholder($in_app, $in_data)
00448     {
00449         // Since the placeholder is used in the url, the placeholder can be urlencoded 
00450         static $s_holder;
00451         if (!isset($s_holder)) {
00452             $s_holder = urlencode($this->currentAppPlaceholder);
00453         }
00454 
00455         $in_data = str_replace($s_holder, $in_app, $in_data);
00456         $in_data = str_replace($this->currentAppPlaceholder, $in_app, $in_data);
00457         return $in_data;
00458     }
00459 
00460     // }}}
00461     // {{{ _getLinkUrl()
00462 
00474     function _getLinkUrl($in_url, $in_sessionInTags)
00475     {
00476         if (is_array($in_url)) {
00477             if (!isset($in_url['app'])) {
00478                 $in_url['app'] = $this->currentAppPlaceholder;
00479             }
00480 
00481             // Always want session to be on url in case someone has cookies off
00482             $in_url[session_name()] = session_id();
00483             $s_url = FastFrame::url('index.php', $in_url);
00484             // Can't have the current session id in the url
00485             if ($in_sessionInTags) {
00486                 $tmp_repl = '<?php echo SID; ?>';
00487             }
00488             else {
00489                 $tmp_repl = '\' . SID . \'';
00490             }
00491 
00492             $s_url = preg_replace('/' . session_name() . '=.*?(&|$)/S', "$tmp_repl\\1", $s_url);
00493             return $s_url;
00494         }
00495         else {
00496             return $in_url;
00497         }
00498     }
00499 
00500     // }}}
00501     // {{{ _formatMenuNodeData()
00502 
00513     function _formatMenuNodeData($in_data, $in_sessionInTags)
00514     {
00515         $in_data['contents'] = isset($in_data['contents']) ? 
00516             addcslashes($in_data['contents'], '\'') : '';
00517         $in_data['statusText'] = isset($in_data['statusText']) ? 
00518             $in_data['statusText'] : $in_data['contents'];
00519         $in_data['icon'] = isset($in_data['icon']) ? 
00520             addcslashes($this->o_output->imgTag($in_data['icon'], 'none', array('align' => 'bottom')), '\'') . ' ' : '';
00521         $in_data['target'] = isset($in_data['target']) ? $in_data['target'] : '_self';
00522         $in_data['urlParams'] = isset($in_data['urlParams']) ? 
00523             $this->_getLinkUrl($in_data['urlParams'], $in_sessionInTags) : '';
00524 
00525         return $in_data;
00526     }
00527 
00528     // }}}
00529     // {{{ _setCacheFile()
00530 
00537     function _setCacheFile()
00538     {
00539         $this->cacheFile = 'menu/' . $this->menuType . '.php';
00540     }
00541 
00542     // }}}
00543 }
00544 ?>

Generated on Fri Jun 23 11:38:17 2006 for FastFrame by  doxygen 1.4.4