source: trunk/admin/batch_manager_global.php @ 28700

Revision 28700, 18.2 KB checked in by mistic100, 5 years ago (diff)

on batch manager global move "invalidate_user_cache" AFTER database updates (SmartAlbums compatibility)

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