source: branches/2.0/admin/menubar.php @ 4005

Last change on this file since 4005 was 3712, checked in by patdenice, 15 years ago

merge r3711 from trunk to branch 2.0
Disable database optimization for menubar order until it has been fixed.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24if (!defined('PHPWG_ROOT_PATH'))
25{
26  die ("Hacking attempt!");
27}
28
29
30function abs_fn_cmp($a, $b)
31{
32  return abs($a)-abs($b);
33}
34
35function make_consecutive( &$orders, $step=50 )
36{
37  uasort( $orders, 'abs_fn_cmp' );
38  $crt = 1;
39  foreach( $orders as $id=>$pos)
40  {
41    $orders[$id] = $step * ($pos<0 ? -$crt : $crt);
42    $crt++;
43  }
44}
45
46
47global $template;
48
49include_once(PHPWG_ROOT_PATH.'include/block.class.php');
50
51$menu = new BlockManager('menubar');
52$menu->load_registered_blocks();
53$reg_blocks = $menu->get_registered_blocks();
54
55$mb_conf = @$conf[ 'blk_'.$menu->get_id() ];
56if ( is_string($mb_conf) )
57  $mb_conf = unserialize( $mb_conf );
58if ( !is_array($mb_conf) )
59  $mb_conf=array();
60
61foreach ($mb_conf as $id => $pos)
62{
63  if (!isset($reg_blocks[$id]))
64    unset($mb_conf[$id]);
65}
66
67if ( isset($_POST['reset']) and !is_adviser())
68{
69  $mb_conf = array();
70  $query = '
71UPDATE '.CONFIG_TABLE.'
72  SET value=""
73  WHERE param="blk_'.addslashes($menu->get_id()).'"
74  LIMIT 1';
75  pwg_query($query);
76}
77
78
79$idx=1;
80foreach ($reg_blocks as $id => $block)
81{
82  if ( !isset($mb_conf[$id]) )
83    $mb_conf[$id] = $idx*50;
84  $idx++;
85}
86
87
88if ( isset($_POST['submit']) and !is_adviser() )
89{
90  foreach ( $mb_conf as $id => $pos )
91  {
92    $hide = isset($_POST['hide_'.$id]);
93    $mb_conf[$id] = ($hide ? -1 : +1)*abs($pos);
94
95    $pos = (int)@$_POST['pos_'.$id];
96    if ($pos>0)
97      $mb_conf[$id] = $mb_conf[$id] > 0 ? $pos : -$pos;
98  }
99  make_consecutive( $mb_conf );
100
101  // BEGIN OPTIM - DONT ASK ABOUT THIS ALGO - but optimizes the size of the array we save in DB
102  /* !!! OPTIM DISABLED UNTIL IT HAS BEEN FIXED !!!
103  $reg_keys = array_keys($reg_blocks);
104  $cnf_keys = array_keys($mb_conf);
105  $best_slice = array( 'len'=>0 );
106  for ($i=0; $i<count($reg_keys); $i++)
107  {
108    for ($j=0; $j<count($cnf_keys); $j++)
109    {
110      for ($k=0; max($i,$j)+$k<count($cnf_keys); $k++)
111      {
112        if ($cnf_keys[$j+$k] == $reg_keys[$i+$k] )
113        {
114          if ( 1+$k>$best_slice['len'])
115          {
116            $best_slice['len'] = 1+$k;
117            $best_slice['start_cnf'] = $j;
118          }
119        }
120        else
121          break;
122      }
123    }
124  }
125  */
126  $mb_conf_db = $mb_conf;
127  /*
128  if ($best_slice['len'])
129  {
130    for ($j=0; $j<$best_slice['start_cnf']; $j++ )
131    {
132      $sign = $mb_conf_db[ $cnf_keys[$j] ] > 0 ? 1 : -1;
133      $mb_conf_db[ $cnf_keys[$j] ] = $sign * ( ($best_slice['start_cnf'])*50 - ($best_slice['start_cnf']-$j) );
134    }
135    for ($j=$best_slice['start_cnf']; $j<$best_slice['start_cnf']+$best_slice['len']; $j++ )
136    {
137      if ($mb_conf_db[ $cnf_keys[$j] ] > 0)
138        unset( $mb_conf_db[ $cnf_keys[$j] ] );
139    }
140  }
141  //var_export( $best_slice ); var_export($mb_conf);  var_export($mb_conf_db);
142  // END OPTIM
143  */
144  $query = '
145UPDATE '.CONFIG_TABLE.'
146  SET value="'.addslashes(serialize($mb_conf_db)).'"
147  WHERE param="blk_'.addslashes($menu->get_id()).'"
148  LIMIT 1';
149  pwg_query($query);
150}
151
152make_consecutive( $mb_conf );
153
154foreach ($mb_conf as $id => $pos )
155{
156  $template->append( 'blocks',
157      array(
158        'pos' => $pos/5,
159        'reg' => $reg_blocks[$id]
160      )
161     );
162}
163$template->set_filename( 'menubar_admin_content', 'menubar.tpl' );
164$template->assign_var_from_handle( 'ADMIN_CONTENT', 'menubar_admin_content');
165?>
Note: See TracBrowser for help on using the repository browser.