source: trunk/include/block.class.php @ 26010

Last change on this file since 26010 was 25762, checked in by mistic100, 10 years ago

feature 2999 : Documentation of menubar function and classes

  • Property svn:eol-style set to LF
File size: 7.3 KB
RevLine 
[2488]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2488]4// +-----------------------------------------------------------------------+
[19703]5// | Copyright(C) 2008-2013 Piwigo Team                  http://piwigo.org |
[2488]6// +-----------------------------------------------------------------------+
7// | This program is free software; you can redistribute it and/or modify  |
8// | it under the terms of the GNU General Public License as published by  |
9// | the Free Software Foundation                                          |
10// |                                                                       |
11// | This program is distributed in the hope that it will be useful, but   |
12// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
13// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
14// | General Public License for more details.                              |
15// |                                                                       |
16// | You should have received a copy of the GNU General Public License     |
17// | along with this program; if not, write to the Free Software           |
18// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
19// | USA.                                                                  |
20// +-----------------------------------------------------------------------+
21
[25762]22/**
23 * @package functions\menubar
24 */
25
26
27/**
28 * Manages a set of RegisteredBlock and DisplayBlock.
29 */
[2488]30class BlockManager
31{
[25762]32  /** @var string */
[2488]33  protected $id;
[25762]34  /** @var RegisteredBlock[] */
35  protected $registered_blocks = array();
36  /** @var DisplayBlock[] */
[2488]37  protected $display_blocks = array();
38
[25762]39  /**
40   * @param string $id
41   */
42  public function __construct($id)
[2488]43  {
44    $this->id = $id;
45  }
46
[25762]47  /**
48   * Triggers a notice that allows plugins of menu blocks to register the blocks.
49   */
[2488]50  public function load_registered_blocks()
51  {
[25762]52    trigger_action('blockmanager_register_blocks', array(&$this));
[2488]53  }
[25762]54 
55  /**
56   * @return string
57   */
[2488]58  public function get_id()
59  {
60    return $this->id;
61  }
62
[25762]63  /**
64   * @return RegisteredBlock[]
65   */
[2488]66  public function get_registered_blocks()
67  {
68    return $this->registered_blocks;
69  }
70
[25762]71  /**
72   * Add a block with the menu. Usually called in 'blockmanager_register_blocks' event.
73   *
74   * @param RegisteredBlock &$block
75   */
[2488]76  public function register_block(&$block)
77  {
[25762]78    if (isset($this->registered_blocks[$block->get_id()]))
[2488]79    {
80      trigger_error("Block '".$block->get_id()."' is already registered", E_USER_WARNING);
81      return false;
82    }
83    $this->registered_blocks[$block->get_id()] = &$block;
84    return true;
85  }
86
[25762]87  /**
88   * Performs one time preparation of registered blocks for display.
89   * Triggers 'blockmanager_prepare_display' event where plugins can
[2488]90   * reposition or hide blocks
[25762]91   */
[2488]92  public function prepare_display()
93  {
94    global $conf;
95    $conf_id = 'blk_'.$this->id;
96    $mb_conf = isset($conf[$conf_id]) ? $conf[$conf_id] : array();
[25762]97    if (!is_array($mb_conf))
98    {
[2488]99      $mb_conf = @unserialize($mb_conf);
[25762]100    }
[2488]101
102    $idx = 1;
[25762]103    foreach ($this->registered_blocks as $id => $block)
[2488]104    {
[25762]105      $pos = isset($mb_conf[$id]) ? $mb_conf[$id] : $idx*50;
106      if ($pos>0)
[2488]107      {
108        $this->display_blocks[$id] = new DisplayBlock($block);
109        $this->display_blocks[$id]->set_position($pos);
110      }
111      $idx++;
112    }
113    $this->sort_blocks();
[25762]114    trigger_action('blockmanager_prepare_display', array(&$this));
[2488]115    $this->sort_blocks();
116  }
117
[25762]118  /**
119   * Returns true if the block is hidden.
120   *
121   * @param string $block_id
122   * @return bool
123   */
[2488]124  public function is_hidden($block_id)
125  {
[25762]126    return !isset($this->display_blocks[$block_id]);
[2488]127  }
128
[25762]129  /**
130   * Remove a block from the displayed blocks.
131   *
132   * @param string $block_id
133   */
[2488]134  public function hide_block($block_id)
135  {
[25762]136    unset($this->display_blocks[$block_id]);
[2488]137  }
138
[25762]139  /**
140   * Returns a visible block.
141   *
142   * @param string $block_id
143   * @return &DisplayBlock|null
144   */
[2488]145  public function &get_block($block_id)
146  {
147    $tmp = null;
[25762]148    if (isset($this->display_blocks[$block_id]))
[2488]149    {
150      return $this->display_blocks[$block_id];
151    }
152    return $tmp;
153  }
154
[25762]155  /**
156   * Changes the position of a block.
157   *
158   * @param string $block_id
159   * @param int $position
160   */
[2488]161  public function set_block_position($block_id, $position)
162  {
[25762]163    if (isset($this->display_blocks[$block_id]))
[2488]164    {
165      $this->display_blocks[$block_id]->set_position($position);
166    }
167  }
168
[25762]169  /**
170   * Sorts the blocks.
171   */
[2488]172  protected function sort_blocks()
173  {
[25762]174    uasort($this->display_blocks, array('BlockManager', 'cmp_by_position'));
[2488]175  }
176
[25762]177  /**
178   * Callback for blocks sorting.
179   */
[2488]180  static protected function cmp_by_position($a, $b)
181  {
182    return $a->get_position() - $b->get_position();
183  }
184
[25762]185  /**
186   * Parse the menu and assign the result in a template variable.
187   *
188   * @param string $var
189   * @param string $file
190   */
[2488]191  public function apply($var, $file)
192  {
193    global $template;
194
195    $template->set_filename('menubar', $file);
196    trigger_action('blockmanager_apply', array(&$this) );
197
[25762]198    foreach ($this->display_blocks as $id=>$block)
[2488]199    {
[25762]200      if (empty($block->raw_content) and empty($block->template))
[2488]201      {
202        $this->hide_block($id);
203      }
204    }
205    $this->sort_blocks();
206    $template->assign('blocks', $this->display_blocks);
207    $template->assign_var_from_handle($var, 'menubar');
208  }
209}
210
[25762]211
[2488]212/**
[25762]213 * Represents a menu block registered in a BlockManager object.
[2488]214 */
215class RegisteredBlock
216{
[25762]217  /** @var string */
[2488]218  protected $id;
[25762]219  /** @var string */
[2488]220  protected $name;
[25762]221  /** @var string */
[2488]222  protected $owner;
223
[25762]224  /**
225   * @param string $id
226   * @param string $name
227   * @param string $owner
228   */
229  public function __construct($id, $name, $owner)
[2488]230  {
231    $this->id = $id;
232    $this->name = $name;
233    $this->owner = $owner;
234  }
235
[25762]236  /**
237   * @return string
238   */
239  public function get_id()
240  {
241    return $this->id;
242  }
243
244  /**
245   * @return string
246   */
247  public function get_name()
248  {
249    return $this->name;
250  }
251
252  /**
253   * @return string
254   */
255  public function get_owner()
256  {
257    return $this->owner;
258  }
[2488]259}
260
[25762]261
[2488]262/**
[25762]263 * Represents a menu block ready for display in the BlockManager object.
[2488]264 */
265class DisplayBlock
266{
[25762]267  /** @var RegisteredBlock */
[2488]268  protected $_registeredBlock;
[25762]269  /** @var int */
[2488]270  protected $_position;
[25762]271  /** @var string */
[2488]272  protected $_title;
273
[25762]274  /** @var mixed */
[2488]275  public $data;
[25762]276  /** @var string */
[2488]277  public $template;
[25762]278  /** @var string */
[2488]279  public $raw_content;
280
[25762]281  /**
282   * @param RegisteredBlock &$block
283   */
284  public function __construct($block)
[2488]285  {
[25762]286    $this->_registeredBlock = &$block;
[2488]287  }
288
[25762]289  /**
290   * @return &RegisteredBlock
291   */
292  public function &get_block()
293  {
294    return $this->_registeredBlock;
295  }
[2488]296
[25762]297  /**
298   * @return int
299   */
300  public function get_position()
301  {
302    return $this->_position;
303  }
304
305  /**
306   * @param int $position
307   */
[2488]308  public function set_position($position)
309  {
310    $this->_position = $position;
311  }
312
[25762]313  /**
314   * @return string
315   */
[2488]316  public function get_title()
317  {
318    if (isset($this->_title))
[25762]319    {
[2488]320      return $this->_title;
[25762]321    }
[2488]322    else
[25762]323    {
[2488]324      return $this->_registeredBlock->get_name();
[25762]325    }
[2488]326  }
327
[25762]328  /**
329   * @param string
330   */
[2488]331  public function set_title($title)
332  {
333    $this->_title = $title;
334  }
335}
336
337?>
Note: See TracBrowser for help on using the repository browser.