source: trunk/admin/batch_manager_global.php @ 9916

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

feature:2237
new trigger to add action on batch manager

File size: 18.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24/**
25 * Management of elements set. Elements can belong to a category or to the
26 * user caddie.
27 *
28 */
29
30if (!defined('PHPWG_ROOT_PATH'))
31{
32  die('Hacking attempt!');
33}
34
35include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
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('dissociate', $_POST, false, PATTERN_ID);
48
49// +-----------------------------------------------------------------------+
50// |                            current selection                          |
51// +-----------------------------------------------------------------------+
52
53$collection = array();
54if (isset($_POST['setSelected']))
55{
56  $collection = $page['cat_elements_id'];
57}
58else if (isset($_POST['selection']))
59{
60  $collection = $_POST['selection'];
61}
62
63// +-----------------------------------------------------------------------+
64// |                       global mode form submission                     |
65// +-----------------------------------------------------------------------+
66
67// $page['prefilter'] is a shortcut to test if the current filter contains a
68// given prefilter. The idea is to make conditions simpler to write in the
69// code.
70$page['prefilter'] = 'none';
71if (isset($_SESSION['bulk_manager_filter']['prefilter']))
72{
73  $page['prefilter'] = $_SESSION['bulk_manager_filter']['prefilter'];
74}
75
76// $page['category'] is a shortcut to test if the current filter contains a
77// given category. The idea is the same as for prefilter
78$page['category'] = -1;
79if (isset($_SESSION['bulk_manager_filter']['category']))
80{
81  $page['category'] = $_SESSION['bulk_manager_filter']['category'];
82}
83
84$redirect_url = get_root_url().'admin.php?page='.$_GET['page'];
85
86if (isset($_POST['submit']))
87{
88  // if the user tries to apply an action, it means that there is at least 1
89  // photo in the selection
90  if (count($collection) == 0)
91  {
92    array_push($page['errors'], l10n('Select at least one photo'));
93  }
94
95  $action = $_POST['selectAction'];
96 
97  if ('remove_from_caddie' == $action)
98  {
99    $query = '
100DELETE
101  FROM '.CADDIE_TABLE.'
102  WHERE element_id IN ('.implode(',', $collection).')
103    AND user_id = '.$user['id'].'
104;';
105    pwg_query($query);
106
107    if ('caddie' == $page['prefilter'])
108    {
109      redirect($redirect_url);
110    }
111   
112    // if we are here in the code, it means that the user is currently
113    // displaying the caddie content, so we have to remove the current
114    // selection from the current set
115    $page['cat_elements_id'] = array_diff($page['cat_elements_id'], $collection);
116  }
117
118  if ('add_tags' == $action)
119  {
120    $tag_ids = get_fckb_tag_ids($_POST['add_tags']);
121    add_tags($tag_ids, $collection);
122
123    if ('with no tag' == $page['prefilter'])
124    {
125      redirect(get_root_url().'admin.php?page='.$_GET['page']);
126    }
127  }
128
129  if ('del_tags' == $action)
130  {
131    if (count($_POST['del_tags']) == 0)
132    {
133      array_push($page['errors'], l10n('Select at least one tag'));
134    }
135   
136    $query = '
137DELETE
138  FROM '.IMAGE_TAG_TABLE.'
139  WHERE image_id IN ('.implode(',', $collection).')
140    AND tag_id IN ('.implode(',', $_POST['del_tags']).')
141;';
142    pwg_query($query);
143  }
144
145  if ('associate' == $action)
146  {
147    associate_images_to_categories(
148      $collection,
149      array($_POST['associate'])
150      );
151
152    $_SESSION['page_infos'] = array(
153      l10n('Information data registered in database')
154      );
155   
156    // let's refresh the page because we the current set might be modified
157    if ('with no album' == $page['prefilter'])
158    {
159      redirect($redirect_url);
160    }
161
162    if ('with no virtual album' == $page['prefilter'])
163    {
164      $category_info = get_cat_info($_POST['associate']);
165      if (empty($category_info['dir']))
166      {
167        redirect($redirect_url);
168      }
169    }
170  }
171
172  if ('dissociate' == $action)
173  {
174    // physical links must not be broken, so we must first retrieve image_id
175    // which create virtual links with the category to "dissociate from".
176    $query = '
177SELECT id
178  FROM '.IMAGE_CATEGORY_TABLE.'
179    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
180  WHERE category_id = '.$_POST['dissociate'].'
181    AND id IN ('.implode(',', $collection).')
182    AND (
183      category_id != storage_category_id
184      OR storage_category_id IS NULL
185    )
186;';
187    $dissociables = array_from_query($query, 'id');
188
189    if (!empty($dissociables))
190    {
191      $query = '
192DELETE
193  FROM '.IMAGE_CATEGORY_TABLE.'
194  WHERE category_id = '.$_POST['dissociate'].'
195    AND image_id IN ('.implode(',', $dissociables).')
196';
197      pwg_query($query);
198
199      update_category($_POST['dissociate']);
200     
201      $_SESSION['page_infos'] = array(
202        l10n('Information data registered in database')
203        );
204     
205      // let's refresh the page because we the current set might be modified
206      redirect($redirect_url);
207    }
208  }
209
210  // author
211  if ('author' == $action)
212  {
213    if (isset($_POST['remove_author']))
214    {
215      $_POST['author'] = null;
216    }
217   
218    $datas = array();
219    foreach ($collection as $image_id)
220    {
221      array_push(
222        $datas,
223        array(
224          'id' => $image_id,
225          'author' => $_POST['author']
226          )
227        );
228    }
229
230    mass_updates(
231      IMAGES_TABLE,
232      array('primary' => array('id'), 'update' => array('author')),
233      $datas
234      );
235  }
236
237  // title
238  if ('title' == $action)
239  {
240    if (isset($_POST['remove_title']))
241    {
242      $_POST['title'] = null;
243    }
244   
245    $datas = array();
246    foreach ($collection as $image_id)
247    {
248      array_push(
249        $datas,
250        array(
251          'id' => $image_id,
252          'name' => $_POST['title']
253          )
254        );
255    }
256
257    mass_updates(
258      IMAGES_TABLE,
259      array('primary' => array('id'), 'update' => array('name')),
260      $datas
261      );
262  }
263 
264  // date_creation
265  if ('date_creation' == $action)
266  {
267    $date_creation = sprintf(
268      '%u-%u-%u',
269      $_POST['date_creation_year'],
270      $_POST['date_creation_month'],
271      $_POST['date_creation_day']
272      );
273
274    if (isset($_POST['remove_date_creation']))
275    {
276      $date_creation = null;
277    }
278
279    $datas = array();
280    foreach ($collection as $image_id)
281    {
282      array_push(
283        $datas,
284        array(
285          'id' => $image_id,
286          'date_creation' => $date_creation
287          )
288        );
289    }
290
291    mass_updates(
292      IMAGES_TABLE,
293      array('primary' => array('id'), 'update' => array('date_creation')),
294      $datas
295      );
296  }
297 
298  // privacy_level
299  if ('level' == $action)
300  {
301    $datas = array();
302    foreach ($collection as $image_id)
303    {
304      array_push(
305        $datas,
306        array(
307          'id' => $image_id,
308          'level' => $_POST['level']
309          )
310        );
311    }
312
313    mass_updates(
314      IMAGES_TABLE,
315      array('primary' => array('id'), 'update' => array('level')),
316      $datas
317      );
318
319    if (isset($_SESSION['bulk_manager_filter']['level']))
320    {
321      if ($_POST['level'] < $_SESSION['bulk_manager_filter']['level'])
322      {
323        redirect($redirect_url);
324      }
325    }
326  }
327 
328  // add_to_caddie
329  if ('add_to_caddie' == $action)
330  {
331    fill_caddie($collection);
332  }
333 
334  // delete
335  if ('delete' == $action)
336  {
337    if (isset($_POST['confirm_deletion']) and 1 == $_POST['confirm_deletion'])
338    {
339      $deleted_count = delete_elements($collection, true);
340      if ($deleted_count > 0)
341      {
342        $_SESSION['page_infos'] = array(
343          sprintf(
344            l10n_dec(
345              '%d photo was deleted',
346              '%d photos were deleted',
347              $deleted_count
348              ),
349            $deleted_count
350            )
351          );
352
353        $redirect_url = get_root_url().'admin.php?page='.$_GET['page'];
354        redirect($redirect_url);
355      }
356      else
357      {
358        array_push($page['errors'], l10n('No photo can be deleted'));
359      }
360    }
361    else
362    {
363      array_push($page['errors'], l10n('You need to confirm deletion'));
364    }
365  }
366
367  // synchronize metadata
368  if ('metadata' == $action)
369  {
370    $query = '
371SELECT id, path
372  FROM '.IMAGES_TABLE.'
373  WHERE id IN ('.implode(',', $collection).')
374;';
375    $id_to_path = array();
376    $result = pwg_query($query);
377    while ($row = pwg_db_fetch_assoc($result))
378    {
379      $id_to_path[$row['id']] = $row['path'];
380    }
381   
382    update_metadata($id_to_path);
383
384    array_push(
385      $page['infos'],
386      l10n('Metadata synchronized from file')
387      );
388  }
389
390  trigger_action('element_set_global_action', $action, $collection);
391}
392
393// +-----------------------------------------------------------------------+
394// |                             template init                             |
395// +-----------------------------------------------------------------------+
396$template->set_filenames(array('batch_manager_global' => 'batch_manager_global.tpl'));
397
398$base_url = get_root_url().'admin.php';
399
400$template->assign(
401  array(
402    'filter' => $_SESSION['bulk_manager_filter'],
403    'selection' => $collection,
404    'U_DISPLAY'=>$base_url.get_query_string_diff(array('display')),
405    'F_ACTION'=>$base_url.get_query_string_diff(array('cat')),
406   )
407 );
408
409// +-----------------------------------------------------------------------+
410// |                            caddie options                             |
411// +-----------------------------------------------------------------------+
412
413$in_caddie = false;
414if (isset($_SESSION['bulk_manager_filter']['prefilter'])
415    and 'caddie' == $_SESSION['bulk_manager_filter']['prefilter'])
416{
417  $in_caddie = true;
418}
419$template->assign('IN_CADDIE', $in_caddie);
420
421// +-----------------------------------------------------------------------+
422// |                            deletion form                              |
423// +-----------------------------------------------------------------------+
424
425// we can only remove photos that have no storage_category_id, in other
426// word, it currently (Butterfly) means that the photo was added with
427// pLoader
428if (count($page['cat_elements_id']) > 0)
429{
430  $query = '
431SELECT
432    id
433  FROM '.IMAGES_TABLE.'
434  WHERE id IN ('.implode(',', $page['cat_elements_id']).')
435    AND file NOT LIKE \'http%\'
436  LIMIT 1
437;';
438  ;
439
440  if ( pwg_db_fetch_row(pwg_query($query)) )
441  {
442    $template->assign('show_delete_form', true);
443  }
444}
445
446// +-----------------------------------------------------------------------+
447// |                           global mode form                            |
448// +-----------------------------------------------------------------------+
449
450// privacy level
451$template->assign(
452    array(
453      'filter_level_options'=> get_privacy_level_options(),
454      'filter_level_options_selected' => isset($_SESSION['bulk_manager_filter']['level'])
455        ? $_SESSION['bulk_manager_filter']['level']
456        : 0,
457    )
458  );
459
460// Virtualy associate a picture to a category
461$query = '
462SELECT id,name,uppercats,global_rank
463  FROM '.CATEGORIES_TABLE.'
464;';
465display_select_cat_wrapper($query, array(), 'associate_options', true);
466
467// in the filter box, which category to select by default
468$selected_category = array();
469
470if (isset($_SESSION['bulk_manager_filter']['category']))
471{
472  $selected_category = array($_SESSION['bulk_manager_filter']['category']);
473}
474else
475{
476  // we need to know the category in which the last photo was added
477  $selected_category = array();
478
479  $query = '
480SELECT
481    category_id,
482    id_uppercat
483  FROM '.IMAGES_TABLE.' AS i
484    JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON image_id = i.id
485    JOIN '.CATEGORIES_TABLE.' AS c ON category_id = c.id
486  ORDER BY i.id DESC
487  LIMIT 1
488;';
489  $result = pwg_query($query);
490  if (pwg_db_num_rows($result) > 0)
491  {
492    $row = pwg_db_fetch_assoc($result);
493 
494    $selected_category = array($row['category_id']);
495  }
496}
497
498$query = '
499SELECT id,name,uppercats,global_rank
500  FROM '.CATEGORIES_TABLE.'
501;';
502display_select_cat_wrapper($query, $selected_category, 'filter_category_options', true);
503
504// Dissociate from a category : categories listed for dissociation can only
505// represent virtual links. We can't create orphans. Links to physical
506// categories can't be broken.
507if (count($page['cat_elements_id']) > 0)
508{
509  $query = '
510SELECT
511    DISTINCT(category_id) AS id,
512    c.name,
513    c.uppercats,
514    c.global_rank
515  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
516    JOIN '.CATEGORIES_TABLE.' AS c ON c.id = ic.category_id
517    JOIN '.IMAGES_TABLE.' AS i ON i.id = ic.image_id
518  WHERE ic.image_id IN ('.implode(',', $page['cat_elements_id']).')
519    AND (
520      ic.category_id != i.storage_category_id
521      OR i.storage_category_id IS NULL
522    )
523;';
524  display_select_cat_wrapper($query, array(), 'dissociate_options', true);
525}
526
527if (count($page['cat_elements_id']) > 0)
528{
529  // remove tags
530  $tags = get_common_tags($page['cat_elements_id'], -1);
531
532  $template->assign(
533    array(
534      'DEL_TAG_SELECTION' => get_html_tag_selection($tags, 'del_tags'),
535      )
536    );
537}
538
539// creation date
540$day =
541empty($_POST['date_creation_day']) ? date('j') : $_POST['date_creation_day'];
542
543$month =
544empty($_POST['date_creation_month']) ? date('n') : $_POST['date_creation_month'];
545
546$year =
547empty($_POST['date_creation_year']) ? date('Y') : $_POST['date_creation_year'];
548
549$month_list = $lang['month'];
550$month_list[0]='------------';
551ksort($month_list);
552$template->assign( array(
553      'month_list'         => $month_list,
554      'DATE_CREATION_DAY'  => (int)$day,
555      'DATE_CREATION_MONTH'=> (int)$month,
556      'DATE_CREATION_YEAR' => (int)$year,
557    )
558  );
559
560// image level options
561$template->assign(
562    array(
563      'level_options'=> get_privacy_level_options(),
564      'level_options_selected' => 0,
565    )
566  );
567
568// metadata
569include_once( PHPWG_ROOT_PATH.'admin/site_reader_local.php');
570$site_reader = new LocalSiteReader('./');
571$used_metadata = implode( ', ', $site_reader->get_metadata_attributes());
572
573$template->assign(
574    array(
575      'used_metadata' => $used_metadata,
576    )
577  );
578
579// +-----------------------------------------------------------------------+
580// |                        global mode thumbnails                         |
581// +-----------------------------------------------------------------------+
582
583// how many items to display on this page
584if (!empty($_GET['display']))
585{
586  if ('all' == $_GET['display'])
587  {
588    $page['nb_images'] = count($page['cat_elements_id']);
589  }
590  else
591  {
592    $page['nb_images'] = intval($_GET['display']);
593  }
594}
595else
596{
597  $page['nb_images'] = 20;
598}
599
600$nb_thumbs_page = 0;
601
602if (count($page['cat_elements_id']) > 0)
603{
604  $nav_bar = create_navigation_bar(
605    $base_url.get_query_string_diff(array('start')),
606    count($page['cat_elements_id']),
607    $page['start'],
608    $page['nb_images']
609    );
610  $template->assign('navbar', $nav_bar);
611
612  $is_category = false;
613  if (isset($_SESSION['bulk_manager_filter']['category'])
614      and !isset($_SESSION['bulk_manager_filter']['category_recursive']))
615  {
616    $is_category = true;
617  }
618
619  if (isset($_SESSION['bulk_manager_filter']['prefilter'])
620      and 'duplicates' == $_SESSION['bulk_manager_filter']['prefilter'])
621  {
622    $conf['order_by'] = ' ORDER BY file, id';
623  }
624
625
626  $query = '
627SELECT id,path,tn_ext,file,filesize,level,name
628  FROM '.IMAGES_TABLE;
629 
630  if ($is_category)
631  {
632    $category_info = get_cat_info($_SESSION['bulk_manager_filter']['category']);
633   
634    $conf['order_by'] = $conf['order_by_inside_category'];
635    if (!empty($category_info['image_order']))
636    {
637      $conf['order_by'] = ' ORDER BY '.$category_info['image_order'];
638    }
639
640    $query.= '
641    JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
642  }
643
644  $query.= '
645  WHERE id IN ('.implode(',', $page['cat_elements_id']).')';
646
647  if ($is_category)
648  {
649    $query.= '
650    AND category_id = '.$_SESSION['bulk_manager_filter']['category'];
651  }
652
653  $query.= '
654  '.$conf['order_by'].'
655  LIMIT '.$page['nb_images'].' OFFSET '.$page['start'].'
656;';
657  $result = pwg_query($query);
658
659  // template thumbnail initialization
660  while ($row = pwg_db_fetch_assoc($result))
661  {
662    $nb_thumbs_page++;
663    $src = get_thumbnail_url($row);
664
665    $title = $row['name'];
666    if (empty($title))
667    {     
668      $title = get_name_from_file($row['file']);
669    }
670
671    $template->append(
672      'thumbnails',
673      array(
674        'ID' => $row['id'],
675        'TN_SRC' => $src,
676        'FILE' => $row['file'],
677        'TITLE' => $title,
678        'LEVEL' => $row['level']
679        )
680      );
681  }
682}
683
684$template->assign(
685  array(
686    'nb_thumbs_page' => $nb_thumbs_page,
687    'nb_thumbs_set' => count($page['cat_elements_id']),
688    )
689  );
690
691trigger_action('loc_end_element_set_global');
692
693//----------------------------------------------------------- sending html code
694$template->assign_var_from_handle('ADMIN_CONTENT', 'batch_manager_global');
695?>
Note: See TracBrowser for help on using the repository browser.