source: trunk/admin/batch_manager.php @ 28087

Last change on this file since 28087 was 28087, checked in by rvelices, 10 years ago

bug 3069: add quick search as filter in batch manager

File size: 15.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// +-----------------------------------------------------------------------+
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    $query = '
293SELECT id
294  FROM '.IMAGES_TABLE.'
295  '.$conf['order_by'];
296
297    $filter_sets[] = array_from_query($query, 'id');
298
299    break;
300  }
301
302  $filter_sets = trigger_event('perform_batch_manager_prefilters', $filter_sets, $_SESSION['bulk_manager_filter']['prefilter']);
303}
304
305if (isset($_SESSION['bulk_manager_filter']['category']))
306{
307  $categories = array();
308
309  if (isset($_SESSION['bulk_manager_filter']['category_recursive']))
310  {
311    $categories = get_subcat_ids(array($_SESSION['bulk_manager_filter']['category']));
312  }
313  else
314  {
315    $categories = array($_SESSION['bulk_manager_filter']['category']);
316  }
317
318  $query = '
319 SELECT DISTINCT(image_id)
320   FROM '.IMAGE_CATEGORY_TABLE.'
321   WHERE category_id IN ('.implode(',', $categories).')
322 ;';
323  $filter_sets[] = array_from_query($query, 'image_id');
324}
325
326if (isset($_SESSION['bulk_manager_filter']['level']))
327{
328  $operator = '=';
329  if (isset($_SESSION['bulk_manager_filter']['level_include_lower']))
330  {
331    $operator = '<=';
332  }
333
334  $query = '
335SELECT id
336  FROM '.IMAGES_TABLE.'
337  WHERE level '.$operator.' '.$_SESSION['bulk_manager_filter']['level'].'
338  '.$conf['order_by'];
339
340  $filter_sets[] = array_from_query($query, 'id');
341}
342
343if (!empty($_SESSION['bulk_manager_filter']['tags']))
344{
345  $filter_sets[] = get_image_ids_for_tags(
346    $_SESSION['bulk_manager_filter']['tags'],
347    $_SESSION['bulk_manager_filter']['tag_mode'],
348    null,
349    null,
350    false // we don't apply permissions in administration screens
351    );
352}
353
354if (isset($_SESSION['bulk_manager_filter']['dimension']))
355{
356  $where_clauses = array();
357  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_width']))
358  {
359    $where_clause[] = 'width >= '.$_SESSION['bulk_manager_filter']['dimension']['min_width'];
360  }
361  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_width']))
362  {
363    $where_clause[] = 'width <= '.$_SESSION['bulk_manager_filter']['dimension']['max_width'];
364  }
365  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_height']))
366  {
367    $where_clause[] = 'height >= '.$_SESSION['bulk_manager_filter']['dimension']['min_height'];
368  }
369  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_height']))
370  {
371    $where_clause[] = 'height <= '.$_SESSION['bulk_manager_filter']['dimension']['max_height'];
372  }
373  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_ratio']))
374  {
375    $where_clause[] = 'width/height >= '.$_SESSION['bulk_manager_filter']['dimension']['min_ratio'];
376  }
377  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_ratio']))
378  {
379    // max_ratio is a floor value, so must be a bit increased
380    $where_clause[] = 'width/height < '.($_SESSION['bulk_manager_filter']['dimension']['max_ratio']+0.01);
381  }
382
383  $query = '
384SELECT id
385  FROM '.IMAGES_TABLE.'
386  WHERE '.implode(' AND ',$where_clause).'
387  '.$conf['order_by'];
388
389  $filter_sets[] = array_from_query($query, 'id');
390}
391
392if (isset($_SESSION['bulk_manager_filter']['search']))
393{
394  include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' );
395  $res = get_quick_search_results($_SESSION['bulk_manager_filter']['search']['q'], array('permissions'=>false));
396  $filter_sets[] = $res['items'];
397}
398
399$current_set = array_shift($filter_sets);
400foreach ($filter_sets as $set)
401{
402  $current_set = array_intersect($current_set, $set);
403}
404$page['cat_elements_id'] = $current_set;
405
406
407// +-----------------------------------------------------------------------+
408// |                       first element to display                        |
409// +-----------------------------------------------------------------------+
410
411// $page['start'] contains the number of the first element in its
412// category. For exampe, $page['start'] = 12 means we must show elements #12
413// and $page['nb_images'] next elements
414
415if (!isset($_REQUEST['start'])
416    or !is_numeric($_REQUEST['start'])
417    or $_REQUEST['start'] < 0
418    or (isset($_REQUEST['display']) and 'all' == $_REQUEST['display']))
419{
420  $page['start'] = 0;
421}
422else
423{
424  $page['start'] = $_REQUEST['start'];
425}
426
427
428// +-----------------------------------------------------------------------+
429// |                                 Tabs                                  |
430// +-----------------------------------------------------------------------+
431$manager_link = get_root_url().'admin.php?page=batch_manager&amp;mode=';
432
433if (isset($_GET['mode']))
434{
435  $page['tab'] = $_GET['mode'];
436}
437else
438{
439  $page['tab'] = 'global';
440}
441
442$tabsheet = new tabsheet();
443$tabsheet->set_id('batch_manager');
444$tabsheet->select($page['tab']);
445$tabsheet->assign();
446
447
448// +-----------------------------------------------------------------------+
449// |                              tags                                     |
450// +-----------------------------------------------------------------------+
451
452$query = '
453SELECT id, name
454  FROM '.TAGS_TABLE.'
455;';
456$template->assign('tags', get_taglist($query, false));
457
458
459// +-----------------------------------------------------------------------+
460// |                              dimensions                               |
461// +-----------------------------------------------------------------------+
462
463$widths = array();
464$heights = array();
465$ratios = array();
466
467// get all width, height and ratios
468$query = '
469SELECT
470  DISTINCT width, height
471  FROM '.IMAGES_TABLE.'
472  WHERE width IS NOT NULL
473    AND height IS NOT NULL
474;';
475$result = pwg_query($query);
476
477if (pwg_db_num_rows($result))
478{
479  while ($row = pwg_db_fetch_assoc($result))
480  {
481    if ($row['width']>0 && $row['height']>0)
482    {
483      $widths[] = $row['width'];
484      $heights[] = $row['height'];
485      $ratios[] = floor($row['width'] / $row['height'] * 100) / 100;
486    }
487  }
488}
489if (empty($widths))
490{ // arbitrary values, only used when no photos on the gallery
491  $widths = array(600, 1920, 3500);
492  $heights = array(480, 1080, 2300);
493  $ratios = array(1.25, 1.52, 1.78);
494}
495
496
497
498$widths = array_unique($widths);
499sort($widths);
500
501$heights = array_unique($heights);
502sort($heights);
503
504$ratios = array_unique($ratios);
505sort($ratios);
506
507$dimensions['widths'] = implode(',', $widths);
508$dimensions['heights'] = implode(',', $heights);
509$dimensions['ratios'] = implode(',', $ratios);
510
511$dimensions['bounds'] = array(
512  'min_width' => $widths[0],
513  'max_width' => $widths[count($widths)-1],
514  'min_height' => $heights[0],
515  'max_height' => $heights[count($heights)-1],
516  'min_ratio' => $ratios[0],
517  'max_ratio' => $ratios[count($ratios)-1],
518  );
519
520// find ratio categories
521$ratio_categories = array(
522  'portrait' => array(),
523  'square' => array(),
524  'landscape' => array(),
525  'panorama' => array(),
526  );
527
528foreach ($ratios as $ratio)
529{
530  if ($ratio < 0.95)
531  {
532    $ratio_categories['portrait'][] = $ratio;
533  }
534  else if ($ratio >= 0.95 and $ratio <= 1.05)
535  {
536    $ratio_categories['square'][] = $ratio;
537  }
538  else if ($ratio > 1.05 and $ratio < 2)
539  {
540    $ratio_categories['landscape'][] = $ratio;
541  }
542  else if ($ratio >= 2)
543  {
544    $ratio_categories['panorama'][] = $ratio;
545  }
546}
547
548foreach (array_keys($ratio_categories) as $ratio_category)
549{
550  if (count($ratio_categories[$ratio_category]) > 0)
551  {
552    $dimensions['ratio_'.$ratio_category] = array(
553      'min' => $ratio_categories[$ratio_category][0],
554      'max' => array_pop($ratio_categories[$ratio_category]),
555      );
556  }
557}
558
559// selected=bound if nothing selected
560foreach (array_keys($dimensions['bounds']) as $type)
561{
562  $dimensions['selected'][$type] = isset($_SESSION['bulk_manager_filter']['dimension'][$type])
563    ? $_SESSION['bulk_manager_filter']['dimension'][$type]
564    : $dimensions['bounds'][$type]
565  ;
566}
567
568$template->assign('dimensions', $dimensions);
569
570
571// +-----------------------------------------------------------------------+
572// |                         open specific mode                            |
573// +-----------------------------------------------------------------------+
574
575include(PHPWG_ROOT_PATH.'admin/batch_manager_'.$page['tab'].'.php');
576?>
Note: See TracBrowser for help on using the repository browser.