source: trunk/admin/batch_manager.php @ 19069

Last change on this file since 19069 was 19069, checked in by plg, 12 years ago

feature 2718: Batch Manager filter on dimensions, only display existing values

improved colors on ui-slider

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