source: trunk/admin/batch_manager.php @ 12851

Last change on this file since 12851 was 12630, checked in by plg, 13 years ago

merge r12629 from branch 2.3 to trunk

bug 2506 fixed: Batch Manager does not take permissions into account when
filtering on tags

feature 2507 added: Batch Manager can filter on "all tags" or "any tag"

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