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 | |
---|
24 | if (!defined('PHPWG_ROOT_PATH')) |
---|
25 | { |
---|
26 | die ("Hacking attempt!"); |
---|
27 | } |
---|
28 | |
---|
29 | |
---|
30 | function abs_fn_cmp($a, $b) |
---|
31 | { |
---|
32 | return abs($a)-abs($b); |
---|
33 | } |
---|
34 | |
---|
35 | function 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 | |
---|
47 | global $template; |
---|
48 | |
---|
49 | include_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() ]; |
---|
56 | if ( is_string($mb_conf) ) |
---|
57 | $mb_conf = unserialize( $mb_conf ); |
---|
58 | if ( !is_array($mb_conf) ) |
---|
59 | $mb_conf=array(); |
---|
60 | |
---|
61 | foreach ($mb_conf as $id => $pos) |
---|
62 | { |
---|
63 | if (!isset($reg_blocks[$id])) |
---|
64 | unset($mb_conf[$id]); |
---|
65 | } |
---|
66 | |
---|
67 | if ( isset($_POST['reset']) and !is_adviser()) |
---|
68 | { |
---|
69 | $mb_conf = array(); |
---|
70 | $query = ' |
---|
71 | UPDATE '.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; |
---|
80 | foreach ($reg_blocks as $id => $block) |
---|
81 | { |
---|
82 | if ( !isset($mb_conf[$id]) ) |
---|
83 | $mb_conf[$id] = $idx*50; |
---|
84 | $idx++; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | if ( 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 | $reg_keys = array_keys($reg_blocks); |
---|
103 | $cnf_keys = array_keys($mb_conf); |
---|
104 | $best_slice = array( 'len'=>0 ); |
---|
105 | for ($i=0; $i<count($reg_keys); $i++) |
---|
106 | { |
---|
107 | for ($j=0; $j<count($cnf_keys); $j++) |
---|
108 | { |
---|
109 | for ($k=0; max($i,$j)+$k<count($cnf_keys); $k++) |
---|
110 | { |
---|
111 | if ($cnf_keys[$j+$k] == $reg_keys[$i+$k] ) |
---|
112 | { |
---|
113 | if ( 1+$k>$best_slice['len']) |
---|
114 | { |
---|
115 | $best_slice['len'] = 1+$k; |
---|
116 | $best_slice['start_cnf'] = $j; |
---|
117 | } |
---|
118 | } |
---|
119 | else |
---|
120 | break; |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | $mb_conf_db = $mb_conf; |
---|
125 | if ($best_slice['len']) |
---|
126 | { |
---|
127 | for ($j=0; $j<$best_slice['start_cnf']; $j++ ) |
---|
128 | { |
---|
129 | $sign = $mb_conf_db[ $cnf_keys[$j] ] > 0 ? 1 : -1; |
---|
130 | $mb_conf_db[ $cnf_keys[$j] ] = $sign * ( ($best_slice['start_cnf'])*50 - ($best_slice['start_cnf']-$j) ); |
---|
131 | } |
---|
132 | for ($j=$best_slice['start_cnf']; $j<$best_slice['start_cnf']+$best_slice['len']; $j++ ) |
---|
133 | { |
---|
134 | if ($mb_conf_db[ $cnf_keys[$j] ] > 0) |
---|
135 | unset( $mb_conf_db[ $cnf_keys[$j] ] ); |
---|
136 | } |
---|
137 | } |
---|
138 | //var_export( $best_slice ); var_export($mb_conf); var_export($mb_conf_db); |
---|
139 | // END OPTIM |
---|
140 | |
---|
141 | $query = ' |
---|
142 | UPDATE '.CONFIG_TABLE.' |
---|
143 | SET value="'.addslashes(serialize($mb_conf_db)).'" |
---|
144 | WHERE param="blk_'.addslashes($menu->get_id()).'" |
---|
145 | LIMIT 1'; |
---|
146 | pwg_query($query); |
---|
147 | } |
---|
148 | |
---|
149 | make_consecutive( $mb_conf ); |
---|
150 | |
---|
151 | foreach ($mb_conf as $id => $pos ) |
---|
152 | { |
---|
153 | $template->append( 'blocks', |
---|
154 | array( |
---|
155 | 'pos' => $pos/5, |
---|
156 | 'reg' => $reg_blocks[$id] |
---|
157 | ) |
---|
158 | ); |
---|
159 | } |
---|
160 | $template->set_filename( 'menubar_admin_content', 'menubar.tpl' ); |
---|
161 | $template->assign_var_from_handle( 'ADMIN_CONTENT', 'menubar_admin_content'); |
---|
162 | ?> |
---|