source: trunk/admin/batch_manager_global.php @ 8417

Last change on this file since 8417 was 8417, checked in by plg, 13 years ago

feature 2089: finish removing element_set, now it's time for batch_manager

File size: 15.9 KB
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
67if (isset($_POST['submit']))
68{
69  // if the user tries to apply an action, it means that there is at least 1
70  // photo in the selection
71  if (count($collection) == 0)
72  {
73    array_push($page['errors'], l10n('Select at least one picture'));
74  }
75
76  $action = $_POST['selectAction'];
77 
78  if ('remove_from_caddie' == $action)
79  {
80    $query = '
81DELETE
82  FROM '.CADDIE_TABLE.'
83  WHERE element_id IN ('.implode(',', $collection).')
84    AND user_id = '.$user['id'].'
85;';
86    pwg_query($query);
87
88    // if we are here in the code, it means that the user is currently
89    // displaying the caddie content, so we have to remove the current
90    // selection from the current set
91    $page['cat_elements_id'] = array_diff($page['cat_elements_id'], $collection);
92  }
93
94  if ('add_tags' == $action)
95  {
96    $tag_ids = get_fckb_tag_ids($_POST['add_tags']);
97    add_tags($tag_ids, $collection);
98  }
99
100  if ('del_tags' == $action)
101  {
102    if (count($_POST['del_tags']) == 0)
103    {
104      array_push($page['errors'], l10n('Select at least one tag'));
105    }
106   
107    $query = '
108DELETE
109  FROM '.IMAGE_TAG_TABLE.'
110  WHERE image_id IN ('.implode(',', $collection).')
111    AND tag_id IN ('.implode(',', $_POST['del_tags']).')
112;';
113    pwg_query($query);
114  }
115
116  if ('associate' == $action)
117  {
118    associate_images_to_categories(
119      $collection,
120      array($_POST['associate'])
121      );
122
123    $_SESSION['page_infos'] = array(
124      l10n('Information data registered in database')
125      );
126   
127    // let's refresh the page because we the current set might be modified
128    $redirect_url = get_root_url().'admin.php?page='.$_GET['page'];
129    redirect($redirect_url);
130  }
131
132  if ('dissociate' == $action)
133  {
134    // physical links must not be broken, so we must first retrieve image_id
135    // which create virtual links with the category to "dissociate from".
136    $query = '
137SELECT id
138  FROM '.IMAGE_CATEGORY_TABLE.'
139    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
140  WHERE category_id = '.$_POST['dissociate'].'
141    AND id IN ('.implode(',', $collection).')
142    AND (
143      category_id != storage_category_id
144      OR storage_category_id IS NULL
145    )
146;';
147    $dissociables = array_from_query($query, 'id');
148
149    if (!empty($dissociables))
150    {
151      $query = '
152DELETE
153  FROM '.IMAGE_CATEGORY_TABLE.'
154  WHERE category_id = '.$_POST['dissociate'].'
155    AND image_id IN ('.implode(',', $dissociables).')
156';
157      pwg_query($query);
158
159      update_category($_POST['dissociate']);
160     
161      $_SESSION['page_infos'] = array(
162        l10n('Information data registered in database')
163        );
164     
165      // let's refresh the page because we the current set might be modified
166      $redirect_url = get_root_url().'admin.php?page='.$_GET['page'];
167      redirect($redirect_url);
168    }
169  }
170
171  // author
172  if ('author' == $action)
173  {
174    $datas = array();
175    foreach ($collection as $image_id)
176    {
177      array_push(
178        $datas,
179        array(
180          'id' => $image_id,
181          'author' => $_POST['author']
182          )
183        );
184    }
185
186    mass_updates(
187      IMAGES_TABLE,
188      array('primary' => array('id'), 'update' => array('author')),
189      $datas
190      );
191  }
192
193  // title
194  if ('title' == $action)
195  {
196    $datas = array();
197    foreach ($collection as $image_id)
198    {
199      array_push(
200        $datas,
201        array(
202          'id' => $image_id,
203          'name' => $_POST['title']
204          )
205        );
206    }
207
208    mass_updates(
209      IMAGES_TABLE,
210      array('primary' => array('id'), 'update' => array('name')),
211      $datas
212      );
213  }
214 
215  // date_creation
216  if ('date_creation' == $action)
217  {
218    $date_creation = sprintf(
219      '%u-%u-%u',
220      $_POST['date_creation_year'],
221      $_POST['date_creation_month'],
222      $_POST['date_creation_day']
223      );
224
225    $datas = array();
226    foreach ($collection as $image_id)
227    {
228      array_push(
229        $datas,
230        array(
231          'id' => $image_id,
232          'date_creation' => $date_creation
233          )
234        );
235    }
236
237    mass_updates(
238      IMAGES_TABLE,
239      array('primary' => array('id'), 'update' => array('date_creation')),
240      $datas
241      );
242  }
243 
244  // privacy_level
245  if ('level' == $action)
246  {
247    $datas = array();
248    foreach ($collection as $image_id)
249    {
250      array_push(
251        $datas,
252        array(
253          'id' => $image_id,
254          'level' => $_POST['level']
255          )
256        );
257    }
258
259    mass_updates(
260      IMAGES_TABLE,
261      array('primary' => array('id'), 'update' => array('level')),
262      $datas
263      );
264  }
265 
266  // add_to_caddie
267  if ('add_to_caddie' == $action)
268  {
269    fill_caddie($collection);
270  }
271 
272  // delete
273  if ('delete' == $action)
274  {
275    if (isset($_POST['confirm_deletion']) and 1 == $_POST['confirm_deletion'])
276    {
277      $deleted_count = delete_elements($collection, true);
278      if ($deleted_count > 0)
279      {
280        $_SESSION['page_infos'] = array(
281          sprintf(
282            l10n_dec(
283              '%d photo was deleted',
284              '%d photos were deleted',
285              $deleted_count
286              ),
287            $deleted_count
288            )
289          );
290
291        $redirect_url = get_root_url().'admin.php?page='.$_GET['page'];
292        redirect($redirect_url);
293      }
294      else
295      {
296        array_push($page['errors'], l10n('No photo can be deleted'));
297      }
298    }
299    else
300    {
301      array_push($page['errors'], l10n('You need to confirm deletion'));
302    }
303  }
304}
305
306// +-----------------------------------------------------------------------+
307// |                             template init                             |
308// +-----------------------------------------------------------------------+
309$template->set_filenames(array('batch_manager_global' => 'batch_manager_global.tpl'));
310
311$base_url = get_root_url().'admin.php';
312
313$template->assign(
314  array(
315    'filter' => $_SESSION['bulk_manager_filter'],
316    'selection' => $collection,
317    'U_DISPLAY'=>$base_url.get_query_string_diff(array('display')),
318    'F_ACTION'=>$base_url.get_query_string_diff(array('cat')),
319   )
320 );
321
322// +-----------------------------------------------------------------------+
323// |                            caddie options                             |
324// +-----------------------------------------------------------------------+
325
326$in_caddie = false;
327if (isset($_SESSION['bulk_manager_filter']['prefilter'])
328    and 'caddie' == $_SESSION['bulk_manager_filter']['prefilter'])
329{
330  $in_caddie = true;
331}
332$template->assign('IN_CADDIE', $in_caddie);
333
334// +-----------------------------------------------------------------------+
335// |                            deletion form                              |
336// +-----------------------------------------------------------------------+
337
338// we can only remove photos that have no storage_category_id, in other
339// word, it currently (Butterfly) means that the photo was added with
340// pLoader
341if (count($page['cat_elements_id']) > 0)
342{
343  $query = '
344SELECT
345    COUNT(*)
346  FROM '.IMAGES_TABLE.'
347  WHERE id IN ('.implode(',', $page['cat_elements_id']).')
348    AND storage_category_id IS NULL
349;';
350  list($counter) = pwg_db_fetch_row(pwg_query($query));
351
352  if ($counter > 0)
353  {
354    $template->assign('show_delete_form', true);
355  }
356}
357
358// +-----------------------------------------------------------------------+
359// |                           global mode form                            |
360// +-----------------------------------------------------------------------+
361
362// privacy level
363$template->assign(
364    array(
365      'filter_level_options'=> get_privacy_level_options(),
366      'filter_level_options_selected' => isset($_SESSION['bulk_manager_filter']['level'])
367        ? $_SESSION['bulk_manager_filter']['level']
368        : 0,
369    )
370  );
371
372// Virtualy associate a picture to a category
373$query = '
374SELECT id,name,uppercats,global_rank
375  FROM '.CATEGORIES_TABLE.'
376;';
377display_select_cat_wrapper($query, array(), 'associate_options', true);
378
379// in the filter box, which category to select by default
380$selected_category = array();
381
382if (isset($_SESSION['bulk_manager_filter']['category']))
383{
384  $selected_category = array($_SESSION['bulk_manager_filter']['category']);
385}
386else
387{
388  // we need to know the category in which the last photo was added
389  $selected_category = array();
390
391  $query = '
392SELECT
393    category_id,
394    id_uppercat
395  FROM '.IMAGES_TABLE.' AS i
396    JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON image_id = i.id
397    JOIN '.CATEGORIES_TABLE.' AS c ON category_id = c.id
398  ORDER BY i.id DESC
399  LIMIT 1
400;';
401  $result = pwg_query($query);
402  if (pwg_db_num_rows($result) > 0)
403  {
404    $row = pwg_db_fetch_assoc($result);
405 
406    $selected_category = array($row['category_id']);
407  }
408}
409
410$query = '
411SELECT id,name,uppercats,global_rank
412  FROM '.CATEGORIES_TABLE.'
413;';
414display_select_cat_wrapper($query, $selected_category, 'filter_category_options', true);
415
416// Dissociate from a category : categories listed for dissociation can only
417// represent virtual links. We can't create orphans. Links to physical
418// categories can't be broken.
419if (count($page['cat_elements_id']) > 0)
420{
421  $query = '
422SELECT
423    DISTINCT(category_id) AS id,
424    c.name,
425    c.uppercats,
426    c.global_rank
427  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
428    JOIN '.CATEGORIES_TABLE.' AS c ON c.id = ic.category_id
429    JOIN '.IMAGES_TABLE.' AS i ON i.id = ic.image_id
430  WHERE ic.image_id IN ('.implode(',', $page['cat_elements_id']).')
431    AND (
432      ic.category_id != i.storage_category_id
433      OR i.storage_category_id IS NULL
434    )
435;';
436  display_select_cat_wrapper($query, array(), 'dissociate_options', true);
437}
438
439if (count($page['cat_elements_id']) > 0)
440{
441  // remove tags
442  $tags = get_common_tags($page['cat_elements_id'], -1);
443
444  $template->assign(
445    array(
446      'DEL_TAG_SELECTION' => get_html_tag_selection($tags, 'del_tags'),
447      )
448    );
449}
450
451// creation date
452$day =
453empty($_POST['date_creation_day']) ? date('j') : $_POST['date_creation_day'];
454
455$month =
456empty($_POST['date_creation_month']) ? date('n') : $_POST['date_creation_month'];
457
458$year =
459empty($_POST['date_creation_year']) ? date('Y') : $_POST['date_creation_year'];
460
461$month_list = $lang['month'];
462$month_list[0]='------------';
463ksort($month_list);
464$template->assign( array(
465      'month_list'         => $month_list,
466      'DATE_CREATION_DAY'  => (int)$day,
467      'DATE_CREATION_MONTH'=> (int)$month,
468      'DATE_CREATION_YEAR' => (int)$year,
469    )
470  );
471
472// image level options
473$template->assign(
474    array(
475      'level_options'=> get_privacy_level_options(),
476      'level_options_selected' => 0,
477    )
478  );
479
480// +-----------------------------------------------------------------------+
481// |                        global mode thumbnails                         |
482// +-----------------------------------------------------------------------+
483
484// how many items to display on this page
485if (!empty($_GET['display']))
486{
487  if ('all' == $_GET['display'])
488  {
489    $page['nb_images'] = count($page['cat_elements_id']);
490  }
491  else
492  {
493    $page['nb_images'] = intval($_GET['display']);
494  }
495}
496else
497{
498  $page['nb_images'] = 20;
499}
500
501$nb_thumbs_page = 0;
502
503if (count($page['cat_elements_id']) > 0)
504{
505  $nav_bar = create_navigation_bar(
506    $base_url.get_query_string_diff(array('start')),
507    count($page['cat_elements_id']),
508    $page['start'],
509    $page['nb_images']
510    );
511  $template->assign('navbar', $nav_bar);
512
513  $is_category = false;
514  if (isset($_SESSION['bulk_manager_filter']['category'])
515      and !isset($_SESSION['bulk_manager_filter']['category_recursive']))
516  {
517    $is_category = true;
518  }
519
520  if (isset($_SESSION['bulk_manager_filter']['prefilter'])
521      and 'duplicates' == $_SESSION['bulk_manager_filter']['prefilter'])
522  {
523    $conf['order_by'] = ' ORDER BY file, id';
524  }
525
526
527  $query = '
528SELECT id,path,tn_ext,file,filesize,level,name
529  FROM '.IMAGES_TABLE;
530 
531  if ($is_category)
532  {
533    $category_info = get_cat_info($_SESSION['bulk_manager_filter']['category']);
534   
535    $conf['order_by'] = $conf['order_by_inside_category'];
536    if (!empty($category_info['image_order']))
537    {
538      $conf['order_by'] = ' ORDER BY '.$category_info['image_order'];
539    }
540
541    $query.= '
542    JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
543  }
544
545  $query.= '
546  WHERE id IN ('.implode(',', $page['cat_elements_id']).')';
547
548  if ($is_category)
549  {
550    $query.= '
551    AND category_id = '.$_SESSION['bulk_manager_filter']['category'];
552  }
553
554  $query.= '
555  '.$conf['order_by'].'
556  LIMIT '.$page['nb_images'].' OFFSET '.$page['start'].'
557;';
558  $result = pwg_query($query);
559
560  // template thumbnail initialization
561  while ($row = pwg_db_fetch_assoc($result))
562  {
563    $nb_thumbs_page++;
564    $src = get_thumbnail_url($row);
565
566    $title = $row['name'];
567    if (empty($title))
568    {     
569      $title = get_name_from_file($row['file']);
570    }
571
572    $template->append(
573      'thumbnails',
574      array(
575        'ID' => $row['id'],
576        'TN_SRC' => $src,
577        'FILE' => $row['file'],
578        'TITLE' => $title,
579        'LEVEL' => $row['level']
580        )
581      );
582  }
583}
584
585$template->assign(
586  array(
587    'nb_thumbs_page' => $nb_thumbs_page,
588    'nb_thumbs_set' => count($page['cat_elements_id']),
589    )
590  );
591
592trigger_action('loc_end_element_set_global');
593
594//----------------------------------------------------------- sending html code
595$template->assign_var_from_handle('ADMIN_CONTENT', 'batch_manager_global');
596?>
Note: See TracBrowser for help on using the repository browser.