source: trunk/admin/element_set_global.php @ 2704

Last change on this file since 2704 was 2678, checked in by plg, 16 years ago

feature 884 added: ability to delete photos uploaded via web API method
pwg.images.add, ie without storage_category_id. pLoader uses this method and
photos cannot be removed in any other way.

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