source: branches/gettext/admin/element_set_global.php @ 5283

Last change on this file since 5283 was 4731, checked in by plg, 15 years ago

merge r4730 from branch 2.0 to trunk

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