aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorrvelices <rv-github@modusoptimus.com>2008-08-28 00:32:39 +0000
committerrvelices <rv-github@modusoptimus.com>2008-08-28 00:32:39 +0000
commitabb2f22b2e96a89a44ad9d36db3bf612c6f88b9f (patch)
treeca36d7b1bd7035f9952c4f564406eaa87c954cdb /include
parent7f956e71b91f32ff6ca87ea6a0f56cc0f02c7005 (diff)
- based on test_menu by grum (thanks to you) - integration of dynamic menu bar to pwg
- the menubar is composed now of dynamic blocks that can be ordered/hidden - plugins can add their own blocks git-svn-id: http://piwigo.org/svn/trunk@2488 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to 'include')
-rw-r--r--include/block.class.php214
-rw-r--r--include/common.inc.php1
-rw-r--r--include/functions_html.inc.php14
-rw-r--r--include/menubar.inc.php467
4 files changed, 461 insertions, 235 deletions
diff --git a/include/block.class.php b/include/block.class.php
new file mode 100644
index 000000000..c40a695aa
--- /dev/null
+++ b/include/block.class.php
@@ -0,0 +1,214 @@
+<?php
+// +-----------------------------------------------------------------------+
+// | Piwigo - a PHP based picture gallery |
+// +-----------------------------------------------------------------------+
+// | Copyright(C) 2008 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;
+ }
+}
+
+?> \ No newline at end of file
diff --git a/include/common.inc.php b/include/common.inc.php
index d12b8bb70..a762eeade 100644
--- a/include/common.inc.php
+++ b/include/common.inc.php
@@ -283,5 +283,6 @@ add_event_handler('render_category_description', 'render_category_description');
add_event_handler('render_comment_content', 'htmlspecialchars');
add_event_handler('render_comment_content', 'parse_comment_content');
add_event_handler('render_comment_author', 'strip_tags');
+add_event_handler('blockmanager_register_blocks', 'register_default_menubar_blocks', EVENT_HANDLER_PRIORITY_NEUTRAL-1);
trigger_action('init');
?>
diff --git a/include/functions_html.inc.php b/include/functions_html.inc.php
index 2578fc8c3..ff03675b1 100644
--- a/include/functions_html.inc.php
+++ b/include/functions_html.inc.php
@@ -772,4 +772,18 @@ function order_by_name($element_ids,$name)
return $ordered_element_ids;
}
+/*event handler for menu*/
+function register_default_menubar_blocks( $menu_ref_arr )
+{
+ $menu = & $menu_ref_arr[0];
+ if ($menu->get_id() != 'menubar')
+ return;
+ $menu->register_block( new RegisteredBlock( 'mbLinks', 'Links', 'piwigo'));
+ $menu->register_block( new RegisteredBlock( 'mbCategories', 'Categories', 'piwigo'));
+ $menu->register_block( new RegisteredBlock( 'mbTags', 'Related tags', 'piwigo'));
+ $menu->register_block( new RegisteredBlock( 'mbSpecials', 'special_categories', 'piwigo'));
+ $menu->register_block( new RegisteredBlock( 'mbMenu', 'title_menu', 'piwigo'));
+ $menu->register_block( new RegisteredBlock( 'mbIdentification', 'identification', 'piwigo') );
+}
+
?>
diff --git a/include/menubar.inc.php b/include/menubar.inc.php
index 0ff64827e..b04a960a8 100644
--- a/include/menubar.inc.php
+++ b/include/menubar.inc.php
@@ -25,281 +25,278 @@
* This file is included by the main page to show the menu bar
*
*/
-$template->set_filenames(
- array(
- 'menubar' => 'menubar.tpl',
- )
- );
-trigger_action('loc_begin_menubar');
+include_once(PHPWG_ROOT_PATH.'include/block.class.php');
-$template->assign(
- array(
- 'NB_PICTURE' => $user['nb_total_images'],
- 'MENU_CATEGORIES_CONTENT' => get_categories_menu(),
- 'U_CATEGORIES' => make_index_url(array('section' => 'categories')),
- 'U_LOST_PASSWORD' => get_root_url().'password.php',
- 'U_UPLOAD' => get_upload_menu_link()
- )
- );
+initialize_menu();
-//-------------------------------------------------------------- external links
-foreach ($conf['links'] as $url => $url_data)
+function initialize_menu()
{
- if (!is_array($url_data))
- {
- $url_data = array('label' => $url_data);
- }
+ global $page, $conf, $user, $template, $filter;
- if
- (
- (!isset($url_data['eval_visible']))
- or
- (eval($url_data['eval_visible']))
- )
- {
- $tpl_var = array(
- 'URL' => $url,
- 'LABEL' => $url_data['label']
- );
+ $menu = new BlockManager("menubar");
+ $menu->load_registered_blocks();
+ $menu->prepare_display();
- if (!isset($url_data['new_window']) or $url_data['new_window'])
+//--------------------------------------------------------------- external links
+ if ( ($block=$menu->get_block('mbLinks')) and !empty($conf['links']) )
+ {
+ $data = array();
+ foreach ($conf['links'] as $url => $url_data)
{
- $tpl_var['new_window'] =
- array(
- 'NAME' => (isset($url_data['nw_name']) ? $url_data['nw_name'] : ''),
- 'FEATURES' => (isset($url_data['nw_features']) ? $url_data['nw_features'] : '')
- );
+ if (!is_array($url_data))
+ {
+ $url_data = array('label' => $url_data);
+ }
+
+ if
+ (
+ (!isset($url_data['eval_visible']))
+ or
+ (eval($url_data['eval_visible']))
+ )
+ {
+ $tpl_var = array(
+ 'URL' => $url,
+ 'LABEL' => $url_data['label']
+ );
+
+ if (!isset($url_data['new_window']) or $url_data['new_window'])
+ {
+ $tpl_var['new_window'] =
+ array(
+ 'NAME' => (isset($url_data['nw_name']) ? $url_data['nw_name'] : ''),
+ 'FEATURES' => (isset($url_data['nw_features']) ? $url_data['nw_features'] : '')
+ );
+ }
+ $data[] = $tpl_var;
+ }
}
- $template->append('links', $tpl_var);
+ $block->template = 'menubar_links.tpl';
+ $block->data = $data;
}
-}
+//-------------------------------------------------------------- categories
+ $block = $menu->get_block('mbCategories');
//------------------------------------------------------------------------ filter
-if (!empty($conf['filter_pages']) and get_filter_page_value('used'))
-{
- if ($filter['enabled'])
+ if (!empty($conf['filter_pages']) and get_filter_page_value('used'))
{
- $template->assign(
- 'U_STOP_FILTER',
- add_url_params(make_index_url(array()), array('filter' => 'stop'))
- );
+ if ($filter['enabled'])
+ {
+ $template->assign(
+ 'U_STOP_FILTER',
+ add_url_params(make_index_url(array()), array('filter' => 'stop'))
+ );
+ }
+ else
+ {
+ $template->assign(
+ 'U_START_FILTER',
+ add_url_params(make_index_url(array()), array('filter' => 'start-recent-'.$user['recent_period']))
+ );
+ }
}
- else
+
+ if ( $block!=null )
{
- $template->assign(
- 'U_START_FILTER',
- add_url_params(make_index_url(array()), array('filter' => 'start-recent-'.$user['recent_period']))
- );
+ $block->data = array(
+ 'NB_PICTURE' => $user['nb_total_images'],
+ 'MENU_CATEGORIES_CONTENT' => get_categories_menu(),
+ 'U_CATEGORIES' => make_index_url(array('section' => 'categories')),
+ 'U_UPLOAD' => get_upload_menu_link()
+ );
+ $block->template = 'menubar_categories.tpl';
}
-}
//------------------------------------------------------------------------ tags
-if ('tags' == @$page['section'])
-{
- // display tags associated to currently tagged items, less current tags
- $tags = array();
- if ( !empty($page['items']) )
+ $block = $menu->get_block('mbTags');
+ if ( $block!=null and 'tags'==@$page['section'] and !empty($page['items']) )
{
$tags = get_common_tags($page['items'],
- $conf['menubar_tag_cloud_items_number'], $page['tag_ids']);
- }
+ $conf['menubar_tag_cloud_items_number'], $page['tag_ids']);
+ $tags = add_level_to_tags($tags);
- $tags = add_level_to_tags($tags);
+ foreach ($tags as $tag)
+ {
+ $block->data[] =
+ array_merge( $tag,
+ array(
+ 'URL' => make_index_url(
+ array(
+ 'tags' => array($tag)
+ )
+ ),
+ 'U_ADD' => make_index_url(
+ array(
+ 'tags' => array_merge(
+ $page['tags'],
+ array($tag)
+ )
+ )
+ ),
+ )
+ );
+ }
+ $block->template = 'menubar_tags.tpl';
+ }
- foreach ($tags as $tag)
+//----------------------------------------------------------- special categories
+ if ( ($block = $menu->get_block('mbSpecials')) != null )
{
- $template->append(
- 'related_tags',
- array_merge( $tag,
+ if ( !is_a_guest() )
+ {// favorites
+ $block->data['favorites'] =
array(
- 'URL' => make_index_url(
- array(
- 'tags' => array($tag)
- )
- ),
+ 'URL' => make_index_url(array('section' => 'favorites')),
+ 'TITLE' => l10n('favorite_cat_hint'),
+ 'NAME' => l10n('favorite_cat')
+ );
+ }
- 'U_ADD' => make_index_url(
- array(
- 'tags' => array_merge(
- $page['tags'],
- array($tag)
- )
- )
- ),
- )
- )
+ $block->data['most_visited'] =
+ array(
+ 'URL' => make_index_url(array('section' => 'most_visited')),
+ 'TITLE' => l10n('most_visited_cat_hint'),
+ 'NAME' => l10n('most_visited_cat')
);
- }
-}
-//---------------------------------------------------------- special categories
-// favorites categories
-if ( !is_a_guest() )
-{
- $template->append(
- 'special_categories',
- array(
- 'URL' => make_index_url(array('section' => 'favorites')),
- 'TITLE' => l10n('favorite_cat_hint'),
- 'NAME' => l10n('favorite_cat')
- ));
-}
-// most visited
-$template->append(
- 'special_categories',
- array(
- 'URL' => make_index_url(array('section' => 'most_visited')),
- 'TITLE' => l10n('most_visited_cat_hint'),
- 'NAME' => l10n('most_visited_cat')
- ));
-// best rated
-if ($conf['rate'])
-{
- $template->append(
- 'special_categories',
- array(
- 'URL' => make_index_url(array('section' => 'best_rated')),
- 'TITLE' => l10n('best_rated_cat_hint'),
- 'NAME' => l10n('best_rated_cat')
- )
- );
-}
-// random
-$template->append(
- 'special_categories',
- array(
- 'URL' => get_root_url().'random.php',
- 'TITLE' => l10n('random_cat_hint'),
- 'NAME' => l10n('random_cat'),
- 'REL'=> 'rel="nofollow"'
- ));
-// recent pics
-$template->append(
- 'special_categories',
- array(
- 'URL' => make_index_url(array('section' => 'recent_pics')),
- 'TITLE' => l10n('recent_pics_cat_hint'),
- 'NAME' => l10n('recent_pics_cat'),
- ));
-// recent cats
-$template->append(
- 'special_categories',
- array(
- 'URL' => make_index_url(array('section' => 'recent_cats')),
- 'TITLE' => l10n('recent_cats_cat_hint'),
- 'NAME' => l10n('recent_cats_cat'),
- ));
-
-// calendar
-$template->append(
- 'special_categories',
- array(
- 'URL' =>
- make_index_url(
+ if ($conf['rate'])
+ {
+ $block->data['best_rated'] =
array(
- 'chronology_field' => ($conf['calendar_datefield']=='date_available'
- ? 'posted' : 'created'),
- 'chronology_style'=> 'monthly',
- 'chronology_view' => 'calendar'
- )
- ),
- 'TITLE' => l10n('calendar_hint'),
- 'NAME' => l10n('calendar'),
- 'REL'=> 'rel="nofollow"'
- )
- );
-//--------------------------------------------------------------------- summary
+ 'URL' => make_index_url(array('section' => 'best_rated')),
+ 'TITLE' => l10n('best_rated_cat_hint'),
+ 'NAME' => l10n('best_rated_cat')
+ );
+ }
-if (is_a_guest())
-{
- $template->assign(
+ $block->data['random'] =
array(
- 'U_IDENTIFY' => get_root_url().'identification.php',
- 'AUTHORIZE_REMEMBERING' => $conf['authorize_remembering']
- )
- );
+ 'URL' => get_root_url().'random.php',
+ 'TITLE' => l10n('random_cat_hint'),
+ 'NAME' => l10n('random_cat'),
+ 'REL'=> 'rel="nofollow"'
+ );
- if ($conf['allow_user_registration'])
- {
- $template->assign( 'U_REGISTER', get_root_url().'register.php');
- }
-}
-else
-{
- $template->assign('USERNAME', $user['username']);
+ $block->data['recent_pics'] =
+ array(
+ 'URL' => make_index_url(array('section' => 'recent_pics')),
+ 'TITLE' => l10n('recent_pics_cat_hint'),
+ 'NAME' => l10n('recent_pics_cat'),
+ );
- if (is_autorize_status(ACCESS_CLASSIC))
- {
- $template->assign('U_PROFILE', get_root_url().'profile.php');
- }
+ $block->data['recent_cats'] =
+ array(
+ 'URL' => make_index_url(array('section' => 'recent_cats')),
+ 'TITLE' => l10n('recent_cats_cat_hint'),
+ 'NAME' => l10n('recent_cats_cat'),
+ );
- // the logout link has no meaning with Apache authentication : it is not
- // possible to logout with this kind of authentication.
- if (!$conf['apache_authentication'])
- {
- $template->assign('U_LOGOUT', get_root_url().'?act=logout');
+
+ $block->data['calendar'] =
+ array(
+ 'URL' =>
+ make_index_url(
+ array(
+ 'chronology_field' => ($conf['calendar_datefield']=='date_available'
+ ? 'posted' : 'created'),
+ 'chronology_style'=> 'monthly',
+ 'chronology_view' => 'calendar'
+ )
+ ),
+ 'TITLE' => l10n('calendar_hint'),
+ 'NAME' => l10n('calendar'),
+ 'REL'=> 'rel="nofollow"'
+ );
+ $block->template = 'menubar_specials.tpl';
}
- if (is_admin())
+
+//---------------------------------------------------------------------- summary
+ if ( ($block=$menu->get_block('mbMenu')) != null )
{
- $template->assign('U_ADMIN', get_root_url().'admin.php');
- }
-}
+ // tags link
+ $block->data['tags'] =
+ array(
+ 'TITLE' => l10n('See available tags'),
+ 'NAME' => l10n('Tags'),
+ 'URL'=> get_root_url().'tags.php',
+ );
-// tags link
-$template->append(
- 'summaries',
- array(
- 'TITLE' => l10n('See available tags'),
- 'NAME' => l10n('Tags'),
- 'U_SUMMARY'=> get_root_url().'tags.php',
- )
- );
+ // search link
+ $block->data['search'] =
+ array(
+ 'TITLE'=>l10n('hint_search'),
+ 'NAME'=>l10n('Search'),
+ 'URL'=> get_root_url().'search.php',
+ 'REL'=> 'rel="search"'
+ );
-// search link
-$template->append(
- 'summaries',
- array(
- 'TITLE'=>l10n('hint_search'),
- 'NAME'=>l10n('Search'),
- 'U_SUMMARY'=> get_root_url().'search.php',
- 'REL'=> 'rel="search"'
- )
- );
+ // comments link
+ $block->data['comments'] =
+ array(
+ 'TITLE'=>l10n('hint_comments'),
+ 'NAME'=>l10n('comments'),
+ 'URL'=> get_root_url().'comments.php',
+ );
-// comments link
-$template->append(
- 'summaries',
- array(
- 'TITLE'=>l10n('hint_comments'),
- 'NAME'=>l10n('comments'),
- 'U_SUMMARY'=> get_root_url().'comments.php',
- )
- );
+ // about link
+ $block->data['about'] =
+ array(
+ 'TITLE' => l10n('about_page_title'),
+ 'NAME' => l10n('About'),
+ 'URL' => get_root_url().'about.php',
+ );
-// about link
-$template->append(
- 'summaries',
- array(
- 'TITLE' => l10n('about_page_title'),
- 'NAME' => l10n('About'),
- 'U_SUMMARY' => get_root_url().'about.php',
- )
- );
+ // notification
+ $block->data['rss'] =
+ array(
+ 'TITLE'=>l10n('RSS feed'),
+ 'NAME'=>l10n('Notification'),
+ 'URL'=> get_root_url().'notification.php',
+ 'REL'=> 'rel="nofollow"'
+ );
+ $block->template = 'menubar_menu.tpl';
+ }
-// notification
-$template->append(
- 'summaries',
- array(
- 'TITLE'=>l10n('RSS feed'),
- 'NAME'=>l10n('Notification'),
- 'U_SUMMARY'=> get_root_url().'notification.php',
- 'REL'=> 'rel="nofollow"'
- )
- );
-trigger_action('loc_end_menubar');
-$template->assign_var_from_handle('MENUBAR', 'menubar');
+//--------------------------------------------------------------- identification
+ if (is_a_guest())
+ {
+ $template->assign(
+ array(
+ 'U_LOGIN' => get_root_url().'identification.php',
+ 'AUTHORIZE_REMEMBERING' => $conf['authorize_remembering']
+ )
+ );
+ if ($conf['allow_user_registration'])
+ {
+ $template->assign( 'U_REGISTER', get_root_url().'register.php');
+ }
+ }
+ else
+ {
+ $template->assign('USERNAME', $user['username']);
+ if (is_autorize_status(ACCESS_CLASSIC))
+ {
+ $template->assign('U_PROFILE', get_root_url().'profile.php');
+ }
+ // the logout link has no meaning with Apache authentication : it is not
+ // possible to logout with this kind of authentication.
+ if (!$conf['apache_authentication'])
+ {
+ $template->assign('U_LOGOUT', get_root_url().'?act=logout');
+ }
+ if (is_admin())
+ {
+ $template->assign('U_ADMIN', get_root_url().'admin.php');
+ }
+ }
+ if ( ($block=$menu->get_block('mbIdentification')) != null )
+ {
+ $block->template = 'menubar_identification.tpl';
+ }
+ $menu->apply('MENUBAR', 'menubar.tpl' );
+}
?>