source: trunk/admin/cat_list.php @ 29239

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

strict standards

  • Property svn:eol-style set to LF
File size: 13.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 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_notify('loc_begin_cat_list');
37
38if (!empty($_POST) or isset($_GET['delete']))
39{
40  check_pwg_token();
41}
42
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
52// +-----------------------------------------------------------------------+
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{
67  $current_rank_for_id_uppercat = array();
68  $current_rank = 0;
69 
70  $datas = array();
71  foreach ($categories as $category)
72  {
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   
90    $datas[] = array('id' => $id, 'rank' => $current_rank);
91  }
92  $fields = array('primary' => array('id'), 'update' => array('rank'));
93  mass_updates(CATEGORIES_TABLE, $fields, $datas);
94
95  update_global_rank();
96}
97
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
169// +-----------------------------------------------------------------------+
170// |                            initialization                             |
171// +-----------------------------------------------------------------------+
172
173check_input_parameter('parent_id', $_GET, false, PATTERN_ID);
174
175$categories = array();
176
177$base_url = get_root_url().'admin.php?page=cat_list';
178$navigation = '<a href="'.$base_url.'">';
179$navigation.= l10n('Home');
180$navigation.= '</a>';
181
182// +-----------------------------------------------------------------------+
183// | tabs                                                                  |
184// +-----------------------------------------------------------------------+
185
186$page['tab'] = 'list';
187include(PHPWG_ROOT_PATH.'admin/include/albums_tab.inc.php');
188
189// +-----------------------------------------------------------------------+
190// |                    virtual categories management                      |
191// +-----------------------------------------------------------------------+
192// request to delete a virtual category
193if (isset($_GET['delete']) and is_numeric($_GET['delete']))
194{
195  delete_categories(array($_GET['delete']));
196  $_SESSION['page_infos'] = array(l10n('Virtual album deleted'));
197  update_global_rank();
198  invalidate_user_cache();
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'];
204  }
205  redirect($redirect_url);
206}
207// request to add a virtual category
208elseif (isset($_POST['submitAdd']))
209{
210  $output_create = create_virtual_category(
211    $_POST['virtual_name'],
212    @$_GET['parent_id']
213    );
214
215  invalidate_user_cache();
216  if (isset($output_create['error']))
217  {
218    $page['errors'][] = $output_create['error'];
219  }
220  else
221  {
222    $page['infos'][] = $output_create['info'];
223  }
224}
225// save manual category ordering
226elseif (isset($_POST['submitManualOrder']))
227{
228  asort($_POST['catOrd'], SORT_NUMERIC);
229  save_categories_order(array_keys($_POST['catOrd']));
230
231  $page['infos'][] = l10n('Album manual order was saved');
232}
233elseif (isset($_POST['submitAutoOrder']))
234{
235  if (!isset($sort_orders[ $_POST['order_by'] ]))
236  {
237    die('Invalid sort order');
238  }
239
240  $query = '
241SELECT id
242  FROM '.CATEGORIES_TABLE.'
243  WHERE id_uppercat '.
244    (!isset($_GET['parent_id']) ? 'IS NULL' : '= '.$_GET['parent_id']).'
245;';
246  $category_ids = array_from_query($query, 'id');
247
248  if (isset($_POST['recursive']))
249  {
250    $category_ids = get_subcat_ids($category_ids);
251  }
252 
253  $categories = array();
254  $sort = array();
255
256  list($order_by_field, $order_by_asc) = explode(' ', $_POST['order_by']);
257 
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
270  $query = '
271SELECT id, name, id_uppercat
272  FROM '.CATEGORIES_TABLE.'
273  WHERE id IN ('.implode(',', $category_ids).')
274;';
275  $result = pwg_query($query);
276  while ($row = pwg_db_fetch_assoc($result))
277  {
278    if ($order_by_date)
279    {
280      $sort[] = $ref_dates[ $row['id'] ];
281    }
282    else
283    {
284      $sort[] = $row['name'];
285    }
286   
287    $categories[] = array(
288      'id' => $row['id'],
289      'id_uppercat' => $row['id_uppercat'],
290      );
291  }
292
293  array_multisort(
294    $sort,
295    SORT_REGULAR,
296    'ASC' == $order_by_asc ? SORT_ASC : SORT_DESC,
297    $categories
298    );
299 
300  save_categories_order($categories);
301
302  $page['infos'][] = l10n('Albums automatically sorted');
303}
304
305// +-----------------------------------------------------------------------+
306// |                            Navigation path                            |
307// +-----------------------------------------------------------------------+
308
309if (isset($_GET['parent_id']))
310{
311  $navigation.= $conf['level_separator'];
312
313  $navigation.= get_cat_display_name_from_id(
314    $_GET['parent_id'],
315    $base_url.'&amp;parent_id='
316    );
317}
318// +-----------------------------------------------------------------------+
319// |                       template initialization                         |
320// +-----------------------------------------------------------------------+
321$template->set_filename('categories', 'cat_list.tpl');
322
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}
328$sort_orders_checked = array_keys($sort_orders);
329
330$template->assign(array(
331  'CATEGORIES_NAV'=>$navigation,
332  'F_ACTION'=>$form_action,
333  'PWG_TOKEN' => get_pwg_token(),
334  'sort_orders' => $sort_orders,
335  'sort_order_checked' => array_shift($sort_orders_checked),
336 ));
337
338// +-----------------------------------------------------------------------+
339// |                          Categories display                           |
340// +-----------------------------------------------------------------------+
341
342$categories = array();
343
344$query = '
345SELECT id, name, permalink, dir, rank, status
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;';
360$categories = hash_from_query($query, 'id');
361
362// get the categories containing images directly
363$categories_with_images = array();
364if (count($categories))
365{
366  $query = '
367SELECT
368    category_id,
369    COUNT(*) AS nb_photos
370  FROM '.IMAGE_CATEGORY_TABLE.'
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  }
411}
412
413$template->assign('categories', array());
414$base_url = get_root_url().'admin.php?page=';
415
416if (isset($_GET['parent_id']))
417{
418  $template->assign(
419    'PARENT_EDIT',
420    $base_url.'album-'.$_GET['parent_id']
421    );
422}
423
424foreach ($categories as $category)
425{
426  $cat_list_url = $base_url.'cat_list';
427
428  $self_url = $cat_list_url;
429  if (isset($_GET['parent_id']))
430  {
431    $self_url.= '&amp;parent_id='.$_GET['parent_id'];
432  }
433
434  $tpl_cat =
435    array(
436      'NAME'       => 
437        trigger_change(
438          'render_category_name',
439          $category['name'],
440          'admin_cat_list'
441          ),
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,
445      'ID'         => $category['id'],
446      'RANK'       => $category['rank']*10,
447
448      'U_JUMPTO'   => make_index_url(
449        array(
450          'category' => $category
451          )
452        ),
453
454      'U_CHILDREN' => $cat_list_url.'&amp;parent_id='.$category['id'],
455      'U_EDIT'     => $base_url.'album-'.$category['id'],
456
457      'IS_VIRTUAL' => empty($category['dir'])
458    );
459
460  if (empty($category['dir']))
461  {
462    $tpl_cat['U_DELETE'] = $self_url.'&amp;delete='.$category['id'];
463    $tpl_cat['U_DELETE'].= '&amp;pwg_token='.get_pwg_token();
464  }
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  }
472
473  $template->append('categories', $tpl_cat);
474}
475
476trigger_notify('loc_end_cat_list');
477
478// +-----------------------------------------------------------------------+
479// |                          sending html code                            |
480// +-----------------------------------------------------------------------+
481$template->assign_var_from_handle('ADMIN_CONTENT', 'categories');
482?>
Note: See TracBrowser for help on using the repository browser.