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

Last change on this file since 27541 was 27370, checked in by rvelices, 10 years ago

remove prehistoric unnecessary object references

  • Property svn:eol-style set to LF
File size: 7.1 KB
RevLine 
[2488]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2488]4// +-----------------------------------------------------------------------+
[26461]5// | Copyright(C) 2008-2014 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  {
[27370]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   *
[27370]74   * @param RegisteredBlock $block
[25762]75   */
[27370]76  public function register_block($block)
[2488]77  {
[25762]78    if (isset($this->registered_blocks[$block->get_id()]))
[2488]79    {
80      return false;
81    }
[27370]82    $this->registered_blocks[$block->get_id()] = $block;
[2488]83    return true;
84  }
85
[25762]86  /**
87   * Performs one time preparation of registered blocks for display.
88   * Triggers 'blockmanager_prepare_display' event where plugins can
[2488]89   * reposition or hide blocks
[25762]90   */
[2488]91  public function prepare_display()
92  {
93    global $conf;
94    $conf_id = 'blk_'.$this->id;
95    $mb_conf = isset($conf[$conf_id]) ? $conf[$conf_id] : array();
[25762]96    if (!is_array($mb_conf))
97    {
[2488]98      $mb_conf = @unserialize($mb_conf);
[25762]99    }
[2488]100
101    $idx = 1;
[25762]102    foreach ($this->registered_blocks as $id => $block)
[2488]103    {
[25762]104      $pos = isset($mb_conf[$id]) ? $mb_conf[$id] : $idx*50;
105      if ($pos>0)
[2488]106      {
107        $this->display_blocks[$id] = new DisplayBlock($block);
108        $this->display_blocks[$id]->set_position($pos);
109      }
110      $idx++;
111    }
112    $this->sort_blocks();
[27370]113    trigger_action('blockmanager_prepare_display', array($this));
[2488]114    $this->sort_blocks();
115  }
116
[25762]117  /**
118   * Returns true if the block is hidden.
119   *
120   * @param string $block_id
121   * @return bool
122   */
[2488]123  public function is_hidden($block_id)
124  {
[25762]125    return !isset($this->display_blocks[$block_id]);
[2488]126  }
127
[25762]128  /**
129   * Remove a block from the displayed blocks.
130   *
131   * @param string $block_id
132   */
[2488]133  public function hide_block($block_id)
134  {
[25762]135    unset($this->display_blocks[$block_id]);
[2488]136  }
137
[25762]138  /**
139   * Returns a visible block.
140   *
141   * @param string $block_id
[27370]142   * @return DisplayBlock|null
[25762]143   */
[27370]144  public function get_block($block_id)
[2488]145  {
[25762]146    if (isset($this->display_blocks[$block_id]))
[2488]147    {
148      return $this->display_blocks[$block_id];
149    }
[27370]150    return null;
[2488]151  }
152
[25762]153  /**
154   * Changes the position of a block.
155   *
156   * @param string $block_id
157   * @param int $position
158   */
[2488]159  public function set_block_position($block_id, $position)
160  {
[25762]161    if (isset($this->display_blocks[$block_id]))
[2488]162    {
163      $this->display_blocks[$block_id]->set_position($position);
164    }
165  }
166
[25762]167  /**
168   * Sorts the blocks.
169   */
[2488]170  protected function sort_blocks()
171  {
[25762]172    uasort($this->display_blocks, array('BlockManager', 'cmp_by_position'));
[2488]173  }
174
[25762]175  /**
176   * Callback for blocks sorting.
177   */
[2488]178  static protected function cmp_by_position($a, $b)
179  {
180    return $a->get_position() - $b->get_position();
181  }
182
[25762]183  /**
184   * Parse the menu and assign the result in a template variable.
185   *
186   * @param string $var
187   * @param string $file
188   */
[2488]189  public function apply($var, $file)
190  {
191    global $template;
192
193    $template->set_filename('menubar', $file);
[27370]194    trigger_action('blockmanager_apply', array($this) );
[2488]195
[25762]196    foreach ($this->display_blocks as $id=>$block)
[2488]197    {
[25762]198      if (empty($block->raw_content) and empty($block->template))
[2488]199      {
200        $this->hide_block($id);
201      }
202    }
203    $this->sort_blocks();
204    $template->assign('blocks', $this->display_blocks);
205    $template->assign_var_from_handle($var, 'menubar');
206  }
207}
208
[25762]209
[2488]210/**
[25762]211 * Represents a menu block registered in a BlockManager object.
[2488]212 */
213class RegisteredBlock
214{
[25762]215  /** @var string */
[2488]216  protected $id;
[25762]217  /** @var string */
[2488]218  protected $name;
[25762]219  /** @var string */
[2488]220  protected $owner;
221
[25762]222  /**
223   * @param string $id
224   * @param string $name
225   * @param string $owner
226   */
227  public function __construct($id, $name, $owner)
[2488]228  {
229    $this->id = $id;
230    $this->name = $name;
231    $this->owner = $owner;
232  }
233
[25762]234  /**
235   * @return string
236   */
237  public function get_id()
238  {
239    return $this->id;
240  }
241
242  /**
243   * @return string
244   */
245  public function get_name()
246  {
247    return $this->name;
248  }
249
250  /**
251   * @return string
252   */
253  public function get_owner()
254  {
255    return $this->owner;
256  }
[2488]257}
258
[25762]259
[2488]260/**
[25762]261 * Represents a menu block ready for display in the BlockManager object.
[2488]262 */
263class DisplayBlock
264{
[25762]265  /** @var RegisteredBlock */
[2488]266  protected $_registeredBlock;
[25762]267  /** @var int */
[2488]268  protected $_position;
[25762]269  /** @var string */
[2488]270  protected $_title;
271
[25762]272  /** @var mixed */
[2488]273  public $data;
[25762]274  /** @var string */
[2488]275  public $template;
[25762]276  /** @var string */
[2488]277  public $raw_content;
278
[25762]279  /**
[27370]280   * @param RegisteredBlock $block
[25762]281   */
282  public function __construct($block)
[2488]283  {
[27370]284    $this->_registeredBlock = $block;
[2488]285  }
286
[25762]287  /**
[27370]288   * @return RegisteredBlock
[25762]289   */
[27370]290  public function get_block()
[25762]291  {
292    return $this->_registeredBlock;
293  }
[2488]294
[25762]295  /**
296   * @return int
297   */
298  public function get_position()
299  {
300    return $this->_position;
301  }
302
303  /**
304   * @param int $position
305   */
[2488]306  public function set_position($position)
307  {
308    $this->_position = $position;
309  }
310
[25762]311  /**
312   * @return string
313   */
[2488]314  public function get_title()
315  {
316    if (isset($this->_title))
[25762]317    {
[2488]318      return $this->_title;
[25762]319    }
[2488]320    else
[25762]321    {
[2488]322      return $this->_registeredBlock->get_name();
[25762]323    }
[2488]324  }
325
[25762]326  /**
327   * @param string
328   */
[2488]329  public function set_title($title)
330  {
331    $this->_title = $title;
332  }
333}
334
335?>
Note: See TracBrowser for help on using the repository browser.