source: trunk/admin/batch_manager_global.php @ 13646

Revision 13646, 21.3 KB checked in by plg, 8 years ago (diff)

feature 2596: Batch Manager, improve ergonomy on privacy level filter

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