source: trunk/admin/batch_manager_global.php @ 9814

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

bug 2168 fixed: Batch Manager, ability to remove author/title/date creation

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