source: branches/2.2/admin/batch_manager_global.php @ 11056

Last change on this file since 11056 was 11056, checked in by mistic100, 13 years ago

bug:2278 fixed (merge r10970 r11008 r11039 from trunk) replace FCBKcomplete by TokenInput to avoid 3rd tag issue on autocomplete

File size: 18.9 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    if (empty($_POST['add_tags']))
121    {
122      array_push($page['errors'], l10n('Select at least one tag'));
123    }
124    else
125    {
126      $tag_ids = get_tag_ids($_POST['add_tags']);
127      add_tags($tag_ids, $collection);
128
129      if ('with no tag' == $page['prefilter'])
130      {
131        redirect(get_root_url().'admin.php?page='.$_GET['page']);
132      }
133    }
134  }
135
136  if ('del_tags' == $action)
137  {
138    if (count($_POST['del_tags']) == 0)
139    {
140      array_push($page['errors'], l10n('Select at least one tag'));
141    }
142   
143    $query = '
144DELETE
145  FROM '.IMAGE_TAG_TABLE.'
146  WHERE image_id IN ('.implode(',', $collection).')
147    AND tag_id IN ('.implode(',', $_POST['del_tags']).')
148;';
149    pwg_query($query);
150  }
151
152  if ('associate' == $action)
153  {
154    associate_images_to_categories(
155      $collection,
156      array($_POST['associate'])
157      );
158
159    $_SESSION['page_infos'] = array(
160      l10n('Information data registered in database')
161      );
162   
163    // let's refresh the page because we the current set might be modified
164    if ('with no album' == $page['prefilter'])
165    {
166      redirect($redirect_url);
167    }
168
169    if ('with no virtual album' == $page['prefilter'])
170    {
171      $category_info = get_cat_info($_POST['associate']);
172      if (empty($category_info['dir']))
173      {
174        redirect($redirect_url);
175      }
176    }
177  }
178
179  if ('dissociate' == $action)
180  {
181    // physical links must not be broken, so we must first retrieve image_id
182    // which create virtual links with the category to "dissociate from".
183    $query = '
184SELECT id
185  FROM '.IMAGE_CATEGORY_TABLE.'
186    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
187  WHERE category_id = '.$_POST['dissociate'].'
188    AND id IN ('.implode(',', $collection).')
189    AND (
190      category_id != storage_category_id
191      OR storage_category_id IS NULL
192    )
193;';
194    $dissociables = array_from_query($query, 'id');
195
196    if (!empty($dissociables))
197    {
198      $query = '
199DELETE
200  FROM '.IMAGE_CATEGORY_TABLE.'
201  WHERE category_id = '.$_POST['dissociate'].'
202    AND image_id IN ('.implode(',', $dissociables).')
203';
204      pwg_query($query);
205
206      update_category($_POST['dissociate']);
207     
208      $_SESSION['page_infos'] = array(
209        l10n('Information data registered in database')
210        );
211     
212      // let's refresh the page because we the current set might be modified
213      redirect($redirect_url);
214    }
215  }
216
217  // author
218  if ('author' == $action)
219  {
220    if (isset($_POST['remove_author']))
221    {
222      $_POST['author'] = null;
223    }
224   
225    $datas = array();
226    foreach ($collection as $image_id)
227    {
228      array_push(
229        $datas,
230        array(
231          'id' => $image_id,
232          'author' => $_POST['author']
233          )
234        );
235    }
236
237    mass_updates(
238      IMAGES_TABLE,
239      array('primary' => array('id'), 'update' => array('author')),
240      $datas
241      );
242  }
243
244  // title
245  if ('title' == $action)
246  {
247    if (isset($_POST['remove_title']))
248    {
249      $_POST['title'] = null;
250    }
251   
252    $datas = array();
253    foreach ($collection as $image_id)
254    {
255      array_push(
256        $datas,
257        array(
258          'id' => $image_id,
259          'name' => $_POST['title']
260          )
261        );
262    }
263
264    mass_updates(
265      IMAGES_TABLE,
266      array('primary' => array('id'), 'update' => array('name')),
267      $datas
268      );
269  }
270 
271  // date_creation
272  if ('date_creation' == $action)
273  {
274    $date_creation = sprintf(
275      '%u-%u-%u',
276      $_POST['date_creation_year'],
277      $_POST['date_creation_month'],
278      $_POST['date_creation_day']
279      );
280
281    if (isset($_POST['remove_date_creation']))
282    {
283      $date_creation = null;
284    }
285
286    $datas = array();
287    foreach ($collection as $image_id)
288    {
289      array_push(
290        $datas,
291        array(
292          'id' => $image_id,
293          'date_creation' => $date_creation
294          )
295        );
296    }
297
298    mass_updates(
299      IMAGES_TABLE,
300      array('primary' => array('id'), 'update' => array('date_creation')),
301      $datas
302      );
303  }
304 
305  // privacy_level
306  if ('level' == $action)
307  {
308    $datas = array();
309    foreach ($collection as $image_id)
310    {
311      array_push(
312        $datas,
313        array(
314          'id' => $image_id,
315          'level' => $_POST['level']
316          )
317        );
318    }
319
320    mass_updates(
321      IMAGES_TABLE,
322      array('primary' => array('id'), 'update' => array('level')),
323      $datas
324      );
325
326    if (isset($_SESSION['bulk_manager_filter']['level']))
327    {
328      if ($_POST['level'] < $_SESSION['bulk_manager_filter']['level'])
329      {
330        redirect($redirect_url);
331      }
332    }
333  }
334 
335  // add_to_caddie
336  if ('add_to_caddie' == $action)
337  {
338    fill_caddie($collection);
339  }
340 
341  // delete
342  if ('delete' == $action)
343  {
344    if (isset($_POST['confirm_deletion']) and 1 == $_POST['confirm_deletion'])
345    {
346      $deleted_count = delete_elements($collection, true);
347      if ($deleted_count > 0)
348      {
349        $_SESSION['page_infos'] = array(
350          sprintf(
351            l10n_dec(
352              '%d photo was deleted',
353              '%d photos were deleted',
354              $deleted_count
355              ),
356            $deleted_count
357            )
358          );
359
360        $redirect_url = get_root_url().'admin.php?page='.$_GET['page'];
361        redirect($redirect_url);
362      }
363      else
364      {
365        array_push($page['errors'], l10n('No photo can be deleted'));
366      }
367    }
368    else
369    {
370      array_push($page['errors'], l10n('You need to confirm deletion'));
371    }
372  }
373
374  // synchronize metadata
375  if ('metadata' == $action)
376  {
377    $query = '
378SELECT id, path
379  FROM '.IMAGES_TABLE.'
380  WHERE id IN ('.implode(',', $collection).')
381;';
382    $id_to_path = array();
383    $result = pwg_query($query);
384    while ($row = pwg_db_fetch_assoc($result))
385    {
386      $id_to_path[$row['id']] = $row['path'];
387    }
388   
389    update_metadata($id_to_path);
390
391    array_push(
392      $page['infos'],
393      l10n('Metadata synchronized from file')
394      );
395  }
396
397  trigger_action('element_set_global_action', $action, $collection);
398}
399
400// +-----------------------------------------------------------------------+
401// |                             template init                             |
402// +-----------------------------------------------------------------------+
403$template->set_filenames(array('batch_manager_global' => 'batch_manager_global.tpl'));
404
405$base_url = get_root_url().'admin.php';
406
407$prefilters = array();
408
409array_push($prefilters,
410  array('ID' => 'caddie', 'NAME' => l10n('caddie')),
411  array('ID' => 'last import', 'NAME' => l10n('last import')),
412  array('ID' => 'with no album', 'NAME' => l10n('with no album')),
413  array('ID' => 'with no tag', 'NAME' => l10n('with no tag')),
414  array('ID' => 'duplicates', 'NAME' => l10n('duplicates')),
415  array('ID' => 'all photos', 'NAME' => l10n('All'))
416);
417
418if ($conf['enable_synchronization'])
419{
420  array_push($prefilters,
421    array('ID' => 'with no virtual album', 'NAME' => l10n('with no virtual album'))
422  );
423}
424
425$prefilters = trigger_event('get_batch_manager_prefilters', $prefilters);
426usort($prefilters, 'UC_name_compare');
427
428$template->assign(
429  array(
430    'prefilters' => $prefilters,
431    'filter' => $_SESSION['bulk_manager_filter'],
432    'selection' => $collection,
433    'U_DISPLAY'=>$base_url.get_query_string_diff(array('display')),
434    'F_ACTION'=>$base_url.get_query_string_diff(array('cat')),
435   )
436 );
437
438// +-----------------------------------------------------------------------+
439// |                            caddie options                             |
440// +-----------------------------------------------------------------------+
441
442$in_caddie = false;
443if (isset($_SESSION['bulk_manager_filter']['prefilter'])
444    and 'caddie' == $_SESSION['bulk_manager_filter']['prefilter'])
445{
446  $in_caddie = true;
447}
448$template->assign('IN_CADDIE', $in_caddie);
449
450// +-----------------------------------------------------------------------+
451// |                            deletion form                              |
452// +-----------------------------------------------------------------------+
453
454// we can only remove photos that have no storage_category_id, in other
455// word, it currently (Butterfly) means that the photo was added with
456// pLoader
457if (count($page['cat_elements_id']) > 0)
458{
459  $query = '
460SELECT
461    id
462  FROM '.IMAGES_TABLE.'
463  WHERE id IN ('.implode(',', $page['cat_elements_id']).')
464    AND file NOT LIKE \'http%\'
465  LIMIT 1
466;';
467  ;
468
469  if ( pwg_db_fetch_row(pwg_query($query)) )
470  {
471    $template->assign('show_delete_form', true);
472  }
473}
474
475// +-----------------------------------------------------------------------+
476// |                           global mode form                            |
477// +-----------------------------------------------------------------------+
478
479// privacy level
480$template->assign(
481    array(
482      'filter_level_options'=> get_privacy_level_options(),
483      'filter_level_options_selected' => isset($_SESSION['bulk_manager_filter']['level'])
484        ? $_SESSION['bulk_manager_filter']['level']
485        : 0,
486    )
487  );
488
489// Virtualy associate a picture to a category
490$query = '
491SELECT id,name,uppercats,global_rank
492  FROM '.CATEGORIES_TABLE.'
493;';
494display_select_cat_wrapper($query, array(), 'associate_options', true);
495
496// in the filter box, which category to select by default
497$selected_category = array();
498
499if (isset($_SESSION['bulk_manager_filter']['category']))
500{
501  $selected_category = array($_SESSION['bulk_manager_filter']['category']);
502}
503else
504{
505  // we need to know the category in which the last photo was added
506  $selected_category = array();
507
508  $query = '
509SELECT
510    category_id,
511    id_uppercat
512  FROM '.IMAGES_TABLE.' AS i
513    JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON image_id = i.id
514    JOIN '.CATEGORIES_TABLE.' AS c ON category_id = c.id
515  ORDER BY i.id DESC
516  LIMIT 1
517;';
518  $result = pwg_query($query);
519  if (pwg_db_num_rows($result) > 0)
520  {
521    $row = pwg_db_fetch_assoc($result);
522 
523    $selected_category = array($row['category_id']);
524  }
525}
526
527$query = '
528SELECT id,name,uppercats,global_rank
529  FROM '.CATEGORIES_TABLE.'
530;';
531display_select_cat_wrapper($query, $selected_category, 'filter_category_options', true);
532
533// Dissociate from a category : categories listed for dissociation can only
534// represent virtual links. We can't create orphans. Links to physical
535// categories can't be broken.
536if (count($page['cat_elements_id']) > 0)
537{
538  $query = '
539SELECT
540    DISTINCT(category_id) AS id,
541    c.name,
542    c.uppercats,
543    c.global_rank
544  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
545    JOIN '.CATEGORIES_TABLE.' AS c ON c.id = ic.category_id
546    JOIN '.IMAGES_TABLE.' AS i ON i.id = ic.image_id
547  WHERE ic.image_id IN ('.implode(',', $page['cat_elements_id']).')
548    AND (
549      ic.category_id != i.storage_category_id
550      OR i.storage_category_id IS NULL
551    )
552;';
553  display_select_cat_wrapper($query, array(), 'dissociate_options', true);
554}
555
556if (count($page['cat_elements_id']) > 0)
557{
558  // remove tags
559  $tags = get_common_tags($page['cat_elements_id'], -1);
560
561  $template->assign(
562    array(
563      'DEL_TAG_SELECTION' => get_html_tag_selection($tags, 'del_tags'),
564      )
565    );
566}
567
568// creation date
569$day =
570empty($_POST['date_creation_day']) ? date('j') : $_POST['date_creation_day'];
571
572$month =
573empty($_POST['date_creation_month']) ? date('n') : $_POST['date_creation_month'];
574
575$year =
576empty($_POST['date_creation_year']) ? date('Y') : $_POST['date_creation_year'];
577
578$month_list = $lang['month'];
579$month_list[0]='------------';
580ksort($month_list);
581$template->assign( array(
582      'month_list'         => $month_list,
583      'DATE_CREATION_DAY'  => (int)$day,
584      'DATE_CREATION_MONTH'=> (int)$month,
585      'DATE_CREATION_YEAR' => (int)$year,
586    )
587  );
588
589// image level options
590$template->assign(
591    array(
592      'level_options'=> get_privacy_level_options(),
593      'level_options_selected' => 0,
594    )
595  );
596
597// metadata
598include_once( PHPWG_ROOT_PATH.'admin/site_reader_local.php');
599$site_reader = new LocalSiteReader('./');
600$used_metadata = implode( ', ', $site_reader->get_metadata_attributes());
601
602$template->assign(
603    array(
604      'used_metadata' => $used_metadata,
605    )
606  );
607
608// +-----------------------------------------------------------------------+
609// |                        global mode thumbnails                         |
610// +-----------------------------------------------------------------------+
611
612// how many items to display on this page
613if (!empty($_GET['display']))
614{
615  if ('all' == $_GET['display'])
616  {
617    $page['nb_images'] = count($page['cat_elements_id']);
618  }
619  else
620  {
621    $page['nb_images'] = intval($_GET['display']);
622  }
623}
624else
625{
626  $page['nb_images'] = 20;
627}
628
629$nb_thumbs_page = 0;
630
631if (count($page['cat_elements_id']) > 0)
632{
633  $nav_bar = create_navigation_bar(
634    $base_url.get_query_string_diff(array('start')),
635    count($page['cat_elements_id']),
636    $page['start'],
637    $page['nb_images']
638    );
639  $template->assign('navbar', $nav_bar);
640
641  $is_category = false;
642  if (isset($_SESSION['bulk_manager_filter']['category'])
643      and !isset($_SESSION['bulk_manager_filter']['category_recursive']))
644  {
645    $is_category = true;
646  }
647
648  if (isset($_SESSION['bulk_manager_filter']['prefilter'])
649      and 'duplicates' == $_SESSION['bulk_manager_filter']['prefilter'])
650  {
651    $conf['order_by'] = ' ORDER BY file, id';
652  }
653
654
655  $query = '
656SELECT id,path,tn_ext,file,filesize,level,name
657  FROM '.IMAGES_TABLE;
658 
659  if ($is_category)
660  {
661    $category_info = get_cat_info($_SESSION['bulk_manager_filter']['category']);
662   
663    $conf['order_by'] = $conf['order_by_inside_category'];
664    if (!empty($category_info['image_order']))
665    {
666      $conf['order_by'] = ' ORDER BY '.$category_info['image_order'];
667    }
668
669    $query.= '
670    JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
671  }
672
673  $query.= '
674  WHERE id IN ('.implode(',', $page['cat_elements_id']).')';
675
676  if ($is_category)
677  {
678    $query.= '
679    AND category_id = '.$_SESSION['bulk_manager_filter']['category'];
680  }
681
682  $query.= '
683  '.$conf['order_by'].'
684  LIMIT '.$page['nb_images'].' OFFSET '.$page['start'].'
685;';
686  $result = pwg_query($query);
687
688  // template thumbnail initialization
689  while ($row = pwg_db_fetch_assoc($result))
690  {
691    $nb_thumbs_page++;
692    $src = get_thumbnail_url($row);
693
694    $title = $row['name'];
695    if (empty($title))
696    {     
697      $title = get_name_from_file($row['file']);
698    }
699
700    $template->append(
701      'thumbnails',
702      array(
703        'ID' => $row['id'],
704        'TN_SRC' => $src,
705        'FILE' => $row['file'],
706        'TITLE' => $title,
707        'LEVEL' => $row['level']
708        )
709      );
710  }
711}
712
713$template->assign(
714  array(
715    'nb_thumbs_page' => $nb_thumbs_page,
716    'nb_thumbs_set' => count($page['cat_elements_id']),
717    )
718  );
719
720trigger_action('loc_end_element_set_global');
721
722//----------------------------------------------------------- sending html code
723$template->assign_var_from_handle('ADMIN_CONTENT', 'batch_manager_global');
724?>
Note: See TracBrowser for help on using the repository browser.