source: trunk/admin/batch_manager_global.php @ 10511

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

feature:2259
Add "Regenerate Websize Photos" action.

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