source: trunk/admin/batch_manager_global.php @ 12930

Last change on this file since 12930 was 12930, checked in by rvelices, 9 years ago

feature 2548 multisize

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