source: trunk/admin/batch_manager_global.php @ 13064

Last change on this file since 13064 was 13064, checked in by plg, 12 years ago

feature 2309 added: ability to "move photos" into an album from the Batch
Manager, ie "dissociate from all albums" + "associate" in a single action.

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