source: trunk/admin/cat_list.php @ 6698

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

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
File size: 9.6 KB
RevLine 
[21]1<?php
[362]2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[5196]5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[345]23
[580]24if (!defined('PHPWG_ROOT_PATH'))
[394]25{
[580]26  die('Hacking attempt!');
[394]27}
[798]28
[1072]29include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
30
[580]31// +-----------------------------------------------------------------------+
[1072]32// | Check Access and exit when user status is not ok                      |
33// +-----------------------------------------------------------------------+
34check_status(ACCESS_ADMINISTRATOR);
35
[5933]36trigger_action('loc_begin_cat_list');
37
[5195]38if (!empty($_POST) or isset($_GET['delete']))
39{
40  check_pwg_token();
41}
42
[1072]43// +-----------------------------------------------------------------------+
[798]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{
[6698]58  $current_rank_for_id_uppercat = array();
[798]59  $current_rank = 0;
[6698]60 
[798]61  $datas = array();
[6698]62  foreach ($categories as $category)
[798]63  {
[6698]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));
[798]82  }
83  $fields = array('primary' => array('id'), 'update' => array('rank'));
84  mass_updates(CATEGORIES_TABLE, $fields, $datas);
85
[2306]86  update_global_rank();
[798]87}
88
89// +-----------------------------------------------------------------------+
[580]90// |                            initialization                             |
91// +-----------------------------------------------------------------------+
[834]92
[5195]93check_input_parameter('parent_id', $_GET, false, PATTERN_ID);
94
[580]95$categories = array();
[834]96
[2306]97$base_url = get_root_url().'admin.php?page=cat_list';
98$navigation = '<a href="'.$base_url.'">';
[5021]99$navigation.= l10n('Home');
[834]100$navigation.= '</a>';
101
[580]102// +-----------------------------------------------------------------------+
103// |                    virtual categories management                      |
104// +-----------------------------------------------------------------------+
[1106]105// request to delete a virtual category / not for an adviser
106if (isset($_GET['delete']) and is_numeric($_GET['delete']) and !is_adviser())
[68]107{
[659]108  delete_categories(array($_GET['delete']));
[5021]109  array_push($page['infos'], l10n('Virtual category deleted'));
[659]110  update_global_rank();
[5193]111  redirect(get_root_url().'admin.php?page=cat_list');
[394]112}
[602]113// request to add a virtual category
[798]114else if (isset($_POST['submitAdd']))
[394]115{
[1064]116  $output_create = create_virtual_category(
117    $_POST['virtual_name'],
118    @$_GET['parent_id']
119    );
120
121  if (isset($output_create['error']))
[580]122  {
[1064]123    array_push($page['errors'], $output_create['error']);
[580]124  }
[1064]125  else
[68]126  {
[1064]127    array_push($page['infos'], $output_create['info']);
[68]128  }
129}
[1066]130// save manual category ordering
[798]131else if (isset($_POST['submitOrder']))
132{
[6698]133  if ('manual' == $_POST['order_type'])
134  {
135    asort($_POST['catOrd'], SORT_NUMERIC);
136    save_categories_order(array_keys($_POST['catOrd']));
[1066]137
[6698]138    array_push(
139      $page['infos'],
140      l10n('Categories manual order was saved')
141      );
142  }
143  else
144  {
145    $query = '
146SELECT id
[1066]147  FROM '.CATEGORIES_TABLE.'
148  WHERE id_uppercat '.
149    (!isset($_GET['parent_id']) ? 'IS NULL' : '= '.$_GET['parent_id']).'
150;';
[6698]151    $category_ids = array_from_query($query, 'id');
[1066]152
[6698]153    if (isset($_POST['recursive']))
154    {
155      $category_ids = get_subcat_ids($category_ids);
156    }
[1066]157
[6698]158    $categories = array();
159    $names = array();
160    $id_uppercats = array();
161 
162    $query = '
163SELECT id, name, id_uppercat
[5079]164  FROM '.CATEGORIES_TABLE.'
[6698]165  WHERE id IN ('.implode(',', $category_ids).')
[5079]166;';
[6698]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    }
[1066]182
[6698]183    array_multisort(
184      $names,
185      SORT_REGULAR,
186      'asc' == $_POST['ascdesc'] ? SORT_ASC : SORT_DESC,
187      $categories
188      );
189    save_categories_order($categories);
[5079]190
[6698]191    array_push(
192      $page['infos'],
193      l10n('Categories automatically sorted')
194      );
195  }
[5079]196}
197
[580]198// +-----------------------------------------------------------------------+
199// |                            Navigation path                            |
200// +-----------------------------------------------------------------------+
[834]201
[394]202if (isset($_GET['parent_id']))
203{
[642]204  $navigation.= $conf['level_separator'];
[580]205
[1861]206  $navigation.= get_cat_display_name_from_id(
207    $_GET['parent_id'],
[1064]208    $base_url.'&amp;parent_id=',
209    false
210    );
[394]211}
[580]212// +-----------------------------------------------------------------------+
213// |                       template initialization                         |
214// +-----------------------------------------------------------------------+
[2530]215$template->set_filename('categories', 'cat_list.tpl');
[394]216
[635]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
[2223]223$template->assign(array(
[394]224  'CATEGORIES_NAV'=>$navigation,
[1004]225  'F_ACTION'=>$form_action,
[5195]226  'PWG_TOKEN' => get_pwg_token(),
[580]227 ));
[1131]228
[580]229// +-----------------------------------------------------------------------+
230// |                          Categories display                           |
231// +-----------------------------------------------------------------------+
[647]232
[798]233$categories = array();
234
235$query = '
[2324]236SELECT id, name, permalink, dir, rank, status
[798]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;';
[2324]251$categories = hash_from_query($query, 'id');
[798]252
[2324]253// get the categories containing images directly
254$categories_with_images = array();
255if ( count($categories) )
[580]256{
[647]257  $query = '
[2324]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') );
[642]262}
263
[2223]264$template->assign('categories', array());
[2324]265$base_url = get_root_url().'admin.php?page=';
[642]266foreach ($categories as $category)
267{
[580]268  $cat_list_url = $base_url.'cat_list';
[1131]269
[580]270  $self_url = $cat_list_url;
[394]271  if (isset($_GET['parent_id']))
[580]272  {
273    $self_url.= '&amp;parent_id='.$_GET['parent_id'];
274  }
[394]275
[2223]276  $tpl_cat =
[580]277    array(
[2433]278      'NAME'       => 
279        trigger_event(
280          'render_category_name',
281          $category['name'],
282          'admin_cat_list'
283          ),
[1082]284      'ID'         => $category['id'],
285      'RANK'       => $category['rank']*10,
[798]286
[1082]287      'U_JUMPTO'   => make_index_url(
288        array(
[1861]289          'category' => $category
[1082]290          )
291        ),
[1131]292
293      'U_CHILDREN' => $cat_list_url.'&amp;parent_id='.$category['id'],
[1082]294      'U_EDIT'     => $base_url.'cat_modify&amp;cat_id='.$category['id'],
[2306]295
[2223]296      'IS_VIRTUAL' => empty($category['dir'])
[798]297    );
[1131]298
[798]299  if (empty($category['dir']))
[21]300  {
[2223]301    $tpl_cat['U_DELETE'] = $self_url.'&amp;delete='.$category['id'];
[5195]302    $tpl_cat['U_DELETE'].= '&amp;pwg_token='.get_pwg_token();
[21]303  }
[1131]304
[2324]305  if ( array_key_exists($category['id'], $categories_with_images) )
[580]306  {
[2223]307    $tpl_cat['U_MANAGE_ELEMENTS']=
308      $base_url.'element_set&amp;cat='.$category['id'];
[21]309  }
[798]310
311  if ('private' == $category['status'])
[394]312  {
[2223]313    $tpl_cat['U_MANAGE_PERMISSIONS']=
314      $base_url.'cat_perm&amp;cat='.$category['id'];
[394]315  }
[2223]316  $template->append('categories', $tpl_cat);
[21]317}
[5933]318
319trigger_action('loc_end_cat_list');
320
[580]321// +-----------------------------------------------------------------------+
322// |                          sending html code                            |
323// +-----------------------------------------------------------------------+
[394]324$template->assign_var_from_handle('ADMIN_CONTENT', 'categories');
[362]325?>
Note: See TracBrowser for help on using the repository browser.