source: branches/2.0/admin/element_set_global.php @ 4730

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

bug 1396: when a photo was deleted, the code to avoid orphans as category
representative was 1) wrong (because a photo doesn't have to belong to a
category to represent it) 2) at the wrong place.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 13.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 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// +-----------------------------------------------------------------------+
40check_status(ACCESS_ADMINISTRATOR);
41
42// +-----------------------------------------------------------------------+
43// |                         deletion form submission                      |
44// +-----------------------------------------------------------------------+
45
46// the $_POST['selection'] was already checked in element_set.php
47check_input_parameter('add_tags', @$_POST['add_tags'], true, PATTERN_ID);
48check_input_parameter('del_tags', @$_POST['del_tags'], true, PATTERN_ID);
49check_input_parameter('associate', @$_POST['associate'], false, PATTERN_ID);
50check_input_parameter('dissociate', @$_POST['dissociate'], false, PATTERN_ID);
51
52if (isset($_POST['delete']))
53{
54  if (isset($_POST['confirm_deletion']) and 1 == $_POST['confirm_deletion'])
55  {
56    $collection = array();
57
58    switch ($_POST['target_deletion'])
59    {
60      case 'all' :
61      {
62        $collection = $page['cat_elements_id'];
63        break;
64      }
65      case 'selection' :
66      {
67        if (!isset($_POST['selection']) or count($_POST['selection']) == 0)
68        {
69          array_push($page['errors'], l10n('Select at least one picture'));
70        }
71        else
72        {
73          $collection = $_POST['selection'];
74        }
75        break;
76      }
77    }
78
79    // filter selection on photos that have no storage_category_id (ie that
80    // were added via pLoader)
81    if (count($collection) > 0)
82    {
83      $query = '
84SELECT
85    id
86  FROM '.IMAGES_TABLE.'
87  WHERE id IN ('.implode(',', $collection).')
88    AND storage_category_id IS NULL
89;';
90      $deletables = array_from_query($query, 'id');
91
92      if (count($deletables) > 0)
93      {
94        $physical_deletion = true;
95        delete_elements($deletables, $physical_deletion);
96
97        array_push(
98          $page['infos'],
99          sprintf(
100            l10n_dec(
101              '%d photo was deleted',
102              '%d photos were deleted',
103              count($deletables)
104              ),
105            count($deletables)
106            )
107          );
108      }
109      else
110      {
111        array_push($page['errors'], l10n('No photo can be deleted'));
112      }
113    }
114  }
115  else
116  {
117    array_push($page['errors'], l10n('You need to confirm deletion'));
118  }
119}
120
121// +-----------------------------------------------------------------------+
122// |                       global mode form submission                     |
123// +-----------------------------------------------------------------------+
124
125if (isset($_POST['submit']))
126{
127  $collection = array();
128
129//   echo '<pre>';
130//   print_r($_POST);
131//   echo '</pre>';
132//   exit();
133
134  switch ($_POST['target'])
135  {
136    case 'all' :
137    {
138      $collection = $page['cat_elements_id'];
139      break;
140    }
141    case 'selection' :
142    {
143      if (!isset($_POST['selection']) or count($_POST['selection']) == 0)
144      {
145        array_push($page['errors'], l10n('Select at least one picture'));
146      }
147      else
148      {
149        $collection = $_POST['selection'];
150      }
151      break;
152    }
153  }
154
155  if (isset($_POST['add_tags']) and count($collection) > 0)
156  {
157    add_tags($_POST['add_tags'], $collection);
158  }
159
160  if (isset($_POST['del_tags']) and count($collection) > 0)
161  {
162    $query = '
163DELETE
164  FROM '.IMAGE_TAG_TABLE.'
165  WHERE image_id IN ('.implode(',', $collection).')
166    AND tag_id IN ('.implode(',', $_POST['del_tags']).')
167;';
168    pwg_query($query);
169  }
170
171  if ($_POST['associate'] != 0 and count($collection) > 0)
172  {
173    associate_images_to_categories(
174      $collection,
175      array($_POST['associate'])
176      );
177  }
178
179  if ($_POST['dissociate'] != 0 and count($collection) > 0)
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      // we remove the dissociated images if we are currently displaying the
207      // category to dissociate from.
208      if (is_numeric($_GET['cat']) and $_POST['dissociate'] == $_GET['cat'])
209      {
210        $page['cat_elements_id'] = array_diff(
211          $page['cat_elements_id'],
212          $dissociables
213          );
214      }
215    }
216
217    update_category($_POST['dissociate']);
218  }
219
220  $datas = array();
221  $dbfields = array('primary' => array('id'), 'update' => array());
222
223  $formfields = array('author', 'name', 'date_creation', 'level');
224  foreach ($formfields as $formfield)
225  {
226    if ($_POST[$formfield.'_action'] != 'leave')
227    {
228      array_push($dbfields['update'], $formfield);
229    }
230  }
231
232  // updating elements is useful only if needed...
233  if (count($dbfields['update']) > 0 and count($collection) > 0)
234  {
235    $query = '
236SELECT id
237  FROM '.IMAGES_TABLE.'
238  WHERE id IN ('.implode(',', $collection).')
239;';
240    $result = pwg_query($query);
241
242    while ($row = mysql_fetch_array($result))
243    {
244      $data = array();
245      $data['id'] = $row['id'];
246
247      if ('set' == $_POST['author_action'])
248      {
249        $data['author'] = $_POST['author'];
250        if ('' == $data['author'])
251        {
252          unset($data['author']);
253        }
254      }
255
256      if ('set' == $_POST['name_action'])
257      {
258        $data['name'] = $_POST['name'];
259        if ('' == $data['name'])
260        {
261          unset($data['name']);
262        }
263      }
264
265      if ('set' == $_POST['date_creation_action'])
266      {
267        $data['date_creation'] =
268          $_POST['date_creation_year']
269          .'-'.$_POST['date_creation_month']
270          .'-'.$_POST['date_creation_day']
271          ;
272      }
273
274      if ('set' == $_POST['level_action'])
275      {
276        $data['level'] = $_POST['level'];
277      }
278
279      array_push($datas, $data);
280    }
281    // echo '<pre>'; print_r($datas); echo '</pre>';
282    mass_updates(IMAGES_TABLE, $dbfields, $datas);
283  }
284}
285
286// +-----------------------------------------------------------------------+
287// |                             template init                             |
288// +-----------------------------------------------------------------------+
289$template->set_filenames(
290  array('element_set_global' => 'element_set_global.tpl'));
291
292$base_url = get_root_url().'admin.php';
293
294// $form_action = $base_url.'?page=element_set_global';
295
296$template->assign(
297  array(
298    'CATEGORIES_NAV'=>$page['title'],
299
300    'U_DISPLAY'=>$base_url.get_query_string_diff(array('display')),
301
302    'U_UNIT_MODE'
303    =>
304    $base_url
305    .get_query_string_diff(array('mode','display'))
306    .'&amp;mode=unit',
307
308    'F_ACTION'=>$base_url.get_query_string_diff(array()),
309   )
310 );
311
312// +-----------------------------------------------------------------------+
313// |                            caddie options                             |
314// +-----------------------------------------------------------------------+
315
316$template->assign('IN_CADDIE', 'caddie' == $_GET['cat'] ? true : false );
317
318// +-----------------------------------------------------------------------+
319// |                            deletion form                              |
320// +-----------------------------------------------------------------------+
321
322// we can only remove photos that have no storage_category_id, in other
323// word, it currently (Butterfly) means that the photo was added with
324// pLoader
325if (count($page['cat_elements_id']) > 0)
326{
327  $query = '
328SELECT
329    COUNT(*)
330  FROM '.IMAGES_TABLE.'
331  WHERE id IN ('.implode(',', $page['cat_elements_id']).')
332    AND storage_category_id IS NULL
333;';
334  list($counter) = mysql_fetch_row(pwg_query($query));
335
336  if ($counter > 0)
337  {
338    $template->assign('show_delete_form', true);
339  }
340}
341
342// +-----------------------------------------------------------------------+
343// |                           global mode form                            |
344// +-----------------------------------------------------------------------+
345
346// Virtualy associate a picture to a category
347$query = '
348SELECT id,name,uppercats,global_rank
349  FROM '.CATEGORIES_TABLE.'
350;';
351display_select_cat_wrapper($query, array(), 'associate_options', true);
352
353// Dissociate from a category : categories listed for dissociation can
354// only represent virtual links. Links to physical categories can't be
355// broken
356if (count($page['cat_elements_id']) > 0)
357{
358  $query = '
359SELECT
360    DISTINCT(category_id) AS id,
361    c.name,
362    c.uppercats,
363    c.global_rank
364  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
365    JOIN '.CATEGORIES_TABLE.' AS c ON c.id = ic.category_id
366    JOIN '.IMAGES_TABLE.' AS i ON i.id = ic.image_id
367  WHERE ic.image_id IN ('.implode(',', $page['cat_elements_id']).')
368    AND (
369      ic.category_id != i.storage_category_id
370      OR i.storage_category_id IS NULL
371    )
372;';
373  display_select_cat_wrapper($query, array(), 'dissociate_options', true);
374}
375
376$all_tags = get_all_tags();
377
378if (count($all_tags) > 0)
379{// add tags
380  $template->assign(
381    array(
382      'ADD_TAG_SELECTION' => get_html_tag_selection(
383                              $all_tags,
384                              'add_tags'
385                              ),
386      )
387    );
388}
389
390if (count($page['cat_elements_id']) > 0)
391{
392  // remove tags
393  $tags = get_common_tags($page['cat_elements_id'], -1);
394
395  $template->assign(
396    array(
397      'DEL_TAG_SELECTION' => get_html_tag_selection($tags, 'del_tags'),
398      )
399    );
400}
401
402// creation date
403$day =
404empty($_POST['date_creation_day']) ? date('j') : $_POST['date_creation_day'];
405
406$month =
407empty($_POST['date_creation_month']) ? date('n') : $_POST['date_creation_month'];
408
409$year =
410empty($_POST['date_creation_year']) ? date('Y') : $_POST['date_creation_year'];
411
412$month_list = $lang['month'];
413$month_list[0]='------------';
414ksort($month_list);
415$template->assign( array(
416      'month_list'         => $month_list,
417      'DATE_CREATION_DAY'  => (int)$day,
418      'DATE_CREATION_MONTH'=> (int)$month,
419      'DATE_CREATION_YEAR' => (int)$year,
420    )
421  );
422
423// image level options
424$tpl_options = array();
425foreach ($conf['available_permission_levels'] as $level)
426{
427  $tpl_options[$level] = l10n( sprintf('Level %d', $level) );
428}
429$template->assign(
430    array(
431      'level_options'=> $tpl_options,
432    )
433  );
434
435// +-----------------------------------------------------------------------+
436// |                        global mode thumbnails                         |
437// +-----------------------------------------------------------------------+
438
439// how many items to display on this page
440if (!empty($_GET['display']))
441{
442  if ('all' == $_GET['display'])
443  {
444    $page['nb_images'] = count($page['cat_elements_id']);
445  }
446  else
447  {
448    $page['nb_images'] = intval($_GET['display']);
449  }
450}
451else
452{
453  $page['nb_images'] = 20;
454}
455
456if (count($page['cat_elements_id']) > 0)
457{
458  $nav_bar = create_navigation_bar(
459    $base_url.get_query_string_diff(array('start')),
460    count($page['cat_elements_id']),
461    $page['start'],
462    $page['nb_images']
463    );
464  $template->assign('NAV_BAR', $nav_bar);
465
466  $query = '
467SELECT id,path,tn_ext,file,filesize,level
468  FROM '.IMAGES_TABLE.'
469  WHERE id IN ('.implode(',', $page['cat_elements_id']).')
470  '.$conf['order_by'].'
471  LIMIT '.$page['start'].', '.$page['nb_images'].'
472;';
473  //echo '<pre>'.$query.'</pre>';
474  $result = pwg_query($query);
475
476  // template thumbnail initialization
477
478  while ($row = mysql_fetch_assoc($result))
479  {
480    $src = get_thumbnail_url($row);
481
482    $template->append(
483      'thumbnails',
484      array(
485        'ID' => $row['id'],
486        'TN_SRC' => $src,
487        'FILE' => $row['file'],
488        'TITLE' => get_thumbnail_title($row),
489        'LEVEL' => $row['level']
490        )
491      );
492  }
493}
494
495//----------------------------------------------------------- sending html code
496$template->assign_var_from_handle('ADMIN_CONTENT', 'element_set_global');
497?>
Note: See TracBrowser for help on using the repository browser.