source: trunk/admin/cat_list.php @ 25401

Last change on this file since 25401 was 25018, checked in by mistic100, 11 years ago

remove all array_push (50% slower than []) + some changes missing for feature:2978

  • Property svn:eol-style set to LF
File size: 9.9 KB
RevLine 
[21]1<?php
[362]2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[19703]5// | Copyright(C) 2008-2013 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   
[25018]81    $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// +-----------------------------------------------------------------------+
[13282]103// | tabs                                                                  |
104// +-----------------------------------------------------------------------+
105
106$page['tab'] = 'list';
107include(PHPWG_ROOT_PATH.'admin/include/albums_tab.inc.php');
108
109// +-----------------------------------------------------------------------+
[580]110// |                    virtual categories management                      |
111// +-----------------------------------------------------------------------+
[8131]112// request to delete a virtual category
[8126]113if (isset($_GET['delete']) and is_numeric($_GET['delete']))
[68]114{
[659]115  delete_categories(array($_GET['delete']));
[8168]116  $_SESSION['page_infos'] = array(l10n('Virtual album deleted'));
[659]117  update_global_rank();
[8168]118
119  $redirect_url = get_root_url().'admin.php?page=cat_list';
120  if (isset($_GET['parent_id']))
121  {
122    $redirect_url.= '&parent_id='.$_GET['parent_id'];
123  } 
124  redirect($redirect_url);
[394]125}
[602]126// request to add a virtual category
[798]127else if (isset($_POST['submitAdd']))
[394]128{
[1064]129  $output_create = create_virtual_category(
130    $_POST['virtual_name'],
131    @$_GET['parent_id']
132    );
133
134  if (isset($output_create['error']))
[580]135  {
[25018]136    $page['errors'][] = $output_create['error'];
[580]137  }
[1064]138  else
[68]139  {
[25018]140    $page['infos'][] = $output_create['info'];
[68]141  }
142}
[1066]143// save manual category ordering
[13282]144else if (isset($_POST['submitManualOrder']))
[798]145{
[13282]146  asort($_POST['catOrd'], SORT_NUMERIC);
147  save_categories_order(array_keys($_POST['catOrd']));
[1066]148
[25018]149  $page['infos'][] = l10n('Album manual order was saved');
[13282]150}
151else if (isset($_POST['submitAutoOrder']))
152{
153  $query = '
[6698]154SELECT id
[1066]155  FROM '.CATEGORIES_TABLE.'
156  WHERE id_uppercat '.
157    (!isset($_GET['parent_id']) ? 'IS NULL' : '= '.$_GET['parent_id']).'
158;';
[13282]159  $category_ids = array_from_query($query, 'id');
[1066]160
[13282]161  if (isset($_POST['recursive']))
162  {
163    $category_ids = get_subcat_ids($category_ids);
164  }
[6698]165 
[13282]166  $categories = array();
167  $names = array();
168  $id_uppercats = array();
169 
170  $query = '
[6698]171SELECT id, name, id_uppercat
[5079]172  FROM '.CATEGORIES_TABLE.'
[6698]173  WHERE id IN ('.implode(',', $category_ids).')
[5079]174;';
[13282]175  $result = pwg_query($query);
176  while ($row = pwg_db_fetch_assoc($result))
177  {
[25018]178    $categories[] = array(
179      'id' => $row['id'],
180      'id_uppercat' => $row['id_uppercat'],
[6698]181      );
[25018]182    $names[] = $row['name'];
[6698]183  }
[13282]184
185  array_multisort(
186    $names,
187    SORT_REGULAR,
188    'asc' == $_POST['ascdesc'] ? SORT_ASC : SORT_DESC,
189    $categories
190    );
191  save_categories_order($categories);
192
[25018]193  $page['infos'][] = l10n('Albums automatically sorted');
[5079]194}
195
[580]196// +-----------------------------------------------------------------------+
197// |                            Navigation path                            |
198// +-----------------------------------------------------------------------+
[834]199
[394]200if (isset($_GET['parent_id']))
201{
[642]202  $navigation.= $conf['level_separator'];
[580]203
[1861]204  $navigation.= get_cat_display_name_from_id(
205    $_GET['parent_id'],
[1064]206    $base_url.'&amp;parent_id=',
207    false
208    );
[394]209}
[580]210// +-----------------------------------------------------------------------+
211// |                       template initialization                         |
212// +-----------------------------------------------------------------------+
[2530]213$template->set_filename('categories', 'cat_list.tpl');
[394]214
[635]215$form_action = PHPWG_ROOT_PATH.'admin.php?page=cat_list';
216if (isset($_GET['parent_id']))
217{
218  $form_action.= '&amp;parent_id='.$_GET['parent_id'];
219}
220
[2223]221$template->assign(array(
[394]222  'CATEGORIES_NAV'=>$navigation,
[1004]223  'F_ACTION'=>$form_action,
[5195]224  'PWG_TOKEN' => get_pwg_token(),
[580]225 ));
[1131]226
[580]227// +-----------------------------------------------------------------------+
228// |                          Categories display                           |
229// +-----------------------------------------------------------------------+
[647]230
[798]231$categories = array();
232
233$query = '
[2324]234SELECT id, name, permalink, dir, rank, status
[798]235  FROM '.CATEGORIES_TABLE;
236if (!isset($_GET['parent_id']))
237{
238  $query.= '
239  WHERE id_uppercat IS NULL';
240}
241else
242{
243  $query.= '
244  WHERE id_uppercat = '.$_GET['parent_id'];
245}
246$query.= '
247  ORDER BY rank ASC
248;';
[2324]249$categories = hash_from_query($query, 'id');
[798]250
[2324]251// get the categories containing images directly
252$categories_with_images = array();
253if ( count($categories) )
[580]254{
[647]255  $query = '
[2324]256SELECT DISTINCT category_id
257  FROM '.IMAGE_CATEGORY_TABLE.'
258  WHERE category_id IN ('.implode(',', array_keys($categories)).')';
259  $categories_with_images = array_flip( array_from_query($query, 'category_id') );
[642]260}
261
[2223]262$template->assign('categories', array());
[2324]263$base_url = get_root_url().'admin.php?page=';
[24985]264
265if (isset($_GET['parent_id']))
266{
267  $template->assign(
268    'PARENT_EDIT',
269    $base_url.'album-'.$_GET['parent_id']
270    );
271}
272
[642]273foreach ($categories as $category)
274{
[580]275  $cat_list_url = $base_url.'cat_list';
[1131]276
[580]277  $self_url = $cat_list_url;
[394]278  if (isset($_GET['parent_id']))
[580]279  {
280    $self_url.= '&amp;parent_id='.$_GET['parent_id'];
281  }
[394]282
[2223]283  $tpl_cat =
[580]284    array(
[2433]285      'NAME'       => 
286        trigger_event(
287          'render_category_name',
288          $category['name'],
289          'admin_cat_list'
290          ),
[1082]291      'ID'         => $category['id'],
292      'RANK'       => $category['rank']*10,
[798]293
[1082]294      'U_JUMPTO'   => make_index_url(
295        array(
[1861]296          'category' => $category
[1082]297          )
298        ),
[1131]299
300      'U_CHILDREN' => $cat_list_url.'&amp;parent_id='.$category['id'],
[13013]301      'U_EDIT'     => $base_url.'album-'.$category['id'],
[2306]302
[2223]303      'IS_VIRTUAL' => empty($category['dir'])
[798]304    );
[1131]305
[798]306  if (empty($category['dir']))
[21]307  {
[2223]308    $tpl_cat['U_DELETE'] = $self_url.'&amp;delete='.$category['id'];
[5195]309    $tpl_cat['U_DELETE'].= '&amp;pwg_token='.get_pwg_token();
[21]310  }
[11041]311  else
312  {
313    if ($conf['enable_synchronization'])
314    {
315      $tpl_cat['U_SYNC'] = $base_url.'site_update&amp;site=1&amp;cat_id='.$category['id'];
316    }
317  }
[1131]318
[2324]319  if ( array_key_exists($category['id'], $categories_with_images) )
[580]320  {
[2223]321    $tpl_cat['U_MANAGE_ELEMENTS']=
[24834]322      $base_url.'batch_manager&amp;filter=album-'.$category['id'];
[21]323  }
[798]324
[2223]325  $template->append('categories', $tpl_cat);
[21]326}
[5933]327
328trigger_action('loc_end_cat_list');
329
[580]330// +-----------------------------------------------------------------------+
331// |                          sending html code                            |
332// +-----------------------------------------------------------------------+
[394]333$template->assign_var_from_handle('ADMIN_CONTENT', 'categories');
[362]334?>
Note: See TracBrowser for help on using the repository browser.