source: trunk/admin/batch_manager_global.php @ 19292

Last change on this file since 19292 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
RevLine 
[8394]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[8394]4// +-----------------------------------------------------------------------+
[12922]5// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
[8394]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);
[13064]47check_input_parameter('move', $_POST, false, PATTERN_ID);
[8394]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
[8422]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
[8394]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  {
[8682]85    array_push($page['errors'], l10n('Select at least one photo'));
[8394]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
[18573]100    // remove from caddie action available only in caddie so reload content
101    redirect($redirect_url);
[8394]102  }
103
104  if ('add_tags' == $action)
105  {
[10008]106    if (empty($_POST['add_tags']))
[8422]107    {
[10008]108      array_push($page['errors'], l10n('Select at least one tag'));
[8422]109    }
[10008]110    else
111    {
[11039]112      $tag_ids = get_tag_ids($_POST['add_tags']);
[10008]113      add_tags($tag_ids, $collection);
114
115      if ('with no tag' == $page['prefilter'])
116      {
[18573]117        redirect($redirect_url);
[10008]118      }
119    }
[8394]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      );
[8403]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
[8422]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    }
[8394]163  }
164
[13064]165  if ('move' == $action)
166  {
[13077]167    move_images_to_categories($collection, array($_POST['move']));
[13064]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
[8394]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
[8403]222      $_SESSION['page_infos'] = array(
223        l10n('Information data registered in database')
224        );
225     
[13064]226      // let's refresh the page because the current set might be modified
[8403]227      redirect($redirect_url);
[8394]228    }
229  }
230
231  // author
232  if ('author' == $action)
233  {
[9035]234    if (isset($_POST['remove_author']))
235    {
236      $_POST['author'] = null;
237    }
238   
[8394]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
[8408]258  // title
259  if ('title' == $action)
[8394]260  {
[9035]261    if (isset($_POST['remove_title']))
262    {
263      $_POST['title'] = null;
264    }
265   
[8394]266    $datas = array();
267    foreach ($collection as $image_id)
268    {
269      array_push(
270        $datas,
271        array(
272          'id' => $image_id,
[8408]273          'name' => $_POST['title']
[8394]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
[9035]295    if (isset($_POST['remove_date_creation']))
296    {
297      $date_creation = null;
298    }
299
[8394]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      );
[8422]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    }
[8394]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    {
[8398]360      $deleted_count = delete_elements($collection, true);
361      if ($deleted_count > 0)
[8394]362      {
[8398]363        $_SESSION['page_infos'] = array(
[8394]364          sprintf(
365            l10n_dec(
366              '%d photo was deleted',
367              '%d photos were deleted',
[8398]368              $deleted_count
[8394]369              ),
[8398]370            $deleted_count
[8394]371            )
372          );
373
[8398]374        $redirect_url = get_root_url().'admin.php?page='.$_GET['page'];
375        redirect($redirect_url);
[8394]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  }
[8422]387
388  // synchronize metadata
389  if ('metadata' == $action)
390  {
[12831]391    sync_metadata($collection);
[8422]392
393    array_push(
394      $page['infos'],
395      l10n('Metadata synchronized from file')
396      );
397  }
[9916]398
[13544]399  if ('delete_derivatives' == $action)
[10389]400  {
[13544]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    }
[10454]411  }
412
[13544]413  if ('generate_derivatives' == $action)
[10454]414  {
415    if ($_POST['regenerateSuccess'] != '0')
[13544]416      array_push($page['infos'], sprintf(l10n('%s photos were generated'), $_POST['regenerateSuccess']));
[10454]417
418    if ($_POST['regenerateError'] != '0')
[13544]419      array_push($page['warnings'], sprintf(l10n('%s photos were not generated'), $_POST['regenerateError']));
[10454]420
421  }
422
[9916]423  trigger_action('element_set_global_action', $action, $collection);
[8394]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
[18573]433$prefilters = array(
[12456]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')),
[10380]439  array('ID' => 'all photos', 'NAME' => l10n('All'))
440);
441
442if ($conf['enable_synchronization'])
443{
444  array_push($prefilters,
[12456]445    array('ID' => 'with no virtual album', 'NAME' => l10n('With no virtual album'))
[10380]446  );
447}
448
449$prefilters = trigger_event('get_batch_manager_prefilters', $prefilters);
450usort($prefilters, 'UC_name_compare');
451
[8394]452$template->assign(
453  array(
[10380]454    'prefilters' => $prefilters,
[8394]455    'filter' => $_SESSION['bulk_manager_filter'],
456    'selection' => $collection,
[10389]457    'all_elements' => $page['cat_elements_id'],
[17289]458    'START' => $page['start'],
[8394]459    'U_DISPLAY'=>$base_url.get_query_string_diff(array('display')),
[18459]460    'F_ACTION'=>$base_url.get_query_string_diff(array('cat','start','tag')),
[8394]461   )
462 );
463
464// +-----------------------------------------------------------------------+
465// |                            caddie options                             |
466// +-----------------------------------------------------------------------+
[18573]467$template->assign('IN_CADDIE', 'caddie' == $page['prefilter']);
[8394]468
469
470// +-----------------------------------------------------------------------+
471// |                           global mode form                            |
472// +-----------------------------------------------------------------------+
473
474// privacy level
[13646]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}
[8394]484$template->assign(
[13646]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,
[8394]490    )
491  );
492
[13646]493// tags
[11853]494if (!empty($_SESSION['bulk_manager_filter']['tags']))
495{
[12295]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));
[11853]504}
505
[8394]506// Virtualy associate a picture to a category
507$query = '
508SELECT id,name,uppercats,global_rank
509  FROM '.CATEGORIES_TABLE.'
510;';
[18573]511$categories = array_from_query($query);
512usort($categories, 'global_rank_compare');
513display_select_categories($categories, array(), 'category_full_name_options', true);
514
[13063]515display_select_cat_wrapper($query, array(), 'category_parent_options');
[8394]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
[18573]545$template->assign( 'filter_category_selected', $selected_category);
[8394]546
[8397]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.
[8394]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
[8422]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
[13544]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}
[13651]628$gen_deriv_map = $del_deriv_map;
629$del_deriv_map[IMG_CUSTOM] = l10n(IMG_CUSTOM);
[13544]630$template->assign(
631    array(
632      'del_derivatives_types' => $del_deriv_map,
[13651]633      'generate_derivatives_types' => $gen_deriv_map,
[13544]634    )
635  );
636
[8394]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
[8397]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
[8404]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
[8394]683  $query = '
[14143]684SELECT id,path,representative_ext,file,filesize,level,name,width,height,rotation
[8397]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.= '
[8394]711  '.$conf['order_by'].'
712  LIMIT '.$page['nb_images'].' OFFSET '.$page['start'].'
713;';
714  $result = pwg_query($query);
715
[17289]716  $thumb_params = ImageStdParams::get_by_type(IMG_THUMB);
[8394]717  // template thumbnail initialization
718  while ($row = pwg_db_fetch_assoc($result))
719  {
720    $nb_thumbs_page++;
[13544]721    $src_image = new SrcImage($row);
[8394]722
[17289]723    $ttitle = render_element_name($row);
724    if ($ttitle != get_name_from_file($row['file']))
[13457]725    {
[17289]726      $ttitle.= ' ('.$row['file'].')';
[8394]727    }
728
729    $template->append(
[17289]730      'thumbnails', array_merge($row,
[8394]731      array(
[17289]732        'thumb' => new DerivativeImage($thumb_params, $src_image),
733        'TITLE' => $ttitle,
[13544]734        'FILE_SRC' => DerivativeImage::url(IMG_LARGE, $src_image),
[13077]735        'U_EDIT' => get_root_url().'admin.php?page=photo-'.$row['id'],
[8394]736        )
[17289]737      ));
[8394]738  }
[17289]739  $template->assign('thumb_params', $thumb_params);
[8394]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.