source: trunk/admin/batch_manager_global.php @ 13544

Last change on this file since 13544 was 13544, checked in by rvelices, 12 years ago
File size: 21.1 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 ('delete_derivatives' == $action)
415  {
416    $query='SELECT path,representative_ext FROM '.IMAGES_TABLE.'
417  WHERE id IN ('.implode(',', $collection).')';
418    $result = pwg_query($query);
419    while ($info = pwg_db_fetch_assoc($result))
420    {
421      foreach( $_POST['del_derivatives_type'] as $type)
422      {
423        delete_element_derivatives($info, $type);
424      }
425    }
426  }
427
428  if ('generate_derivatives' == $action)
429  {
430    if ($_POST['regenerateSuccess'] != '0')
431      array_push($page['infos'], sprintf(l10n('%s photos were generated'), $_POST['regenerateSuccess']));
432
433    if ($_POST['regenerateError'] != '0')
434      array_push($page['warnings'], sprintf(l10n('%s photos were not generated'), $_POST['regenerateError']));
435
436  }
437
438  trigger_action('element_set_global_action', $action, $collection);
439}
440
441// +-----------------------------------------------------------------------+
442// |                             template init                             |
443// +-----------------------------------------------------------------------+
444$template->set_filenames(array('batch_manager_global' => 'batch_manager_global.tpl'));
445
446$base_url = get_root_url().'admin.php';
447
448$prefilters = array();
449
450array_push($prefilters,
451  array('ID' => 'caddie', 'NAME' => l10n('Caddie')),
452  array('ID' => 'last import', 'NAME' => l10n('Last import')),
453  array('ID' => 'with no album', 'NAME' => l10n('With no album')),
454  array('ID' => 'with no tag', 'NAME' => l10n('With no tag')),
455  array('ID' => 'duplicates', 'NAME' => l10n('Duplicates')),
456  array('ID' => 'all photos', 'NAME' => l10n('All'))
457);
458
459if ($conf['enable_synchronization'])
460{
461  array_push($prefilters,
462    array('ID' => 'with no virtual album', 'NAME' => l10n('With no virtual album'))
463  );
464}
465
466$prefilters = trigger_event('get_batch_manager_prefilters', $prefilters);
467usort($prefilters, 'UC_name_compare');
468
469$template->assign(
470  array(
471    'prefilters' => $prefilters,
472    'filter' => $_SESSION['bulk_manager_filter'],
473    'selection' => $collection,
474    'all_elements' => $page['cat_elements_id'],
475    'U_DISPLAY'=>$base_url.get_query_string_diff(array('display')),
476    'F_ACTION'=>$base_url.get_query_string_diff(array('cat')),
477   )
478 );
479
480// +-----------------------------------------------------------------------+
481// |                            caddie options                             |
482// +-----------------------------------------------------------------------+
483
484$in_caddie = false;
485if (isset($_SESSION['bulk_manager_filter']['prefilter'])
486    and 'caddie' == $_SESSION['bulk_manager_filter']['prefilter'])
487{
488  $in_caddie = true;
489}
490$template->assign('IN_CADDIE', $in_caddie);
491
492// +-----------------------------------------------------------------------+
493// |                            deletion form                              |
494// +-----------------------------------------------------------------------+
495
496// we can only remove photos that have no storage_category_id, in other
497// word, it currently (Butterfly) means that the photo was added with
498// pLoader
499if (count($page['cat_elements_id']) > 0)
500{
501  $query = '
502SELECT
503    id
504  FROM '.IMAGES_TABLE.'
505  WHERE id IN ('.implode(',', $page['cat_elements_id']).')
506    AND file NOT LIKE \'http%\'
507  LIMIT 1
508;';
509  ;
510
511  if ( pwg_db_fetch_row(pwg_query($query)) )
512  {
513    $template->assign('show_delete_form', true);
514  }
515}
516
517// +-----------------------------------------------------------------------+
518// |                           global mode form                            |
519// +-----------------------------------------------------------------------+
520
521// privacy level
522$template->assign(
523    array(
524      'filter_level_options'=> get_privacy_level_options(),
525      'filter_level_options_selected' => isset($_SESSION['bulk_manager_filter']['level'])
526        ? $_SESSION['bulk_manager_filter']['level']
527        : 0,
528    )
529  );
530
531if (!empty($_SESSION['bulk_manager_filter']['tags']))
532{
533  $query = '
534SELECT
535    id,
536    name
537  FROM '.TAGS_TABLE.'
538  WHERE id IN ('.implode(',', $_SESSION['bulk_manager_filter']['tags']).')
539;';
540  $template->assign('filter_tags', get_taglist($query));
541}
542
543// Virtualy associate a picture to a category
544$query = '
545SELECT id,name,uppercats,global_rank
546  FROM '.CATEGORIES_TABLE.'
547;';
548display_select_cat_wrapper($query, array(), 'associate_options', true);
549display_select_cat_wrapper($query, array(), 'move_options', true);
550display_select_cat_wrapper($query, array(), 'category_parent_options');
551
552// in the filter box, which category to select by default
553$selected_category = array();
554
555if (isset($_SESSION['bulk_manager_filter']['category']))
556{
557  $selected_category = array($_SESSION['bulk_manager_filter']['category']);
558}
559else
560{
561  // we need to know the category in which the last photo was added
562  $selected_category = array();
563
564  $query = '
565SELECT
566    category_id,
567    id_uppercat
568  FROM '.IMAGES_TABLE.' AS i
569    JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON image_id = i.id
570    JOIN '.CATEGORIES_TABLE.' AS c ON category_id = c.id
571  ORDER BY i.id DESC
572  LIMIT 1
573;';
574  $result = pwg_query($query);
575  if (pwg_db_num_rows($result) > 0)
576  {
577    $row = pwg_db_fetch_assoc($result);
578 
579    $selected_category = array($row['category_id']);
580  }
581}
582
583$query = '
584SELECT id,name,uppercats,global_rank
585  FROM '.CATEGORIES_TABLE.'
586;';
587display_select_cat_wrapper($query, $selected_category, 'filter_category_options', true);
588
589// Dissociate from a category : categories listed for dissociation can only
590// represent virtual links. We can't create orphans. Links to physical
591// categories can't be broken.
592if (count($page['cat_elements_id']) > 0)
593{
594  $query = '
595SELECT
596    DISTINCT(category_id) AS id,
597    c.name,
598    c.uppercats,
599    c.global_rank
600  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
601    JOIN '.CATEGORIES_TABLE.' AS c ON c.id = ic.category_id
602    JOIN '.IMAGES_TABLE.' AS i ON i.id = ic.image_id
603  WHERE ic.image_id IN ('.implode(',', $page['cat_elements_id']).')
604    AND (
605      ic.category_id != i.storage_category_id
606      OR i.storage_category_id IS NULL
607    )
608;';
609  display_select_cat_wrapper($query, array(), 'dissociate_options', true);
610}
611
612if (count($page['cat_elements_id']) > 0)
613{
614  // remove tags
615  $tags = get_common_tags($page['cat_elements_id'], -1);
616
617  $template->assign(
618    array(
619      'DEL_TAG_SELECTION' => get_html_tag_selection($tags, 'del_tags'),
620      )
621    );
622}
623
624// creation date
625$day =
626empty($_POST['date_creation_day']) ? date('j') : $_POST['date_creation_day'];
627
628$month =
629empty($_POST['date_creation_month']) ? date('n') : $_POST['date_creation_month'];
630
631$year =
632empty($_POST['date_creation_year']) ? date('Y') : $_POST['date_creation_year'];
633
634$month_list = $lang['month'];
635$month_list[0]='------------';
636ksort($month_list);
637$template->assign( array(
638      'month_list'         => $month_list,
639      'DATE_CREATION_DAY'  => (int)$day,
640      'DATE_CREATION_MONTH'=> (int)$month,
641      'DATE_CREATION_YEAR' => (int)$year,
642    )
643  );
644
645// image level options
646$template->assign(
647    array(
648      'level_options'=> get_privacy_level_options(),
649      'level_options_selected' => 0,
650    )
651  );
652
653// metadata
654include_once( PHPWG_ROOT_PATH.'admin/site_reader_local.php');
655$site_reader = new LocalSiteReader('./');
656$used_metadata = implode( ', ', $site_reader->get_metadata_attributes());
657
658$template->assign(
659    array(
660      'used_metadata' => $used_metadata,
661    )
662  );
663
664//derivatives
665$del_deriv_map = array();
666foreach(ImageStdParams::get_defined_type_map() as $params)
667{
668  $del_deriv_map[$params->type] = l10n($params->type);
669}
670$template->assign(
671    array(
672      'del_derivatives_types' => $del_deriv_map,
673      'generate_derivatives_types' => $del_deriv_map,
674    )
675  );
676
677// +-----------------------------------------------------------------------+
678// |                        global mode thumbnails                         |
679// +-----------------------------------------------------------------------+
680
681// how many items to display on this page
682if (!empty($_GET['display']))
683{
684  if ('all' == $_GET['display'])
685  {
686    $page['nb_images'] = count($page['cat_elements_id']);
687  }
688  else
689  {
690    $page['nb_images'] = intval($_GET['display']);
691  }
692}
693else
694{
695  $page['nb_images'] = 20;
696}
697
698$nb_thumbs_page = 0;
699
700if (count($page['cat_elements_id']) > 0)
701{
702  $nav_bar = create_navigation_bar(
703    $base_url.get_query_string_diff(array('start')),
704    count($page['cat_elements_id']),
705    $page['start'],
706    $page['nb_images']
707    );
708  $template->assign('navbar', $nav_bar);
709
710  $is_category = false;
711  if (isset($_SESSION['bulk_manager_filter']['category'])
712      and !isset($_SESSION['bulk_manager_filter']['category_recursive']))
713  {
714    $is_category = true;
715  }
716
717  if (isset($_SESSION['bulk_manager_filter']['prefilter'])
718      and 'duplicates' == $_SESSION['bulk_manager_filter']['prefilter'])
719  {
720    $conf['order_by'] = ' ORDER BY file, id';
721  }
722
723
724  $query = '
725SELECT id,path,representative_ext,file,filesize,level,name
726  FROM '.IMAGES_TABLE;
727 
728  if ($is_category)
729  {
730    $category_info = get_cat_info($_SESSION['bulk_manager_filter']['category']);
731   
732    $conf['order_by'] = $conf['order_by_inside_category'];
733    if (!empty($category_info['image_order']))
734    {
735      $conf['order_by'] = ' ORDER BY '.$category_info['image_order'];
736    }
737
738    $query.= '
739    JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
740  }
741
742  $query.= '
743  WHERE id IN ('.implode(',', $page['cat_elements_id']).')';
744
745  if ($is_category)
746  {
747    $query.= '
748    AND category_id = '.$_SESSION['bulk_manager_filter']['category'];
749  }
750
751  $query.= '
752  '.$conf['order_by'].'
753  LIMIT '.$page['nb_images'].' OFFSET '.$page['start'].'
754;';
755  $result = pwg_query($query);
756
757  // template thumbnail initialization
758  while ($row = pwg_db_fetch_assoc($result))
759  {
760    $nb_thumbs_page++;
761    $src_image = new SrcImage($row);
762
763    $title = render_element_name($row);
764    if ($title != get_name_from_file($row['file']))
765    {
766      $title.= ' ('.$row['file'].')';
767    }
768
769    $template->append(
770      'thumbnails',
771      array(
772        'ID' => $row['id'],
773        'TN_SRC' => DerivativeImage::url(IMG_THUMB, $src_image),
774        'FILE' => $row['file'],
775        'TITLE' => $title,
776        'LEVEL' => $row['level'],
777        'FILE_SRC' => DerivativeImage::url(IMG_LARGE, $src_image),
778        'U_EDIT' => get_root_url().'admin.php?page=photo-'.$row['id'],
779        )
780      );
781  }
782}
783
784$template->assign(
785  array(
786    'nb_thumbs_page' => $nb_thumbs_page,
787    'nb_thumbs_set' => count($page['cat_elements_id']),
788    )
789  );
790
791trigger_action('loc_end_element_set_global');
792
793//----------------------------------------------------------- sending html code
794$template->assign_var_from_handle('ADMIN_CONTENT', 'batch_manager_global');
795?>
Note: See TracBrowser for help on using the repository browser.