source: trunk/admin/batch_manager.php @ 19121

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

feature:2718 Add batch manager filters for photo dimensions, small cleaning in the code

File size: 15.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 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
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');
36include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
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>';
53  unset($_REQUEST['start']); // new photo set must reset the page
54  $_SESSION['bulk_manager_filter'] = array();
55
56  if (isset($_POST['filter_prefilter_use']))
57  {
58    $_SESSION['bulk_manager_filter']['prefilter'] = $_POST['filter_prefilter'];
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
71  if (isset($_POST['filter_tags_use']))
72  {
73    $_SESSION['bulk_manager_filter']['tags'] = get_tag_ids($_POST['filter_tags'], false);
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    }
79  }
80
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'];
86     
87      if (isset($_POST['filter_level_include_lower']))
88      {
89        $_SESSION['bulk_manager_filter']['level_include_lower'] = true;
90      }
91    }
92  }
93 
94  if (isset($_POST['filter_dimension_use']))
95  {
96    foreach (array('min_width','max_width','min_height','max_height') as $type)
97    {
98      if ( preg_match('#^[0-9]+$#', $_POST['filter_dimension_'. $type ]) )
99      {
100        $_SESSION['bulk_manager_filter']['dimension'][$type] = $_POST['filter_dimension_'. $type ];
101      }
102    }
103    foreach (array('min_ratio','max_ratio') as $type)
104    {
105      if ( preg_match('#^[0-9\.]+$#', $_POST['filter_dimension_'. $type ]) )
106      {
107        $_SESSION['bulk_manager_filter']['dimension'][$type] = $_POST['filter_dimension_'. $type ];
108      }
109    }
110  }
111}
112else if (isset($_GET['cat']))
113{
114  if ('caddie' == $_GET['cat'])
115  {
116    $_SESSION['bulk_manager_filter'] = array(
117      'prefilter' => 'caddie'
118      );
119  }
120  else if ('recent' == $_GET['cat'])
121  {
122    $_SESSION['bulk_manager_filter'] = array(
123      'prefilter' => 'last import'
124      );
125  }
126  else if (is_numeric($_GET['cat']))
127  {
128    $_SESSION['bulk_manager_filter'] = array(
129      'category' => $_GET['cat']
130      );
131  }
132}
133else if (isset($_GET['tag']))
134{
135  if (is_numeric($_GET['tag']))
136  {
137    $_SESSION['bulk_manager_filter'] = array(
138      'tags' => array($_GET['tag']),
139      'tag_mode' => 'AND',
140      );
141  }
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  }
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');
200
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  }
222
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
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
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.
259
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');
267
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  }
279
280  if ('all photos' == $_SESSION['bulk_manager_filter']['prefilter'])
281  {
282    $query = '
283SELECT id
284  FROM '.IMAGES_TABLE.'
285  '.$conf['order_by'];
286
287    $filter_sets[] = array_from_query($query, 'id');
288  }
289
290  $filter_sets = trigger_event('perform_batch_manager_prefilters', $filter_sets, $_SESSION['bulk_manager_filter']['prefilter']);
291}
292
293if (isset($_SESSION['bulk_manager_filter']['category']))
294{
295  $categories = array();
296
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  }
305
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{
319  $operator = '=';
320  if (isset($_SESSION['bulk_manager_filter']['level_include_lower']))
321  {
322    $operator = '<=';
323  }
324 
325  $query = '
326SELECT id
327  FROM '.IMAGES_TABLE.'
328  WHERE level '.$operator.' '.$_SESSION['bulk_manager_filter']['level'].'
329  '.$conf['order_by'];
330
331  $filter_sets[] = array_from_query($query, 'id');
332}
333
334if (!empty($_SESSION['bulk_manager_filter']['tags']))
335{
336  array_push(
337    $filter_sets,
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    );
346}
347
348if (isset($_SESSION['bulk_manager_filter']['dimension']))
349{
350  $where_clauses = array();
351  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_width']))
352  {
353    $where_clause[] = 'width >= '.$_SESSION['bulk_manager_filter']['dimension']['min_width'];
354  }
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  }
367  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_ratio']))
368  {
369    $where_clause[] = 'width/height >= '.$_SESSION['bulk_manager_filter']['dimension']['min_ratio'];
370  }
371  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_ratio']))
372  {
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);
375  }
376 
377  $query = '
378SELECT id
379  FROM '.IMAGES_TABLE.'
380  WHERE '.implode(' AND ',$where_clause).'
381  '.$conf['order_by'];
382
383  $filter_sets[] = array_from_query($query, 'id');
384}
385
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
401if (!isset($_REQUEST['start'])
402    or !is_numeric($_REQUEST['start'])
403    or $_REQUEST['start'] < 0
404    or (isset($_REQUEST['display']) and 'all' == $_REQUEST['display']))
405{
406  $page['start'] = 0;
407}
408else
409{
410  $page['start'] = $_REQUEST['start'];
411}
412
413// +-----------------------------------------------------------------------+
414// |                                 Tabs                                  |
415// +-----------------------------------------------------------------------+
416$manager_link = get_root_url().'admin.php?page=batch_manager&amp;mode=';
417
418if (isset($_GET['mode']))
419{
420  $page['tab'] = $_GET['mode'];
421}
422else
423{
424  $page['tab'] = 'global';
425}
426
427$tabsheet = new tabsheet();
428$tabsheet->set_id('batch_manager');
429$tabsheet->select($page['tab']);
430$tabsheet->assign();
431
432// +-----------------------------------------------------------------------+
433// |                              tags                                     |
434// +-----------------------------------------------------------------------+
435
436$query = '
437SELECT id, name
438  FROM '.TAGS_TABLE.'
439;';
440$template->assign('tags', get_taglist($query, false));
441
442// +-----------------------------------------------------------------------+
443// |                              dimensions                               |
444// +-----------------------------------------------------------------------+
445
446$widths = array();
447$heights = array();
448$ratios = array();
449
450// get all width, height and ratios
451$query = '
452SELECT
453  DISTINCT width, height
454  FROM '.IMAGES_TABLE.'
455  WHERE width IS NOT NULL
456    AND height IS NOT NULL
457;';
458$result = pwg_query($query);
459
460while ($row = pwg_db_fetch_assoc($result))
461{
462  $widths[] = $row['width'];
463  $heights[] = $row['height'];
464  $ratios[] = floor($row['width'] * 100 / $row['height']) / 100;
465}
466
467$widths = array_unique($widths);
468sort($widths);
469
470$heights = array_unique($heights);
471sort($heights);
472
473$ratios = array_unique($ratios);
474sort($ratios);
475
476$dimensions['widths'] = implode(',', $widths);
477$dimensions['heights'] = implode(',', $heights);
478$dimensions['ratios'] = implode(',', $ratios);
479
480$dimensions['bounds'] = array(
481  'min_width' => $widths[0],
482  'max_width' => $widths[count($widths)-1],
483  'min_height' => $heights[0],
484  'max_height' => $heights[count($heights)-1],
485  'min_ratio' => $ratios[0],
486  'max_ratio' => $ratios[count($ratios)-1],
487  );
488
489// find ratio categories
490$ratio_categories = array(
491  'portrait' => array(),
492  'square' => array(),
493  'landscape' => array(),
494  'panorama' => array(),
495  );
496
497foreach ($ratios as $ratio)
498{
499  if ($ratio < 0.95)
500  {
501    $ratio_categories['portrait'][] = $ratio;
502  }
503  else if ($ratio >= 0.95 and $ratio <= 1.05)
504  {
505    $ratio_categories['square'][] = $ratio;
506  }
507  else if ($ratio > 1.05 and $ratio < 2)
508  {
509    $ratio_categories['landscape'][] = $ratio;
510  }
511  else if ($ratio >= 2)
512  {
513    $ratio_categories['panorama'][] = $ratio;
514  }
515}
516
517foreach (array_keys($ratio_categories) as $ratio_category)
518{
519  if (count($ratio_categories[$ratio_category]) > 0)
520  {
521    $dimensions['ratio_'.$ratio_category] = array(
522      'min' => $ratio_categories[$ratio_category][0],
523      'max' => $ratio_categories[$ratio_category][count($ratio_categories[$ratio_category]) - 1]
524      );
525  }
526}
527
528// selected=bound if nothing selected
529foreach (array_keys($dimensions['bounds']) as $type)
530{
531  $dimensions['selected'][$type] = isset($_SESSION['bulk_manager_filter']['dimension'][$type])
532    ? $_SESSION['bulk_manager_filter']['dimension'][$type]
533    : $dimensions['bounds'][$type]
534  ;
535}
536
537$template->assign('dimensions', $dimensions);
538
539
540// +-----------------------------------------------------------------------+
541// |                         open specific mode                            |
542// +-----------------------------------------------------------------------+
543
544include(PHPWG_ROOT_PATH.'admin/batch_manager_'.$page['tab'].'.php');
545?>
Note: See TracBrowser for help on using the repository browser.