source: trunk/admin/batch_manager.php @ 9912

Revision 9912, 9.8 KB checked in by patdenice, 9 years ago (diff)

feature:2236
add "all photos" prefilter

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