source: trunk/admin/cat_list.php @ 5193

Last change on this file since 5193 was 5193, checked in by nikrou, 14 years ago

remove picture delete displayed twice
redirect after delete to avoid errors

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