source: extensions/bulk_manager/element_set_global.php @ 6769

Last change on this file since 6769 was 6756, checked in by plg, 14 years ago

initial release for Piwigo 2.2 bulk manager

File size: 14.1 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    'selection' => $collection,
328   
329    'CATEGORIES_NAV'=>$page['title'],
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()),
340   )
341 );
342
343// +-----------------------------------------------------------------------+
344// |                            caddie options                             |
345// +-----------------------------------------------------------------------+
346
347$template->assign('IN_CADDIE', 'caddie' == $_GET['cat'] ? true : false );
348
349// +-----------------------------------------------------------------------+
350// |                            deletion form                              |
351// +-----------------------------------------------------------------------+
352
353// we can only remove photos that have no storage_category_id, in other
354// word, it currently (Butterfly) means that the photo was added with
355// pLoader
356if (count($page['cat_elements_id']) > 0)
357{
358  $query = '
359SELECT
360    COUNT(*)
361  FROM '.IMAGES_TABLE.'
362  WHERE id IN ('.implode(',', $page['cat_elements_id']).')
363    AND storage_category_id IS NULL
364;';
365  list($counter) = pwg_db_fetch_row(pwg_query($query));
366
367  if ($counter > 0)
368  {
369    $template->assign('show_delete_form', true);
370  }
371}
372
373// +-----------------------------------------------------------------------+
374// |                           global mode form                            |
375// +-----------------------------------------------------------------------+
376
377// Virtualy associate a picture to a category
378$query = '
379SELECT id,name,uppercats,global_rank
380  FROM '.CATEGORIES_TABLE.'
381;';
382display_select_cat_wrapper($query, array(), 'associate_options', true);
383
384// Dissociate from a category : categories listed for dissociation can
385// only represent virtual links. Links to physical categories can't be
386// broken
387if (count($page['cat_elements_id']) > 0)
388{
389  $query = '
390SELECT
391    DISTINCT(category_id) AS id,
392    c.name,
393    c.uppercats,
394    c.global_rank
395  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
396    JOIN '.CATEGORIES_TABLE.' AS c ON c.id = ic.category_id
397    JOIN '.IMAGES_TABLE.' AS i ON i.id = ic.image_id
398  WHERE ic.image_id IN ('.implode(',', $page['cat_elements_id']).')
399    AND (
400      ic.category_id != i.storage_category_id
401      OR i.storage_category_id IS NULL
402    )
403;';
404  display_select_cat_wrapper($query, array(), 'dissociate_options', true);
405}
406
407if (count($page['cat_elements_id']) > 0)
408{
409  // remove tags
410  $tags = get_common_tags($page['cat_elements_id'], -1);
411
412  $template->assign(
413    array(
414      'DEL_TAG_SELECTION' => get_html_tag_selection($tags, 'del_tags'),
415      )
416    );
417}
418
419// creation date
420$day =
421empty($_POST['date_creation_day']) ? date('j') : $_POST['date_creation_day'];
422
423$month =
424empty($_POST['date_creation_month']) ? date('n') : $_POST['date_creation_month'];
425
426$year =
427empty($_POST['date_creation_year']) ? date('Y') : $_POST['date_creation_year'];
428
429$month_list = $lang['month'];
430$month_list[0]='------------';
431ksort($month_list);
432$template->assign( array(
433      'month_list'         => $month_list,
434      'DATE_CREATION_DAY'  => (int)$day,
435      'DATE_CREATION_MONTH'=> (int)$month,
436      'DATE_CREATION_YEAR' => (int)$year,
437    )
438  );
439
440// image level options
441$template->assign(
442    array(
443      'level_options'=> get_privacy_level_options(),
444      'level_options_selected' => 0,
445    )
446  );
447
448// +-----------------------------------------------------------------------+
449// |                        global mode thumbnails                         |
450// +-----------------------------------------------------------------------+
451
452// how many items to display on this page
453if (!empty($_GET['display']))
454{
455  if ('all' == $_GET['display'])
456  {
457    $page['nb_images'] = count($page['cat_elements_id']);
458  }
459  else
460  {
461    $page['nb_images'] = intval($_GET['display']);
462  }
463}
464else
465{
466  $page['nb_images'] = 20;
467}
468
469$nb_thumbs_page = 0;
470
471if (count($page['cat_elements_id']) > 0)
472{
473  $nav_bar = create_navigation_bar(
474    $base_url.get_query_string_diff(array('start')),
475    count($page['cat_elements_id']),
476    $page['start'],
477    $page['nb_images']
478    );
479  $template->assign('navbar', $nav_bar);
480
481  $query = '
482SELECT id,path,tn_ext,file,filesize,level,name
483  FROM '.IMAGES_TABLE.'
484  WHERE id IN ('.implode(',', $page['cat_elements_id']).')
485  '.$conf['order_by'].'
486  LIMIT '.$page['nb_images'].' OFFSET '.$page['start'].'
487;';
488  $result = pwg_query($query);
489
490  // template thumbnail initialization
491  while ($row = pwg_db_fetch_assoc($result))
492  {
493    $nb_thumbs_page++;
494    $src = get_thumbnail_url($row);
495
496    $title = $row['name'];
497    if (empty($title))
498    {     
499      $title = get_name_from_file($row['file']);
500    }
501
502    $template->append(
503      'thumbnails',
504      array(
505        'ID' => $row['id'],
506        'TN_SRC' => $src,
507        'FILE' => $row['file'],
508        'TITLE' => $title,
509        'LEVEL' => $row['level']
510        )
511      );
512  }
513}
514
515$template->assign(
516  array(
517    'nb_thumbs_page' => $nb_thumbs_page,
518    'nb_thumbs_set' => count($page['cat_elements_id']),
519    )
520  );
521
522trigger_action('loc_end_element_set_global');
523
524//----------------------------------------------------------- sending html code
525$template->assign_var_from_handle('ADMIN_CONTENT', 'element_set_global');
526?>
Note: See TracBrowser for help on using the repository browser.