aboutsummaryrefslogtreecommitdiffstats
path: root/include/block.class.php
blob: cce92ec4e06d8f82541bb674a51cebeb3b124a87 (plain)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based photo gallery                                    |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify  |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation                                          |
// |                                                                       |
// | This program is distributed in the hope that it will be useful, but   |
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
// | General Public License for more details.                              |
// |                                                                       |
// | You should have received a copy of the GNU General Public License     |
// | along with this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+

/**
 * @package functions\menubar
 */


/**
 * Manages a set of RegisteredBlock and DisplayBlock.
 */
class BlockManager
{
  /** @var string */
  protected $id;
  /** @var RegisteredBlock[] */
  protected $registered_blocks = array();
  /** @var DisplayBlock[] */
  protected $display_blocks = array();

  /**
   * @param string $id
   */
  public function __construct($id)
  {
    $this->id = $id;
  }

  /**
   * Triggers a notice that allows plugins of menu blocks to register the blocks.
   */
  public function load_registered_blocks()
  {
    trigger_notify('blockmanager_register_blocks', array($this));
  }
  
  /**
   * @return string
   */
  public function get_id()
  {
    return $this->id;
  }

  /**
   * @return RegisteredBlock[]
   */
  public function get_registered_blocks()
  {
    return $this->registered_blocks;
  }

  /**
   * Add a block with the menu. Usually called in 'blockmanager_register_blocks' event.
   *
   * @param RegisteredBlock $block
   */
  public function register_block($block)
  {
    if (isset($this->registered_blocks[$block->get_id()]))
    {
      return false;
    }
    $this->registered_blocks[$block->get_id()] = $block;
    return true;
  }

  /**
   * Performs one time preparation of registered blocks for display.
   * Triggers 'blockmanager_prepare_display' event where plugins can
   * reposition or hide blocks
   */
  public function prepare_display()
  {
    global $conf;
    $conf_id = 'blk_'.$this->id;
    $mb_conf = isset($conf[$conf_id]) ? $conf[$conf_id] : array();
    if (!is_array($mb_conf))
    {
      $mb_conf = @unserialize($mb_conf);
    }

    $idx = 1;
    foreach ($this->registered_blocks as $id => $block)
    {
      $pos = isset($mb_conf[$id]) ? $mb_conf[$id] : $idx*50;
      if ($pos>0)
      {
        $this->display_blocks[$id] = new DisplayBlock($block);
        $this->display_blocks[$id]->set_position($pos);
      }
      $idx++;
    }
    $this->sort_blocks();
    trigger_notify('blockmanager_prepare_display', array($this));
    $this->sort_blocks();
  }

  /**
   * Returns true if the block is hidden.
   *
   * @param string $block_id
   * @return bool
   */
  public function is_hidden($block_id)
  {
    return !isset($this->display_blocks[$block_id]);
  }

  /**
   * Remove a block from the displayed blocks.
   *
   * @param string $block_id
   */
  public function hide_block($block_id)
  {
    unset($this->display_blocks[$block_id]);
  }

  /**
   * Returns a visible block.
   *
   * @param string $block_id
   * @return DisplayBlock|null
   */
  public function get_block($block_id)
  {
    if (isset($this->display_blocks[$block_id]))
    {
      return $this->display_blocks[$block_id];
    }
    return null;
  }

  /**
   * Changes the position of a block.
   *
   * @param string $block_id
   * @param int $position
   */
  public function set_block_position($block_id, $position)
  {
    if (isset($this->display_blocks[$block_id]))
    {
      $this->display_blocks[$block_id]->set_position($position);
    }
  }

  /**
   * Sorts the blocks.
   */
  protected function sort_blocks()
  {
    uasort($this->display_blocks, array('BlockManager', 'cmp_by_position'));
  }

  /**
   * Callback for blocks sorting.
   */
  static protected function cmp_by_position($a, $b)
  {
    return $a->get_position() - $b->get_position();
  }

  /**
   * Parse the menu and assign the result in a template variable.
   *
   * @param string $var
   * @param string $file
   */
  public function apply($var, $file)
  {
    global $template;

    $template->set_filename('menubar', $file);
    trigger_notify('blockmanager_apply', array($this) );

    foreach ($this->display_blocks as $id=>$block)
    {
      if (empty($block->raw_content) and empty($block->template))
      {
        $this->hide_block($id);
      }
    }
    $this->sort_blocks();
    $template->assign('blocks', $this->display_blocks);
    $template->assign_var_from_handle($var, 'menubar');
  }
}


/**
 * Represents a menu block registered in a BlockManager object.
 */
class RegisteredBlock
{
  /** @var string */
  protected $id;
  /** @var string */
  protected $name;
  /** @var string */
  protected $owner;

  /**
   * @param string $id
   * @param string $name
   * @param string $owner
   */
  public function __construct($id, $name, $owner)
  {
    $this->id = $id;
    $this->name = $name;
    $this->owner = $owner;
  }

  /**
   * @return string
   */
  public function get_id()
  {
    return $this->id;
  }

  /**
   * @return string
   */
  public function get_name()
  {
    return $this->name;
  }

  /**
   * @return string
   */
  public function get_owner()
  {
    return $this->owner;
  }
}


/**
 * Represents a menu block ready for display in the BlockManager object.
 */
class DisplayBlock
{
  /** @var RegisteredBlock */
  protected $_registeredBlock;
  /** @var int */
  protected $_position;
  /** @var string */
  protected $_title;

  /** @var mixed */
  public $data;
  /** @var string */
  public $template;
  /** @var string */
  public $raw_content;

  /**
   * @param RegisteredBlock $block
   */
  public function __construct($block)
  {
    $this->_registeredBlock = $block;
  }

  /**
   * @return RegisteredBlock
   */
  public function get_block()
  {
    return $this->_registeredBlock;
  }

  /**
   * @return int
   */
  public function get_position()
  {
    return $this->_position;
  }

  /**
   * @param int $position
   */
  public function set_position($position)
  {
    $this->_position = $position;
  }

  /**
   * @return string
   */
  public function get_title()
  {
    if (isset($this->_title))
    {
      return $this->_title;
    }
    else
    {
      return $this->_registeredBlock->get_name();
    }
  }

  /**
   * @param string
   */
  public function set_title($title)
  {
    $this->_title = $title;
  }
}

?>