source: trunk/admin/batch_manager_global.php @ 12922

Last change on this file since 12922 was 12922, checked in by mistic100, 12 years ago

update Piwigo headers to 2012, last change before the expected (or not) apocalypse

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