source: trunk/admin/batch_manager.php @ 28436

Last change on this file since 28436 was 28318, checked in by rvelices, 11 years ago

batch manager prefilter all does not make a sql query if there is also another filter

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