source: trunk/admin/batch_manager_global.php @ 28490

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