source: trunk/admin/batch_manager.php @ 8419

Last change on this file since 8419 was 8419, checked in by plg, 14 years ago

feature 2091: Batch Manager, find all orphan photos

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