source: trunk/admin/cat_list.php @ 6698

Revision 6698, 9.6 KB checked in by plg, 10 years ago (diff)

feature 408: ability to automatically sort sub-categories. The number of SQL
queries is constant, no matter the deepth of your tree.

+ refactor to avoid duplicate code (ascending/desceding should be only a
single parameter in a function, not 20 lines of duplicated code)

  • Property svn:eol-style set to LF
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_for_id_uppercat = array();
59  $current_rank = 0;
60 
61  $datas = array();
62  foreach ($categories as $category)
63  {
64    if (is_array($category))
65    {
66      $id = $category['id'];
67      $id_uppercat = $category['id_uppercat'];
68
69      if (!isset($current_rank_for_id_uppercat[$id_uppercat]))
70      {
71        $current_rank_for_id_uppercat[$id_uppercat] = 0;
72      }
73      $current_rank = ++$current_rank_for_id_uppercat[$id_uppercat];
74    }
75    else
76    {
77      $id = $category;
78      $current_rank++;
79    }
80   
81    array_push($datas, array('id' => $id, 'rank' => $current_rank));
82  }
83  $fields = array('primary' => array('id'), 'update' => array('rank'));
84  mass_updates(CATEGORIES_TABLE, $fields, $datas);
85
86  update_global_rank();
87}
88
89// +-----------------------------------------------------------------------+
90// |                            initialization                             |
91// +-----------------------------------------------------------------------+
92
93check_input_parameter('parent_id', $_GET, false, PATTERN_ID);
94
95$categories = array();
96
97$base_url = get_root_url().'admin.php?page=cat_list';
98$navigation = '<a href="'.$base_url.'">';
99$navigation.= l10n('Home');
100$navigation.= '</a>';
101
102// +-----------------------------------------------------------------------+
103// |                    virtual categories management                      |
104// +-----------------------------------------------------------------------+
105// request to delete a virtual category / not for an adviser
106if (isset($_GET['delete']) and is_numeric($_GET['delete']) and !is_adviser())
107{
108  delete_categories(array($_GET['delete']));
109  array_push($page['infos'], l10n('Virtual category deleted'));
110  update_global_rank();
111  redirect(get_root_url().'admin.php?page=cat_list');
112}
113// request to add a virtual category
114else if (isset($_POST['submitAdd']))
115{
116  $output_create = create_virtual_category(
117    $_POST['virtual_name'],
118    @$_GET['parent_id']
119    );
120
121  if (isset($output_create['error']))
122  {
123    array_push($page['errors'], $output_create['error']);
124  }
125  else
126  {
127    array_push($page['infos'], $output_create['info']);
128  }
129}
130// save manual category ordering
131else if (isset($_POST['submitOrder']))
132{
133  if ('manual' == $_POST['order_type'])
134  {
135    asort($_POST['catOrd'], SORT_NUMERIC);
136    save_categories_order(array_keys($_POST['catOrd']));
137
138    array_push(
139      $page['infos'],
140      l10n('Categories manual order was saved')
141      );
142  }
143  else
144  {
145    $query = '
146SELECT id
147  FROM '.CATEGORIES_TABLE.'
148  WHERE id_uppercat '.
149    (!isset($_GET['parent_id']) ? 'IS NULL' : '= '.$_GET['parent_id']).'
150;';
151    $category_ids = array_from_query($query, 'id');
152
153    if (isset($_POST['recursive']))
154    {
155      $category_ids = get_subcat_ids($category_ids);
156    }
157
158    $categories = array();
159    $names = array();
160    $id_uppercats = array();
161 
162    $query = '
163SELECT id, name, id_uppercat
164  FROM '.CATEGORIES_TABLE.'
165  WHERE id IN ('.implode(',', $category_ids).')
166;';
167    $result = pwg_query($query);
168    while ($row = pwg_db_fetch_assoc($result))
169    {
170      array_push(
171        $categories,
172        array(
173          'id' => $row['id'],
174          'id_uppercat' => $row['id_uppercat'],
175          )
176        );
177      array_push(
178        $names,
179        $row['name']
180        );
181    }
182
183    array_multisort(
184      $names,
185      SORT_REGULAR,
186      'asc' == $_POST['ascdesc'] ? SORT_ASC : SORT_DESC,
187      $categories
188      );
189    save_categories_order($categories);
190
191    array_push(
192      $page['infos'],
193      l10n('Categories automatically sorted')
194      );
195  }
196}
197
198// +-----------------------------------------------------------------------+
199// |                            Navigation path                            |
200// +-----------------------------------------------------------------------+
201
202if (isset($_GET['parent_id']))
203{
204  $navigation.= $conf['level_separator'];
205
206  $navigation.= get_cat_display_name_from_id(
207    $_GET['parent_id'],
208    $base_url.'&amp;parent_id=',
209    false
210    );
211}
212// +-----------------------------------------------------------------------+
213// |                       template initialization                         |
214// +-----------------------------------------------------------------------+
215$template->set_filename('categories', 'cat_list.tpl');
216
217$form_action = PHPWG_ROOT_PATH.'admin.php?page=cat_list';
218if (isset($_GET['parent_id']))
219{
220  $form_action.= '&amp;parent_id='.$_GET['parent_id'];
221}
222
223$template->assign(array(
224  'CATEGORIES_NAV'=>$navigation,
225  'F_ACTION'=>$form_action,
226  'PWG_TOKEN' => get_pwg_token(),
227 ));
228
229// +-----------------------------------------------------------------------+
230// |                          Categories display                           |
231// +-----------------------------------------------------------------------+
232
233$categories = array();
234
235$query = '
236SELECT id, name, permalink, dir, rank, status
237  FROM '.CATEGORIES_TABLE;
238if (!isset($_GET['parent_id']))
239{
240  $query.= '
241  WHERE id_uppercat IS NULL';
242}
243else
244{
245  $query.= '
246  WHERE id_uppercat = '.$_GET['parent_id'];
247}
248$query.= '
249  ORDER BY rank ASC
250;';
251$categories = hash_from_query($query, 'id');
252
253// get the categories containing images directly
254$categories_with_images = array();
255if ( count($categories) )
256{
257  $query = '
258SELECT DISTINCT category_id
259  FROM '.IMAGE_CATEGORY_TABLE.'
260  WHERE category_id IN ('.implode(',', array_keys($categories)).')';
261  $categories_with_images = array_flip( array_from_query($query, 'category_id') );
262}
263
264$template->assign('categories', array());
265$base_url = get_root_url().'admin.php?page=';
266foreach ($categories as $category)
267{
268  $cat_list_url = $base_url.'cat_list';
269
270  $self_url = $cat_list_url;
271  if (isset($_GET['parent_id']))
272  {
273    $self_url.= '&amp;parent_id='.$_GET['parent_id'];
274  }
275
276  $tpl_cat =
277    array(
278      'NAME'       => 
279        trigger_event(
280          'render_category_name',
281          $category['name'],
282          'admin_cat_list'
283          ),
284      'ID'         => $category['id'],
285      'RANK'       => $category['rank']*10,
286
287      'U_JUMPTO'   => make_index_url(
288        array(
289          'category' => $category
290          )
291        ),
292
293      'U_CHILDREN' => $cat_list_url.'&amp;parent_id='.$category['id'],
294      'U_EDIT'     => $base_url.'cat_modify&amp;cat_id='.$category['id'],
295
296      'IS_VIRTUAL' => empty($category['dir'])
297    );
298
299  if (empty($category['dir']))
300  {
301    $tpl_cat['U_DELETE'] = $self_url.'&amp;delete='.$category['id'];
302    $tpl_cat['U_DELETE'].= '&amp;pwg_token='.get_pwg_token();
303  }
304
305  if ( array_key_exists($category['id'], $categories_with_images) )
306  {
307    $tpl_cat['U_MANAGE_ELEMENTS']=
308      $base_url.'element_set&amp;cat='.$category['id'];
309  }
310
311  if ('private' == $category['status'])
312  {
313    $tpl_cat['U_MANAGE_PERMISSIONS']=
314      $base_url.'cat_perm&amp;cat='.$category['id'];
315  }
316  $template->append('categories', $tpl_cat);
317}
318
319trigger_action('loc_end_cat_list');
320
321// +-----------------------------------------------------------------------+
322// |                          sending html code                            |
323// +-----------------------------------------------------------------------+
324$template->assign_var_from_handle('ADMIN_CONTENT', 'categories');
325?>
Note: See TracBrowser for help on using the repository browser.