source: trunk/admin/batch_manager_global.php @ 8398

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

feature 2089: report feature:1845 in the new Batch Manager.

+ once the photos are deleted, redirect the page to reset the current photo set
and avoid being on an empty 2nd page.

File size: 15.8 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');
36
37// +-----------------------------------------------------------------------+
38// | Check Access and exit when user status is not ok                      |
39// +-----------------------------------------------------------------------+
40
41check_status(ACCESS_ADMINISTRATOR);
42
43trigger_action('loc_begin_element_set_global');
44
45// the $_POST['selection'] was already checked in element_set.php
46check_input_parameter('del_tags', $_POST, true, PATTERN_ID);
47check_input_parameter('associate', $_POST, false, PATTERN_ID);
48check_input_parameter('dissociate', $_POST, false, PATTERN_ID);
49
50// +-----------------------------------------------------------------------+
51// |                            current selection                          |
52// +-----------------------------------------------------------------------+
53
54$collection = array();
55if (isset($_POST['setSelected']))
56{
57  $collection = $page['cat_elements_id'];
58}
59else if (isset($_POST['selection']))
60{
61  $collection = $_POST['selection'];
62}
63
64// +-----------------------------------------------------------------------+
65// |                       global mode form submission                     |
66// +-----------------------------------------------------------------------+
67
68if (isset($_POST['submit']))
69{
70  // if the user tries to apply an action, it means that there is at least 1
71  // photo in the selection
72  if (count($collection) == 0)
73  {
74    array_push($page['errors'], l10n('Select at least one picture'));
75  }
76
77  $action = $_POST['selectAction'];
78 
79  if ('remove_from_caddie' == $action)
80  {
81    $query = '
82DELETE
83  FROM '.CADDIE_TABLE.'
84  WHERE element_id IN ('.implode(',', $collection).')
85    AND user_id = '.$user['id'].'
86;';
87    pwg_query($query);
88
89    // if we are here in the code, it means that the user is currently
90    // displaying the caddie content, so we have to remove the current
91    // selection from the current set
92    $page['cat_elements_id'] = array_diff($page['cat_elements_id'], $collection);
93  }
94
95  if ('add_tags' == $action)
96  {
97    $tag_ids = get_fckb_tag_ids($_POST['add_tags']);
98    add_tags($tag_ids, $collection);
99  }
100
101  if ('del_tags' == $action)
102  {
103    if (count($_POST['del_tags']) == 0)
104    {
105      array_push($page['errors'], l10n('Select at least one tag'));
106    }
107   
108    $query = '
109DELETE
110  FROM '.IMAGE_TAG_TABLE.'
111  WHERE image_id IN ('.implode(',', $collection).')
112    AND tag_id IN ('.implode(',', $_POST['del_tags']).')
113;';
114    pwg_query($query);
115  }
116
117  if ('associate' == $action)
118  {
119    associate_images_to_categories(
120      $collection,
121      array($_POST['associate'])
122      );
123  }
124
125  if ('dissociate' == $action)
126  {
127    // physical links must not be broken, so we must first retrieve image_id
128    // which create virtual links with the category to "dissociate from".
129    $query = '
130SELECT id
131  FROM '.IMAGE_CATEGORY_TABLE.'
132    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
133  WHERE category_id = '.$_POST['dissociate'].'
134    AND id IN ('.implode(',', $collection).')
135    AND (
136      category_id != storage_category_id
137      OR storage_category_id IS NULL
138    )
139;';
140    $dissociables = array_from_query($query, 'id');
141
142    if (!empty($dissociables))
143    {
144      $query = '
145DELETE
146  FROM '.IMAGE_CATEGORY_TABLE.'
147  WHERE category_id = '.$_POST['dissociate'].'
148    AND image_id IN ('.implode(',', $dissociables).')
149';
150      pwg_query($query);
151
152      // we remove the dissociated images if we are currently displaying the
153      // category to dissociate from.
154      //
155      // TODO we can display the photo of a given album without the $_GET['cat']
156      if (is_numeric($_GET['cat']) and $_POST['dissociate'] == $_GET['cat'])
157      {
158        $page['cat_elements_id'] = array_diff(
159          $page['cat_elements_id'],
160          $dissociables
161          );
162      }
163    }
164
165    update_category($_POST['dissociate']);
166  }
167
168  // author
169  if ('author' == $action)
170  {
171    $datas = array();
172    foreach ($collection as $image_id)
173    {
174      array_push(
175        $datas,
176        array(
177          'id' => $image_id,
178          'author' => $_POST['author']
179          )
180        );
181    }
182
183    mass_updates(
184      IMAGES_TABLE,
185      array('primary' => array('id'), 'update' => array('author')),
186      $datas
187      );
188  }
189
190  // name
191  if ('name' == $action)
192  {
193    $datas = array();
194    foreach ($collection as $image_id)
195    {
196      array_push(
197        $datas,
198        array(
199          'id' => $image_id,
200          'name' => $_POST['name']
201          )
202        );
203    }
204
205    mass_updates(
206      IMAGES_TABLE,
207      array('primary' => array('id'), 'update' => array('name')),
208      $datas
209      );
210  }
211 
212  // date_creation
213  if ('date_creation' == $action)
214  {
215    $date_creation = sprintf(
216      '%u-%u-%u',
217      $_POST['date_creation_year'],
218      $_POST['date_creation_month'],
219      $_POST['date_creation_day']
220      );
221
222    $datas = array();
223    foreach ($collection as $image_id)
224    {
225      array_push(
226        $datas,
227        array(
228          'id' => $image_id,
229          'date_creation' => $date_creation
230          )
231        );
232    }
233
234    mass_updates(
235      IMAGES_TABLE,
236      array('primary' => array('id'), 'update' => array('date_creation')),
237      $datas
238      );
239  }
240 
241  // privacy_level
242  if ('level' == $action)
243  {
244    $datas = array();
245    foreach ($collection as $image_id)
246    {
247      array_push(
248        $datas,
249        array(
250          'id' => $image_id,
251          'level' => $_POST['level']
252          )
253        );
254    }
255
256    mass_updates(
257      IMAGES_TABLE,
258      array('primary' => array('id'), 'update' => array('level')),
259      $datas
260      );
261  }
262 
263  // add_to_caddie
264  if ('add_to_caddie' == $action)
265  {
266    fill_caddie($collection);
267  }
268 
269  // delete
270  if ('delete' == $action)
271  {
272    if (isset($_POST['confirm_deletion']) and 1 == $_POST['confirm_deletion'])
273    {
274      $deleted_count = delete_elements($collection, true);
275      if ($deleted_count > 0)
276      {
277        $_SESSION['page_infos'] = array(
278          sprintf(
279            l10n_dec(
280              '%d photo was deleted',
281              '%d photos were deleted',
282              $deleted_count
283              ),
284            $deleted_count
285            )
286          );
287
288        $redirect_url = get_root_url().'admin.php?page='.$_GET['page'];
289        redirect($redirect_url);
290      }
291      else
292      {
293        array_push($page['errors'], l10n('No photo can be deleted'));
294      }
295    }
296    else
297    {
298      array_push($page['errors'], l10n('You need to confirm deletion'));
299    }
300  }
301}
302
303// +-----------------------------------------------------------------------+
304// |                             template init                             |
305// +-----------------------------------------------------------------------+
306$template->set_filenames(array('batch_manager_global' => 'batch_manager_global.tpl'));
307
308$base_url = get_root_url().'admin.php';
309
310$template->assign(
311  array(
312    'filter' => $_SESSION['bulk_manager_filter'],
313   
314    'selection' => $collection,
315   
316    'U_DISPLAY'=>$base_url.get_query_string_diff(array('display')),
317
318    'U_UNIT_MODE'
319    =>
320    $base_url
321    .get_query_string_diff(array('mode','display'))
322    .'&amp;mode=unit',
323
324    'F_ACTION'=>$base_url.get_query_string_diff(array('cat')),
325   )
326 );
327
328// +-----------------------------------------------------------------------+
329// |                            caddie options                             |
330// +-----------------------------------------------------------------------+
331
332$in_caddie = false;
333if (isset($_SESSION['bulk_manager_filter']['prefilter'])
334    and 'caddie' == $_SESSION['bulk_manager_filter']['prefilter'])
335{
336  $in_caddie = true;
337}
338$template->assign('IN_CADDIE', $in_caddie);
339
340// +-----------------------------------------------------------------------+
341// |                            deletion form                              |
342// +-----------------------------------------------------------------------+
343
344// we can only remove photos that have no storage_category_id, in other
345// word, it currently (Butterfly) means that the photo was added with
346// pLoader
347if (count($page['cat_elements_id']) > 0)
348{
349  $query = '
350SELECT
351    COUNT(*)
352  FROM '.IMAGES_TABLE.'
353  WHERE id IN ('.implode(',', $page['cat_elements_id']).')
354    AND storage_category_id IS NULL
355;';
356  list($counter) = pwg_db_fetch_row(pwg_query($query));
357
358  if ($counter > 0)
359  {
360    $template->assign('show_delete_form', true);
361  }
362}
363
364// +-----------------------------------------------------------------------+
365// |                           global mode form                            |
366// +-----------------------------------------------------------------------+
367
368// privacy level
369$template->assign(
370    array(
371      'filter_level_options'=> get_privacy_level_options(),
372      'filter_level_options_selected' => isset($_SESSION['bulk_manager_filter']['level'])
373        ? $_SESSION['bulk_manager_filter']['level']
374        : 0,
375    )
376  );
377
378// Virtualy associate a picture to a category
379$query = '
380SELECT id,name,uppercats,global_rank
381  FROM '.CATEGORIES_TABLE.'
382;';
383display_select_cat_wrapper($query, array(), 'associate_options', true);
384
385// in the filter box, which category to select by default
386$selected_category = array();
387
388if (isset($_SESSION['bulk_manager_filter']['category']))
389{
390  $selected_category = array($_SESSION['bulk_manager_filter']['category']);
391}
392else
393{
394  // we need to know the category in which the last photo was added
395  $selected_category = array();
396
397  $query = '
398SELECT
399    category_id,
400    id_uppercat
401  FROM '.IMAGES_TABLE.' AS i
402    JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON image_id = i.id
403    JOIN '.CATEGORIES_TABLE.' AS c ON category_id = c.id
404  ORDER BY i.id DESC
405  LIMIT 1
406;';
407  $result = pwg_query($query);
408  if (pwg_db_num_rows($result) > 0)
409  {
410    $row = pwg_db_fetch_assoc($result);
411 
412    $selected_category = array($row['category_id']);
413  }
414}
415
416$query = '
417SELECT id,name,uppercats,global_rank
418  FROM '.CATEGORIES_TABLE.'
419;';
420display_select_cat_wrapper($query, $selected_category, 'filter_category_options', true);
421
422// Dissociate from a category : categories listed for dissociation can only
423// represent virtual links. We can't create orphans. Links to physical
424// categories can't be broken.
425if (count($page['cat_elements_id']) > 0)
426{
427  $query = '
428SELECT
429    DISTINCT(category_id) AS id,
430    c.name,
431    c.uppercats,
432    c.global_rank
433  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
434    JOIN '.CATEGORIES_TABLE.' AS c ON c.id = ic.category_id
435    JOIN '.IMAGES_TABLE.' AS i ON i.id = ic.image_id
436  WHERE ic.image_id IN ('.implode(',', $page['cat_elements_id']).')
437    AND (
438      ic.category_id != i.storage_category_id
439      OR i.storage_category_id IS NULL
440    )
441;';
442  display_select_cat_wrapper($query, array(), 'dissociate_options', true);
443}
444
445if (count($page['cat_elements_id']) > 0)
446{
447  // remove tags
448  $tags = get_common_tags($page['cat_elements_id'], -1);
449
450  $template->assign(
451    array(
452      'DEL_TAG_SELECTION' => get_html_tag_selection($tags, 'del_tags'),
453      )
454    );
455}
456
457// creation date
458$day =
459empty($_POST['date_creation_day']) ? date('j') : $_POST['date_creation_day'];
460
461$month =
462empty($_POST['date_creation_month']) ? date('n') : $_POST['date_creation_month'];
463
464$year =
465empty($_POST['date_creation_year']) ? date('Y') : $_POST['date_creation_year'];
466
467$month_list = $lang['month'];
468$month_list[0]='------------';
469ksort($month_list);
470$template->assign( array(
471      'month_list'         => $month_list,
472      'DATE_CREATION_DAY'  => (int)$day,
473      'DATE_CREATION_MONTH'=> (int)$month,
474      'DATE_CREATION_YEAR' => (int)$year,
475    )
476  );
477
478// image level options
479$template->assign(
480    array(
481      'level_options'=> get_privacy_level_options(),
482      'level_options_selected' => 0,
483    )
484  );
485
486// +-----------------------------------------------------------------------+
487// |                        global mode thumbnails                         |
488// +-----------------------------------------------------------------------+
489
490// how many items to display on this page
491if (!empty($_GET['display']))
492{
493  if ('all' == $_GET['display'])
494  {
495    $page['nb_images'] = count($page['cat_elements_id']);
496  }
497  else
498  {
499    $page['nb_images'] = intval($_GET['display']);
500  }
501}
502else
503{
504  $page['nb_images'] = 20;
505}
506
507$nb_thumbs_page = 0;
508
509if (count($page['cat_elements_id']) > 0)
510{
511  $nav_bar = create_navigation_bar(
512    $base_url.get_query_string_diff(array('start')),
513    count($page['cat_elements_id']),
514    $page['start'],
515    $page['nb_images']
516    );
517  $template->assign('navbar', $nav_bar);
518
519  $is_category = false;
520  if (isset($_SESSION['bulk_manager_filter']['category'])
521      and !isset($_SESSION['bulk_manager_filter']['category_recursive']))
522  {
523    $is_category = true;
524  }
525
526  $query = '
527SELECT id,path,tn_ext,file,filesize,level,name
528  FROM '.IMAGES_TABLE;
529 
530  if ($is_category)
531  {
532    $category_info = get_cat_info($_SESSION['bulk_manager_filter']['category']);
533   
534    $conf['order_by'] = $conf['order_by_inside_category'];
535    if (!empty($category_info['image_order']))
536    {
537      $conf['order_by'] = ' ORDER BY '.$category_info['image_order'];
538    }
539
540    $query.= '
541    JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
542  }
543
544  $query.= '
545  WHERE id IN ('.implode(',', $page['cat_elements_id']).')';
546
547  if ($is_category)
548  {
549    $query.= '
550    AND category_id = '.$_SESSION['bulk_manager_filter']['category'];
551  }
552
553  $query.= '
554  '.$conf['order_by'].'
555  LIMIT '.$page['nb_images'].' OFFSET '.$page['start'].'
556;';
557  $result = pwg_query($query);
558
559  // template thumbnail initialization
560  while ($row = pwg_db_fetch_assoc($result))
561  {
562    $nb_thumbs_page++;
563    $src = get_thumbnail_url($row);
564
565    $title = $row['name'];
566    if (empty($title))
567    {     
568      $title = get_name_from_file($row['file']);
569    }
570
571    $template->append(
572      'thumbnails',
573      array(
574        'ID' => $row['id'],
575        'TN_SRC' => $src,
576        'FILE' => $row['file'],
577        'TITLE' => $title,
578        'LEVEL' => $row['level']
579        )
580      );
581  }
582}
583
584$template->assign(
585  array(
586    'nb_thumbs_page' => $nb_thumbs_page,
587    'nb_thumbs_set' => count($page['cat_elements_id']),
588    )
589  );
590
591trigger_action('loc_end_element_set_global');
592
593//----------------------------------------------------------- sending html code
594$template->assign_var_from_handle('ADMIN_CONTENT', 'batch_manager_global');
595?>
Note: See TracBrowser for help on using the repository browser.