source: trunk/admin/batch_manager_global.php @ 10589

Last change on this file since 10589 was 10589, checked in by patdenice, 13 years ago

Create a function to save upload form settings.

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