source: trunk/admin/batch_manager.php @ 29732

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

typo fixed

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