source: trunk/admin/batch_manager.php @ 18459

Revision 18459, 12.6 KB checked in by mistic100, 7 years ago (diff)

bug:2756 feature:2737 on batch manager use "tag" url parameter instead of re-use "cat"

RevLine 
[8394]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[8394]4// +-----------------------------------------------------------------------+
[12922]5// | Copyright(C) 2008-2012 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
46// +-----------------------------------------------------------------------+
47// |                      initialize current set                           |
48// +-----------------------------------------------------------------------+
49
50if (isset($_POST['submitFilter']))
51{
52  // echo '<pre>'; print_r($_POST); echo '</pre>';
[17289]53  unset($_REQUEST['start']); // new photo set must reset the page
[8394]54  $_SESSION['bulk_manager_filter'] = array();
55
56  if (isset($_POST['filter_prefilter_use']))
57  {
[10354]58    $_SESSION['bulk_manager_filter']['prefilter'] = $_POST['filter_prefilter'];
[8394]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
[11853]71  if (isset($_POST['filter_tags_use']))
72  {
73    $_SESSION['bulk_manager_filter']['tags'] = get_tag_ids($_POST['filter_tags'], false);
[12630]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    }
[11853]79  }
80
[8394]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'];
[13646]86     
87      if (isset($_POST['filter_level_include_lower']))
88      {
89        $_SESSION['bulk_manager_filter']['level_include_lower'] = true;
90      }
[8394]91    }
92  }
[17931]93 
94  if (isset($_POST['filter_dimension_use']))
95  {
96    if ( $_POST['filter_dimension'] != 'format' and !preg_match('#^[0-9]+$#', $_POST['filter_dimension_'. $_POST['filter_dimension'] ]) )
97    {
98      array_push($page['errors'], l10n('Invalid dimension'));
99    }
100    else
101    {
102      $_SESSION['bulk_manager_filter']['dimension'] = $_POST['filter_dimension'];
103      $_SESSION['bulk_manager_filter']['dimension_'. $_POST['filter_dimension'] ] = $_POST['filter_dimension_'. $_POST['filter_dimension'] ];
104    }
105  }
[8394]106}
[18459]107else if (isset($_GET['cat']))
[8394]108{
109  if ('caddie' == $_GET['cat'])
110  {
111    $_SESSION['bulk_manager_filter'] = array(
112      'prefilter' => 'caddie'
113      );
114  }
[18459]115  else if ('recent' == $_GET['cat'])
[8423]116  {
117    $_SESSION['bulk_manager_filter'] = array(
118      'prefilter' => 'last import'
119      );
120  }
[18459]121  else if (is_numeric($_GET['cat']))
[8394]122  {
123    $_SESSION['bulk_manager_filter'] = array(
124      'category' => $_GET['cat']
125      );
126  }
[18459]127}
128else if (isset($_GET['tag']))
129{
130  if (is_numeric($_GET['tag']))
[17765]131  {
[18459]132    $_SESSION['bulk_manager_filter'] = array(
133      'tags' => array($_GET['tag']),
134      'tag_mode' => 'AND',
135      );
[17765]136  }
[8394]137}
138
139if (!isset($_SESSION['bulk_manager_filter']))
140{
141  $_SESSION['bulk_manager_filter'] = array(
142    'prefilter' => 'caddie'
143    );
144}
145
146// echo '<pre>'; print_r($_SESSION['bulk_manager_filter']); echo '</pre>';
147
148// depending on the current filter (in session), we find the appropriate
149// photos
150$filter_sets = array();
151if (isset($_SESSION['bulk_manager_filter']['prefilter']))
152{
153  if ('caddie' == $_SESSION['bulk_manager_filter']['prefilter'])
154  {
155    $query = '
156SELECT element_id
157  FROM '.CADDIE_TABLE.'
158  WHERE user_id = '.$user['id'].'
159;';
160    array_push(
161      $filter_sets,
162      array_from_query($query, 'element_id')
163      );
164  }
165
166  if ('last import'== $_SESSION['bulk_manager_filter']['prefilter'])
167  {
168    $query = '
169SELECT MAX(date_available) AS date
170  FROM '.IMAGES_TABLE.'
171;';
172    $row = pwg_db_fetch_assoc(pwg_query($query));
173    if (!empty($row['date']))
174    {
175      $query = '
176SELECT id
177  FROM '.IMAGES_TABLE.'
178  WHERE date_available BETWEEN '.pwg_db_get_recent_period_expression(1, $row['date']).' AND \''.$row['date'].'\'
179;';
180      array_push(
181        $filter_sets,
182        array_from_query($query, 'id')
183        );
184    }
185  }
[8403]186
187  if ('with no virtual album' == $_SESSION['bulk_manager_filter']['prefilter'])
188  {
189    // we are searching elements not linked to any virtual category
190    $query = '
191 SELECT id
192   FROM '.IMAGES_TABLE.'
193 ;';
194    $all_elements = array_from_query($query, 'id');
[9068]195
[8403]196    $query = '
197 SELECT id
198   FROM '.CATEGORIES_TABLE.'
199   WHERE dir IS NULL
200 ;';
201    $virtual_categories = array_from_query($query, 'id');
202    if (!empty($virtual_categories))
203    {
204      $query = '
205 SELECT DISTINCT(image_id)
206   FROM '.IMAGE_CATEGORY_TABLE.'
207   WHERE category_id IN ('.implode(',', $virtual_categories).')
208 ;';
209      $linked_to_virtual = array_from_query($query, 'image_id');
210    }
211
212    array_push(
213      $filter_sets,
214      array_diff($all_elements, $linked_to_virtual)
215      );
216  }
[8404]217
[8419]218  if ('with no album' == $_SESSION['bulk_manager_filter']['prefilter'])
219  {
220    $query = '
221SELECT
222    id
223  FROM '.IMAGES_TABLE.'
224    LEFT JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id
225  WHERE category_id is null
226;';
227    array_push(
228      $filter_sets,
229      array_from_query($query, 'id')
230      );
231  }
232
[8422]233  if ('with no tag' == $_SESSION['bulk_manager_filter']['prefilter'])
234  {
235    $query = '
236SELECT
237    id
238  FROM '.IMAGES_TABLE.'
239    LEFT JOIN '.IMAGE_TAG_TABLE.' ON id = image_id
240  WHERE tag_id is null
241;';
242    array_push(
243      $filter_sets,
244      array_from_query($query, 'id')
245      );
246  }
247
248
[8404]249  if ('duplicates' == $_SESSION['bulk_manager_filter']['prefilter'])
250  {
251    // we could use the group_concat MySQL function to retrieve the list of
252    // image_ids but it would not be compatible with PostgreSQL, so let's
253    // perform 2 queries instead. We hope there are not too many duplicates.
[9068]254
[8404]255    $query = '
256SELECT file
257  FROM '.IMAGES_TABLE.'
258  GROUP BY file
259  HAVING COUNT(*) > 1
260;';
261    $duplicate_files = array_from_query($query, 'file');
[9068]262
[8404]263    $query = '
264SELECT id
265  FROM '.IMAGES_TABLE.'
266  WHERE file IN (\''.implode("','", $duplicate_files).'\')
267;';
268
269    array_push(
270      $filter_sets,
271      array_from_query($query, 'id')
272      );
273  }
[9912]274
275  if ('all photos' == $_SESSION['bulk_manager_filter']['prefilter'])
276  {
277    $query = '
278SELECT id
279  FROM '.IMAGES_TABLE.'
[14689]280  '.$conf['order_by'];
[9912]281
[14689]282    $filter_sets[] = array_from_query($query, 'id');
[9912]283  }
[10354]284
[10380]285  $filter_sets = trigger_event('perform_batch_manager_prefilters', $filter_sets, $_SESSION['bulk_manager_filter']['prefilter']);
[8394]286}
287
288if (isset($_SESSION['bulk_manager_filter']['category']))
289{
290  $categories = array();
[9068]291
[8394]292  if (isset($_SESSION['bulk_manager_filter']['category_recursive']))
293  {
294    $categories = get_subcat_ids(array($_SESSION['bulk_manager_filter']['category']));
295  }
296  else
297  {
298    $categories = array($_SESSION['bulk_manager_filter']['category']);
299  }
[9068]300
[8394]301  $query = '
302 SELECT DISTINCT(image_id)
303   FROM '.IMAGE_CATEGORY_TABLE.'
304   WHERE category_id IN ('.implode(',', $categories).')
305 ;';
306  array_push(
307    $filter_sets,
308    array_from_query($query, 'image_id')
309    );
310}
311
312if (isset($_SESSION['bulk_manager_filter']['level']))
313{
[13646]314  $operator = '=';
315  if (isset($_SESSION['bulk_manager_filter']['level_include_lower']))
316  {
317    $operator = '<=';
318  }
319 
[8394]320  $query = '
321SELECT id
322  FROM '.IMAGES_TABLE.'
[13646]323  WHERE level '.$operator.' '.$_SESSION['bulk_manager_filter']['level'].'
[14689]324  '.$conf['order_by'];
325
326  $filter_sets[] = array_from_query($query, 'id');
[8394]327}
328
[11853]329if (!empty($_SESSION['bulk_manager_filter']['tags']))
330{
[12630]331  array_push(
[11853]332    $filter_sets,
[12630]333    get_image_ids_for_tags(
334      $_SESSION['bulk_manager_filter']['tags'],
335      $_SESSION['bulk_manager_filter']['tag_mode'],
336      null,
337      null,
338      false // we don't apply permissions in administration screens
339      )
340    );
[11853]341}
342
[17931]343if (isset($_SESSION['bulk_manager_filter']['dimension']))
344{
345  switch ($_SESSION['bulk_manager_filter']['dimension'])
346  {
347    case 'min_width':
348      $where_clause = 'width >= '.$_SESSION['bulk_manager_filter']['dimension_min_width']; break;
349    case 'max_width':
350      $where_clause = 'width <= '.$_SESSION['bulk_manager_filter']['dimension_max_width']; break;
351    case 'min_height':
352      $where_clause = 'height >= '.$_SESSION['bulk_manager_filter']['dimension_min_height']; break;
353    case 'max_height':
354      $where_clause = 'height <= '.$_SESSION['bulk_manager_filter']['dimension_max_height']; break;
355    case 'format':
356    {
357      switch ($_SESSION['bulk_manager_filter']['dimension_format'])
358      {
359        case 'portrait':
360          $where_clause = 'width/height < 0.95'; break;
361        case 'square':
362          $where_clause = 'width/height >= 0.95 AND width/height <= 1.05'; break;
363        case 'landscape':
364          $where_clause = 'width/height > 1.05 AND width/height < 2.5'; break;
365        case 'panorama':
366          $where_clause = 'width/height >= 2.5'; break;
367      }
368      break;
369    }
370  }
371 
372  $query = '
373SELECT id
374  FROM '.IMAGES_TABLE.'
375  WHERE '.$where_clause.'
376  '.$conf['order_by'];
377
378  $filter_sets[] = array_from_query($query, 'id');
379}
380
[8394]381$current_set = array_shift($filter_sets);
382foreach ($filter_sets as $set)
383{
384  $current_set = array_intersect($current_set, $set);
385}
386$page['cat_elements_id'] = $current_set;
387
388// +-----------------------------------------------------------------------+
389// |                       first element to display                        |
390// +-----------------------------------------------------------------------+
391
392// $page['start'] contains the number of the first element in its
393// category. For exampe, $page['start'] = 12 means we must show elements #12
394// and $page['nb_images'] next elements
395
[17289]396if (!isset($_REQUEST['start'])
397    or !is_numeric($_REQUEST['start'])
398    or $_REQUEST['start'] < 0
399    or (isset($_REQUEST['display']) and 'all' == $_REQUEST['display']))
[8394]400{
401  $page['start'] = 0;
402}
403else
404{
[17289]405  $page['start'] = $_REQUEST['start'];
[8394]406}
407
408// +-----------------------------------------------------------------------+
[8413]409// |                                 Tabs                                  |
[8394]410// +-----------------------------------------------------------------------+
[16928]411$manager_link = get_root_url().'admin.php?page=batch_manager&amp;mode=';
[8394]412
[9068]413if (isset($_GET['mode']))
[8394]414{
[8413]415  $page['tab'] = $_GET['mode'];
[8394]416}
[8413]417else
418{
[16928]419  $page['tab'] = 'global';
[8413]420}
421
[16928]422$tabsheet = new tabsheet();
423$tabsheet->set_id('batch_manager');
424$tabsheet->select($page['tab']);
425$tabsheet->assign();
[8413]426
427// +-----------------------------------------------------------------------+
[11039]428// |                              tags                                     |
429// +-----------------------------------------------------------------------+
430
431$query = '
[11853]432SELECT id, name
[11039]433  FROM '.TAGS_TABLE.'
434;';
[12259]435$template->assign('tags', get_taglist($query, false));
[11039]436
437// +-----------------------------------------------------------------------+
[8413]438// |                         open specific mode                            |
439// +-----------------------------------------------------------------------+
440
[16928]441include(PHPWG_ROOT_PATH.'admin/batch_manager_'.$page['tab'].'.php');
[9068]442?>
Note: See TracBrowser for help on using the repository browser.