source: trunk/admin/cat_list.php @ 29729

Last change on this file since 29729 was 28939, checked in by mistic100, 10 years ago

strict standards

  • Property svn:eol-style set to LF
File size: 13.4 KB
RevLine 
[21]1<?php
[362]2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[26461]5// | Copyright(C) 2008-2014 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
[28587]36trigger_notify('loc_begin_cat_list');
[5933]37
[5195]38if (!empty($_POST) or isset($_GET['delete']))
39{
40  check_pwg_token();
41}
42
[28934]43$sort_orders = array(
44  'name ASC' => l10n('Album name, A &rarr; Z'),
45  'name DESC' => l10n('Album name, Z &rarr; A'),
46  'date_creation DESC' => l10n('Date created, new &rarr; old'),
47  'date_creation ASC' => l10n('Date created, old &rarr; new'),
48  'date_available DESC' => l10n('Date posted, new &rarr; old'),
49  'date_available ASC' => l10n('Date posted, old &rarr; new'),
50  );
51
[1072]52// +-----------------------------------------------------------------------+
[798]53// |                               functions                               |
54// +-----------------------------------------------------------------------+
55
56/**
57 * save the rank depending on given categories order
58 *
59 * The list of ordered categories id is supposed to be in the same parent
60 * category
61 *
62 * @param array categories
63 * @return void
64 */
65function save_categories_order($categories)
66{
[6698]67  $current_rank_for_id_uppercat = array();
[798]68  $current_rank = 0;
[6698]69 
[798]70  $datas = array();
[6698]71  foreach ($categories as $category)
[798]72  {
[6698]73    if (is_array($category))
74    {
75      $id = $category['id'];
76      $id_uppercat = $category['id_uppercat'];
77
78      if (!isset($current_rank_for_id_uppercat[$id_uppercat]))
79      {
80        $current_rank_for_id_uppercat[$id_uppercat] = 0;
81      }
82      $current_rank = ++$current_rank_for_id_uppercat[$id_uppercat];
83    }
84    else
85    {
86      $id = $category;
87      $current_rank++;
88    }
89   
[25018]90    $datas[] = array('id' => $id, 'rank' => $current_rank);
[798]91  }
92  $fields = array('primary' => array('id'), 'update' => array('rank'));
93  mass_updates(CATEGORIES_TABLE, $fields, $datas);
94
[2306]95  update_global_rank();
[798]96}
97
[28934]98function get_categories_ref_date($ids, $field='date_available', $minmax='max')
99{
100  // we need to work on the whole tree under each category, even if we don't
101  // want to sort sub categories
102  $category_ids = get_subcat_ids($ids);
103 
104  // search for the reference date of each album
105  $query = '
106SELECT
107    category_id,
108    '.$minmax.'('.$field.') as ref_date
109  FROM '.IMAGE_CATEGORY_TABLE.'
110    JOIN '.IMAGES_TABLE.' ON image_id = id
111  WHERE category_id IN ('.implode(',', $category_ids).')
112  GROUP BY category_id
113;';
114  $ref_dates = query2array($query, 'category_id', 'ref_date');
115
116  // the iterate on all albums (having a ref_date or not) to find the
117  // reference_date, with a search on sub-albums
118  $query = '
119SELECT
120    id,
121    uppercats
122  FROM '.CATEGORIES_TABLE.'
123  WHERE id IN ('.implode(',', $category_ids).')
124;';
125  $uppercats_of = query2array($query, 'id', 'uppercats');
126
127  foreach (array_keys($uppercats_of) as $cat_id)
128  {
129    // find the subcats
130    $subcat_ids = array();
131   
132    foreach ($uppercats_of as $id => $uppercats)
133    {
134      if (preg_match('/(^|,)'.$cat_id.'(,|$)/', $uppercats))
135      {
136        $subcat_ids[] = $id;
137      }
138    }
139
140    $to_compare = array();
141    foreach ($subcat_ids as $id)
142    {
143      if (isset($ref_dates[$id]))
144      {
145        $to_compare[] = $ref_dates[$id];
146      }
147    }
148
149    if (count($to_compare) > 0)
150    {
151      $ref_dates[$cat_id] = 'max' == $minmax ? max($to_compare) : min($to_compare);
152    }
153    else
154    {
155      $ref_dates[$cat_id] = null;
156    }
157  }
158
159  // only return the list of $ids, not the sub-categories
160  $return = array();
161  foreach ($ids as $id)
162  {
163    $return[$id] = $ref_dates[$id];
164  }
165 
166  return $return;
167}
168
[798]169// +-----------------------------------------------------------------------+
[580]170// |                            initialization                             |
171// +-----------------------------------------------------------------------+
[834]172
[5195]173check_input_parameter('parent_id', $_GET, false, PATTERN_ID);
174
[580]175$categories = array();
[834]176
[2306]177$base_url = get_root_url().'admin.php?page=cat_list';
178$navigation = '<a href="'.$base_url.'">';
[5021]179$navigation.= l10n('Home');
[834]180$navigation.= '</a>';
181
[580]182// +-----------------------------------------------------------------------+
[13282]183// | tabs                                                                  |
184// +-----------------------------------------------------------------------+
185
186$page['tab'] = 'list';
187include(PHPWG_ROOT_PATH.'admin/include/albums_tab.inc.php');
188
189// +-----------------------------------------------------------------------+
[580]190// |                    virtual categories management                      |
191// +-----------------------------------------------------------------------+
[8131]192// request to delete a virtual category
[8126]193if (isset($_GET['delete']) and is_numeric($_GET['delete']))
[68]194{
[659]195  delete_categories(array($_GET['delete']));
[8168]196  $_SESSION['page_infos'] = array(l10n('Virtual album deleted'));
[659]197  update_global_rank();
[25975]198  invalidate_user_cache();
[8168]199
200  $redirect_url = get_root_url().'admin.php?page=cat_list';
201  if (isset($_GET['parent_id']))
202  {
203    $redirect_url.= '&parent_id='.$_GET['parent_id'];
[25975]204  }
[8168]205  redirect($redirect_url);
[394]206}
[602]207// request to add a virtual category
[25975]208elseif (isset($_POST['submitAdd']))
[394]209{
[1064]210  $output_create = create_virtual_category(
211    $_POST['virtual_name'],
212    @$_GET['parent_id']
213    );
214
[25975]215  invalidate_user_cache();
[1064]216  if (isset($output_create['error']))
[580]217  {
[25018]218    $page['errors'][] = $output_create['error'];
[580]219  }
[1064]220  else
[68]221  {
[25018]222    $page['infos'][] = $output_create['info'];
[68]223  }
224}
[1066]225// save manual category ordering
[25975]226elseif (isset($_POST['submitManualOrder']))
[798]227{
[13282]228  asort($_POST['catOrd'], SORT_NUMERIC);
229  save_categories_order(array_keys($_POST['catOrd']));
[1066]230
[25018]231  $page['infos'][] = l10n('Album manual order was saved');
[13282]232}
[25975]233elseif (isset($_POST['submitAutoOrder']))
[13282]234{
[28934]235  if (!isset($sort_orders[ $_POST['order_by'] ]))
236  {
237    die('Invalid sort order');
238  }
239
[13282]240  $query = '
[6698]241SELECT id
[1066]242  FROM '.CATEGORIES_TABLE.'
243  WHERE id_uppercat '.
244    (!isset($_GET['parent_id']) ? 'IS NULL' : '= '.$_GET['parent_id']).'
245;';
[13282]246  $category_ids = array_from_query($query, 'id');
[1066]247
[13282]248  if (isset($_POST['recursive']))
249  {
250    $category_ids = get_subcat_ids($category_ids);
251  }
[6698]252 
[13282]253  $categories = array();
[28934]254  $sort = array();
255
256  list($order_by_field, $order_by_asc) = explode(' ', $_POST['order_by']);
[13282]257 
[28934]258  $order_by_date = false;
259  if (strpos($order_by_field, 'date_') === 0)
260  {
261    $order_by_date = true;
262   
263    $ref_dates = get_categories_ref_date(
264      $category_ids,
265      $order_by_field,
266      'ASC' == $order_by_asc ? 'min' : 'max'
267      );
268  }
269
[13282]270  $query = '
[6698]271SELECT id, name, id_uppercat
[5079]272  FROM '.CATEGORIES_TABLE.'
[6698]273  WHERE id IN ('.implode(',', $category_ids).')
[5079]274;';
[13282]275  $result = pwg_query($query);
276  while ($row = pwg_db_fetch_assoc($result))
277  {
[28934]278    if ($order_by_date)
279    {
280      $sort[] = $ref_dates[ $row['id'] ];
281    }
282    else
283    {
284      $sort[] = $row['name'];
285    }
286   
[25018]287    $categories[] = array(
288      'id' => $row['id'],
289      'id_uppercat' => $row['id_uppercat'],
[6698]290      );
291  }
[13282]292
293  array_multisort(
[28934]294    $sort,
[13282]295    SORT_REGULAR,
[28934]296    'ASC' == $order_by_asc ? SORT_ASC : SORT_DESC,
[13282]297    $categories
298    );
[28934]299 
[13282]300  save_categories_order($categories);
301
[25018]302  $page['infos'][] = l10n('Albums automatically sorted');
[5079]303}
304
[580]305// +-----------------------------------------------------------------------+
306// |                            Navigation path                            |
307// +-----------------------------------------------------------------------+
[834]308
[394]309if (isset($_GET['parent_id']))
310{
[642]311  $navigation.= $conf['level_separator'];
[580]312
[1861]313  $navigation.= get_cat_display_name_from_id(
314    $_GET['parent_id'],
[25425]315    $base_url.'&amp;parent_id='
[1064]316    );
[394]317}
[580]318// +-----------------------------------------------------------------------+
319// |                       template initialization                         |
320// +-----------------------------------------------------------------------+
[2530]321$template->set_filename('categories', 'cat_list.tpl');
[394]322
[635]323$form_action = PHPWG_ROOT_PATH.'admin.php?page=cat_list';
324if (isset($_GET['parent_id']))
325{
326  $form_action.= '&amp;parent_id='.$_GET['parent_id'];
327}
[28939]328$sort_orders_checked = array_keys($sort_orders);
[635]329
[2223]330$template->assign(array(
[394]331  'CATEGORIES_NAV'=>$navigation,
[1004]332  'F_ACTION'=>$form_action,
[5195]333  'PWG_TOKEN' => get_pwg_token(),
[28934]334  'sort_orders' => $sort_orders,
[28939]335  'sort_order_checked' => array_shift($sort_orders_checked),
[580]336 ));
[1131]337
[580]338// +-----------------------------------------------------------------------+
339// |                          Categories display                           |
340// +-----------------------------------------------------------------------+
[647]341
[798]342$categories = array();
343
344$query = '
[2324]345SELECT id, name, permalink, dir, rank, status
[798]346  FROM '.CATEGORIES_TABLE;
347if (!isset($_GET['parent_id']))
348{
349  $query.= '
350  WHERE id_uppercat IS NULL';
351}
352else
353{
354  $query.= '
355  WHERE id_uppercat = '.$_GET['parent_id'];
356}
357$query.= '
358  ORDER BY rank ASC
359;';
[2324]360$categories = hash_from_query($query, 'id');
[798]361
[2324]362// get the categories containing images directly
363$categories_with_images = array();
[26399]364if (count($categories))
[580]365{
[647]366  $query = '
[26399]367SELECT
368    category_id,
369    COUNT(*) AS nb_photos
[2324]370  FROM '.IMAGE_CATEGORY_TABLE.'
[26399]371  GROUP BY category_id
372;';
373  // WHERE category_id IN ('.implode(',', array_keys($categories)).')
374
375  $nb_photos_in = query2array($query, 'category_id', 'nb_photos');
376
377  $query = '
378SELECT
379    id,
380    uppercats
381  FROM '.CATEGORIES_TABLE.'
382;';
383  $all_categories = query2array($query, 'id', 'uppercats');
384  $subcats_of = array();
385
386  foreach (array_keys($categories) as $cat_id)
387  {
388    foreach ($all_categories as $id => $uppercats)
389    {
390      if (preg_match('/(^|,)'.$cat_id.',/', $uppercats))
391      {
392        @$subcats_of[$cat_id][] = $id;
393      }
394    }
395  }
396
397  $nb_sub_photos = array();
398  foreach ($subcats_of as $cat_id => $subcat_ids)
399  {
400    $nb_photos = 0;
401    foreach ($subcat_ids as $id)
402    {
403      if (isset($nb_photos_in[$id]))
404      {
405        $nb_photos+= $nb_photos_in[$id];
406      }
407    }
408
409    $nb_sub_photos[$cat_id] = $nb_photos;
410  }
[642]411}
412
[2223]413$template->assign('categories', array());
[2324]414$base_url = get_root_url().'admin.php?page=';
[24985]415
416if (isset($_GET['parent_id']))
417{
418  $template->assign(
419    'PARENT_EDIT',
420    $base_url.'album-'.$_GET['parent_id']
421    );
422}
423
[642]424foreach ($categories as $category)
425{
[580]426  $cat_list_url = $base_url.'cat_list';
[1131]427
[580]428  $self_url = $cat_list_url;
[394]429  if (isset($_GET['parent_id']))
[580]430  {
431    $self_url.= '&amp;parent_id='.$_GET['parent_id'];
432  }
[394]433
[2223]434  $tpl_cat =
[580]435    array(
[2433]436      'NAME'       => 
[28587]437        trigger_change(
[2433]438          'render_category_name',
439          $category['name'],
440          'admin_cat_list'
441          ),
[26399]442      'NB_PHOTOS' => isset($nb_photos_in[$category['id']]) ? $nb_photos_in[$category['id']] : 0,
443      'NB_SUB_PHOTOS' => isset($nb_sub_photos[$category['id']]) ? $nb_sub_photos[$category['id']] : 0,
444      'NB_SUB_ALBUMS' => isset($subcats_of[$category['id']]) ? count($subcats_of[$category['id']]) : 0,
[1082]445      'ID'         => $category['id'],
446      'RANK'       => $category['rank']*10,
[798]447
[1082]448      'U_JUMPTO'   => make_index_url(
449        array(
[1861]450          'category' => $category
[1082]451          )
452        ),
[1131]453
454      'U_CHILDREN' => $cat_list_url.'&amp;parent_id='.$category['id'],
[13013]455      'U_EDIT'     => $base_url.'album-'.$category['id'],
[2306]456
[2223]457      'IS_VIRTUAL' => empty($category['dir'])
[798]458    );
[1131]459
[798]460  if (empty($category['dir']))
[21]461  {
[2223]462    $tpl_cat['U_DELETE'] = $self_url.'&amp;delete='.$category['id'];
[5195]463    $tpl_cat['U_DELETE'].= '&amp;pwg_token='.get_pwg_token();
[21]464  }
[11041]465  else
466  {
467    if ($conf['enable_synchronization'])
468    {
469      $tpl_cat['U_SYNC'] = $base_url.'site_update&amp;site=1&amp;cat_id='.$category['id'];
470    }
471  }
[1131]472
[2223]473  $template->append('categories', $tpl_cat);
[21]474}
[5933]475
[28587]476trigger_notify('loc_end_cat_list');
[5933]477
[580]478// +-----------------------------------------------------------------------+
479// |                          sending html code                            |
480// +-----------------------------------------------------------------------+
[394]481$template->assign_var_from_handle('ADMIN_CONTENT', 'categories');
[362]482?>
Note: See TracBrowser for help on using the repository browser.