source: branches/2.2/admin/batch_manager.php @ 10355

Last change on this file since 10355 was 10355, checked in by patdenice, 13 years ago

merge r10354 from trunk to branch 2.2
feature:2264
new trigger to add prefilter on batch manager

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