<?php
require_once 'newedge/classes/NE_MenuItem.php';

/**
* Parent class for menu hooks (to generate the menu dropdowns at the top of the screen)
*
* @package Newedge_API
* @abstract
*/
class NE_MenuHook
{
    /**
    * Configuration array
    *
    * @access protected
    * @see NE_Model::configuration
    * @var array
    */
    var $configuration;

    /**
    * Settings object.
    *
    * @access protected
    * @var Settings
    */
    var $settings;


    /**
    * Logger object.
    *
    * @access protected
    * @var NE_Observable
    */
    var $logger;

    /**
    * The user object
    *
    * @access protected
    * @var User
    */
    var $user;

    var $db;

    /**
    * Description that will appear under the menu item, when viewed from the customer's section
    *
    * @access public
    */
    var $menuDescription;

    /**
    * The customer object, or false if logged-in user is an admin that hasn't selected a customer.
    *
    * @access protected
    * @var User
    */
    var $customer;

    /**
    * Template object.
    *
    * @access protected
    * @var NE_Template
    */
    var $tpl;

    /**
    * List of menuitems for this menu
    *
    * @access protected
    * @var NE_MenuItem
    */
    var $menuItems = array();

    /**
    * This is "package private" rather.
    *
    * @access private
    */
    function NE_MenuHook(&$user, &$customer, &$tpl)
    {
            $this->user =& $user;
            $this->customer =& $customer;
            $this->tpl =& $tpl;
            $this->db = NE_Registry::getDatabase();
            $this->settings =& NE_Registry::getSettings();
            $this->configuration = NE_Registry::getConfiguration();
            $this->logger = NE_Registry::getLogger();
    }

    function addItem($menuItem){
            $this->menuItems[] = $menuItem;
    }

    function getItems($checkUserPermission=false){
            if($checkUserPermission){
                    $returnArray = array();
                    //go through all the items and check permission before we return this
                    foreach($this->menuItems as $menuItem){
                            $haspermission = true;
                            foreach ($menuItem->getPermissions() as $permission) {
                                     if (!$this->user->hasPermission($permission)){
                                            $haspermission = false;
                                     }
                            }
                            
                            //if user has permission the add menu to the returned collection
                            if($haspermission)
                            {
                                $returnArray[] = $menuItem;
                            }
                    }
                    return $returnArray;
            }
            return $this->menuItems;
    }

    function getModulesViews()
    {
        return $this->modulesViews;
    }

    function getMenuDescription()
    {
        return $this->menuDescription;
    }

}

?>
