source: trunk/admin/cat_list.php @ 798

Last change on this file since 798 was 798, checked in by plg, 19 years ago
  • categories management : new display with icon for actions (delete, sub-categories, elements, edit, jump to, permissions)
  • categories management : semantic HTML layout (using common lists)
  • categories management : new way to order categories of the same level : a text field let the admin reorder all categories at once.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 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-30 21:00:07 +0000 (Thu, 30 Jun 2005) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 798 $
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// +-----------------------------------------------------------------------+
35// |                               functions                               |
36// +-----------------------------------------------------------------------+
37
38/**
39 * save the rank depending on given categories order
40 *
41 * The list of ordered categories id is supposed to be in the same parent
42 * category
43 *
44 * @param array categories
45 * @return void
46 */
47function save_categories_order($categories)
48{
49  $current_rank = 0;
50  $datas = array();
51  foreach ($categories as $id)
52  {
53    array_push($datas, array('id' => $id, 'rank' => ++$current_rank));
54  }
55  $fields = array('primary' => array('id'), 'update' => array('rank'));
56  mass_updates(CATEGORIES_TABLE, $fields, $datas);
57
58  update_global_rank(@$_GET['parent_id']);
59}
60
61// +-----------------------------------------------------------------------+
62// |                            initialization                             |
63// +-----------------------------------------------------------------------+
64$categories = array();
65$navigation = $lang['home'];
66// +-----------------------------------------------------------------------+
67// |                    virtual categories management                      |
68// +-----------------------------------------------------------------------+
69// request to delete a virtual category
70if (isset($_GET['delete']) and is_numeric($_GET['delete']))
71{
72  delete_categories(array($_GET['delete']));
73  array_push($page['infos'], $lang['cat_virtual_deleted']);
74  ordering();
75  update_global_rank();
76}
77// request to add a virtual category
78else if (isset($_POST['submitAdd']))
79{
80  // is the given category name only containing blank spaces ?
81  if (preg_match('/^\s*$/', $_POST['virtual_name']))
82  {
83    array_push($page['errors'], $lang['cat_error_name']);
84  }
85       
86  if (!count($page['errors']))
87  {
88    $parent_id = !empty($_GET['parent_id'])?$_GET['parent_id']:'NULL';
89   
90    if ($parent_id != 'NULL')
91    {
92      $query = '
93SELECT id,uppercats,global_rank,visible,status
94  FROM '.CATEGORIES_TABLE.'
95  WHERE id = '.$parent_id.'
96;';
97      $row = mysql_fetch_array(pwg_query($query));
98      $parent = array('id' => $row['id'],
99                      'uppercats' => $row['uppercats'],
100                      'visible' => $row['visible'],
101                      'status' => $row['status'],
102                      'global_rank' => $row['global_rank']);
103    }
104
105    // what will be the inserted id ?
106    $query = '
107SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1)
108  FROM '.CATEGORIES_TABLE.'
109;';
110    list($next_id) = mysql_fetch_array(pwg_query($query));
111   
112    $insert = array();
113    $insert{'id'} = $next_id++;
114    $insert{'name'} = $_POST['virtual_name'];
115    $insert{'rank'} = $_POST['rank'];
116    $insert{'commentable'} = $conf['newcat_default_commentable'];
117
118    // a virtual category can't be uploadable
119    $insert{'uploadable'} = 'false';
120   
121    if (isset($parent))
122    {
123      $insert{'id_uppercat'} = $parent{'id'};
124      $insert{'uppercats'}   = $parent{'uppercats'}.','.$insert{'id'};
125      $insert{'global_rank'} = $parent{'global_rank'}.'.'.$insert{'rank'};
126      // at creation, must a category be visible or not ? Warning : if
127      // the parent category is invisible, the category is automatically
128      // create invisible. (invisible = locked)
129      if ('false' == $parent['visible'])
130      {
131        $insert{'visible'} = 'false';
132      }
133      else
134      {
135        $insert{'visible'} = $conf['newcat_default_visible'];
136      }
137      // at creation, must a category be public or private ? Warning :
138      // if the parent category is private, the category is
139      // automatically create private.
140      if ('private' == $parent['status'])
141      {
142        $insert{'status'} = 'private';
143      }
144      else
145      {
146        $insert{'status'} = $conf['newcat_default_status'];
147      }
148    }
149    else
150    {
151      $insert{'visible'} = $conf['newcat_default_visible'];
152      $insert{'status'} = $conf['newcat_default_status'];
153      $insert{'uppercats'} = $insert{'id'};
154      $insert{'global_rank'} = $insert{'rank'};
155    }
156
157    $inserts = array($insert);
158   
159    // we have then to add the virtual category
160    $dbfields = array('id','site_id','name','id_uppercat','rank',
161                      'commentable','uploadable','visible','status',
162                      'uppercats','global_rank');
163    mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts);
164
165    array_push($page['infos'], $lang['cat_virtual_added']);
166  }
167}
168else if (isset($_POST['submitOrder']))
169{
170  asort($_POST['catOrd'], SORT_NUMERIC);
171  save_categories_order(array_keys($_POST['catOrd']));
172}
173// +-----------------------------------------------------------------------+
174// |                           Cache management                            |
175// +-----------------------------------------------------------------------+
176$query = '
177SELECT *
178  FROM '.CATEGORIES_TABLE;
179if (!isset($_GET['parent_id']))
180{
181  $query.= '
182  WHERE id_uppercat IS NULL';
183}
184else
185{
186  $query.= '
187  WHERE id_uppercat = '.$_GET['parent_id'];
188}
189$query.= '
190  ORDER BY rank ASC
191;';
192$result = pwg_query($query);
193while ($row = mysql_fetch_assoc($result))
194{
195  $categories[$row['rank']] = $row;
196  $categories[$row['rank']]['nb_subcats'] = 0;
197}
198// +-----------------------------------------------------------------------+
199// |                            Navigation path                            |
200// +-----------------------------------------------------------------------+
201if (isset($_GET['parent_id']))
202{
203  $base_url = PHPWG_ROOT_PATH.'admin.php?page=cat_list';
204 
205  $navigation = '<a class="" href="'.add_session_id($base_url).'">';
206  $navigation.= $lang['home'];
207  $navigation.= '</a>';
208  $navigation.= $conf['level_separator'];
209
210  $current_category = get_cat_info($_GET['parent_id']);
211  $navigation.= get_cat_display_name($current_category['name'],
212                                     $base_url.'&amp;parent_id=',
213                                     false);
214}
215// +-----------------------------------------------------------------------+
216// |                       template initialization                         |
217// +-----------------------------------------------------------------------+
218$template->set_filenames(array('categories'=>'admin/cat_list.tpl'));
219
220$form_action = PHPWG_ROOT_PATH.'admin.php?page=cat_list';
221if (isset($_GET['parent_id']))
222{
223  $form_action.= '&amp;parent_id='.$_GET['parent_id'];
224}
225
226if (count($categories) > 0)
227{
228  $next_rank = max(array_keys($categories)) + 1;
229}
230else
231{
232  $next_rank = 1;
233}
234
235$template->assign_vars(array(
236  'CATEGORIES_NAV'=>$navigation,
237  'NEXT_RANK'=>$next_rank,
238  'F_ACTION'=>$form_action,
239 
240  'L_ADD_VIRTUAL'=>$lang['cat_add'],
241  'L_SUBMIT'=>$lang['submit'],
242  'L_STORAGE'=>$lang['storage'],
243  'L_NB_IMG'=>$lang['pictures'],
244  'L_MOVE_UP'=>$lang['up'],
245  'L_MOVE_DOWN'=>$lang['down'],
246  'L_EDIT'=>$lang['edit'],
247  'L_INFO_IMG'=>$lang['cat_image_info'],
248  'L_DELETE'=>$lang['delete'],
249 ));
250 
251$tpl = array('cat_first','cat_last');
252// +-----------------------------------------------------------------------+
253// |                          Categories display                           |
254// +-----------------------------------------------------------------------+
255
256$categories = array();
257
258$query = '
259SELECT id, name, dir, rank, nb_images, status
260  FROM '.CATEGORIES_TABLE;
261if (!isset($_GET['parent_id']))
262{
263  $query.= '
264  WHERE id_uppercat IS NULL';
265}
266else
267{
268  $query.= '
269  WHERE id_uppercat = '.$_GET['parent_id'];
270}
271$query.= '
272  ORDER BY rank ASC
273;';
274$result = pwg_query($query);
275while ($row = mysql_fetch_array($result))
276{
277  $categories[$row['id']] = $row;
278  // by default, let's consider there is no sub-categories. This will be
279  // calculated after.
280  $categories[$row['id']]['nb_subcats'] = 0;
281}
282
283if (count($categories) > 0)
284{
285  $query = '
286SELECT id_uppercat, COUNT(*) AS nb_subcats
287  FROM '. CATEGORIES_TABLE.'
288  WHERE id_uppercat IN ('.implode(',', array_keys($categories)).')
289  GROUP BY id_uppercat
290;';
291  $result = pwg_query($query);
292  while ($row = mysql_fetch_array($result))
293  {
294    $categories[$row['id_uppercat']]['nb_subcats'] = $row['nb_subcats'];
295  }
296}
297
298foreach ($categories as $category)
299{
300  $images_folder = PHPWG_ROOT_PATH.'template/';
301  $images_folder.= $user['template'].'/admin/images';
302 
303  $base_url = PHPWG_ROOT_PATH.'admin.php?page=';
304  $cat_list_url = $base_url.'cat_list';
305 
306  $self_url = $cat_list_url;
307  if (isset($_GET['parent_id']))
308  {
309    $self_url.= '&amp;parent_id='.$_GET['parent_id'];
310  }
311
312  $template->assign_block_vars(
313    'category',
314    array(
315      'NAME'=>$category['name'],
316      'ID'=>$category['id'],
317      'RANK'=>$category['rank']*10,
318
319      'U_JUMPTO'=>
320      add_session_id(PHPWG_ROOT_PATH.'category.php?cat='.$category['id']),
321     
322      'U_CHILDREN'=>
323      add_session_id($cat_list_url.'&amp;parent_id='.$category['id']),
324     
325      'U_EDIT'=>
326      add_session_id($base_url.'cat_modify&amp;cat_id='.$category['id'])
327      )
328    );
329 
330  if (empty($category['dir']))
331  {
332    $template->assign_block_vars(
333      'category.delete',
334      array(
335        'URL'=>add_session_id($self_url.'&amp;delete='.$category['id'])
336        )
337      );
338  }
339 
340  if ($category['nb_images'] > 0)
341  {
342    $template->assign_block_vars(
343      'category.elements',
344      array(
345        'URL'=>add_session_id($base_url.'element_set&amp;cat='.$category['id'])
346        )
347      );
348  }
349
350  if ('private' == $category['status'])
351  {
352    $template->assign_block_vars(
353      'category.permissions',
354      array(
355        'URL'=>add_session_id($base_url.'cat_perm&amp;cat='.$category['id'])
356        )
357      );
358  }
359}
360// +-----------------------------------------------------------------------+
361// |                          sending html code                            |
362// +-----------------------------------------------------------------------+
363$template->assign_var_from_handle('ADMIN_CONTENT', 'categories');
364?>
Note: See TracBrowser for help on using the repository browser.