source: trunk/admin/batch_manager_global.php @ 27888

Last change on this file since 27888 was 26461, checked in by mistic100, 10 years ago

Update headers to 2014. Happy new year!!

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