[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkActionView/helpers/ -> menu_helper.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  // +----------------------------------------------------------------------+
   5  // | Akelos Framework - http://www.akelos.org                             |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
   8  // | Released under the GNU Lesser General Public License, see LICENSE.txt|
   9  // +----------------------------------------------------------------------+
  10  
  11  /**
  12   * @package ActionView
  13   * @subpackage Helpers
  14   * @author Bermi Ferrer <bermi a.t akelos c.om>
  15   * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  16   * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  17   */
  18  
  19  
  20  /**
  21  * The Menu Helper makes it easier to create simple menus from controllers actions.
  22  */
  23  class MenuHelper extends AkActionViewHelper
  24  {
  25  
  26      /**
  27      * Returns a menu for all or several actions in all or several controllers.
  28      *
  29      * Let +menu_options+ defaults and this will generate a menu with all actions in all controllers.
  30      * Set +menu_options+ to an array with keys as controller name and values as actions names.
  31      *
  32      * <?php echo $menu_helper->menu_for_controllers(array('advertiser' => array('buy', 'partial_in_template'))); ?>
  33      * will generate something like :
  34      * <div id="menu">
  35      *  <ul>
  36      *   <li>
  37      *    <h2><a href="/advertiser/">Advertiser</a></h2>
  38      *    <ul>
  39      *     <li><a href="/advertiser/buy/">Buy</a></li>
  40      *     <li><a href="/advertiser/partial_in_template/">Partial in template</a></li>
  41      *    </ul>
  42      *   </li>
  43      *  </ul>
  44      * </div>
  45      *
  46      * +div_menu_id+: the id of the main div (default is "menu")
  47      * +current_class+: the class name of the current controller or the current action (default is "current")
  48      * +title_tag+: the tag that will contain the controller name link (default is "h2"). If it's empty, it won't be present
  49      */
  50      function menu_for_controllers($menu_options = array(), $div_menu_id = 'menu', $current_class = 'current', $title_tag = 'h2')
  51      {
  52          $menu_options = empty($menu_options) ? $this->_get_default_full_menu() : $menu_options;
  53          $menu = '';
  54          
  55          foreach ($menu_options as $controller => $actions){
  56              $controller_name = AkInflector::classify($controller);
  57              $current_controller_name = $this->_controller->getControllerName();
  58              $current_action_name = $this->_controller->Request->getAction();
  59              $controller_class_name = $controller_name.'Controller';
  60              $controller_human_name = AkInflector::humanize($controller);
  61              $controller_file_name = AkInflector::toControllerFilename($controller);
  62              if(file_exists($controller_file_name)){
  63                  include_once($controller_file_name);
  64                  if(class_exists($controller_class_name)){
  65                      $class = $current_controller_name == $controller_name ? array('class' => $current_class) : array();
  66                      $href = array('href' => $this->_controller->urlFor(array('controller' => $controller)));
  67                      if (empty($title_tag)) {
  68                          $_title_tag = 'a';
  69                          $content = $controller_human_name;
  70                          $options = array_merge($class, $href);
  71                      } else {
  72                          $content = TagHelper::content_tag('a', $controller_human_name, $href);
  73                          $options = $class;
  74                          $_title_tag = $title_tag;
  75                      }
  76                      $menu_header = TagHelper::content_tag($_title_tag, $content, $options);
  77                      $submenu = '';
  78                      foreach ((array)$actions as $action){
  79                          if($action[0] == '_'){
  80                              continue;
  81                          }
  82                          $submenu .= TagHelper::content_tag('li', TagHelper::content_tag('a', AkInflector::humanize($action), array('href' => $this->_controller->urlFor(array('controller' => $controller, 'action' => $action)))), $current_controller_name == $controller_name && $current_action_name == $action ? array('class' => $current_class) : array());
  83                      }
  84                      $menu .= !empty($submenu) ? TagHelper::content_tag('ul', TagHelper::content_tag('li', $menu_header.TagHelper::content_tag('ul', $submenu))) : '';
  85                  }
  86              }
  87          }
  88          return TagHelper::content_tag('div', $menu, array('id' => $div_menu_id));
  89      }
  90  
  91      function _get_default_full_menu()
  92      {
  93          $controller_file_names = array_map('array_pop', (array)Ak::dir(AK_CONTROLLERS_DIR, array('files'=>false)));
  94  
  95          sort($controller_file_names);
  96          
  97          $menu_options = array();
  98          foreach ($controller_file_names as $controller_file_name){
  99              $controller_name = str_replace('.php','',$controller_file_name);
 100              if(strstr($controller_file_name,'_controller.php') && file_exists(AK_CONTROLLERS_DIR.DS.$controller_file_name)){
 101                  include_once(AK_CONTROLLERS_DIR.DS.$controller_file_name);
 102                  $controller_class_name = AkInflector::classify($controller_name);
 103                  $menu_options[str_replace('_controller','',$controller_name)] = $this->_get_this_class_methods($controller_class_name);
 104              }
 105              
 106          }
 107          return $menu_options;
 108      }
 109  
 110      function _get_this_class_methods($class)
 111      {
 112          $array1 = get_class_methods($class);
 113          if($parent_class = get_parent_class($class)){
 114              $array2 = get_class_methods($parent_class);
 115              $array3 = array_diff($array1, $array2);
 116          }else{
 117              $array3 = $array1;
 118          }
 119          
 120          $array3 = array_map('strtolower',(array)$array3);
 121          $array3 = array_diff($array3, array(strtolower($class), 'index', 'destroy', 'edit', 'show'));
 122          return($array3);
 123      }
 124  }
 125  
 126  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6