1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
5 | // | Copyright (C) 2003-2004 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | branch : BSF (Best So Far) |
---|
8 | // | file : $RCSfile$ |
---|
9 | // | last update : $Date: 2004-12-12 21:06:39 +0000 (Sun, 12 Dec 2004) $ |
---|
10 | // | last modifier : $Author: plg $ |
---|
11 | // | revision : $Revision: 642 $ |
---|
12 | // +-----------------------------------------------------------------------+ |
---|
13 | // | This program is free software; you can redistribute it and/or modify | |
---|
14 | // | it under the terms of the GNU General Public License as published by | |
---|
15 | // | the Free Software Foundation | |
---|
16 | // | | |
---|
17 | // | This program is distributed in the hope that it will be useful, but | |
---|
18 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
19 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
20 | // | General Public License for more details. | |
---|
21 | // | | |
---|
22 | // | You should have received a copy of the GNU General Public License | |
---|
23 | // | along with this program; if not, write to the Free Software | |
---|
24 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
25 | // | USA. | |
---|
26 | // +-----------------------------------------------------------------------+ |
---|
27 | |
---|
28 | if (!defined('PHPWG_ROOT_PATH')) |
---|
29 | { |
---|
30 | die('Hacking attempt!'); |
---|
31 | } |
---|
32 | include_once(PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php'); |
---|
33 | // +-----------------------------------------------------------------------+ |
---|
34 | // | initialization | |
---|
35 | // +-----------------------------------------------------------------------+ |
---|
36 | $errors = array(); |
---|
37 | $infos = array(); |
---|
38 | $categories = array(); |
---|
39 | $navigation = $lang['home']; |
---|
40 | // +-----------------------------------------------------------------------+ |
---|
41 | // | virtual categories management | |
---|
42 | // +-----------------------------------------------------------------------+ |
---|
43 | // request to delete a virtual category |
---|
44 | if (isset($_GET['delete']) and is_numeric($_GET['delete'])) |
---|
45 | { |
---|
46 | $to_delete_categories = array(); |
---|
47 | array_push($to_delete_categories,$_GET['delete']); |
---|
48 | delete_categories($to_delete_categories); |
---|
49 | array_push($infos, $lang['cat_virtual_deleted']); |
---|
50 | } |
---|
51 | // request to add a virtual category |
---|
52 | else if (isset($_POST['submit'])) |
---|
53 | { |
---|
54 | // is the given category name only containing blank spaces ? |
---|
55 | if (preg_match('/^\s*$/', $_POST['virtual_name'])) |
---|
56 | { |
---|
57 | array_push($errors, $lang['cat_error_name']); |
---|
58 | } |
---|
59 | |
---|
60 | if (!count($errors)) |
---|
61 | { |
---|
62 | $parent_id = !empty($_GET['parent_id'])?$_GET['parent_id']:'NULL'; |
---|
63 | |
---|
64 | if ($parent_id != 'NULL') |
---|
65 | { |
---|
66 | $query = ' |
---|
67 | SELECT id,uppercats,global_rank,visible,status |
---|
68 | FROM '.CATEGORIES_TABLE.' |
---|
69 | WHERE id = '.$parent_id.' |
---|
70 | ;'; |
---|
71 | $row = mysql_fetch_array(pwg_query($query)); |
---|
72 | $parent = array('id' => $row['id'], |
---|
73 | 'uppercats' => $row['uppercats'], |
---|
74 | 'visible' => $row['visible'], |
---|
75 | 'status' => $row['status'], |
---|
76 | 'global_rank' => $row['global_rank']); |
---|
77 | } |
---|
78 | |
---|
79 | $insert = array(); |
---|
80 | $insert{'name'} = $_POST['virtual_name']; |
---|
81 | $insert{'rank'} = $_POST['rank']; |
---|
82 | $insert{'commentable'} = $conf['newcat_default_commentable']; |
---|
83 | $insert{'uploadable'} = $conf['newcat_default_uploadable']; |
---|
84 | |
---|
85 | if (isset($parent)) |
---|
86 | { |
---|
87 | $insert{'id_uppercat'} = $parent{'id'}; |
---|
88 | // at creation, must a category be visible or not ? Warning : if |
---|
89 | // the parent category is invisible, the category is automatically |
---|
90 | // create invisible. (invisible = locked) |
---|
91 | if ('false' == $parent['visible']) |
---|
92 | { |
---|
93 | $insert{'visible'} = 'false'; |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | $insert{'visible'} = $conf['newcat_default_visible']; |
---|
98 | } |
---|
99 | // at creation, must a category be public or private ? Warning : |
---|
100 | // if the parent category is private, the category is |
---|
101 | // automatically create private. |
---|
102 | if ('private' == $parent['status']) |
---|
103 | { |
---|
104 | $insert{'status'} = 'private'; |
---|
105 | } |
---|
106 | else |
---|
107 | { |
---|
108 | $insert{'status'} = $conf['newcat_default_status']; |
---|
109 | } |
---|
110 | } |
---|
111 | else |
---|
112 | { |
---|
113 | $insert{'visible'} = $conf['newcat_default_visible']; |
---|
114 | $insert{'status'} = $conf['newcat_default_status']; |
---|
115 | } |
---|
116 | |
---|
117 | $inserts = array($insert); |
---|
118 | |
---|
119 | // we have then to add the virtual category |
---|
120 | $dbfields = array('site_id','name','id_uppercat','rank','commentable', |
---|
121 | 'uploadable','visible','status'); |
---|
122 | mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts); |
---|
123 | |
---|
124 | // And last we update the uppercats |
---|
125 | $query = ' |
---|
126 | SELECT MAX(id) |
---|
127 | FROM '.CATEGORIES_TABLE.' |
---|
128 | ;'; |
---|
129 | $my_id = array_pop(mysql_fetch_array(pwg_query($query))); |
---|
130 | |
---|
131 | $query = ' |
---|
132 | UPDATE '.CATEGORIES_TABLE; |
---|
133 | if (isset($parent)) |
---|
134 | { |
---|
135 | $query.= " |
---|
136 | SET uppercats = CONCAT('".$parent['uppercats']."',',',id) |
---|
137 | , global_rank = CONCAT('".$parent['global_rank']."','.',rank)"; |
---|
138 | } |
---|
139 | else |
---|
140 | { |
---|
141 | $query.= ' |
---|
142 | SET uppercats = id |
---|
143 | , global_rank = id'; |
---|
144 | } |
---|
145 | $query.= ' |
---|
146 | WHERE id = '.$my_id.' |
---|
147 | ;'; |
---|
148 | pwg_query($query); |
---|
149 | array_push($infos, $lang['cat_virtual_added']); |
---|
150 | } |
---|
151 | } |
---|
152 | // +-----------------------------------------------------------------------+ |
---|
153 | // | Cache management | |
---|
154 | // +-----------------------------------------------------------------------+ |
---|
155 | $query = ' |
---|
156 | SELECT * |
---|
157 | FROM '.CATEGORIES_TABLE; |
---|
158 | if (!isset($_GET['parent_id'])) |
---|
159 | { |
---|
160 | $query.= ' |
---|
161 | WHERE id_uppercat IS NULL'; |
---|
162 | } |
---|
163 | else |
---|
164 | { |
---|
165 | $query.= ' |
---|
166 | WHERE id_uppercat = '.$_GET['parent_id']; |
---|
167 | } |
---|
168 | $query.= ' |
---|
169 | ORDER BY rank ASC |
---|
170 | ;'; |
---|
171 | $result = pwg_query($query); |
---|
172 | while ($row = mysql_fetch_assoc($result)) |
---|
173 | { |
---|
174 | $categories[$row['rank']] = $row; |
---|
175 | $categories[$row['rank']]['nb_subcats'] = 0; |
---|
176 | } |
---|
177 | // +-----------------------------------------------------------------------+ |
---|
178 | // | Navigation path | |
---|
179 | // +-----------------------------------------------------------------------+ |
---|
180 | if (isset($_GET['parent_id'])) |
---|
181 | { |
---|
182 | $base_url = PHPWG_ROOT_PATH.'admin.php?page=cat_list'; |
---|
183 | |
---|
184 | $navigation = '<a class="" href="'.add_session_id($base_url).'">'; |
---|
185 | $navigation.= $lang['home']; |
---|
186 | $navigation.= '</a>'; |
---|
187 | $navigation.= $conf['level_separator']; |
---|
188 | |
---|
189 | $current_category = get_cat_info($_GET['parent_id']); |
---|
190 | $navigation.= get_cat_display_name($current_category['name'], |
---|
191 | $base_url.'&parent_id=', |
---|
192 | false); |
---|
193 | } |
---|
194 | // +-----------------------------------------------------------------------+ |
---|
195 | // | rank updates | |
---|
196 | // +-----------------------------------------------------------------------+ |
---|
197 | $current_rank = 0; |
---|
198 | if (isset($_GET['up']) and is_numeric($_GET['up'])) |
---|
199 | { |
---|
200 | // 1. searching the id of the category just above at the same level |
---|
201 | while (list ($id,$current) = each($categories)) |
---|
202 | { |
---|
203 | if ($current['id'] == $_GET['up']) |
---|
204 | { |
---|
205 | $current_rank = $current['rank']; |
---|
206 | break; |
---|
207 | } |
---|
208 | } |
---|
209 | if ($current_rank > 1) |
---|
210 | { |
---|
211 | // 2. Exchanging ranks between the two categories |
---|
212 | $query = ' |
---|
213 | UPDATE '.CATEGORIES_TABLE.' |
---|
214 | SET rank = '.($current_rank-1).' |
---|
215 | WHERE id = '.$_GET['up'].' |
---|
216 | ;'; |
---|
217 | pwg_query($query); |
---|
218 | $query = ' |
---|
219 | UPDATE '.CATEGORIES_TABLE.' |
---|
220 | SET rank = '.$current_rank.' |
---|
221 | WHERE id = '.$categories[($current_rank-1)]['id'].' |
---|
222 | ;'; |
---|
223 | pwg_query($query); |
---|
224 | // 3. Updating the cache array |
---|
225 | $categories[$current_rank] = $categories[($current_rank-1)]; |
---|
226 | $categories[($current_rank-1)] = $current; |
---|
227 | } |
---|
228 | else |
---|
229 | { |
---|
230 | // 2. Updating the rank of our category to be after the previous max rank |
---|
231 | $query = ' |
---|
232 | UPDATE '.CATEGORIES_TABLE.' |
---|
233 | SET rank = '.(count($categories) + 1).' |
---|
234 | WHERE id = '.$_GET['up'].' |
---|
235 | ;'; |
---|
236 | pwg_query($query); |
---|
237 | $query = ' |
---|
238 | UPDATE '.CATEGORIES_TABLE.' |
---|
239 | SET rank = rank-1 |
---|
240 | WHERE id_uppercat '; |
---|
241 | if (empty($_GET['parent_id'])) |
---|
242 | { |
---|
243 | $query.= 'IS NULL'; |
---|
244 | } |
---|
245 | else |
---|
246 | { |
---|
247 | $query.= '= '.$_GET['parent_id']; |
---|
248 | } |
---|
249 | $query.= ' |
---|
250 | ;'; |
---|
251 | pwg_query($query); |
---|
252 | // 3. Updating the cache array |
---|
253 | array_push($categories, $current); |
---|
254 | array_shift($categories); |
---|
255 | } |
---|
256 | update_global_rank(@$_GET['parent_id']); |
---|
257 | } |
---|
258 | else if (isset($_GET['down']) and is_numeric($_GET['down'])) |
---|
259 | { |
---|
260 | // 1. searching the id of the category just above at the same level |
---|
261 | while (list ($id,$current) = each($categories)) |
---|
262 | { |
---|
263 | if ($current['id'] == $_GET['down']) |
---|
264 | { |
---|
265 | $current_rank = $current['rank']; |
---|
266 | break; |
---|
267 | } |
---|
268 | } |
---|
269 | if ($current_rank < count($categories)) |
---|
270 | { |
---|
271 | // 2. Exchanging ranks between the two categories |
---|
272 | $query = ' |
---|
273 | UPDATE '.CATEGORIES_TABLE.' |
---|
274 | SET rank = '.($current_rank+1).' |
---|
275 | WHERE id = '.$_GET['down'].' |
---|
276 | ;'; |
---|
277 | pwg_query($query); |
---|
278 | $query = ' |
---|
279 | UPDATE '.CATEGORIES_TABLE.' |
---|
280 | SET rank = '.$current_rank.' |
---|
281 | WHERE id = '.$categories[($current_rank+1)]['id'].' |
---|
282 | ;'; |
---|
283 | pwg_query($query); |
---|
284 | // 3. Updating the cache array |
---|
285 | $categories[$current_rank]=$categories[($current_rank+1)]; |
---|
286 | $categories[($current_rank+1)] = $current; |
---|
287 | } |
---|
288 | else |
---|
289 | { |
---|
290 | // 2. updating the rank of our category to be the first one |
---|
291 | $query = ' |
---|
292 | UPDATE '.CATEGORIES_TABLE.' |
---|
293 | SET rank = 0 |
---|
294 | WHERE id = '.$_GET['down'].' |
---|
295 | ;'; |
---|
296 | pwg_query($query); |
---|
297 | $query = ' |
---|
298 | UPDATE '.CATEGORIES_TABLE.' |
---|
299 | SET rank = rank+1 |
---|
300 | WHERE id_uppercat '; |
---|
301 | if (empty($_GET['parent_id'])) |
---|
302 | { |
---|
303 | $query.= 'IS NULL'; |
---|
304 | } |
---|
305 | else |
---|
306 | { |
---|
307 | $query.= '= '.$_GET['parent_id']; |
---|
308 | } |
---|
309 | $query.= ' |
---|
310 | ;'; |
---|
311 | pwg_query($query); |
---|
312 | // 3. Updating the cache array |
---|
313 | array_unshift($categories, $current); |
---|
314 | array_pop($categories); |
---|
315 | } |
---|
316 | update_global_rank(@$_GET['parent_id']); |
---|
317 | } |
---|
318 | reset($categories); |
---|
319 | // +-----------------------------------------------------------------------+ |
---|
320 | // | template initialization | |
---|
321 | // +-----------------------------------------------------------------------+ |
---|
322 | $template->set_filenames(array('categories'=>'admin/cat_list.tpl')); |
---|
323 | |
---|
324 | $form_action = PHPWG_ROOT_PATH.'admin.php?page=cat_list'; |
---|
325 | if (isset($_GET['parent_id'])) |
---|
326 | { |
---|
327 | $form_action.= '&parent_id='.$_GET['parent_id']; |
---|
328 | } |
---|
329 | |
---|
330 | $template->assign_vars(array( |
---|
331 | 'CATEGORIES_NAV'=>$navigation, |
---|
332 | 'NEXT_RANK'=>max(array_keys($categories))+1, |
---|
333 | 'F_ACTION'=>$form_action, |
---|
334 | |
---|
335 | 'L_ADD_VIRTUAL'=>$lang['cat_add'], |
---|
336 | 'L_SUBMIT'=>$lang['submit'], |
---|
337 | 'L_STORAGE'=>$lang['storage'], |
---|
338 | 'L_NB_IMG'=>$lang['pictures'], |
---|
339 | 'L_MOVE_UP'=>$lang['up'], |
---|
340 | 'L_MOVE_DOWN'=>$lang['down'], |
---|
341 | 'L_EDIT'=>$lang['edit'], |
---|
342 | 'L_INFO_IMG'=>$lang['cat_image_info'], |
---|
343 | 'L_DELETE'=>$lang['delete'], |
---|
344 | )); |
---|
345 | |
---|
346 | $tpl = array('cat_first','cat_last'); |
---|
347 | // +-----------------------------------------------------------------------+ |
---|
348 | // | errors & infos | |
---|
349 | // +-----------------------------------------------------------------------+ |
---|
350 | if (count($errors) != 0) |
---|
351 | { |
---|
352 | $template->assign_block_vars('errors',array()); |
---|
353 | foreach ($errors as $error) |
---|
354 | { |
---|
355 | $template->assign_block_vars('errors.error',array('ERROR'=>$error)); |
---|
356 | } |
---|
357 | } |
---|
358 | if (count($infos) != 0) |
---|
359 | { |
---|
360 | $template->assign_block_vars('infos',array()); |
---|
361 | foreach ($infos as $info) |
---|
362 | { |
---|
363 | $template->assign_block_vars('infos.info',array('INFO'=>$info)); |
---|
364 | } |
---|
365 | } |
---|
366 | // +-----------------------------------------------------------------------+ |
---|
367 | // | Categories display | |
---|
368 | // +-----------------------------------------------------------------------+ |
---|
369 | $ranks = array(); |
---|
370 | foreach ($categories as $category) |
---|
371 | { |
---|
372 | $ranks[$category['id']] = $category['rank']; |
---|
373 | } |
---|
374 | |
---|
375 | $query = ' |
---|
376 | SELECT id_uppercat, COUNT(*) AS nb_subcats |
---|
377 | FROM '. CATEGORIES_TABLE.' |
---|
378 | WHERE id_uppercat IN ('.implode(',', array_keys($ranks)).') |
---|
379 | GROUP BY id_uppercat |
---|
380 | ;'; |
---|
381 | $result = pwg_query($query); |
---|
382 | while ($row = mysql_fetch_array($result)) |
---|
383 | { |
---|
384 | $categories[$ranks[$row['id_uppercat']]]['nb_subcats'] = $row['nb_subcats']; |
---|
385 | } |
---|
386 | |
---|
387 | foreach ($categories as $category) |
---|
388 | { |
---|
389 | $images_folder = PHPWG_ROOT_PATH.'template/'; |
---|
390 | $images_folder.= $user['template'].'/admin/images'; |
---|
391 | |
---|
392 | if ($category['visible'] == 'false') |
---|
393 | { |
---|
394 | $image_src = $images_folder.'/icon_folder_lock.gif'; |
---|
395 | $image_alt = $lang['cat_private']; |
---|
396 | $image_title = $lang['cat_private']; |
---|
397 | } |
---|
398 | else if (empty($category['dir'])) |
---|
399 | { |
---|
400 | $image_src = $images_folder.'/icon_folder_link.gif'; |
---|
401 | $image_alt = $lang['cat_virtual']; |
---|
402 | $image_title = $lang['cat_virtual']; |
---|
403 | } |
---|
404 | else |
---|
405 | { |
---|
406 | if ($category['nb_subcats'] > 0) |
---|
407 | { |
---|
408 | $image_src = $images_folder.'/icon_subfolder.gif'; |
---|
409 | } |
---|
410 | else |
---|
411 | { |
---|
412 | $image_src = $images_folder.'/icon_folder.gif'; |
---|
413 | } |
---|
414 | $image_alt = ''; |
---|
415 | $image_title = ''; |
---|
416 | } |
---|
417 | |
---|
418 | $base_url = PHPWG_ROOT_PATH.'admin.php?page='; |
---|
419 | $cat_list_url = $base_url.'cat_list'; |
---|
420 | |
---|
421 | $self_url = $cat_list_url; |
---|
422 | if (isset($_GET['parent_id'])) |
---|
423 | { |
---|
424 | $self_url.= '&parent_id='.$_GET['parent_id']; |
---|
425 | } |
---|
426 | |
---|
427 | $template->assign_block_vars( |
---|
428 | 'category', |
---|
429 | array( |
---|
430 | 'CATEGORY_IMG_SRC'=>$image_src, |
---|
431 | 'CATEGORY_IMG_ALT'=>$image_alt, |
---|
432 | 'CATEGORY_IMG_TITLE'=>$image_title, |
---|
433 | 'CATEGORY_NAME'=>$category['name'], |
---|
434 | 'CATEGORY_DIR'=>@$category['dir'], |
---|
435 | 'CATEGORY_NB_IMG'=>$category['nb_images'], |
---|
436 | |
---|
437 | 'U_CATEGORY'=> |
---|
438 | add_session_id($cat_list_url.'&parent_id='.$category['id']), |
---|
439 | |
---|
440 | 'U_MOVE_UP'=>add_session_id($self_url.'&up='.$category['id']), |
---|
441 | |
---|
442 | 'U_MOVE_DOWN'=>add_session_id($self_url.'&down='.$category['id']), |
---|
443 | |
---|
444 | 'U_CAT_EDIT'=> |
---|
445 | add_session_id($base_url.'cat_modify&cat_id='.$category['id']), |
---|
446 | |
---|
447 | 'U_CAT_DELETE'=>add_session_id($self_url.'&delete='.$category['id']), |
---|
448 | |
---|
449 | 'U_INFO_IMG' |
---|
450 | => add_session_id($base_url.'infos_images&cat_id='.$category['id']) |
---|
451 | )); |
---|
452 | |
---|
453 | if (!empty($category['dir'])) |
---|
454 | { |
---|
455 | $template->assign_block_vars('category.storage' ,array()); |
---|
456 | } |
---|
457 | else |
---|
458 | { |
---|
459 | $template->assign_block_vars('category.virtual' ,array()); |
---|
460 | } |
---|
461 | |
---|
462 | if ($category['nb_images'] > 0) |
---|
463 | { |
---|
464 | $template->assign_block_vars('category.image_info' ,array()); |
---|
465 | } |
---|
466 | else |
---|
467 | { |
---|
468 | $template->assign_block_vars('category.no_image_info' ,array()); |
---|
469 | } |
---|
470 | } |
---|
471 | // +-----------------------------------------------------------------------+ |
---|
472 | // | sending html code | |
---|
473 | // +-----------------------------------------------------------------------+ |
---|
474 | $template->assign_var_from_handle('ADMIN_CONTENT', 'categories'); |
---|
475 | ?> |
---|