source: trunk/admin/batch_manager_global.php @ 12877

Last change on this file since 12877 was 12831, checked in by rvelices, 12 years ago

feature 2548 multisize

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