source: trunk/admin/batch_manager_global.php @ 10389

Revision 10389, 21.0 KB checked in by patdenice, 9 years ago (diff)

feature:2259
Add thumbnails regeneration in batch manager

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