source: trunk/admin/cat_list.php @ 751

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