source: trunk/admin/batch_manager_global.php @ 30838

Last change on this file since 30838 was 29760, checked in by mistic100, 10 years ago

filter "dissociate" action + display bug of Selectize widget

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