source: trunk/admin/batch_manager.php @ 29377

Last change on this file since 29377 was 29377, checked in by plg, 10 years ago

feature 3130: new triggers batch_manager_register_filters and
batch_manager_perform_filters to simplify adding filters in
Batch Manager with a plugin.

File size: 18.9 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
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// | specific actions                                                      |
48// +-----------------------------------------------------------------------+
49
50if (isset($_GET['action']))
51{
52  if ('empty_caddie' == $_GET['action'])
53  {
54    $query = '
55DELETE FROM '.CADDIE_TABLE.'
56  WHERE user_id = '.$user['id'].'
57;';
58    pwg_query($query);
59
60    $_SESSION['page_infos'] = array(
61      l10n('Information data registered in database')
62      );
63   
64    redirect(get_root_url().'admin.php?page='.$_GET['page']);
65  }
66}
67
68// +-----------------------------------------------------------------------+
69// |                      initialize current set                           |
70// +-----------------------------------------------------------------------+
71
72// filters from form
73if (isset($_POST['submitFilter']))
74{
75  // echo '<pre>'; print_r($_POST); echo '</pre>';
76  unset($_REQUEST['start']); // new photo set must reset the page
77  $_SESSION['bulk_manager_filter'] = array();
78
79  if (isset($_POST['filter_prefilter_use']))
80  {
81    $_SESSION['bulk_manager_filter']['prefilter'] = $_POST['filter_prefilter'];
82
83    if ('duplicates' == $_POST['filter_prefilter'])
84    {
85      if (isset($_POST['filter_duplicates_date']))
86      {
87        $_SESSION['bulk_manager_filter']['duplicates_date'] = true;
88      }
89     
90      if (isset($_POST['filter_duplicates_dimensions']))
91      {
92        $_SESSION['bulk_manager_filter']['duplicates_dimensions'] = true;
93      }
94    }
95  }
96
97  if (isset($_POST['filter_category_use']))
98  {
99    $_SESSION['bulk_manager_filter']['category'] = $_POST['filter_category'];
100
101    if (isset($_POST['filter_category_recursive']))
102    {
103      $_SESSION['bulk_manager_filter']['category_recursive'] = true;
104    }
105  }
106
107  if (isset($_POST['filter_tags_use']))
108  {
109    $_SESSION['bulk_manager_filter']['tags'] = get_tag_ids($_POST['filter_tags'], false);
110
111    if (isset($_POST['tag_mode']) and in_array($_POST['tag_mode'], array('AND', 'OR')))
112    {
113      $_SESSION['bulk_manager_filter']['tag_mode'] = $_POST['tag_mode'];
114    }
115  }
116
117  if (isset($_POST['filter_level_use']))
118  {
119    if (in_array($_POST['filter_level'], $conf['available_permission_levels']))
120    {
121      $_SESSION['bulk_manager_filter']['level'] = $_POST['filter_level'];
122
123      if (isset($_POST['filter_level_include_lower']))
124      {
125        $_SESSION['bulk_manager_filter']['level_include_lower'] = true;
126      }
127    }
128  }
129
130  if (isset($_POST['filter_dimension_use']))
131  {
132    foreach (array('min_width','max_width','min_height','max_height') as $type)
133    {
134      if (filter_var($_POST['filter_dimension_'.$type], FILTER_VALIDATE_INT) !== false)
135      {
136        $_SESSION['bulk_manager_filter']['dimension'][$type] = $_POST['filter_dimension_'. $type ];
137      }
138    }
139    foreach (array('min_ratio','max_ratio') as $type)
140    {
141      if (filter_var($_POST['filter_dimension_'.$type], FILTER_VALIDATE_FLOAT) !== false)
142      {
143        $_SESSION['bulk_manager_filter']['dimension'][$type] = $_POST['filter_dimension_'. $type ];
144      }
145    }
146  }
147
148  if (isset($_POST['filter_filesize_use']))
149  {
150    foreach (array('min','max') as $type)
151    {
152      if (filter_var($_POST['filter_filesize_'.$type], FILTER_VALIDATE_FLOAT) !== false)
153      {
154        $_SESSION['bulk_manager_filter']['filesize'][$type] = $_POST['filter_filesize_'. $type ];
155      }
156    }
157  }
158
159  if (isset($_POST['filter_search_use']))
160  {
161    $_SESSION['bulk_manager_filter']['search']['q'] = $_POST['q'];
162  }
163
164  trigger_notify('batch_manager_register_filters');
165}
166// filters from url
167elseif (isset($_GET['filter']))
168{
169  if (!is_array($_GET['filter']))
170  {
171    $_GET['filter'] = explode(',', $_GET['filter']);
172  }
173
174  $_SESSION['bulk_manager_filter'] = array();
175
176  foreach ($_GET['filter'] as $filter)
177  {
178    list($type, $value) = explode('-', $filter);
179
180    switch ($type)
181    {
182    case 'prefilter':
183      $_SESSION['bulk_manager_filter']['prefilter'] = $value;
184      break;
185
186    case 'album':
187      if (is_numeric($value))
188      {
189        $_SESSION['bulk_manager_filter']['category'] = $value;
190      }
191      break;
192
193    case 'tag':
194      if (is_numeric($value))
195      {
196        $_SESSION['bulk_manager_filter']['tags'] = array($value);
197        $_SESSION['bulk_manager_filter']['tag_mode'] = 'AND';
198      }
199      break;
200
201    case 'level':
202      if (is_numeric($value) && in_array($value, $conf['available_permission_levels']))
203      {
204        $_SESSION['bulk_manager_filter']['level'] = $value;
205      }
206      break;
207    }
208  }
209}
210
211if (empty($_SESSION['bulk_manager_filter']))
212{
213  $_SESSION['bulk_manager_filter'] = array(
214    'prefilter' => 'caddie'
215    );
216}
217
218// echo '<pre>'; print_r($_SESSION['bulk_manager_filter']); echo '</pre>';
219
220// depending on the current filter (in session), we find the appropriate photos
221$filter_sets = array();
222if (isset($_SESSION['bulk_manager_filter']['prefilter']))
223{
224  switch ($_SESSION['bulk_manager_filter']['prefilter'])
225  {
226  case 'caddie':
227    $query = '
228SELECT element_id
229  FROM '.CADDIE_TABLE.'
230  WHERE user_id = '.$user['id'].'
231;';
232    $filter_sets[] = query2array($query, null, 'element_id');
233
234    break;
235
236  case 'favorites':
237    $query = '
238SELECT image_id
239  FROM '.FAVORITES_TABLE.'
240  WHERE user_id = '.$user['id'].'
241;';
242    $filter_sets[] = query2array($query, null, 'image_id');
243
244    break;
245
246  case 'last_import':
247    $query = '
248SELECT MAX(date_available) AS date
249  FROM '.IMAGES_TABLE.'
250;';
251    $row = pwg_db_fetch_assoc(pwg_query($query));
252    if (!empty($row['date']))
253    {
254      $query = '
255SELECT id
256  FROM '.IMAGES_TABLE.'
257  WHERE date_available BETWEEN '.pwg_db_get_recent_period_expression(1, $row['date']).' AND \''.$row['date'].'\'
258;';
259      $filter_sets[] = query2array($query, null, 'id');
260    }
261
262    break;
263
264  case 'no_virtual_album':
265    // we are searching elements not linked to any virtual category
266    $query = '
267 SELECT id
268   FROM '.IMAGES_TABLE.'
269 ;';
270    $all_elements = query2array($query, null, 'id');
271
272    $query = '
273 SELECT id
274   FROM '.CATEGORIES_TABLE.'
275   WHERE dir IS NULL
276 ;';
277    $virtual_categories = query2array($query, null, 'id');
278    if (!empty($virtual_categories))
279    {
280      $query = '
281 SELECT DISTINCT(image_id)
282   FROM '.IMAGE_CATEGORY_TABLE.'
283   WHERE category_id IN ('.implode(',', $virtual_categories).')
284 ;';
285      $linked_to_virtual = query2array($query, null, 'image_id');
286    }
287
288    $filter_sets[] = array_diff($all_elements, $linked_to_virtual);
289
290    break;
291
292  case 'no_album':
293    $query = '
294SELECT
295    id
296  FROM '.IMAGES_TABLE.'
297    LEFT JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
298  WHERE category_id is null
299;';
300    $filter_sets[] = query2array($query, null, 'id');
301
302    break;
303
304  case 'no_tag':
305    $query = '
306SELECT
307    id
308  FROM '.IMAGES_TABLE.'
309    LEFT JOIN '.IMAGE_TAG_TABLE.' ON id = image_id
310  WHERE tag_id is null
311;';
312    $filter_sets[] = query2array($query, null, 'id');
313
314    break;
315
316
317  case 'duplicates':
318    $duplicates_on_fields = array('file');
319
320    if (isset($_SESSION['bulk_manager_filter']['duplicates_date']))
321    {
322      $duplicates_on_fields[] = 'date_creation';
323    }
324   
325    if (isset($_SESSION['bulk_manager_filter']['duplicates_dimensions']))
326    {
327      $duplicates_on_fields[] = 'width';
328      $duplicates_on_fields[] = 'height';
329    }
330   
331    $query = '
332SELECT
333    GROUP_CONCAT(id) AS ids
334  FROM '.IMAGES_TABLE.'
335  GROUP BY '.implode(',', $duplicates_on_fields).'
336  HAVING COUNT(*) > 1
337;';
338    $array_of_ids_string = query2array($query, null, 'ids');
339
340    $ids = array();
341   
342    foreach ($array_of_ids_string as $ids_string)
343    {
344      $ids = array_merge($ids, explode(',', $ids_string));
345    }
346   
347    $filter_sets[] = $ids;
348
349    break;
350
351  case 'all_photos':
352    if ( count($_SESSION['bulk_manager_filter']) == 1 )
353    {// make the query only if this is the only filter
354      $query = '
355SELECT id
356  FROM '.IMAGES_TABLE.'
357  '.$conf['order_by'];
358
359      $filter_sets[] = query2array($query, null, 'id');
360    }
361    break;
362  }
363
364  $filter_sets = trigger_change('perform_batch_manager_prefilters', $filter_sets, $_SESSION['bulk_manager_filter']['prefilter']);
365}
366
367if (isset($_SESSION['bulk_manager_filter']['category']))
368{
369  $categories = array();
370
371  if (isset($_SESSION['bulk_manager_filter']['category_recursive']))
372  {
373    $categories = get_subcat_ids(array($_SESSION['bulk_manager_filter']['category']));
374  }
375  else
376  {
377    $categories = array($_SESSION['bulk_manager_filter']['category']);
378  }
379
380  $query = '
381 SELECT DISTINCT(image_id)
382   FROM '.IMAGE_CATEGORY_TABLE.'
383   WHERE category_id IN ('.implode(',', $categories).')
384 ;';
385  $filter_sets[] = query2array($query, null, 'image_id');
386}
387
388if (isset($_SESSION['bulk_manager_filter']['level']))
389{
390  $operator = '=';
391  if (isset($_SESSION['bulk_manager_filter']['level_include_lower']))
392  {
393    $operator = '<=';
394  }
395
396  $query = '
397SELECT id
398  FROM '.IMAGES_TABLE.'
399  WHERE level '.$operator.' '.$_SESSION['bulk_manager_filter']['level'].'
400  '.$conf['order_by'];
401
402  $filter_sets[] = query2array($query, null, 'id');
403}
404
405if (!empty($_SESSION['bulk_manager_filter']['tags']))
406{
407  $filter_sets[] = get_image_ids_for_tags(
408    $_SESSION['bulk_manager_filter']['tags'],
409    $_SESSION['bulk_manager_filter']['tag_mode'],
410    null,
411    null,
412    false // we don't apply permissions in administration screens
413    );
414}
415
416if (isset($_SESSION['bulk_manager_filter']['dimension']))
417{
418  $where_clauses = array();
419  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_width']))
420  {
421    $where_clause[] = 'width >= '.$_SESSION['bulk_manager_filter']['dimension']['min_width'];
422  }
423  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_width']))
424  {
425    $where_clause[] = 'width <= '.$_SESSION['bulk_manager_filter']['dimension']['max_width'];
426  }
427  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_height']))
428  {
429    $where_clause[] = 'height >= '.$_SESSION['bulk_manager_filter']['dimension']['min_height'];
430  }
431  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_height']))
432  {
433    $where_clause[] = 'height <= '.$_SESSION['bulk_manager_filter']['dimension']['max_height'];
434  }
435  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_ratio']))
436  {
437    $where_clause[] = 'width/height >= '.$_SESSION['bulk_manager_filter']['dimension']['min_ratio'];
438  }
439  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_ratio']))
440  {
441    // max_ratio is a floor value, so must be a bit increased
442    $where_clause[] = 'width/height < '.($_SESSION['bulk_manager_filter']['dimension']['max_ratio']+0.01);
443  }
444
445  $query = '
446SELECT id
447  FROM '.IMAGES_TABLE.'
448  WHERE '.implode(' AND ',$where_clause).'
449  '.$conf['order_by'];
450
451  $filter_sets[] = query2array($query, null, 'id');
452}
453
454if (isset($_SESSION['bulk_manager_filter']['filesize']))
455{
456  $where_clauses = array();
457 
458  if (isset($_SESSION['bulk_manager_filter']['filesize']['min']))
459  {
460    $where_clause[] = 'filesize >= '.$_SESSION['bulk_manager_filter']['filesize']['min']*1024;
461  }
462 
463  if (isset($_SESSION['bulk_manager_filter']['filesize']['max']))
464  {
465    $where_clause[] = 'filesize <= '.$_SESSION['bulk_manager_filter']['filesize']['max']*1024;
466  }
467
468  $query = '
469SELECT id
470  FROM '.IMAGES_TABLE.'
471  WHERE '.implode(' AND ',$where_clause).'
472  '.$conf['order_by'];
473
474  $filter_sets[] = query2array($query, null, 'id');
475}
476
477if (isset($_SESSION['bulk_manager_filter']['search']))
478{
479  include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' );
480  $res = get_quick_search_results_no_cache($_SESSION['bulk_manager_filter']['search']['q'], array('permissions'=>false));
481  $filter_sets[] = $res['items'];
482}
483
484$filter_sets = trigger_change('batch_manager_perform_filters', $filter_sets);
485
486$current_set = array_shift($filter_sets);
487foreach ($filter_sets as $set)
488{
489  $current_set = array_intersect($current_set, $set);
490}
491$page['cat_elements_id'] = $current_set;
492
493
494// +-----------------------------------------------------------------------+
495// |                       first element to display                        |
496// +-----------------------------------------------------------------------+
497
498// $page['start'] contains the number of the first element in its
499// category. For exampe, $page['start'] = 12 means we must show elements #12
500// and $page['nb_images'] next elements
501
502if (!isset($_REQUEST['start'])
503    or !is_numeric($_REQUEST['start'])
504    or $_REQUEST['start'] < 0
505    or (isset($_REQUEST['display']) and 'all' == $_REQUEST['display']))
506{
507  $page['start'] = 0;
508}
509else
510{
511  $page['start'] = $_REQUEST['start'];
512}
513
514
515// +-----------------------------------------------------------------------+
516// |                                 Tabs                                  |
517// +-----------------------------------------------------------------------+
518$manager_link = get_root_url().'admin.php?page=batch_manager&amp;mode=';
519
520if (isset($_GET['mode']))
521{
522  $page['tab'] = $_GET['mode'];
523}
524else
525{
526  $page['tab'] = 'global';
527}
528
529$tabsheet = new tabsheet();
530$tabsheet->set_id('batch_manager');
531$tabsheet->select($page['tab']);
532$tabsheet->assign();
533
534
535// +-----------------------------------------------------------------------+
536// |                              dimensions                               |
537// +-----------------------------------------------------------------------+
538
539$widths = array();
540$heights = array();
541$ratios = array();
542$dimensions = array();
543
544// get all width, height and ratios
545$query = '
546SELECT
547  DISTINCT width, height
548  FROM '.IMAGES_TABLE.'
549  WHERE width IS NOT NULL
550    AND height IS NOT NULL
551;';
552$result = pwg_query($query);
553
554if (pwg_db_num_rows($result))
555{
556  while ($row = pwg_db_fetch_assoc($result))
557  {
558    if ($row['width']>0 && $row['height']>0)
559    {
560      $widths[] = $row['width'];
561      $heights[] = $row['height'];
562      $ratios[] = floor($row['width'] / $row['height'] * 100) / 100;
563    }
564  }
565}
566if (empty($widths))
567{ // arbitrary values, only used when no photos on the gallery
568  $widths = array(600, 1920, 3500);
569  $heights = array(480, 1080, 2300);
570  $ratios = array(1.25, 1.52, 1.78);
571}
572
573foreach (array('widths','heights','ratios') as $type)
574{
575  ${$type} = array_unique(${$type});
576  sort(${$type});
577  $dimensions[$type] = implode(',', ${$type});
578}
579
580$dimensions['bounds'] = array(
581  'min_width' => $widths[0],
582  'max_width' => end($widths),
583  'min_height' => $heights[0],
584  'max_height' => end($heights),
585  'min_ratio' => $ratios[0],
586  'max_ratio' => end($ratios),
587  );
588
589// find ratio categories
590$ratio_categories = array(
591  'portrait' => array(),
592  'square' => array(),
593  'landscape' => array(),
594  'panorama' => array(),
595  );
596
597foreach ($ratios as $ratio)
598{
599  if ($ratio < 0.95)
600  {
601    $ratio_categories['portrait'][] = $ratio;
602  }
603  else if ($ratio >= 0.95 and $ratio <= 1.05)
604  {
605    $ratio_categories['square'][] = $ratio;
606  }
607  else if ($ratio > 1.05 and $ratio < 2)
608  {
609    $ratio_categories['landscape'][] = $ratio;
610  }
611  else if ($ratio >= 2)
612  {
613    $ratio_categories['panorama'][] = $ratio;
614  }
615}
616
617foreach (array_keys($ratio_categories) as $type)
618{
619  if (count($ratio_categories[$type]) > 0)
620  {
621    $dimensions['ratio_'.$type] = array(
622      'min' => $ratio_categories[$type][0],
623      'max' => end($ratio_categories[$type]),
624      );
625  }
626}
627
628// selected=bound if nothing selected
629foreach (array_keys($dimensions['bounds']) as $type)
630{
631  $dimensions['selected'][$type] = isset($_SESSION['bulk_manager_filter']['dimension'][$type])
632    ? $_SESSION['bulk_manager_filter']['dimension'][$type]
633    : $dimensions['bounds'][$type]
634  ;
635}
636
637$template->assign('dimensions', $dimensions);
638
639// +-----------------------------------------------------------------------+
640// | filesize                                                              |
641// +-----------------------------------------------------------------------+
642
643$filesizes = array();
644$filesize = array();
645
646$query = '
647SELECT
648  filesize
649  FROM '.IMAGES_TABLE.'
650  WHERE filesize IS NOT NULL
651  GROUP BY filesize
652;';
653$result = pwg_query($query);
654
655while ($row = pwg_db_fetch_assoc($result))
656{
657  $filesizes[] = sprintf('%.1f', $row['filesize']/1024);
658}
659
660if (empty($filesizes))
661{ // arbitrary values, only used when no photos on the gallery
662  $filesizes = array(0, 1, 2, 5, 8, 15);
663}
664
665$filesizes = array_unique($filesizes);
666sort($filesizes);
667
668// add 0.1MB to the last value, to make sure the heavier photo will be in
669// the result
670$filesizes[count($filesizes)-1]+= 0.1;
671
672$filesize['list'] = implode(',', $filesizes);
673
674$filesize['bounds'] = array(
675  'min' => $filesizes[0],
676  'max' => end($filesizes),
677  );
678
679// selected=bound if nothing selected
680foreach (array_keys($filesize['bounds']) as $type)
681{
682  $filesize['selected'][$type] = isset($_SESSION['bulk_manager_filter']['filesize'][$type])
683    ? $_SESSION['bulk_manager_filter']['filesize'][$type]
684    : $filesize['bounds'][$type]
685  ;
686}
687
688$template->assign('filesize', $filesize);
689
690// +-----------------------------------------------------------------------+
691// |                         open specific mode                            |
692// +-----------------------------------------------------------------------+
693
694include(PHPWG_ROOT_PATH.'admin/batch_manager_'.$page['tab'].'.php');
695?>
Note: See TracBrowser for help on using the repository browser.