source: trunk/admin/batch_manager_global.php @ 13330

Last change on this file since 13330 was 13077, checked in by plg, 13 years ago

feature 2564: redesign on photo administration screen.

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