source: trunk/admin/batch_manager.php @ 29481

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

ability to set the filter-search by url (useful for TakeATour)

File size: 19.0 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    case 'search':
209      $_SESSION['bulk_manager_filter']['search']['q'] = $value;
210      break;
211    }
212  }
213}
214
215if (empty($_SESSION['bulk_manager_filter']))
216{
217  $_SESSION['bulk_manager_filter'] = array(
218    'prefilter' => 'caddie'
219    );
220}
221
222// echo '<pre>'; print_r($_SESSION['bulk_manager_filter']); echo '</pre>';
223
224// depending on the current filter (in session), we find the appropriate photos
225$filter_sets = array();
226if (isset($_SESSION['bulk_manager_filter']['prefilter']))
227{
228  switch ($_SESSION['bulk_manager_filter']['prefilter'])
229  {
230  case 'caddie':
231    $query = '
232SELECT element_id
233  FROM '.CADDIE_TABLE.'
234  WHERE user_id = '.$user['id'].'
235;';
236    $filter_sets[] = query2array($query, null, 'element_id');
237
238    break;
239
240  case 'favorites':
241    $query = '
242SELECT image_id
243  FROM '.FAVORITES_TABLE.'
244  WHERE user_id = '.$user['id'].'
245;';
246    $filter_sets[] = query2array($query, null, 'image_id');
247
248    break;
249
250  case 'last_import':
251    $query = '
252SELECT MAX(date_available) AS date
253  FROM '.IMAGES_TABLE.'
254;';
255    $row = pwg_db_fetch_assoc(pwg_query($query));
256    if (!empty($row['date']))
257    {
258      $query = '
259SELECT id
260  FROM '.IMAGES_TABLE.'
261  WHERE date_available BETWEEN '.pwg_db_get_recent_period_expression(1, $row['date']).' AND \''.$row['date'].'\'
262;';
263      $filter_sets[] = query2array($query, null, 'id');
264    }
265
266    break;
267
268  case 'no_virtual_album':
269    // we are searching elements not linked to any virtual category
270    $query = '
271 SELECT id
272   FROM '.IMAGES_TABLE.'
273 ;';
274    $all_elements = query2array($query, null, 'id');
275
276    $query = '
277 SELECT id
278   FROM '.CATEGORIES_TABLE.'
279   WHERE dir IS NULL
280 ;';
281    $virtual_categories = query2array($query, null, 'id');
282    if (!empty($virtual_categories))
283    {
284      $query = '
285 SELECT DISTINCT(image_id)
286   FROM '.IMAGE_CATEGORY_TABLE.'
287   WHERE category_id IN ('.implode(',', $virtual_categories).')
288 ;';
289      $linked_to_virtual = query2array($query, null, 'image_id');
290    }
291
292    $filter_sets[] = array_diff($all_elements, $linked_to_virtual);
293
294    break;
295
296  case 'no_album':
297    $query = '
298SELECT
299    id
300  FROM '.IMAGES_TABLE.'
301    LEFT JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
302  WHERE category_id is null
303;';
304    $filter_sets[] = query2array($query, null, 'id');
305
306    break;
307
308  case 'no_tag':
309    $query = '
310SELECT
311    id
312  FROM '.IMAGES_TABLE.'
313    LEFT JOIN '.IMAGE_TAG_TABLE.' ON id = image_id
314  WHERE tag_id is null
315;';
316    $filter_sets[] = query2array($query, null, 'id');
317
318    break;
319
320
321  case 'duplicates':
322    $duplicates_on_fields = array('file');
323
324    if (isset($_SESSION['bulk_manager_filter']['duplicates_date']))
325    {
326      $duplicates_on_fields[] = 'date_creation';
327    }
328   
329    if (isset($_SESSION['bulk_manager_filter']['duplicates_dimensions']))
330    {
331      $duplicates_on_fields[] = 'width';
332      $duplicates_on_fields[] = 'height';
333    }
334   
335    $query = '
336SELECT
337    GROUP_CONCAT(id) AS ids
338  FROM '.IMAGES_TABLE.'
339  GROUP BY '.implode(',', $duplicates_on_fields).'
340  HAVING COUNT(*) > 1
341;';
342    $array_of_ids_string = query2array($query, null, 'ids');
343
344    $ids = array();
345   
346    foreach ($array_of_ids_string as $ids_string)
347    {
348      $ids = array_merge($ids, explode(',', $ids_string));
349    }
350   
351    $filter_sets[] = $ids;
352
353    break;
354
355  case 'all_photos':
356    if ( count($_SESSION['bulk_manager_filter']) == 1 )
357    {// make the query only if this is the only filter
358      $query = '
359SELECT id
360  FROM '.IMAGES_TABLE.'
361  '.$conf['order_by'];
362
363      $filter_sets[] = query2array($query, null, 'id');
364    }
365    break;
366  }
367
368  $filter_sets = trigger_change('perform_batch_manager_prefilters', $filter_sets, $_SESSION['bulk_manager_filter']['prefilter']);
369}
370
371if (isset($_SESSION['bulk_manager_filter']['category']))
372{
373  $categories = array();
374
375  if (isset($_SESSION['bulk_manager_filter']['category_recursive']))
376  {
377    $categories = get_subcat_ids(array($_SESSION['bulk_manager_filter']['category']));
378  }
379  else
380  {
381    $categories = array($_SESSION['bulk_manager_filter']['category']);
382  }
383
384  $query = '
385 SELECT DISTINCT(image_id)
386   FROM '.IMAGE_CATEGORY_TABLE.'
387   WHERE category_id IN ('.implode(',', $categories).')
388 ;';
389  $filter_sets[] = query2array($query, null, 'image_id');
390}
391
392if (isset($_SESSION['bulk_manager_filter']['level']))
393{
394  $operator = '=';
395  if (isset($_SESSION['bulk_manager_filter']['level_include_lower']))
396  {
397    $operator = '<=';
398  }
399
400  $query = '
401SELECT id
402  FROM '.IMAGES_TABLE.'
403  WHERE level '.$operator.' '.$_SESSION['bulk_manager_filter']['level'].'
404  '.$conf['order_by'];
405
406  $filter_sets[] = query2array($query, null, 'id');
407}
408
409if (!empty($_SESSION['bulk_manager_filter']['tags']))
410{
411  $filter_sets[] = get_image_ids_for_tags(
412    $_SESSION['bulk_manager_filter']['tags'],
413    $_SESSION['bulk_manager_filter']['tag_mode'],
414    null,
415    null,
416    false // we don't apply permissions in administration screens
417    );
418}
419
420if (isset($_SESSION['bulk_manager_filter']['dimension']))
421{
422  $where_clauses = array();
423  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_width']))
424  {
425    $where_clause[] = 'width >= '.$_SESSION['bulk_manager_filter']['dimension']['min_width'];
426  }
427  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_width']))
428  {
429    $where_clause[] = 'width <= '.$_SESSION['bulk_manager_filter']['dimension']['max_width'];
430  }
431  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_height']))
432  {
433    $where_clause[] = 'height >= '.$_SESSION['bulk_manager_filter']['dimension']['min_height'];
434  }
435  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_height']))
436  {
437    $where_clause[] = 'height <= '.$_SESSION['bulk_manager_filter']['dimension']['max_height'];
438  }
439  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_ratio']))
440  {
441    $where_clause[] = 'width/height >= '.$_SESSION['bulk_manager_filter']['dimension']['min_ratio'];
442  }
443  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_ratio']))
444  {
445    // max_ratio is a floor value, so must be a bit increased
446    $where_clause[] = 'width/height < '.($_SESSION['bulk_manager_filter']['dimension']['max_ratio']+0.01);
447  }
448
449  $query = '
450SELECT id
451  FROM '.IMAGES_TABLE.'
452  WHERE '.implode(' AND ',$where_clause).'
453  '.$conf['order_by'];
454
455  $filter_sets[] = query2array($query, null, 'id');
456}
457
458if (isset($_SESSION['bulk_manager_filter']['filesize']))
459{
460  $where_clauses = array();
461 
462  if (isset($_SESSION['bulk_manager_filter']['filesize']['min']))
463  {
464    $where_clause[] = 'filesize >= '.$_SESSION['bulk_manager_filter']['filesize']['min']*1024;
465  }
466 
467  if (isset($_SESSION['bulk_manager_filter']['filesize']['max']))
468  {
469    $where_clause[] = 'filesize <= '.$_SESSION['bulk_manager_filter']['filesize']['max']*1024;
470  }
471
472  $query = '
473SELECT id
474  FROM '.IMAGES_TABLE.'
475  WHERE '.implode(' AND ',$where_clause).'
476  '.$conf['order_by'];
477
478  $filter_sets[] = query2array($query, null, 'id');
479}
480
481if (isset($_SESSION['bulk_manager_filter']['search']))
482{
483  include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' );
484  $res = get_quick_search_results_no_cache($_SESSION['bulk_manager_filter']['search']['q'], array('permissions'=>false));
485  $filter_sets[] = $res['items'];
486}
487
488$filter_sets = trigger_change('batch_manager_perform_filters', $filter_sets);
489
490$current_set = array_shift($filter_sets);
491foreach ($filter_sets as $set)
492{
493  $current_set = array_intersect($current_set, $set);
494}
495$page['cat_elements_id'] = $current_set;
496
497
498// +-----------------------------------------------------------------------+
499// |                       first element to display                        |
500// +-----------------------------------------------------------------------+
501
502// $page['start'] contains the number of the first element in its
503// category. For exampe, $page['start'] = 12 means we must show elements #12
504// and $page['nb_images'] next elements
505
506if (!isset($_REQUEST['start'])
507    or !is_numeric($_REQUEST['start'])
508    or $_REQUEST['start'] < 0
509    or (isset($_REQUEST['display']) and 'all' == $_REQUEST['display']))
510{
511  $page['start'] = 0;
512}
513else
514{
515  $page['start'] = $_REQUEST['start'];
516}
517
518
519// +-----------------------------------------------------------------------+
520// |                                 Tabs                                  |
521// +-----------------------------------------------------------------------+
522$manager_link = get_root_url().'admin.php?page=batch_manager&amp;mode=';
523
524if (isset($_GET['mode']))
525{
526  $page['tab'] = $_GET['mode'];
527}
528else
529{
530  $page['tab'] = 'global';
531}
532
533$tabsheet = new tabsheet();
534$tabsheet->set_id('batch_manager');
535$tabsheet->select($page['tab']);
536$tabsheet->assign();
537
538
539// +-----------------------------------------------------------------------+
540// |                              dimensions                               |
541// +-----------------------------------------------------------------------+
542
543$widths = array();
544$heights = array();
545$ratios = array();
546$dimensions = array();
547
548// get all width, height and ratios
549$query = '
550SELECT
551  DISTINCT width, height
552  FROM '.IMAGES_TABLE.'
553  WHERE width IS NOT NULL
554    AND height IS NOT NULL
555;';
556$result = pwg_query($query);
557
558if (pwg_db_num_rows($result))
559{
560  while ($row = pwg_db_fetch_assoc($result))
561  {
562    if ($row['width']>0 && $row['height']>0)
563    {
564      $widths[] = $row['width'];
565      $heights[] = $row['height'];
566      $ratios[] = floor($row['width'] / $row['height'] * 100) / 100;
567    }
568  }
569}
570if (empty($widths))
571{ // arbitrary values, only used when no photos on the gallery
572  $widths = array(600, 1920, 3500);
573  $heights = array(480, 1080, 2300);
574  $ratios = array(1.25, 1.52, 1.78);
575}
576
577foreach (array('widths','heights','ratios') as $type)
578{
579  ${$type} = array_unique(${$type});
580  sort(${$type});
581  $dimensions[$type] = implode(',', ${$type});
582}
583
584$dimensions['bounds'] = array(
585  'min_width' => $widths[0],
586  'max_width' => end($widths),
587  'min_height' => $heights[0],
588  'max_height' => end($heights),
589  'min_ratio' => $ratios[0],
590  'max_ratio' => end($ratios),
591  );
592
593// find ratio categories
594$ratio_categories = array(
595  'portrait' => array(),
596  'square' => array(),
597  'landscape' => array(),
598  'panorama' => array(),
599  );
600
601foreach ($ratios as $ratio)
602{
603  if ($ratio < 0.95)
604  {
605    $ratio_categories['portrait'][] = $ratio;
606  }
607  else if ($ratio >= 0.95 and $ratio <= 1.05)
608  {
609    $ratio_categories['square'][] = $ratio;
610  }
611  else if ($ratio > 1.05 and $ratio < 2)
612  {
613    $ratio_categories['landscape'][] = $ratio;
614  }
615  else if ($ratio >= 2)
616  {
617    $ratio_categories['panorama'][] = $ratio;
618  }
619}
620
621foreach (array_keys($ratio_categories) as $type)
622{
623  if (count($ratio_categories[$type]) > 0)
624  {
625    $dimensions['ratio_'.$type] = array(
626      'min' => $ratio_categories[$type][0],
627      'max' => end($ratio_categories[$type]),
628      );
629  }
630}
631
632// selected=bound if nothing selected
633foreach (array_keys($dimensions['bounds']) as $type)
634{
635  $dimensions['selected'][$type] = isset($_SESSION['bulk_manager_filter']['dimension'][$type])
636    ? $_SESSION['bulk_manager_filter']['dimension'][$type]
637    : $dimensions['bounds'][$type]
638  ;
639}
640
641$template->assign('dimensions', $dimensions);
642
643// +-----------------------------------------------------------------------+
644// | filesize                                                              |
645// +-----------------------------------------------------------------------+
646
647$filesizes = array();
648$filesize = array();
649
650$query = '
651SELECT
652  filesize
653  FROM '.IMAGES_TABLE.'
654  WHERE filesize IS NOT NULL
655  GROUP BY filesize
656;';
657$result = pwg_query($query);
658
659while ($row = pwg_db_fetch_assoc($result))
660{
661  $filesizes[] = sprintf('%.1f', $row['filesize']/1024);
662}
663
664if (empty($filesizes))
665{ // arbitrary values, only used when no photos on the gallery
666  $filesizes = array(0, 1, 2, 5, 8, 15);
667}
668
669$filesizes = array_unique($filesizes);
670sort($filesizes);
671
672// add 0.1MB to the last value, to make sure the heavier photo will be in
673// the result
674$filesizes[count($filesizes)-1]+= 0.1;
675
676$filesize['list'] = implode(',', $filesizes);
677
678$filesize['bounds'] = array(
679  'min' => $filesizes[0],
680  'max' => end($filesizes),
681  );
682
683// selected=bound if nothing selected
684foreach (array_keys($filesize['bounds']) as $type)
685{
686  $filesize['selected'][$type] = isset($_SESSION['bulk_manager_filter']['filesize'][$type])
687    ? $_SESSION['bulk_manager_filter']['filesize'][$type]
688    : $filesize['bounds'][$type]
689  ;
690}
691
692$template->assign('filesize', $filesize);
693
694// +-----------------------------------------------------------------------+
695// |                         open specific mode                            |
696// +-----------------------------------------------------------------------+
697
698include(PHPWG_ROOT_PATH.'admin/batch_manager_'.$page['tab'].'.php');
699?>
Note: See TracBrowser for help on using the repository browser.