source: trunk/admin/batch_manager_global.php @ 24445

Last change on this file since 24445 was 23746, checked in by flop25, 11 years ago

bug:2808
add "Your Favorites" to the filters of the Batch Manager

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