aboutsummaryrefslogtreecommitdiffstats
path: root/include/block.class.php
blob: e8f09174182dc53c548ca534284bf110bf645af1 (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
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based picture gallery                                  |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008-2010 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.                                                                  |
// +-----------------------------------------------------------------------+

class BlockManager
{
  protected $id;
  protected $registered_blocks=array();
  protected $display_blocks = array();

  public function BlockManager($id)
  {
    $this->id = $id;
  }

  /** triggers an action that allows implementors of menu blocks to register the blocks*/
  public function load_registered_blocks()
  {
    trigger_action('blockmanager_register_blocks', array(&$this) );
  }

  public function get_id()
  {
    return $this->id;
  }

  public function get_registered_blocks()
  {
    return $this->registered_blocks;
  }

  /** registers a block with this menu. usually called as a result of menubar_register_blocks action
   * @param MenuBlock block
  */
  public function register_block(&$block)
  {
    if ( isset($this->registered_blocks[$block->get_id()] ) )
    {
      trigger_error("Block '".$block->get_id()."' is already registered", E_USER_WARNING);
      return false;
    }
    $this->registered_blocks[$block->get_id()] = &$block;
    return true;
  }

  /** performs one time preparation of registered blocks for display;
   * triggers the action menubar_prepare_display where implementors 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_action( 'blockmanager_prepare_display', array(&$this) );
    $this->sort_blocks();
  }

  /** returns true if the block whose id is hidden
   * @param string block_id
  */
  public function is_hidden($block_id)
  {
    return isset($this->display_blocks[$block_id]) ? false : true;
  }

  public function hide_block($block_id)
  {
    unset( $this->display_blocks[$block_id] );
  }

  public function &get_block($block_id)
  {
    $tmp = null;
    if ( isset($this->display_blocks[$block_id]) )
    {
      return $this->display_blocks[$block_id];
    }
    return $tmp;
  }

  public function set_block_position($block_id, $position)
  {
    if ( isset($this->display_blocks[$block_id]) )
    {
      $this->display_blocks[$block_id]->set_position($position);
    }
  }

  protected function sort_blocks()
  {
    uasort( $this->display_blocks, array('BlockManager', 'cmp_by_position') );
  }

  static protected function cmp_by_position($a, $b)
  {
    return $a->get_position() - $b->get_position();
  }

  public function apply($var, $file)
  {
    global $template;

    $template->set_filename('menubar', $file);
    trigger_action('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 Menu object.
 */
class RegisteredBlock
{
  protected $id;
  protected $name;
  protected $owner;

  public function RegisteredBlock($id, $name, $owner)
  {
    $this->id = $id;
    $this->name = $name;
    $this->owner = $owner;
  }

  public function get_id() { return $this->id; }
  public function get_name() { return $this->name; }
  public function get_owner() { return $this->owner; }
}

/**
 * Represents a menu block ready for display in the Menu object.
 */
class DisplayBlock
{
  protected $_registeredBlock;
  protected $_position;

  protected $_title;

  public $data;
  public $template;
  public $raw_content;

  public function DisplayBlock($registeredBlock)
  {
    $this->_registeredBlock = &$registeredBlock;
  }

  public function &get_block() { return $this->_registeredBlock; }

  public function get_position() { return $this->_position; }
  public function set_position($position)
  {
    $this->_position = $position;
  }

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

  public function set_title($title)
  {
    $this->_title = $title;
  }
}

?>