source: extensions/bulk_manager/element_set_global.php @ 19456

Last change on this file since 19456 was 6772, checked in by plg, 14 years ago

feature 1802 added: filtering system. Only 3 filters available, more to come
later. Ability to add/remove filters on the fly.

When an administrator displays the "caddie" content, it means bulk manager
with a single filter "prefilter=caddie". Same principle for the photos of a
given album.

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