Classes

File phpboost/menu/Menu.class.php

File phpboost/menu/Menu.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 
<?php
/**
 * This class represents a menu element and is used to build any kind of menu
 * @package     PHPBoost
 * @subpackage  Menu
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Loic ROUCHON <horn@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2016 07 26
 * @since       PHPBoost 2.0 - 2008 11 15
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
*/

abstract class Menu
{
    const MENU_AUTH_BIT = 1;
    const MENU_ENABLE_OR_NOT = 42;
    const MENU_ENABLED = true;
    const MENU_NOT_ENABLED = false;

    const BLOCK_POSITION__NOT_ENABLED = 0;
    const BLOCK_POSITION__HEADER = 1;
    const BLOCK_POSITION__SUB_HEADER = 2;
    const BLOCK_POSITION__TOP_CENTRAL = 3;
    const BLOCK_POSITION__BOTTOM_CENTRAL = 4;
    const BLOCK_POSITION__TOP_FOOTER = 5;
    const BLOCK_POSITION__FOOTER = 6;
    const BLOCK_POSITION__LEFT = 7;
    const BLOCK_POSITION__RIGHT = 8;
    const BLOCK_POSITION__ALL = 9;

    const MENU__CLASS = 'Menu';

    /**
     * @access protected
     * @var int the element identifier, only used by the service
     */
    public $id = 0;
    /**
     * @access protected
     * @var string the Menu title
     */
    public $title = '';
    /**
     * @access protected
     * @var int[string] Represents the Menu authorisations array
     */
    public $auth = null;
    /**
     * @access protected
     * @var bool true if the Menu is used
     */
    public $enabled = self::MENU_NOT_ENABLED;
    /**
     * @access protected
     * @var int The Menu block position
     */
    public $block = self::BLOCK_POSITION__NOT_ENABLED;
    /**
     * @access protected
     * @var int The Menu position on the website
     */
    public $position = -1;
    /**
    * @access protected
    * @var bool menu hidden or not with small screens
    */
    protected $hidden_with_small_screens = false;
    /**
     * @access protected
     * @var Array<Filter> The filter list
     */
    public $filters = array();
    /**
     * @access protected
     * @var Template the template of the menu
     */
    protected $template = null;


    /**
     * Build a Menu element.
     * @param string $title the Menu title
     * @param int $id its id in the database
     */
    public function __construct($title)
    {
        $this->title = TextHelper::strprotect($title, TextHelper::HTML_PROTECT, TextHelper::ADDSLASHES_NONE);
        $this->filters[] = new MenuStringFilter('/');
    }

    /**
     * Check if the menu needs to be cached
     * @return bool true if the menu need to be cached
     */
    public function need_cached_string()
    {
        return false;
    }

    /**
     * Display the menu
     * @abstract
     * @return string the menu parsed in xHTML
     */
    abstract public function display();

    /**
     * Display the menu admin gui
     * @return string the menu parsed in xHTML
     */
    public function admin_display()
    {
        return $this->display();
    }

    /**
     * @param int $id Set the Menu database id
     */
    public function id($id) { $this->id = $id; }

    /**
     * Assign tpl vars
     * @access protected
     * @param Template $template the template on which we gonna assign vars
     */
    protected function _assign($template)
    {
        MenuService::assign_positions_conditions($template, $this->get_block());
    }

    /**
     * Assign default tpl vars
     * @access protected
     * @param Template $template the template on which we gonna assign vars
     */
    protected function assign_common_template_variables(Template $template)
    {
        $template->put_all(array(
            'C_VERTICAL_BLOCK' => ($this->get_block() == Menu::BLOCK_POSITION__LEFT || $this->get_block() == Menu::BLOCK_POSITION__RIGHT),
            'C_HIDDEN_WITH_SMALL_SCREENS' => $this->hidden_with_small_screens
        ));
    }

    /**
     * Check the user authorization to see the LinksMenuElement
     * @return bool true if the user is authorised, false otherwise
     */
    public function check_auth()
    {
        return $this->auth === null || AppContext::get_current_user()->check_auth($this->auth, self::MENU_AUTH_BIT);
    }

    ## Setters ##
    /**
     * @param string $image the value to set
     */
    public function set_title($title) { $this->title = TextHelper::strprotect($title, TextHelper::HTML_PROTECT, TextHelper::ADDSLASHES_NONE); }
    /**
     * @param array $url the authorisation array to set
     */
    public function set_auth($auth) { $this->auth = $auth; }
    /**
     * @param bool $enabled Enable or not the Menu
     */
    public function enabled($enabled = self::MENU_ENABLED) { $this->enabled = $enabled; }
    /**
     * @return int the Menu $block position
     */
    public function set_block($block) { $this->block = $block; }
    /**
     * @param int $position the Menu position to set
     */
    public function set_block_position($position) { $this->position = $position; }
    /*
    * @param bool $value true if menu hidden with small screens
    */
    public function set_hidden_with_small_screens($value) { $this->hidden_with_small_screens = $value; }

    ## Getters ##
    /**
     * @return string the displayable Menu $title
     */
    public function get_formated_title() { return $this->title; }
    /**
     * @return string the Menu $title
     */
    public function get_title() { return $this->title; }
    /**
     * @return array the authorization array $auth
     */
    public function get_auth() { return is_array($this->auth) ? $this->auth : array('r-1' => self::MENU_AUTH_BIT, 'r0' => self::MENU_AUTH_BIT, 'r1' => self::MENU_AUTH_BIT); }
    /**
     * @return int the $id of the menu in the database
     */
    public function get_id() { return $this->id; }
    /**
     * @return int the Menu $block position
     */
    public function get_block() { return $this->block; }
    /**
     * @return int the Menu $position
     */
    public function get_block_position() { return $this->position; }
    /**
     * @return bool true if the Menu is enabled, false otherwise
     */
    public function is_enabled() { return $this->enabled; }
    /**
    * @return bool check if menu is hidden with small screens
    */
    public function is_hidden_with_small_screens() { return $this->hidden_with_small_screens; }
    /**
    * @return string the menu filters
    */
    public function get_filters() { return $this->filters; }

    /**
    * Sets the filters of the menu
    *
    * @param Array<Filter> $filters Filters of the menu
    */
    public function set_filters($filters) { $this->filters = $filters; }

     /**
    * Sets the template of the menu
    *
    * @param Template $template Template of the menu
    */
    public function set_template(Template $template)
    {
        $this->template = $template;
    }

    /**
     * @return Template
     */
    protected function get_template_to_use()
    {
        if ($this->template !== null)
        {
            return $this->template;
        }
        else
        {
            return $this->get_default_template();
        }
    }

    /**
    * Get the default template of the menu
    * @abstract
    * @return string the default template of the menu
    */
    protected function get_default_template() {}
}
?>