Changeset 5945


Ignore:
Timestamp:
01/18/12 18:38:52 (4 months ago)
Author:
vain
Message:
  • added array key condition to menunavigation array items
  • the key is evaluated and the boolean value is reassigned, making a conditional display of menu items possible
  • thereby the view can decide whether to display or not display the item
  • example usage: sysinfo module, where apc has to be loaded to get it's stats, the menu item is senseless, if apc is not loaded.
  • closes ticket #231
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/core/viewhelper/smarty/function.modulenavigation.php

    r5059 r5945  
    2727function smarty_function_modulenavigation($params, $smarty) 
    2828{ 
    29     $modulename = Clansuite_HttpRequest::getRoute()->getModuleName(); 
    30     $modulenavigation_file = ROOT_MOD. $modulename . DS . $modulename . '.menu.php'; 
     29    $module = Clansuite_HttpRequest::getRoute()->getModuleName(); 
    3130 
    32     if( is_file($modulenavigation_file) ) 
     31    $file = ROOT_MOD. $module . DS . $module . '.menu.php'; 
     32 
     33    if( is_file($file) ) 
    3334    { 
    3435        # this includes the file, which contains a php array name $modulenavigation 
    35         include $modulenavigation_file; 
     36        include $file; 
    3637 
    37         # convert URLs by callback 
    38         $modulenavigation = array_map("convertURLs", $modulenavigation); 
     38        # push the $modulenavigation array to a callback function 
     39        # for further processing of the menu items 
     40        $modulenavigation = array_map("applyCallbacks", $modulenavigation); 
     41 
     42        var_dump($modulenavigation); 
    3943 
    4044        $smarty->assign('modulenavigation', $modulenavigation); 
     45 
    4146        # The file is located in clansuite/themes/core/view/smarty/modulenavigation-generic.tpl 
    4247        return $smarty->fetch('modulenavigation-generic.tpl'); 
    4348    } 
    44     else 
     49    else # the module menu navigation file is missing 
    4550    { 
    46         $smarty->assign('modulename', $modulename); 
     51        $smarty->assign('modulename', $module); 
    4752        $errormessage = $smarty->fetch('modulenavigation_not_found.tpl'); 
    4853        trigger_error($errormessage); 
     
    5156 
    5257/** 
    53  * array_map callback function to replace the values of the 'url' key 
    54  * because these might be shorthands like "/index/show" etc. 
     58 * array_map callback function 
    5559 * 
    56  * @param array $array 
     60 * 1) convert short urls 
     61 * 2) execute callback conditions of menu items 
     62 * 
     63 * @param array $modulenavigation 
    5764 */ 
    58 function convertURLs($array) 
     65function applyCallbacks(array $modulenavigation) 
    5966{ 
    60     $array['url'] = Clansuite_Router::buildURL($array['url']); 
    61     return $array; 
     67    /** 
     68     * 1) Convert Short Urls 
     69     * 
     70     * This replaces the values of the 'url' key (array['url']), 
     71     * because these might be shorthands, like "/index/show". 
     72     */ 
     73    $modulenavigation['url'] = Clansuite_Router::buildURL($modulenavigation['url']); 
     74 
     75    /** 
     76     * 2) Conditions of menu items 
     77     * 
     78     * If the condition of the menu item is not met, 
     79     * then condition is set to false, otherwise true. 
     80     */ 
     81    if(isset($modulenavigation['condition']) === true) 
     82    { 
     83        /** 
     84         * the if statement evaluates the content of the key condition 
     85         * and compares it to false, then reassigns the boolean value as 
     86         * the condition value. 
     87         * 
     88         * for now you might define 'condition' => extension_loaded('apc') 
     89         * 
     90         * @todo check usage of closures 
     91         */ 
     92        if($modulenavigation['condition'] === false) 
     93        { 
     94            $modulenavigation['condition'] = false; 
     95        } 
     96        else 
     97        { 
     98            $modulenavigation['condition'] = true; 
     99        } 
     100    } 
     101 
     102    return $modulenavigation; 
    62103} 
    63104?> 
  • trunk/modules/systeminfo/systeminfo.menu.php

    r4768 r5945  
    5353        'url' => '/systeminfo/admin/show_apc', 
    5454        'icon' => '', 
    55         'title' => '' 
     55        'title' => '', 
     56        'condition' => extension_loaded('apc') 
    5657    ), 
    5758); 
  • trunk/themes/core/view/smarty/modulenavigation-generic.tpl

    r5155 r5945  
    7878    <ul> 
    7979    {foreach $modulenavigation as $item} 
     80 
     81    {* if the condition is not met, then do not display the menu item *} 
     82    {if isset($item.condition) === true and $item.condition === false} 
     83    {continue} 
     84    {/if} 
     85     
    8086    <li> 
    8187        <a href="{$item.url}" title="{$item.title}"> <span>{$item.name}</span> </a> 
Note: See TracChangeset for help on using the changeset viewer.