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

Last change on this file since 27370 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
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
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
22/**
23 * @package functions\menubar
24 */
25
26
27/**
28 * Manages a set of RegisteredBlock and DisplayBlock.
29 */
30class BlockManager
31{
32  /** @var string */
33  protected $id;
34  /** @var RegisteredBlock[] */
35  protected $registered_blocks = array();
36  /** @var DisplayBlock[] */
37  protected $display_blocks = array();
38
39  /**
40   * @param string $id
41   */
42  public function __construct($id)
43  {
44    $this->id = $id;
45  }
46
47  /**
48   * Triggers a notice that allows plugins of menu blocks to register the blocks.
49   */
50  public function load_registered_blocks()
51  {
52    trigger_action('blockmanager_register_blocks', array($this));
53  }
54 
55  /**
56   * @return string
57   */
58  public function get_id()
59  {
60    return $this->id;
61  }
62
63  /**
64   * @return RegisteredBlock[]
65   */
66  public function get_registered_blocks()
67  {
68    return $this->registered_blocks;
69  }
70
71  /**
72   * Add a block with the menu. Usually called in 'blockmanager_register_blocks' event.
73   *
74   * @param RegisteredBlock $block
75   */
76  public function register_block($block)
77  {
78    if (isset($this->registered_blocks[$block->get_id()]))
79    {
80      return false;
81    }
82    $this->registered_blocks[$block->get_id()] = $block;
83    return true;
84  }
85
86  /**
87   * Performs one time preparation of registered blocks for display.
88   * Triggers 'blockmanager_prepare_display' event where plugins can
89   * reposition or hide blocks
90   */
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();
96    if (!is_array($mb_conf))
97    {
98      $mb_conf = @unserialize($mb_conf);
99    }
100
101    $idx = 1;
102    foreach ($this->registered_blocks as $id => $block)
103    {
104      $pos = isset($mb_conf[$id]) ? $mb_conf[$id] : $idx*50;
105      if ($pos>0)
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();
113    trigger_action('blockmanager_prepare_display', array($this));
114    $this->sort_blocks();
115  }
116
117  /**
118   * Returns true if the block is hidden.
119   *
120   * @param string $block_id
121   * @return bool
122   */
123  public function is_hidden($block_id)
124  {
125    return !isset($this->display_blocks[$block_id]);
126  }
127
128  /**
129   * Remove a block from the displayed blocks.
130   *
131   * @param string $block_id
132   */
133  public function hide_block($block_id)
134  {
135    unset($this->display_blocks[$block_id]);
136  }
137
138  /**
139   * Returns a visible block.
140   *
141   * @param string $block_id
142   * @return DisplayBlock|null
143   */
144  public function get_block($block_id)
145  {
146    if (isset($this->display_blocks[$block_id]))
147    {
148      return $this->display_blocks[$block_id];
149    }
150    return null;
151  }
152
153  /**
154   * Changes the position of a block.
155   *
156   * @param string $block_id
157   * @param int $position
158   */
159  public function set_block_position($block_id, $position)
160  {
161    if (isset($this->display_blocks[$block_id]))
162    {
163      $this->display_blocks[$block_id]->set_position($position);
164    }
165  }
166
167  /**
168   * Sorts the blocks.
169   */
170  protected function sort_blocks()
171  {
172    uasort($this->display_blocks, array('BlockManager', 'cmp_by_position'));
173  }
174
175  /**
176   * Callback for blocks sorting.
177   */
178  static protected function cmp_by_position($a, $b)
179  {
180    return $a->get_position() - $b->get_position();
181  }
182
183  /**
184   * Parse the menu and assign the result in a template variable.
185   *
186   * @param string $var
187   * @param string $file
188   */
189  public function apply($var, $file)
190  {
191    global $template;
192
193    $template->set_filename('menubar', $file);
194    trigger_action('blockmanager_apply', array($this) );
195
196    foreach ($this->display_blocks as $id=>$block)
197    {
198      if (empty($block->raw_content) and empty($block->template))
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
209
210/**
211 * Represents a menu block registered in a BlockManager object.
212 */
213class RegisteredBlock
214{
215  /** @var string */
216  protected $id;
217  /** @var string */
218  protected $name;
219  /** @var string */
220  protected $owner;
221
222  /**
223   * @param string $id
224   * @param string $name
225   * @param string $owner
226   */
227  public function __construct($id, $name, $owner)
228  {
229    $this->id = $id;
230    $this->name = $name;
231    $this->owner = $owner;
232  }
233
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  }
257}
258
259
260/**
261 * Represents a menu block ready for display in the BlockManager object.
262 */
263class DisplayBlock
264{
265  /** @var RegisteredBlock */
266  protected $_registeredBlock;
267  /** @var int */
268  protected $_position;
269  /** @var string */
270  protected $_title;
271
272  /** @var mixed */
273  public $data;
274  /** @var string */
275  public $template;
276  /** @var string */
277  public $raw_content;
278
279  /**
280   * @param RegisteredBlock $block
281   */
282  public function __construct($block)
283  {
284    $this->_registeredBlock = $block;
285  }
286
287  /**
288   * @return RegisteredBlock
289   */
290  public function get_block()
291  {
292    return $this->_registeredBlock;
293  }
294
295  /**
296   * @return int
297   */
298  public function get_position()
299  {
300    return $this->_position;
301  }
302
303  /**
304   * @param int $position
305   */
306  public function set_position($position)
307  {
308    $this->_position = $position;
309  }
310
311  /**
312   * @return string
313   */
314  public function get_title()
315  {
316    if (isset($this->_title))
317    {
318      return $this->_title;
319    }
320    else
321    {
322      return $this->_registeredBlock->get_name();
323    }
324  }
325
326  /**
327   * @param string
328   */
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.