source: trunk/admin/cat_list.php @ 792

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