source: trunk/admin/batch_manager.php @ 27558

Last change on this file since 27558 was 26461, checked in by mistic100, 10 years ago

Update headers to 2014. Happy new year!!

File size: 15.6 KB
RevLine 
[8394]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[8394]4// +-----------------------------------------------------------------------+
[26461]5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
[8394]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');
[8413]36include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
[8394]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
[24834]46
[8394]47// +-----------------------------------------------------------------------+
48// |                      initialize current set                           |
49// +-----------------------------------------------------------------------+
50
[24834]51// filters from form
[8394]52if (isset($_POST['submitFilter']))
53{
54  // echo '<pre>'; print_r($_POST); echo '</pre>';
[17289]55  unset($_REQUEST['start']); // new photo set must reset the page
[8394]56  $_SESSION['bulk_manager_filter'] = array();
57
58  if (isset($_POST['filter_prefilter_use']))
59  {
[10354]60    $_SESSION['bulk_manager_filter']['prefilter'] = $_POST['filter_prefilter'];
[8394]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
[11853]73  if (isset($_POST['filter_tags_use']))
74  {
75    $_SESSION['bulk_manager_filter']['tags'] = get_tag_ids($_POST['filter_tags'], false);
[12630]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    }
[11853]81  }
82
[8394]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'];
[13646]88     
89      if (isset($_POST['filter_level_include_lower']))
90      {
91        $_SESSION['bulk_manager_filter']['level_include_lower'] = true;
92      }
[8394]93    }
94  }
[17931]95 
96  if (isset($_POST['filter_dimension_use']))
97  {
[18758]98    foreach (array('min_width','max_width','min_height','max_height') as $type)
[17931]99    {
[18758]100      if ( preg_match('#^[0-9]+$#', $_POST['filter_dimension_'. $type ]) )
101      {
102        $_SESSION['bulk_manager_filter']['dimension'][$type] = $_POST['filter_dimension_'. $type ];
103      }
[17931]104    }
[18988]105    foreach (array('min_ratio','max_ratio') as $type)
[17931]106    {
[18988]107      if ( preg_match('#^[0-9\.]+$#', $_POST['filter_dimension_'. $type ]) )
[18758]108      {
[18988]109        $_SESSION['bulk_manager_filter']['dimension'][$type] = $_POST['filter_dimension_'. $type ];
[18758]110      }
[17931]111    }
112  }
[8394]113}
[24834]114// filters from url
115else if (isset($_GET['filter']))
[8394]116{
[24834]117  if (!is_array($_GET['filter']))
[8394]118  {
[24834]119    $_GET['filter'] = explode(',', $_GET['filter']);
[8394]120  }
[24834]121 
122  $_SESSION['bulk_manager_filter'] = array();
123 
124  foreach ($_GET['filter'] as $filter)
[8423]125  {
[24834]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    }
[8423]156  }
[18459]157}
[8394]158
[24834]159if (empty($_SESSION['bulk_manager_filter']))
[8394]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
[24834]168// depending on the current filter (in session), we find the appropriate photos
[8394]169$filter_sets = array();
170if (isset($_SESSION['bulk_manager_filter']['prefilter']))
171{
[24834]172  switch ($_SESSION['bulk_manager_filter']['prefilter'])
[8394]173  {
[24834]174  case 'caddie':
[8394]175    $query = '
176SELECT element_id
177  FROM '.CADDIE_TABLE.'
178  WHERE user_id = '.$user['id'].'
179;';
[25018]180    $filter_sets[] = array_from_query($query, 'element_id');
[24834]181   
182    break;
[8394]183
[24834]184  case 'favorites':
[23746]185    $query = '
186SELECT image_id
187  FROM '.FAVORITES_TABLE.'
188  WHERE user_id = '.$user['id'].'
189;';
[25018]190    $filter_sets[] = array_from_query($query, 'image_id');
[24834]191   
192    break;
[23746]193
[24834]194  case 'last_import':
[8394]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;';
[25018]207      $filter_sets[] = array_from_query($query, 'id');
[8394]208    }
[24834]209   
210    break;
[8403]211
[24834]212  case 'no_virtual_album':
[8403]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');
[9068]219
[8403]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
[25018]236    $filter_sets[] = array_diff($all_elements, $linked_to_virtual);
[24834]237   
238    break;
[8404]239
[24834]240  case 'no_album':
[8419]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;';
[25018]248    $filter_sets[] = array_from_query($query, 'id');
[24834]249   
250    break;
[8419]251
[24834]252  case 'no_tag':
[8422]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;';
[25018]260    $filter_sets[] = array_from_query($query, 'id');
[24834]261   
262    break;
[8422]263
264
[24834]265  case 'duplicates':
[8404]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');
[9068]276
[8404]277    $query = '
278SELECT id
279  FROM '.IMAGES_TABLE.'
[25224]280  WHERE file IN (\''.implode("','", array_map('pwg_db_real_escape_string', $duplicate_files)).'\')
[8404]281;';
[25018]282    $filter_sets[] = array_from_query($query, 'id');
[24834]283   
284    break;
[9912]285
[24834]286  case 'all_photos':
[9912]287    $query = '
288SELECT id
289  FROM '.IMAGES_TABLE.'
[14689]290  '.$conf['order_by'];
[9912]291
[14689]292    $filter_sets[] = array_from_query($query, 'id');
[24834]293   
294    break;
[9912]295  }
[10354]296
[10380]297  $filter_sets = trigger_event('perform_batch_manager_prefilters', $filter_sets, $_SESSION['bulk_manager_filter']['prefilter']);
[8394]298}
299
300if (isset($_SESSION['bulk_manager_filter']['category']))
301{
302  $categories = array();
[9068]303
[8394]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  }
[9068]312
[8394]313  $query = '
314 SELECT DISTINCT(image_id)
315   FROM '.IMAGE_CATEGORY_TABLE.'
316   WHERE category_id IN ('.implode(',', $categories).')
317 ;';
[25018]318  $filter_sets[] = array_from_query($query, 'image_id');
[8394]319}
320
321if (isset($_SESSION['bulk_manager_filter']['level']))
322{
[13646]323  $operator = '=';
324  if (isset($_SESSION['bulk_manager_filter']['level_include_lower']))
325  {
326    $operator = '<=';
327  }
328 
[8394]329  $query = '
330SELECT id
331  FROM '.IMAGES_TABLE.'
[13646]332  WHERE level '.$operator.' '.$_SESSION['bulk_manager_filter']['level'].'
[14689]333  '.$conf['order_by'];
334
335  $filter_sets[] = array_from_query($query, 'id');
[8394]336}
337
[11853]338if (!empty($_SESSION['bulk_manager_filter']['tags']))
339{
[25018]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
[12630]346    );
[11853]347}
348
[18988]349if (isset($_SESSION['bulk_manager_filter']['dimension']))
[17931]350{
[18758]351  $where_clauses = array();
352  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_width']))
[17931]353  {
[18758]354    $where_clause[] = 'width >= '.$_SESSION['bulk_manager_filter']['dimension']['min_width'];
[17931]355  }
[18758]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  }
[18988]368  if (isset($_SESSION['bulk_manager_filter']['dimension']['min_ratio']))
[18758]369  {
[18988]370    $where_clause[] = 'width/height >= '.$_SESSION['bulk_manager_filter']['dimension']['min_ratio'];
[18758]371  }
[18988]372  if (isset($_SESSION['bulk_manager_filter']['dimension']['max_ratio']))
[18758]373  {
[19121]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);
[18758]376  }
[17931]377 
378  $query = '
379SELECT id
380  FROM '.IMAGES_TABLE.'
[18758]381  WHERE '.implode(' AND ',$where_clause).'
[17931]382  '.$conf['order_by'];
383
384  $filter_sets[] = array_from_query($query, 'id');
385}
386
[8394]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
[24834]394
[8394]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
[17289]403if (!isset($_REQUEST['start'])
404    or !is_numeric($_REQUEST['start'])
405    or $_REQUEST['start'] < 0
406    or (isset($_REQUEST['display']) and 'all' == $_REQUEST['display']))
[8394]407{
408  $page['start'] = 0;
409}
410else
411{
[17289]412  $page['start'] = $_REQUEST['start'];
[8394]413}
414
[24834]415
[8394]416// +-----------------------------------------------------------------------+
[8413]417// |                                 Tabs                                  |
[8394]418// +-----------------------------------------------------------------------+
[16928]419$manager_link = get_root_url().'admin.php?page=batch_manager&amp;mode=';
[8394]420
[9068]421if (isset($_GET['mode']))
[8394]422{
[8413]423  $page['tab'] = $_GET['mode'];
[8394]424}
[8413]425else
426{
[16928]427  $page['tab'] = 'global';
[8413]428}
429
[16928]430$tabsheet = new tabsheet();
431$tabsheet->set_id('batch_manager');
432$tabsheet->select($page['tab']);
433$tabsheet->assign();
[8413]434
[24834]435
[8413]436// +-----------------------------------------------------------------------+
[11039]437// |                              tags                                     |
438// +-----------------------------------------------------------------------+
439
440$query = '
[11853]441SELECT id, name
[11039]442  FROM '.TAGS_TABLE.'
443;';
[12259]444$template->assign('tags', get_taglist($query, false));
[11039]445
[24834]446
[11039]447// +-----------------------------------------------------------------------+
[18988]448// |                              dimensions                               |
449// +-----------------------------------------------------------------------+
[19069]450
451$widths = array();
452$heights = array();
453$ratios = array();
454
[19121]455// get all width, height and ratios
[18988]456$query = '
457SELECT
[19069]458  DISTINCT width, height
[18988]459  FROM '.IMAGES_TABLE.'
[19069]460  WHERE width IS NOT NULL
461    AND height IS NOT NULL
[18988]462;';
[19069]463$result = pwg_query($query);
[19121]464
[24835]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))
[19920]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}
[19069]483
[19920]484
[24835]485
[19069]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
[19121]508// find ratio categories
[19069]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  }
[19121]522  else if ($ratio >= 0.95 and $ratio <= 1.05)
[19069]523  {
524    $ratio_categories['square'][] = $ratio;
525  }
[19121]526  else if ($ratio > 1.05 and $ratio < 2)
[19069]527  {
528    $ratio_categories['landscape'][] = $ratio;
529  }
[19121]530  else if ($ratio >= 2)
[19069]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],
[24835]542      'max' => array_pop($ratio_categories[$ratio_category]),
[19069]543      );
544  }
545}
546
[19121]547// selected=bound if nothing selected
[18988]548foreach (array_keys($dimensions['bounds']) as $type)
549{
[19069]550  $dimensions['selected'][$type] = isset($_SESSION['bulk_manager_filter']['dimension'][$type])
551    ? $_SESSION['bulk_manager_filter']['dimension'][$type]
552    : $dimensions['bounds'][$type]
553  ;
[18988]554}
[19069]555
[18988]556$template->assign('dimensions', $dimensions);
557
558
559// +-----------------------------------------------------------------------+
[8413]560// |                         open specific mode                            |
561// +-----------------------------------------------------------------------+
562
[16928]563include(PHPWG_ROOT_PATH.'admin/batch_manager_'.$page['tab'].'.php');
[9068]564?>
Note: See TracBrowser for help on using the repository browser.