source: trunk/admin/batch_manager_global.php @ 24880

Last change on this file since 24880 was 24834, checked in by mistic100, 11 years ago

feature 2969: Unified Batch Manager URL

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