Advertisement
Guest User

Untitled

a guest
Jun 27th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.51 KB | None | 0 0
  1. <?php
  2. /**
  3.  * This class defines a menu item that will be used with Zend_Navigation to generate a menu
  4.  *
  5.  * @author Joel Lord
  6.  * @since 20 mai 2012
  7.  *
  8.  */
  9. class ZARD_Object_Module_Multimodmenu {
  10.     /**
  11.      * Instance of the class
  12.      * @var ZARD_Object_Module_Multimodmenu
  13.      */
  14.     private static $instance;
  15.    
  16.     /**
  17.      * Name of the module
  18.      * @var Zend_Navigation_Container
  19.      */
  20.     private static $_menu;
  21.    
  22.     /**
  23.      * Holds the ACL helper
  24.      * @var Zend_Controller_Action_Helper_Abstract
  25.      */
  26.     protected $_acl;
  27.    
  28.     /**
  29.      * id of the menu item
  30.      * @var string
  31.      */
  32.     public $id;
  33.     /**
  34.      * Label to be displayed
  35.      * @var string
  36.      */
  37.     public $label;
  38.     /**
  39.      * Parent menu (moduleName_id)
  40.      * @var string
  41.      */
  42.     public $parent;
  43.     /**
  44.      * Module to be called
  45.      * @var string
  46.      */
  47.     public $module;
  48.     /**
  49.      * Controller to be called
  50.      * @var string
  51.      */
  52.     public $controller;
  53.     /**
  54.      * Action to be called
  55.      * @var string
  56.      */
  57.     public $action;
  58.     /**
  59.      * External URL if link should be outside the site
  60.      * @var string
  61.      */
  62.     public $extUrl;
  63.     /**
  64.      * Where should the item appear in the menu
  65.      * @var int
  66.      */
  67.     public $order;
  68.     /**
  69.      * Options used to build the url
  70.      * @var array string
  71.      */
  72.     public $params;
  73.    
  74.     private function __construct() {
  75.        
  76.     }
  77.    
  78.     /**
  79.      * get an Instance of the multimodmenu
  80.      * Singleton pattern
  81.      * @return Zend_Object_Module_Multimodmenu
  82.      */
  83.     public static function getInstance() {
  84.         if(!isset(self::$instance)) {
  85.             self::$instance = new ZARD_Object_Module_Multimodmenu();
  86.             self::$_menu = new Zend_Navigation();
  87.         }
  88.         return self::$instance;
  89.     }
  90.    
  91.     /**
  92.      * This function adds a menu item to the menu and flushes all vars
  93.      */
  94.     public function addItem() {
  95.         //Check for privileges before adding the menu item
  96.         //Load the $acl helper
  97.         if(!Zend_Controller_Action_HelperBroker::hasHelper('IsAllowed'))
  98.             Zend_Controller_Action_HelperBroker::addHelper(new ZARD_Helper_IsAllowed());
  99.        
  100.         $acl = Zend_Controller_Action_HelperBroker::getStaticHelper('IsAllowed');
  101.        
  102.         //Check the rights
  103.         if(!$this->module && !$this->controller && !$this->action) {
  104.             //No module/controller/action triplet entered, this is a submenu
  105.             $submenu = true;
  106.         } else {
  107.             //Check if user has the rights to view this menu item
  108.             $allowed = $acl->isAllowed($this->module, $this->controller, $this->action);
  109.         }
  110.            
  111.         if($allowed || $submenu) {
  112.        
  113.             //Prepare the item depeping if the item is an external link or internal
  114.             if($this->extUrl) {
  115.                 $item = new Zend_Navigation_Page_Uri();
  116.                 $item->setUri($this->extUrl);
  117.             } else {
  118.                 $item = new Zend_Navigation_Page_Mvc();
  119.                 $item->setModule($this->module);
  120.                 $item->setController($this->controller);
  121.                 $item->setAction($this->action);
  122.             }
  123.    
  124.             //Set the label, the id and the order
  125.             $item->setLabel($this->label);
  126.             $item->setId($this->id);
  127.             $item->setOrder($this->order);
  128.             $item->setParams($this->params);
  129.            
  130.             //Add to submenu if needed
  131.             $submenu = self::$_menu->findBy('id', $this->parent, false);
  132.             if($submenu) {
  133.                 $submenu->addPage($item);
  134.             } else {
  135.                 self::$_menu->addPage($item);
  136.             }
  137.         }
  138.        
  139.         //Flush all vars
  140.         $this->id = null;
  141.         $this->label = null;
  142.         $this->parent = null;
  143.         $this->module = null;
  144.         $this->controller = null;
  145.         $this->action = null;
  146.         $this->extUrl = null;
  147.         $this->order = null;
  148.         $this->params = null;
  149.     }
  150.    
  151.     /**
  152.      * This method returns the menu
  153.      * @return Zend_Navigation_Container
  154.      */
  155.     public function getMenu() {
  156.         return self::$_menu;
  157.     }
  158.    
  159.    
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement