source: trunk/admin/batch_manager.php @ 23746

Last change on this file since 23746 was 23746, checked in by flop25, 11 years ago

bug:2808
add "Your Favorites" to the filters of the Batch Manager

File size: 15.7 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2013 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 ('favorites' == $_SESSION['bulk_manager_filter']['prefilter'])
172  {
173    $query = '
174SELECT image_id
175  FROM '.FAVORITES_TABLE.'
176  WHERE user_id = '.$user['id'].'
177;';
178    array_push(
179      $filter_sets,
180      array_from_query($query, 'image_id')
181      );
182  }
183
184  if ('last import'== $_SESSION['bulk_manager_filter']['prefilter'])
185  {
186    $query = '
187SELECT MAX(date_available) AS date
188  FROM '.IMAGES_TABLE.'
189;';
190    $row = pwg_db_fetch_assoc(pwg_query($query));
191    if (!empty($row['date']))
192    {
193      $query = '
194SELECT id
195  FROM '.IMAGES_TABLE.'
196  WHERE date_available BETWEEN '.pwg_db_get_recent_period_expression(1, $row['date']).' AND \''.$row['date'].'\'
197;';
198      array_push(
199        $filter_sets,
200        array_from_query($query, 'id')
201        );
202    }
203  }
204
205  if ('with no virtual album' == $_SESSION['bulk_manager_filter']['prefilter'])
206  {
207    // we are searching elements not linked to any virtual category
208    $query = '
209 SELECT id
210   FROM '.IMAGES_TABLE.'
211 ;';
212    $all_elements = array_from_query($query, 'id');
213
214    $query = '
215 SELECT id
216   FROM '.CATEGORIES_TABLE.'
217   WHERE dir IS NULL
218 ;';
219    $virtual_categories = array_from_query($query, 'id');
220    if (!empty($virtual_categories))
221    {
222      $query = '
223 SELECT DISTINCT(image_id)
224   FROM '.IMAGE_CATEGORY_TABLE.'
225   WHERE category_id IN ('.implode(',', $virtual_categories).')
226 ;';
227      $linked_to_virtual = array_from_query($query, 'image_id');
228    }
229
230    array_push(
231      $filter_sets,
232      array_diff($all_elements, $linked_to_virtual)
233      );
234  }
235
236  if ('with no album' == $_SESSION['bulk_manager_filter']['prefilter'])
237  {
238    $query = '
239SELECT
240    id
241  FROM '.IMAGES_TABLE.'
242    LEFT JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
243  WHERE category_id is null
244;';
245    array_push(
246      $filter_sets,
247      array_from_query($query, 'id')
248      );
249  }
250
251  if ('with no tag' == $_SESSION['bulk_manager_filter']['prefilter'])
252  {
253    $query = '
254SELECT
255    id
256  FROM '.IMAGES_TABLE.'
257    LEFT JOIN '.IMAGE_TAG_TABLE.' ON id = image_id
258  WHERE tag_id is null
259;';
260    array_push(
261      $filter_sets,
262      array_from_query($query, 'id')
263      );
264  }
265
266
267  if ('duplicates' == $_SESSION['bulk_manager_filter']['prefilter'])
268  {
269    // we could use the group_concat MySQL function to retrieve the list of
270    // image_ids but it would not be compatible with PostgreSQL, so let's
271    // perform 2 queries instead. We hope there are not too many duplicates.
272
273    $query = '
274SELECT file
275  FROM '.IMAGES_TABLE.'
276  GROUP BY file
277  HAVING COUNT(*) > 1
278;';
279    $duplicate_files = array_from_query($query, 'file');
280
281    $query = '
282SELECT id
283  FROM '.IMAGES_TABLE.'
284  WHERE file IN (\''.implode("','", $duplicate_files).'\')
285;';
286
287    array_push(
288      $filter_sets,
289      array_from_query($query, 'id')
290      );
291  }
292
293  if ('all photos' == $_SESSION['bulk_manager_filter']['prefilter'])
294  {
295    $query = '
296SELECT id
297  FROM '.IMAGES_TABLE.'
298  '.$conf['order_by'];
299
300    $filter_sets[] = array_from_query($query, 'id');
301  }
302
303  $filter_sets = trigger_event('perform_batch_manager_prefilters', $filter_sets, $_SESSION['bulk_manager_filter']['prefilter']);
304}
305
306if (isset($_SESSION['bulk_manager_filter']['category']))
307{
308  $categories = array();
309
310  if (isset($_SESSION['bulk_manager_filter']['category_recursive']))
311  {
312    $categories = get_subcat_ids(array($_SESSION['bulk_manager_filter']['category']));
313  }
314  else
315  {
316    $categories = array($_SESSION['bulk_manager_filter']['category']);
317  }
318
319  $query = '
320 SELECT DISTINCT(image_id)
321   FROM '.IMAGE_CATEGORY_TABLE.'
322   WHERE category_id IN ('.implode(',', $categories).')
323 ;';
324  array_push(
325    $filter_sets,
326    array_from_query($query, 'image_id')
327    );
328}
329
330if (isset($_SESSION['bulk_manager_filter']['level']))
331{
332  $operator = '=';
333  if (isset($_SESSION['bulk_manager_filter']['level_include_lower']))
334  {
335    $operator = '<=';
336  }
337 
338  $query = '
339SELECT id
340  FROM '.IMAGES_TABLE.'
341  WHERE level '.$operator.' '.$_SESSION['bulk_manager_filter']['level'].'
342  '.$conf['order_by'];
343
344  $filter_sets[] = array_from_query($query, 'id');
345}
346
347if (!empty($_SESSION['bulk_manager_filter']['tags']))
348{
349  array_push(
350    $filter_sets,
351    get_image_ids_for_tags(
352      $_SESSION['bulk_manager_filter']['tags'],
353      $_SESSION['bulk_manager_filter']['tag_mode'],
354      null,
355      null,
356      false // we don't apply permissions in administration screens
357      )
358    );
359}
360
361if (isset($_SESSION['bulk_manager_filter']['dimension']))
362{
363  $where_clauses = array();
364  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_width']))
365  {
366    $where_clause[] = 'width >= '.$_SESSION['bulk_manager_filter']['dimension']['min_width'];
367  }
368  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_width']))
369  {
370    $where_clause[] = 'width <= '.$_SESSION['bulk_manager_filter']['dimension']['max_width'];
371  }
372  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_height']))
373  {
374    $where_clause[] = 'height >= '.$_SESSION['bulk_manager_filter']['dimension']['min_height'];
375  }
376  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_height']))
377  {
378    $where_clause[] = 'height <= '.$_SESSION['bulk_manager_filter']['dimension']['max_height'];
379  }
380  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_ratio']))
381  {
382    $where_clause[] = 'width/height >= '.$_SESSION['bulk_manager_filter']['dimension']['min_ratio'];
383  }
384  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_ratio']))
385  {
386    // max_ratio is a floor value, so must be a bit increased
387    $where_clause[] = 'width/height < '.($_SESSION['bulk_manager_filter']['dimension']['max_ratio']+0.01);
388  }
389 
390  $query = '
391SELECT id
392  FROM '.IMAGES_TABLE.'
393  WHERE '.implode(' AND ',$where_clause).'
394  '.$conf['order_by'];
395
396  $filter_sets[] = array_from_query($query, 'id');
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// |                       first element to display                        |
408// +-----------------------------------------------------------------------+
409
410// $page['start'] contains the number of the first element in its
411// category. For exampe, $page['start'] = 12 means we must show elements #12
412// and $page['nb_images'] next elements
413
414if (!isset($_REQUEST['start'])
415    or !is_numeric($_REQUEST['start'])
416    or $_REQUEST['start'] < 0
417    or (isset($_REQUEST['display']) and 'all' == $_REQUEST['display']))
418{
419  $page['start'] = 0;
420}
421else
422{
423  $page['start'] = $_REQUEST['start'];
424}
425
426// +-----------------------------------------------------------------------+
427// |                                 Tabs                                  |
428// +-----------------------------------------------------------------------+
429$manager_link = get_root_url().'admin.php?page=batch_manager&amp;mode=';
430
431if (isset($_GET['mode']))
432{
433  $page['tab'] = $_GET['mode'];
434}
435else
436{
437  $page['tab'] = 'global';
438}
439
440$tabsheet = new tabsheet();
441$tabsheet->set_id('batch_manager');
442$tabsheet->select($page['tab']);
443$tabsheet->assign();
444
445// +-----------------------------------------------------------------------+
446// |                              tags                                     |
447// +-----------------------------------------------------------------------+
448
449$query = '
450SELECT id, name
451  FROM '.TAGS_TABLE.'
452;';
453$template->assign('tags', get_taglist($query, false));
454
455// +-----------------------------------------------------------------------+
456// |                              dimensions                               |
457// +-----------------------------------------------------------------------+
458
459$widths = array();
460$heights = array();
461$ratios = array();
462
463// get all width, height and ratios
464$query = '
465SELECT
466  DISTINCT width, height
467  FROM '.IMAGES_TABLE.'
468  WHERE width IS NOT NULL
469    AND height IS NOT NULL
470;';
471$result = pwg_query($query);
472
473if (!pwg_db_num_rows($result))
474{ // arbitrary values, only used when no photos on the gallery
475  $widths = array(600, 1920, 3500);
476  $heights = array(480, 1080, 2300);
477  $ratios = array(1.25, 1.52, 1.78);
478}
479else
480{
481  while ($row = pwg_db_fetch_assoc($result))
482  {
483    $widths[] = $row['width'];
484    $heights[] = $row['height'];
485    $ratios[] = floor($row['width'] * 100 / $row['height']) / 100;
486  }
487}
488
489
490$widths = array_unique($widths);
491sort($widths);
492
493$heights = array_unique($heights);
494sort($heights);
495
496$ratios = array_unique($ratios);
497sort($ratios);
498
499$dimensions['widths'] = implode(',', $widths);
500$dimensions['heights'] = implode(',', $heights);
501$dimensions['ratios'] = implode(',', $ratios);
502
503$dimensions['bounds'] = array(
504  'min_width' => $widths[0],
505  'max_width' => $widths[count($widths)-1],
506  'min_height' => $heights[0],
507  'max_height' => $heights[count($heights)-1],
508  'min_ratio' => $ratios[0],
509  'max_ratio' => $ratios[count($ratios)-1],
510  );
511
512// find ratio categories
513$ratio_categories = array(
514  'portrait' => array(),
515  'square' => array(),
516  'landscape' => array(),
517  'panorama' => array(),
518  );
519
520foreach ($ratios as $ratio)
521{
522  if ($ratio < 0.95)
523  {
524    $ratio_categories['portrait'][] = $ratio;
525  }
526  else if ($ratio >= 0.95 and $ratio <= 1.05)
527  {
528    $ratio_categories['square'][] = $ratio;
529  }
530  else if ($ratio > 1.05 and $ratio < 2)
531  {
532    $ratio_categories['landscape'][] = $ratio;
533  }
534  else if ($ratio >= 2)
535  {
536    $ratio_categories['panorama'][] = $ratio;
537  }
538}
539
540foreach (array_keys($ratio_categories) as $ratio_category)
541{
542  if (count($ratio_categories[$ratio_category]) > 0)
543  {
544    $dimensions['ratio_'.$ratio_category] = array(
545      'min' => $ratio_categories[$ratio_category][0],
546      'max' => $ratio_categories[$ratio_category][count($ratio_categories[$ratio_category]) - 1]
547      );
548  }
549}
550
551// selected=bound if nothing selected
552foreach (array_keys($dimensions['bounds']) as $type)
553{
554  $dimensions['selected'][$type] = isset($_SESSION['bulk_manager_filter']['dimension'][$type])
555    ? $_SESSION['bulk_manager_filter']['dimension'][$type]
556    : $dimensions['bounds'][$type]
557  ;
558}
559
560$template->assign('dimensions', $dimensions);
561
562
563// +-----------------------------------------------------------------------+
564// |                         open specific mode                            |
565// +-----------------------------------------------------------------------+
566
567include(PHPWG_ROOT_PATH.'admin/batch_manager_'.$page['tab'].'.php');
568?>
Note: See TracBrowser for help on using the repository browser.