source: trunk/admin/batch_manager.php @ 23428

Last change on this file since 23428 was 19920, checked in by mistic100, 11 years ago

feature:2718 errors when no photo on the gallery, use arbitrary values instead

File size: 15.4 KB
RevLine 
[8394]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[8394]4// +-----------------------------------------------------------------------+
[19703]5// | Copyright(C) 2008-2013 Piwigo Team                  http://piwigo.org |
[8394]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
24/**
25 * Management of elements set. Elements can belong to a category or to the
26 * user caddie.
27 *
28 */
29
30if (!defined('PHPWG_ROOT_PATH'))
31{
32  die('Hacking attempt!');
33}
34
35include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
[8413]36include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
[8394]37
38// +-----------------------------------------------------------------------+
39// | Check Access and exit when user status is not ok                      |
40// +-----------------------------------------------------------------------+
41
42check_status(ACCESS_ADMINISTRATOR);
43
44check_input_parameter('selection', $_POST, true, PATTERN_ID);
45
46// +-----------------------------------------------------------------------+
47// |                      initialize current set                           |
48// +-----------------------------------------------------------------------+
49
50if (isset($_POST['submitFilter']))
51{
52  // echo '<pre>'; print_r($_POST); echo '</pre>';
[17289]53  unset($_REQUEST['start']); // new photo set must reset the page
[8394]54  $_SESSION['bulk_manager_filter'] = array();
55
56  if (isset($_POST['filter_prefilter_use']))
57  {
[10354]58    $_SESSION['bulk_manager_filter']['prefilter'] = $_POST['filter_prefilter'];
[8394]59  }
60
61  if (isset($_POST['filter_category_use']))
62  {
63    $_SESSION['bulk_manager_filter']['category'] = $_POST['filter_category'];
64
65    if (isset($_POST['filter_category_recursive']))
66    {
67      $_SESSION['bulk_manager_filter']['category_recursive'] = true;
68    }
69  }
70
[11853]71  if (isset($_POST['filter_tags_use']))
72  {
73    $_SESSION['bulk_manager_filter']['tags'] = get_tag_ids($_POST['filter_tags'], false);
[12630]74
75    if (isset($_POST['tag_mode']) and in_array($_POST['tag_mode'], array('AND', 'OR')))
76    {
77      $_SESSION['bulk_manager_filter']['tag_mode'] = $_POST['tag_mode'];
78    }
[11853]79  }
80
[8394]81  if (isset($_POST['filter_level_use']))
82  {
83    if (in_array($_POST['filter_level'], $conf['available_permission_levels']))
84    {
85      $_SESSION['bulk_manager_filter']['level'] = $_POST['filter_level'];
[13646]86     
87      if (isset($_POST['filter_level_include_lower']))
88      {
89        $_SESSION['bulk_manager_filter']['level_include_lower'] = true;
90      }
[8394]91    }
92  }
[17931]93 
94  if (isset($_POST['filter_dimension_use']))
95  {
[18758]96    foreach (array('min_width','max_width','min_height','max_height') as $type)
[17931]97    {
[18758]98      if ( preg_match('#^[0-9]+$#', $_POST['filter_dimension_'. $type ]) )
99      {
100        $_SESSION['bulk_manager_filter']['dimension'][$type] = $_POST['filter_dimension_'. $type ];
101      }
[17931]102    }
[18988]103    foreach (array('min_ratio','max_ratio') as $type)
[17931]104    {
[18988]105      if ( preg_match('#^[0-9\.]+$#', $_POST['filter_dimension_'. $type ]) )
[18758]106      {
[18988]107        $_SESSION['bulk_manager_filter']['dimension'][$type] = $_POST['filter_dimension_'. $type ];
[18758]108      }
[17931]109    }
110  }
[8394]111}
[18459]112else if (isset($_GET['cat']))
[8394]113{
114  if ('caddie' == $_GET['cat'])
115  {
116    $_SESSION['bulk_manager_filter'] = array(
117      'prefilter' => 'caddie'
118      );
119  }
[18459]120  else if ('recent' == $_GET['cat'])
[8423]121  {
122    $_SESSION['bulk_manager_filter'] = array(
123      'prefilter' => 'last import'
124      );
125  }
[18459]126  else if (is_numeric($_GET['cat']))
[8394]127  {
128    $_SESSION['bulk_manager_filter'] = array(
129      'category' => $_GET['cat']
130      );
131  }
[18459]132}
133else if (isset($_GET['tag']))
134{
135  if (is_numeric($_GET['tag']))
[17765]136  {
[18459]137    $_SESSION['bulk_manager_filter'] = array(
138      'tags' => array($_GET['tag']),
139      'tag_mode' => 'AND',
140      );
[17765]141  }
[8394]142}
143
144if (!isset($_SESSION['bulk_manager_filter']))
145{
146  $_SESSION['bulk_manager_filter'] = array(
147    'prefilter' => 'caddie'
148    );
149}
150
151// echo '<pre>'; print_r($_SESSION['bulk_manager_filter']); echo '</pre>';
152
153// depending on the current filter (in session), we find the appropriate
154// photos
155$filter_sets = array();
156if (isset($_SESSION['bulk_manager_filter']['prefilter']))
157{
158  if ('caddie' == $_SESSION['bulk_manager_filter']['prefilter'])
159  {
160    $query = '
161SELECT element_id
162  FROM '.CADDIE_TABLE.'
163  WHERE user_id = '.$user['id'].'
164;';
165    array_push(
166      $filter_sets,
167      array_from_query($query, 'element_id')
168      );
169  }
170
171  if ('last import'== $_SESSION['bulk_manager_filter']['prefilter'])
172  {
173    $query = '
174SELECT MAX(date_available) AS date
175  FROM '.IMAGES_TABLE.'
176;';
177    $row = pwg_db_fetch_assoc(pwg_query($query));
178    if (!empty($row['date']))
179    {
180      $query = '
181SELECT id
182  FROM '.IMAGES_TABLE.'
183  WHERE date_available BETWEEN '.pwg_db_get_recent_period_expression(1, $row['date']).' AND \''.$row['date'].'\'
184;';
185      array_push(
186        $filter_sets,
187        array_from_query($query, 'id')
188        );
189    }
190  }
[8403]191
192  if ('with no virtual album' == $_SESSION['bulk_manager_filter']['prefilter'])
193  {
194    // we are searching elements not linked to any virtual category
195    $query = '
196 SELECT id
197   FROM '.IMAGES_TABLE.'
198 ;';
199    $all_elements = array_from_query($query, 'id');
[9068]200
[8403]201    $query = '
202 SELECT id
203   FROM '.CATEGORIES_TABLE.'
204   WHERE dir IS NULL
205 ;';
206    $virtual_categories = array_from_query($query, 'id');
207    if (!empty($virtual_categories))
208    {
209      $query = '
210 SELECT DISTINCT(image_id)
211   FROM '.IMAGE_CATEGORY_TABLE.'
212   WHERE category_id IN ('.implode(',', $virtual_categories).')
213 ;';
214      $linked_to_virtual = array_from_query($query, 'image_id');
215    }
216
217    array_push(
218      $filter_sets,
219      array_diff($all_elements, $linked_to_virtual)
220      );
221  }
[8404]222
[8419]223  if ('with no album' == $_SESSION['bulk_manager_filter']['prefilter'])
224  {
225    $query = '
226SELECT
227    id
228  FROM '.IMAGES_TABLE.'
229    LEFT JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
230  WHERE category_id is null
231;';
232    array_push(
233      $filter_sets,
234      array_from_query($query, 'id')
235      );
236  }
237
[8422]238  if ('with no tag' == $_SESSION['bulk_manager_filter']['prefilter'])
239  {
240    $query = '
241SELECT
242    id
243  FROM '.IMAGES_TABLE.'
244    LEFT JOIN '.IMAGE_TAG_TABLE.' ON id = image_id
245  WHERE tag_id is null
246;';
247    array_push(
248      $filter_sets,
249      array_from_query($query, 'id')
250      );
251  }
252
253
[8404]254  if ('duplicates' == $_SESSION['bulk_manager_filter']['prefilter'])
255  {
256    // we could use the group_concat MySQL function to retrieve the list of
257    // image_ids but it would not be compatible with PostgreSQL, so let's
258    // perform 2 queries instead. We hope there are not too many duplicates.
[9068]259
[8404]260    $query = '
261SELECT file
262  FROM '.IMAGES_TABLE.'
263  GROUP BY file
264  HAVING COUNT(*) > 1
265;';
266    $duplicate_files = array_from_query($query, 'file');
[9068]267
[8404]268    $query = '
269SELECT id
270  FROM '.IMAGES_TABLE.'
271  WHERE file IN (\''.implode("','", $duplicate_files).'\')
272;';
273
274    array_push(
275      $filter_sets,
276      array_from_query($query, 'id')
277      );
278  }
[9912]279
280  if ('all photos' == $_SESSION['bulk_manager_filter']['prefilter'])
281  {
282    $query = '
283SELECT id
284  FROM '.IMAGES_TABLE.'
[14689]285  '.$conf['order_by'];
[9912]286
[14689]287    $filter_sets[] = array_from_query($query, 'id');
[9912]288  }
[10354]289
[10380]290  $filter_sets = trigger_event('perform_batch_manager_prefilters', $filter_sets, $_SESSION['bulk_manager_filter']['prefilter']);
[8394]291}
292
293if (isset($_SESSION['bulk_manager_filter']['category']))
294{
295  $categories = array();
[9068]296
[8394]297  if (isset($_SESSION['bulk_manager_filter']['category_recursive']))
298  {
299    $categories = get_subcat_ids(array($_SESSION['bulk_manager_filter']['category']));
300  }
301  else
302  {
303    $categories = array($_SESSION['bulk_manager_filter']['category']);
304  }
[9068]305
[8394]306  $query = '
307 SELECT DISTINCT(image_id)
308   FROM '.IMAGE_CATEGORY_TABLE.'
309   WHERE category_id IN ('.implode(',', $categories).')
310 ;';
311  array_push(
312    $filter_sets,
313    array_from_query($query, 'image_id')
314    );
315}
316
317if (isset($_SESSION['bulk_manager_filter']['level']))
318{
[13646]319  $operator = '=';
320  if (isset($_SESSION['bulk_manager_filter']['level_include_lower']))
321  {
322    $operator = '<=';
323  }
324 
[8394]325  $query = '
326SELECT id
327  FROM '.IMAGES_TABLE.'
[13646]328  WHERE level '.$operator.' '.$_SESSION['bulk_manager_filter']['level'].'
[14689]329  '.$conf['order_by'];
330
331  $filter_sets[] = array_from_query($query, 'id');
[8394]332}
333
[11853]334if (!empty($_SESSION['bulk_manager_filter']['tags']))
335{
[12630]336  array_push(
[11853]337    $filter_sets,
[12630]338    get_image_ids_for_tags(
339      $_SESSION['bulk_manager_filter']['tags'],
340      $_SESSION['bulk_manager_filter']['tag_mode'],
341      null,
342      null,
343      false // we don't apply permissions in administration screens
344      )
345    );
[11853]346}
347
[18988]348if (isset($_SESSION['bulk_manager_filter']['dimension']))
[17931]349{
[18758]350  $where_clauses = array();
351  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_width']))
[17931]352  {
[18758]353    $where_clause[] = 'width >= '.$_SESSION['bulk_manager_filter']['dimension']['min_width'];
[17931]354  }
[18758]355  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_width']))
356  {
357    $where_clause[] = 'width <= '.$_SESSION['bulk_manager_filter']['dimension']['max_width'];
358  }
359  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_height']))
360  {
361    $where_clause[] = 'height >= '.$_SESSION['bulk_manager_filter']['dimension']['min_height'];
362  }
363  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_height']))
364  {
365    $where_clause[] = 'height <= '.$_SESSION['bulk_manager_filter']['dimension']['max_height'];
366  }
[18988]367  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_ratio']))
[18758]368  {
[18988]369    $where_clause[] = 'width/height >= '.$_SESSION['bulk_manager_filter']['dimension']['min_ratio'];
[18758]370  }
[18988]371  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_ratio']))
[18758]372  {
[19121]373    // max_ratio is a floor value, so must be a bit increased
374    $where_clause[] = 'width/height < '.($_SESSION['bulk_manager_filter']['dimension']['max_ratio']+0.01);
[18758]375  }
[17931]376 
377  $query = '
378SELECT id
379  FROM '.IMAGES_TABLE.'
[18758]380  WHERE '.implode(' AND ',$where_clause).'
[17931]381  '.$conf['order_by'];
382
383  $filter_sets[] = array_from_query($query, 'id');
384}
385
[8394]386$current_set = array_shift($filter_sets);
387foreach ($filter_sets as $set)
388{
389  $current_set = array_intersect($current_set, $set);
390}
391$page['cat_elements_id'] = $current_set;
392
393// +-----------------------------------------------------------------------+
394// |                       first element to display                        |
395// +-----------------------------------------------------------------------+
396
397// $page['start'] contains the number of the first element in its
398// category. For exampe, $page['start'] = 12 means we must show elements #12
399// and $page['nb_images'] next elements
400
[17289]401if (!isset($_REQUEST['start'])
402    or !is_numeric($_REQUEST['start'])
403    or $_REQUEST['start'] < 0
404    or (isset($_REQUEST['display']) and 'all' == $_REQUEST['display']))
[8394]405{
406  $page['start'] = 0;
407}
408else
409{
[17289]410  $page['start'] = $_REQUEST['start'];
[8394]411}
412
413// +-----------------------------------------------------------------------+
[8413]414// |                                 Tabs                                  |
[8394]415// +-----------------------------------------------------------------------+
[16928]416$manager_link = get_root_url().'admin.php?page=batch_manager&amp;mode=';
[8394]417
[9068]418if (isset($_GET['mode']))
[8394]419{
[8413]420  $page['tab'] = $_GET['mode'];
[8394]421}
[8413]422else
423{
[16928]424  $page['tab'] = 'global';
[8413]425}
426
[16928]427$tabsheet = new tabsheet();
428$tabsheet->set_id('batch_manager');
429$tabsheet->select($page['tab']);
430$tabsheet->assign();
[8413]431
432// +-----------------------------------------------------------------------+
[11039]433// |                              tags                                     |
434// +-----------------------------------------------------------------------+
435
436$query = '
[11853]437SELECT id, name
[11039]438  FROM '.TAGS_TABLE.'
439;';
[12259]440$template->assign('tags', get_taglist($query, false));
[11039]441
442// +-----------------------------------------------------------------------+
[18988]443// |                              dimensions                               |
444// +-----------------------------------------------------------------------+
[19069]445
446$widths = array();
447$heights = array();
448$ratios = array();
449
[19121]450// get all width, height and ratios
[18988]451$query = '
452SELECT
[19069]453  DISTINCT width, height
[18988]454  FROM '.IMAGES_TABLE.'
[19069]455  WHERE width IS NOT NULL
456    AND height IS NOT NULL
[18988]457;';
[19069]458$result = pwg_query($query);
[19121]459
[19920]460if (!pwg_db_num_rows($result))
461{ // arbitrary values, only used when no photos on the gallery
462  $widths = array(600, 1920, 3500);
463  $heights = array(480, 1080, 2300);
464  $ratios = array(1.25, 1.52, 1.78);
465}
466else
[19069]467{
[19920]468  while ($row = pwg_db_fetch_assoc($result))
469  {
470    $widths[] = $row['width'];
471    $heights[] = $row['height'];
472    $ratios[] = floor($row['width'] * 100 / $row['height']) / 100;
473  }
[19069]474}
475
[19920]476
[19069]477$widths = array_unique($widths);
478sort($widths);
479
480$heights = array_unique($heights);
481sort($heights);
482
483$ratios = array_unique($ratios);
484sort($ratios);
485
486$dimensions['widths'] = implode(',', $widths);
487$dimensions['heights'] = implode(',', $heights);
488$dimensions['ratios'] = implode(',', $ratios);
489
490$dimensions['bounds'] = array(
491  'min_width' => $widths[0],
492  'max_width' => $widths[count($widths)-1],
493  'min_height' => $heights[0],
494  'max_height' => $heights[count($heights)-1],
495  'min_ratio' => $ratios[0],
496  'max_ratio' => $ratios[count($ratios)-1],
497  );
498
[19121]499// find ratio categories
[19069]500$ratio_categories = array(
501  'portrait' => array(),
502  'square' => array(),
503  'landscape' => array(),
504  'panorama' => array(),
505  );
506
507foreach ($ratios as $ratio)
508{
509  if ($ratio < 0.95)
510  {
511    $ratio_categories['portrait'][] = $ratio;
512  }
[19121]513  else if ($ratio >= 0.95 and $ratio <= 1.05)
[19069]514  {
515    $ratio_categories['square'][] = $ratio;
516  }
[19121]517  else if ($ratio > 1.05 and $ratio < 2)
[19069]518  {
519    $ratio_categories['landscape'][] = $ratio;
520  }
[19121]521  else if ($ratio >= 2)
[19069]522  {
523    $ratio_categories['panorama'][] = $ratio;
524  }
525}
526
527foreach (array_keys($ratio_categories) as $ratio_category)
528{
529  if (count($ratio_categories[$ratio_category]) > 0)
530  {
531    $dimensions['ratio_'.$ratio_category] = array(
532      'min' => $ratio_categories[$ratio_category][0],
533      'max' => $ratio_categories[$ratio_category][count($ratio_categories[$ratio_category]) - 1]
534      );
535  }
536}
537
[19121]538// selected=bound if nothing selected
[18988]539foreach (array_keys($dimensions['bounds']) as $type)
540{
[19069]541  $dimensions['selected'][$type] = isset($_SESSION['bulk_manager_filter']['dimension'][$type])
542    ? $_SESSION['bulk_manager_filter']['dimension'][$type]
543    : $dimensions['bounds'][$type]
544  ;
[18988]545}
[19069]546
[18988]547$template->assign('dimensions', $dimensions);
548
549
550// +-----------------------------------------------------------------------+
[8413]551// |                         open specific mode                            |
552// +-----------------------------------------------------------------------+
553
[16928]554include(PHPWG_ROOT_PATH.'admin/batch_manager_'.$page['tab'].'.php');
[9068]555?>
Note: See TracBrowser for help on using the repository browser.