source: trunk/admin/batch_manager_global.php @ 8422

Revision 8422, 17.8 KB checked in by plg, 9 years ago (diff)

feature 2092 added: Batch Manager can filter all photos with no tag

feature 1866 added: Batch Manager can synchronize metadata

Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 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 picture'));
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    $datas = array();
214    foreach ($collection as $image_id)
215    {
216      array_push(
217        $datas,
218        array(
219          'id' => $image_id,
220          'author' => $_POST['author']
221          )
222        );
223    }
224
225    mass_updates(
226      IMAGES_TABLE,
227      array('primary' => array('id'), 'update' => array('author')),
228      $datas
229      );
230  }
231
232  // title
233  if ('title' == $action)
234  {
235    $datas = array();
236    foreach ($collection as $image_id)
237    {
238      array_push(
239        $datas,
240        array(
241          'id' => $image_id,
242          'name' => $_POST['title']
243          )
244        );
245    }
246
247    mass_updates(
248      IMAGES_TABLE,
249      array('primary' => array('id'), 'update' => array('name')),
250      $datas
251      );
252  }
253 
254  // date_creation
255  if ('date_creation' == $action)
256  {
257    $date_creation = sprintf(
258      '%u-%u-%u',
259      $_POST['date_creation_year'],
260      $_POST['date_creation_month'],
261      $_POST['date_creation_day']
262      );
263
264    $datas = array();
265    foreach ($collection as $image_id)
266    {
267      array_push(
268        $datas,
269        array(
270          'id' => $image_id,
271          'date_creation' => $date_creation
272          )
273        );
274    }
275
276    mass_updates(
277      IMAGES_TABLE,
278      array('primary' => array('id'), 'update' => array('date_creation')),
279      $datas
280      );
281  }
282 
283  // privacy_level
284  if ('level' == $action)
285  {
286    $datas = array();
287    foreach ($collection as $image_id)
288    {
289      array_push(
290        $datas,
291        array(
292          'id' => $image_id,
293          'level' => $_POST['level']
294          )
295        );
296    }
297
298    mass_updates(
299      IMAGES_TABLE,
300      array('primary' => array('id'), 'update' => array('level')),
301      $datas
302      );
303
304    if (isset($_SESSION['bulk_manager_filter']['level']))
305    {
306      if ($_POST['level'] < $_SESSION['bulk_manager_filter']['level'])
307      {
308        redirect($redirect_url);
309      }
310    }
311  }
312 
313  // add_to_caddie
314  if ('add_to_caddie' == $action)
315  {
316    fill_caddie($collection);
317  }
318 
319  // delete
320  if ('delete' == $action)
321  {
322    if (isset($_POST['confirm_deletion']) and 1 == $_POST['confirm_deletion'])
323    {
324      $deleted_count = delete_elements($collection, true);
325      if ($deleted_count > 0)
326      {
327        $_SESSION['page_infos'] = array(
328          sprintf(
329            l10n_dec(
330              '%d photo was deleted',
331              '%d photos were deleted',
332              $deleted_count
333              ),
334            $deleted_count
335            )
336          );
337
338        $redirect_url = get_root_url().'admin.php?page='.$_GET['page'];
339        redirect($redirect_url);
340      }
341      else
342      {
343        array_push($page['errors'], l10n('No photo can be deleted'));
344      }
345    }
346    else
347    {
348      array_push($page['errors'], l10n('You need to confirm deletion'));
349    }
350  }
351
352  // synchronize metadata
353  if ('metadata' == $action)
354  {
355    $query = '
356SELECT id, path
357  FROM '.IMAGES_TABLE.'
358  WHERE id IN ('.implode(',', $collection).')
359;';
360    $id_to_path = array();
361    $result = pwg_query($query);
362    while ($row = pwg_db_fetch_assoc($result))
363    {
364      $id_to_path[$row['id']] = $row['path'];
365    }
366   
367    update_metadata($id_to_path);
368
369    array_push(
370      $page['infos'],
371      l10n('Metadata synchronized from file')
372      );
373  }
374}
375
376// +-----------------------------------------------------------------------+
377// |                             template init                             |
378// +-----------------------------------------------------------------------+
379$template->set_filenames(array('batch_manager_global' => 'batch_manager_global.tpl'));
380
381$base_url = get_root_url().'admin.php';
382
383$template->assign(
384  array(
385    'filter' => $_SESSION['bulk_manager_filter'],
386    'selection' => $collection,
387    'U_DISPLAY'=>$base_url.get_query_string_diff(array('display')),
388    'F_ACTION'=>$base_url.get_query_string_diff(array('cat')),
389   )
390 );
391
392// +-----------------------------------------------------------------------+
393// |                            caddie options                             |
394// +-----------------------------------------------------------------------+
395
396$in_caddie = false;
397if (isset($_SESSION['bulk_manager_filter']['prefilter'])
398    and 'caddie' == $_SESSION['bulk_manager_filter']['prefilter'])
399{
400  $in_caddie = true;
401}
402$template->assign('IN_CADDIE', $in_caddie);
403
404// +-----------------------------------------------------------------------+
405// |                            deletion form                              |
406// +-----------------------------------------------------------------------+
407
408// we can only remove photos that have no storage_category_id, in other
409// word, it currently (Butterfly) means that the photo was added with
410// pLoader
411if (count($page['cat_elements_id']) > 0)
412{
413  $query = '
414SELECT
415    COUNT(*)
416  FROM '.IMAGES_TABLE.'
417  WHERE id IN ('.implode(',', $page['cat_elements_id']).')
418    AND storage_category_id IS NULL
419;';
420  list($counter) = pwg_db_fetch_row(pwg_query($query));
421
422  if ($counter > 0)
423  {
424    $template->assign('show_delete_form', true);
425  }
426}
427
428// +-----------------------------------------------------------------------+
429// |                           global mode form                            |
430// +-----------------------------------------------------------------------+
431
432// privacy level
433$template->assign(
434    array(
435      'filter_level_options'=> get_privacy_level_options(),
436      'filter_level_options_selected' => isset($_SESSION['bulk_manager_filter']['level'])
437        ? $_SESSION['bulk_manager_filter']['level']
438        : 0,
439    )
440  );
441
442// Virtualy associate a picture to a category
443$query = '
444SELECT id,name,uppercats,global_rank
445  FROM '.CATEGORIES_TABLE.'
446;';
447display_select_cat_wrapper($query, array(), 'associate_options', true);
448
449// in the filter box, which category to select by default
450$selected_category = array();
451
452if (isset($_SESSION['bulk_manager_filter']['category']))
453{
454  $selected_category = array($_SESSION['bulk_manager_filter']['category']);
455}
456else
457{
458  // we need to know the category in which the last photo was added
459  $selected_category = array();
460
461  $query = '
462SELECT
463    category_id,
464    id_uppercat
465  FROM '.IMAGES_TABLE.' AS i
466    JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON image_id = i.id
467    JOIN '.CATEGORIES_TABLE.' AS c ON category_id = c.id
468  ORDER BY i.id DESC
469  LIMIT 1
470;';
471  $result = pwg_query($query);
472  if (pwg_db_num_rows($result) > 0)
473  {
474    $row = pwg_db_fetch_assoc($result);
475 
476    $selected_category = array($row['category_id']);
477  }
478}
479
480$query = '
481SELECT id,name,uppercats,global_rank
482  FROM '.CATEGORIES_TABLE.'
483;';
484display_select_cat_wrapper($query, $selected_category, 'filter_category_options', true);
485
486// Dissociate from a category : categories listed for dissociation can only
487// represent virtual links. We can't create orphans. Links to physical
488// categories can't be broken.
489if (count($page['cat_elements_id']) > 0)
490{
491  $query = '
492SELECT
493    DISTINCT(category_id) AS id,
494    c.name,
495    c.uppercats,
496    c.global_rank
497  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
498    JOIN '.CATEGORIES_TABLE.' AS c ON c.id = ic.category_id
499    JOIN '.IMAGES_TABLE.' AS i ON i.id = ic.image_id
500  WHERE ic.image_id IN ('.implode(',', $page['cat_elements_id']).')
501    AND (
502      ic.category_id != i.storage_category_id
503      OR i.storage_category_id IS NULL
504    )
505;';
506  display_select_cat_wrapper($query, array(), 'dissociate_options', true);
507}
508
509if (count($page['cat_elements_id']) > 0)
510{
511  // remove tags
512  $tags = get_common_tags($page['cat_elements_id'], -1);
513
514  $template->assign(
515    array(
516      'DEL_TAG_SELECTION' => get_html_tag_selection($tags, 'del_tags'),
517      )
518    );
519}
520
521// creation date
522$day =
523empty($_POST['date_creation_day']) ? date('j') : $_POST['date_creation_day'];
524
525$month =
526empty($_POST['date_creation_month']) ? date('n') : $_POST['date_creation_month'];
527
528$year =
529empty($_POST['date_creation_year']) ? date('Y') : $_POST['date_creation_year'];
530
531$month_list = $lang['month'];
532$month_list[0]='------------';
533ksort($month_list);
534$template->assign( array(
535      'month_list'         => $month_list,
536      'DATE_CREATION_DAY'  => (int)$day,
537      'DATE_CREATION_MONTH'=> (int)$month,
538      'DATE_CREATION_YEAR' => (int)$year,
539    )
540  );
541
542// image level options
543$template->assign(
544    array(
545      'level_options'=> get_privacy_level_options(),
546      'level_options_selected' => 0,
547    )
548  );
549
550// metadata
551include_once( PHPWG_ROOT_PATH.'admin/site_reader_local.php');
552$site_reader = new LocalSiteReader('./');
553$used_metadata = implode( ', ', $site_reader->get_metadata_attributes());
554
555$template->assign(
556    array(
557      'used_metadata' => $used_metadata,
558    )
559  );
560
561// +-----------------------------------------------------------------------+
562// |                        global mode thumbnails                         |
563// +-----------------------------------------------------------------------+
564
565// how many items to display on this page
566if (!empty($_GET['display']))
567{
568  if ('all' == $_GET['display'])
569  {
570    $page['nb_images'] = count($page['cat_elements_id']);
571  }
572  else
573  {
574    $page['nb_images'] = intval($_GET['display']);
575  }
576}
577else
578{
579  $page['nb_images'] = 20;
580}
581
582$nb_thumbs_page = 0;
583
584if (count($page['cat_elements_id']) > 0)
585{
586  $nav_bar = create_navigation_bar(
587    $base_url.get_query_string_diff(array('start')),
588    count($page['cat_elements_id']),
589    $page['start'],
590    $page['nb_images']
591    );
592  $template->assign('navbar', $nav_bar);
593
594  $is_category = false;
595  if (isset($_SESSION['bulk_manager_filter']['category'])
596      and !isset($_SESSION['bulk_manager_filter']['category_recursive']))
597  {
598    $is_category = true;
599  }
600
601  if (isset($_SESSION['bulk_manager_filter']['prefilter'])
602      and 'duplicates' == $_SESSION['bulk_manager_filter']['prefilter'])
603  {
604    $conf['order_by'] = ' ORDER BY file, id';
605  }
606
607
608  $query = '
609SELECT id,path,tn_ext,file,filesize,level,name
610  FROM '.IMAGES_TABLE;
611 
612  if ($is_category)
613  {
614    $category_info = get_cat_info($_SESSION['bulk_manager_filter']['category']);
615   
616    $conf['order_by'] = $conf['order_by_inside_category'];
617    if (!empty($category_info['image_order']))
618    {
619      $conf['order_by'] = ' ORDER BY '.$category_info['image_order'];
620    }
621
622    $query.= '
623    JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
624  }
625
626  $query.= '
627  WHERE id IN ('.implode(',', $page['cat_elements_id']).')';
628
629  if ($is_category)
630  {
631    $query.= '
632    AND category_id = '.$_SESSION['bulk_manager_filter']['category'];
633  }
634
635  $query.= '
636  '.$conf['order_by'].'
637  LIMIT '.$page['nb_images'].' OFFSET '.$page['start'].'
638;';
639  $result = pwg_query($query);
640
641  // template thumbnail initialization
642  while ($row = pwg_db_fetch_assoc($result))
643  {
644    $nb_thumbs_page++;
645    $src = get_thumbnail_url($row);
646
647    $title = $row['name'];
648    if (empty($title))
649    {     
650      $title = get_name_from_file($row['file']);
651    }
652
653    $template->append(
654      'thumbnails',
655      array(
656        'ID' => $row['id'],
657        'TN_SRC' => $src,
658        'FILE' => $row['file'],
659        'TITLE' => $title,
660        'LEVEL' => $row['level']
661        )
662      );
663  }
664}
665
666$template->assign(
667  array(
668    'nb_thumbs_page' => $nb_thumbs_page,
669    'nb_thumbs_set' => count($page['cat_elements_id']),
670    )
671  );
672
673trigger_action('loc_end_element_set_global');
674
675//----------------------------------------------------------- sending html code
676$template->assign_var_from_handle('ADMIN_CONTENT', 'batch_manager_global');
677?>
Note: See TracBrowser for help on using the repository browser.