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

Last change on this file since 5986 was 5986, checked in by plg, 14 years ago

bug 1484: after a "quick connect" you are correctly redirect to the same page.

File size: 5.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 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
22class BlockManager
23{
24  protected $id;
25  protected $registered_blocks=array();
26  protected $display_blocks = array();
27
28  public function BlockManager($id)
29  {
30    $this->id = $id;
31  }
32
33  /** triggers an action that allows implementors of menu blocks to register the blocks*/
34  public function load_registered_blocks()
35  {
36    trigger_action('blockmanager_register_blocks', array(&$this) );
37  }
38
39  public function get_id()
40  {
41    return $this->id;
42  }
43
44  public function get_registered_blocks()
45  {
46    return $this->registered_blocks;
47  }
48
49  /** registers a block with this menu. usually called as a result of menubar_register_blocks action
50   * @param MenuBlock block
51  */
52  public function register_block(&$block)
53  {
54    if ( isset($this->registered_blocks[$block->get_id()] ) )
55    {
56      trigger_error("Block '".$block->get_id()."' is already registered", E_USER_WARNING);
57      return false;
58    }
59    $this->registered_blocks[$block->get_id()] = &$block;
60    return true;
61  }
62
63  /** performs one time preparation of registered blocks for display;
64   * triggers the action menubar_prepare_display where implementors can
65   * reposition or hide blocks
66  */
67  public function prepare_display()
68  {
69    global $conf;
70    $conf_id = 'blk_'.$this->id;
71    $mb_conf = isset($conf[$conf_id]) ? $conf[$conf_id] : array();
72    if ( !is_array($mb_conf) )
73      $mb_conf = @unserialize($mb_conf);
74
75    $idx = 1;
76    foreach( $this->registered_blocks as $id => $block )
77    {
78      $pos = isset( $mb_conf[$id] ) ? $mb_conf[$id] : $idx*50;
79      if ( $pos>0 )
80      {
81        $this->display_blocks[$id] = new DisplayBlock($block);
82        $this->display_blocks[$id]->set_position($pos);
83      }
84      $idx++;
85    }
86    $this->sort_blocks();
87    trigger_action( 'blockmanager_prepare_display', array(&$this) );
88    $this->sort_blocks();
89  }
90
91  /** returns true if the block whose id is hidden
92   * @param string block_id
93  */
94  public function is_hidden($block_id)
95  {
96    return isset($this->display_blocks[$block_id]) ? false : true;
97  }
98
99  public function hide_block($block_id)
100  {
101    unset( $this->display_blocks[$block_id] );
102  }
103
104  public function &get_block($block_id)
105  {
106    $tmp = null;
107    if ( isset($this->display_blocks[$block_id]) )
108    {
109      return $this->display_blocks[$block_id];
110    }
111    return $tmp;
112  }
113
114  public function set_block_position($block_id, $position)
115  {
116    if ( isset($this->display_blocks[$block_id]) )
117    {
118      $this->display_blocks[$block_id]->set_position($position);
119    }
120  }
121
122  protected function sort_blocks()
123  {
124    uasort( $this->display_blocks, array('BlockManager', 'cmp_by_position') );
125  }
126
127  static protected function cmp_by_position($a, $b)
128  {
129    return $a->get_position() - $b->get_position();
130  }
131
132  public function apply($var, $file)
133  {
134    global $template;
135
136    $template->set_filename('menubar', $file);
137    $template->assign(array('U_REDIRECT' => $_SERVER['REQUEST_URI']));
138    trigger_action('blockmanager_apply', array(&$this) );
139
140    foreach( $this->display_blocks as $id=>$block)
141    {
142      if (empty($block->raw_content) and empty($block->template) )
143      {
144        $this->hide_block($id);
145      }
146    }
147    $this->sort_blocks();
148    $template->assign('blocks', $this->display_blocks);
149    $template->assign_var_from_handle($var, 'menubar');
150  }
151}
152
153/**
154 * Represents a menu block registered in a Menu object.
155 */
156class RegisteredBlock
157{
158  protected $id;
159  protected $name;
160  protected $owner;
161
162  public function RegisteredBlock($id, $name, $owner)
163  {
164    $this->id = $id;
165    $this->name = $name;
166    $this->owner = $owner;
167  }
168
169  public function get_id() { return $this->id; }
170  public function get_name() { return $this->name; }
171  public function get_owner() { return $this->owner; }
172}
173
174/**
175 * Represents a menu block ready for display in the Menu object.
176 */
177class DisplayBlock
178{
179  protected $_registeredBlock;
180  protected $_position;
181
182  protected $_title;
183
184  public $data;
185  public $template;
186  public $raw_content;
187
188  public function DisplayBlock($registeredBlock)
189  {
190    $this->_registeredBlock = &$registeredBlock;
191  }
192
193  public function &get_block() { return $this->_registeredBlock; }
194
195  public function get_position() { return $this->_position; }
196  public function set_position($position)
197  {
198    $this->_position = $position;
199  }
200
201  public function get_title()
202  {
203    if (isset($this->_title))
204      return $this->_title;
205    else
206      return $this->_registeredBlock->get_name();
207  }
208
209  public function set_title($title)
210  {
211    $this->_title = $title;
212  }
213}
214
215?>
Note: See TracBrowser for help on using the repository browser.