source: branches/2.6/include/block.class.php @ 27715

Last change on this file since 27715 was 26461, checked in by mistic100, 10 years ago

Update headers to 2014. Happy new year!!

  • Property svn:eol-style set to LF
File size: 7.3 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      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
87  /**
88   * Performs one time preparation of registered blocks for display.
89   * Triggers 'blockmanager_prepare_display' event where plugins can
90   * reposition or hide blocks
91   */
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();
97    if (!is_array($mb_conf))
98    {
99      $mb_conf = @unserialize($mb_conf);
100    }
101
102    $idx = 1;
103    foreach ($this->registered_blocks as $id => $block)
104    {
105      $pos = isset($mb_conf[$id]) ? $mb_conf[$id] : $idx*50;
106      if ($pos>0)
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();
114    trigger_action('blockmanager_prepare_display', array(&$this));
115    $this->sort_blocks();
116  }
117
118  /**
119   * Returns true if the block is hidden.
120   *
121   * @param string $block_id
122   * @return bool
123   */
124  public function is_hidden($block_id)
125  {
126    return !isset($this->display_blocks[$block_id]);
127  }
128
129  /**
130   * Remove a block from the displayed blocks.
131   *
132   * @param string $block_id
133   */
134  public function hide_block($block_id)
135  {
136    unset($this->display_blocks[$block_id]);
137  }
138
139  /**
140   * Returns a visible block.
141   *
142   * @param string $block_id
143   * @return &DisplayBlock|null
144   */
145  public function &get_block($block_id)
146  {
147    $tmp = null;
148    if (isset($this->display_blocks[$block_id]))
149    {
150      return $this->display_blocks[$block_id];
151    }
152    return $tmp;
153  }
154
155  /**
156   * Changes the position of a block.
157   *
158   * @param string $block_id
159   * @param int $position
160   */
161  public function set_block_position($block_id, $position)
162  {
163    if (isset($this->display_blocks[$block_id]))
164    {
165      $this->display_blocks[$block_id]->set_position($position);
166    }
167  }
168
169  /**
170   * Sorts the blocks.
171   */
172  protected function sort_blocks()
173  {
174    uasort($this->display_blocks, array('BlockManager', 'cmp_by_position'));
175  }
176
177  /**
178   * Callback for blocks sorting.
179   */
180  static protected function cmp_by_position($a, $b)
181  {
182    return $a->get_position() - $b->get_position();
183  }
184
185  /**
186   * Parse the menu and assign the result in a template variable.
187   *
188   * @param string $var
189   * @param string $file
190   */
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
198    foreach ($this->display_blocks as $id=>$block)
199    {
200      if (empty($block->raw_content) and empty($block->template))
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
211
212/**
213 * Represents a menu block registered in a BlockManager object.
214 */
215class RegisteredBlock
216{
217  /** @var string */
218  protected $id;
219  /** @var string */
220  protected $name;
221  /** @var string */
222  protected $owner;
223
224  /**
225   * @param string $id
226   * @param string $name
227   * @param string $owner
228   */
229  public function __construct($id, $name, $owner)
230  {
231    $this->id = $id;
232    $this->name = $name;
233    $this->owner = $owner;
234  }
235
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  }
259}
260
261
262/**
263 * Represents a menu block ready for display in the BlockManager object.
264 */
265class DisplayBlock
266{
267  /** @var RegisteredBlock */
268  protected $_registeredBlock;
269  /** @var int */
270  protected $_position;
271  /** @var string */
272  protected $_title;
273
274  /** @var mixed */
275  public $data;
276  /** @var string */
277  public $template;
278  /** @var string */
279  public $raw_content;
280
281  /**
282   * @param RegisteredBlock &$block
283   */
284  public function __construct($block)
285  {
286    $this->_registeredBlock = &$block;
287  }
288
289  /**
290   * @return &RegisteredBlock
291   */
292  public function &get_block()
293  {
294    return $this->_registeredBlock;
295  }
296
297  /**
298   * @return int
299   */
300  public function get_position()
301  {
302    return $this->_position;
303  }
304
305  /**
306   * @param int $position
307   */
308  public function set_position($position)
309  {
310    $this->_position = $position;
311  }
312
313  /**
314   * @return string
315   */
316  public function get_title()
317  {
318    if (isset($this->_title))
319    {
320      return $this->_title;
321    }
322    else
323    {
324      return $this->_registeredBlock->get_name();
325    }
326  }
327
328  /**
329   * @param string
330   */
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.