source: trunk/admin/batch_manager.php @ 29505

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

add URL filter input for dimension and filesize + batch_manager_url_filter trigger + fixes in doubleSlider.js

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