source: trunk/admin/batch_manager_global.php @ 19700

Last change on this file since 19700 was 18573, checked in by rvelices, 12 years ago

batch manager - remove unused code, less sql queries and avoid 4 calls to same display_select function

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