source: branches/2.1/admin/cat_list.php @ 8341

Last change on this file since 8341 was 8167, checked in by plg, 14 years ago

bug 2031 fixed: redirection after category deletion preserves the current
node in the category tree.

+ the confirmation message is displayed after redirect, thanks to session

  • Property svn:eol-style set to LF
File size: 9.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 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
24if (!defined('PHPWG_ROOT_PATH'))
25{
26  die('Hacking attempt!');
27}
28
29include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
30
31// +-----------------------------------------------------------------------+
32// | Check Access and exit when user status is not ok                      |
33// +-----------------------------------------------------------------------+
34check_status(ACCESS_ADMINISTRATOR);
35
36trigger_action('loc_begin_cat_list');
37
38if (!empty($_POST) or isset($_GET['delete']))
39{
40  check_pwg_token();
41}
42
43// +-----------------------------------------------------------------------+
44// |                               functions                               |
45// +-----------------------------------------------------------------------+
46
47/**
48 * save the rank depending on given categories order
49 *
50 * The list of ordered categories id is supposed to be in the same parent
51 * category
52 *
53 * @param array categories
54 * @return void
55 */
56function save_categories_order($categories)
57{
58  $current_rank = 0;
59  $datas = array();
60  foreach ($categories as $id)
61  {
62    array_push($datas, array('id' => $id, 'rank' => ++$current_rank));
63  }
64  $fields = array('primary' => array('id'), 'update' => array('rank'));
65  mass_updates(CATEGORIES_TABLE, $fields, $datas);
66
67  update_global_rank();
68}
69
70// +-----------------------------------------------------------------------+
71// |                            initialization                             |
72// +-----------------------------------------------------------------------+
73
74check_input_parameter('parent_id', $_GET, false, PATTERN_ID);
75
76$categories = array();
77
78$base_url = get_root_url().'admin.php?page=cat_list';
79$navigation = '<a href="'.$base_url.'">';
80$navigation.= l10n('Home');
81$navigation.= '</a>';
82
83// +-----------------------------------------------------------------------+
84// |                    virtual categories management                      |
85// +-----------------------------------------------------------------------+
86// request to delete a virtual category / not for an adviser
87if (isset($_GET['delete']) and is_numeric($_GET['delete']) and !is_adviser())
88{
89  delete_categories(array($_GET['delete']));
90  $_SESSION['page_infos'] = array(l10n('Virtual category deleted'));
91  update_global_rank();
92
93  $redirect_url = get_root_url().'admin.php?page=cat_list';
94  if (isset($_GET['parent_id']))
95  {
96    $redirect_url.= '&parent_id='.$_GET['parent_id'];
97  } 
98  redirect($redirect_url);
99}
100// request to add a virtual category
101else if (isset($_POST['submitAdd']))
102{
103  $output_create = create_virtual_category(
104    $_POST['virtual_name'],
105    @$_GET['parent_id']
106    );
107
108  if (isset($output_create['error']))
109  {
110    array_push($page['errors'], $output_create['error']);
111  }
112  else
113  {
114    array_push($page['infos'], $output_create['info']);
115  }
116}
117// save manual category ordering
118else if (isset($_POST['submitOrder']))
119{
120  asort($_POST['catOrd'], SORT_NUMERIC);
121  save_categories_order(array_keys($_POST['catOrd']));
122
123  array_push(
124    $page['infos'],
125    l10n('Categories manual order was saved')
126    );
127}
128// sort categories alpha-numerically
129else if (isset($_POST['submitOrderAlphaNum']))
130{
131  $query = '
132SELECT id, name
133  FROM '.CATEGORIES_TABLE.'
134  WHERE id_uppercat '.
135    (!isset($_GET['parent_id']) ? 'IS NULL' : '= '.$_GET['parent_id']).'
136;';
137  $result = pwg_query($query);
138  while ($row = pwg_db_fetch_assoc($result))
139  {
140    $categories[ $row['id'] ] = strtolower($row['name']);
141  }
142
143  asort($categories, SORT_REGULAR);
144  save_categories_order(array_keys($categories));
145
146  array_push(
147    $page['infos'],
148    l10n('Categories ordered alphanumerically')
149    );
150}
151// sort categories alpha-numerically reverse
152else if (isset($_POST['submitOrderAlphaNumReverse']))
153{
154  $query = '
155SELECT id, name
156  FROM '.CATEGORIES_TABLE.'
157  WHERE id_uppercat '.
158    (!isset($_GET['parent_id']) ? 'IS NULL' : '= '.$_GET['parent_id']).'
159;';
160  $result = pwg_query($query);
161  while ($row = pwg_db_fetch_assoc($result))
162  {
163    $categories[ $row['id'] ] = strtolower($row['name']);
164  }
165
166  arsort($categories, SORT_REGULAR);
167  save_categories_order(array_keys($categories));
168
169  array_push(
170    $page['infos'],
171    l10n('Categories ordered alphanumerically reverse')
172    );
173}
174
175// +-----------------------------------------------------------------------+
176// |                            Navigation path                            |
177// +-----------------------------------------------------------------------+
178
179if (isset($_GET['parent_id']))
180{
181  $navigation.= $conf['level_separator'];
182
183  $navigation.= get_cat_display_name_from_id(
184    $_GET['parent_id'],
185    $base_url.'&amp;parent_id=',
186    false
187    );
188}
189// +-----------------------------------------------------------------------+
190// |                       template initialization                         |
191// +-----------------------------------------------------------------------+
192$template->set_filename('categories', 'cat_list.tpl');
193
194$form_action = PHPWG_ROOT_PATH.'admin.php?page=cat_list';
195if (isset($_GET['parent_id']))
196{
197  $form_action.= '&amp;parent_id='.$_GET['parent_id'];
198}
199
200$template->assign(array(
201  'CATEGORIES_NAV'=>$navigation,
202  'F_ACTION'=>$form_action,
203  'PWG_TOKEN' => get_pwg_token(),
204 ));
205
206// +-----------------------------------------------------------------------+
207// |                          Categories display                           |
208// +-----------------------------------------------------------------------+
209
210$categories = array();
211
212$query = '
213SELECT id, name, permalink, dir, rank, status
214  FROM '.CATEGORIES_TABLE;
215if (!isset($_GET['parent_id']))
216{
217  $query.= '
218  WHERE id_uppercat IS NULL';
219}
220else
221{
222  $query.= '
223  WHERE id_uppercat = '.$_GET['parent_id'];
224}
225$query.= '
226  ORDER BY rank ASC
227;';
228$categories = hash_from_query($query, 'id');
229
230// get the categories containing images directly
231$categories_with_images = array();
232if ( count($categories) )
233{
234  $query = '
235SELECT DISTINCT category_id
236  FROM '.IMAGE_CATEGORY_TABLE.'
237  WHERE category_id IN ('.implode(',', array_keys($categories)).')';
238  $categories_with_images = array_flip( array_from_query($query, 'category_id') );
239}
240
241$template->assign('categories', array());
242$base_url = get_root_url().'admin.php?page=';
243foreach ($categories as $category)
244{
245  $cat_list_url = $base_url.'cat_list';
246
247  $self_url = $cat_list_url;
248  if (isset($_GET['parent_id']))
249  {
250    $self_url.= '&amp;parent_id='.$_GET['parent_id'];
251  }
252
253  $tpl_cat =
254    array(
255      'NAME'       => 
256        trigger_event(
257          'render_category_name',
258          $category['name'],
259          'admin_cat_list'
260          ),
261      'ID'         => $category['id'],
262      'RANK'       => $category['rank']*10,
263
264      'U_JUMPTO'   => make_index_url(
265        array(
266          'category' => $category
267          )
268        ),
269
270      'U_CHILDREN' => $cat_list_url.'&amp;parent_id='.$category['id'],
271      'U_EDIT'     => $base_url.'cat_modify&amp;cat_id='.$category['id'],
272
273      'IS_VIRTUAL' => empty($category['dir'])
274    );
275
276  if (empty($category['dir']))
277  {
278    $tpl_cat['U_DELETE'] = $self_url.'&amp;delete='.$category['id'];
279    $tpl_cat['U_DELETE'].= '&amp;pwg_token='.get_pwg_token();
280  }
281
282  if ( array_key_exists($category['id'], $categories_with_images) )
283  {
284    $tpl_cat['U_MANAGE_ELEMENTS']=
285      $base_url.'element_set&amp;cat='.$category['id'];
286  }
287
288  if ('private' == $category['status'])
289  {
290    $tpl_cat['U_MANAGE_PERMISSIONS']=
291      $base_url.'cat_perm&amp;cat='.$category['id'];
292  }
293  $template->append('categories', $tpl_cat);
294}
295
296trigger_action('loc_end_cat_list');
297
298// +-----------------------------------------------------------------------+
299// |                          sending html code                            |
300// +-----------------------------------------------------------------------+
301$template->assign_var_from_handle('ADMIN_CONTENT', 'categories');
302?>
Note: See TracBrowser for help on using the repository browser.