source: trunk/admin/batch_manager_global.php @ 10553

Revision 10553, 22.0 KB checked in by patdenice, 9 years ago (diff)

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