source: trunk/admin/batch_manager.php @ 25085

Last change on this file since 25085 was 25018, checked in by mistic100, 10 years ago

remove all array_push (50% slower than []) + some changes missing for feature:2978

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