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

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

improvement: avoid the use of @ instead of a real test

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 13.7 KB
RevLine 
[755]1<?php
2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[3046]5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
[2297]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
[4495]46// the $_POST['selection'] was already checked in element_set.php
[5003]47check_input_parameter('add_tags', $_POST, true, PATTERN_ID);
48check_input_parameter('del_tags', $_POST, true, PATTERN_ID);
49check_input_parameter('associate', $_POST, false, PATTERN_ID);
50check_input_parameter('dissociate', $_POST, false, PATTERN_ID);
[4495]51
[2678]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// +-----------------------------------------------------------------------+
[755]122// |                       global mode form submission                     |
123// +-----------------------------------------------------------------------+
124
125if (isset($_POST['submit']))
126{
127  $collection = array();
[1092]128
[755]129//   echo '<pre>';
[762]130//   print_r($_POST);
[755]131//   echo '</pre>';
132//   exit();
133
134  switch ($_POST['target'])
135  {
136    case 'all' :
137    {
[764]138      $collection = $page['cat_elements_id'];
[755]139      break;
140    }
141    case 'selection' :
142    {
[875]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      }
[755]151      break;
152    }
153  }
154
[1119]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  }
[1120]170
[875]171  if ($_POST['associate'] != 0 and count($collection) > 0)
[755]172  {
[1121]173    associate_images_to_categories(
174      $collection,
175      array($_POST['associate'])
176      );
[755]177  }
178
[875]179  if ($_POST['dissociate'] != 0 and count($collection) > 0)
[755]180  {
[1121]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".
[755]183    $query = '
[1121]184SELECT id
[1065]185  FROM '.IMAGE_CATEGORY_TABLE.'
[1121]186    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
[755]187  WHERE category_id = '.$_POST['dissociate'].'
[1121]188    AND id IN ('.implode(',', $collection).')
[2575]189    AND (
190      category_id != storage_category_id
191      OR storage_category_id IS NULL
192    )
[755]193;';
[1121]194    $dissociables = array_from_query($query, 'id');
[755]195
[2003]196    if (!empty($dissociables))
197    {
198      $query = '
[1065]199DELETE
200  FROM '.IMAGE_CATEGORY_TABLE.'
[1121]201  WHERE category_id = '.$_POST['dissociate'].'
202    AND image_id IN ('.implode(',', $dissociables).')
[755]203';
[2003]204      pwg_query($query);
[1609]205
[2576]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      }
[2003]215    }
216
[1121]217    update_category($_POST['dissociate']);
[755]218  }
[762]219
220  $datas = array();
221  $dbfields = array('primary' => array('id'), 'update' => array());
222
[2084]223  $formfields = array('author', 'name', 'date_creation', 'level');
[762]224  foreach ($formfields as $formfield)
225  {
226    if ($_POST[$formfield.'_action'] != 'leave')
227    {
228      array_push($dbfields['update'], $formfield);
229    }
230  }
[1092]231
[762]232  // updating elements is useful only if needed...
[875]233  if (count($dbfields['update']) > 0 and count($collection) > 0)
[762]234  {
235    $query = '
[1119]236SELECT id
[762]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'];
[1092]246
[762]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      }
[1092]273
[2084]274      if ('set' == $_POST['level_action'])
275      {
276        $data['level'] = $_POST['level'];
277      }
278
[762]279      array_push($datas, $data);
280    }
[763]281    // echo '<pre>'; print_r($datas); echo '</pre>';
[762]282    mass_updates(IMAGES_TABLE, $dbfields, $datas);
283  }
[755]284}
285
286// +-----------------------------------------------------------------------+
287// |                             template init                             |
288// +-----------------------------------------------------------------------+
289$template->set_filenames(
[2530]290  array('element_set_global' => 'element_set_global.tpl'));
[755]291
[2249]292$base_url = get_root_url().'admin.php';
[755]293
[762]294// $form_action = $base_url.'?page=element_set_global';
295
[2249]296$template->assign(
[755]297  array(
[834]298    'CATEGORIES_NAV'=>$page['title'],
[1092]299
[764]300    'U_DISPLAY'=>$base_url.get_query_string_diff(array('display')),
[1092]301
[764]302    'U_UNIT_MODE'
303    =>
304    $base_url
305    .get_query_string_diff(array('mode','display'))
306    .'&amp;mode=unit',
[1092]307
[762]308    'F_ACTION'=>$base_url.get_query_string_diff(array()),
[755]309   )
310 );
[764]311
[755]312// +-----------------------------------------------------------------------+
[764]313// |                            caddie options                             |
314// +-----------------------------------------------------------------------+
315
[2249]316$template->assign('IN_CADDIE', 'caddie' == $_GET['cat'] ? true : false );
[764]317
318// +-----------------------------------------------------------------------+
[2678]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// +-----------------------------------------------------------------------+
[755]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;';
[2249]351display_select_cat_wrapper($query, array(), 'associate_options', true);
[755]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
[764]356if (count($page['cat_elements_id']) > 0)
357{
358  $query = '
[2575]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
[764]367  WHERE ic.image_id IN ('.implode(',', $page['cat_elements_id']).')
[2575]368    AND (
369      ic.category_id != i.storage_category_id
370      OR i.storage_category_id IS NULL
371    )
[755]372;';
[2249]373  display_select_cat_wrapper($query, array(), 'dissociate_options', true);
[764]374}
[755]375
[1314]376$all_tags = get_all_tags();
377
[2249]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      )
[1314]387    );
388}
389
[1120]390if (count($page['cat_elements_id']) > 0)
[762]391{
[1120]392  // remove tags
[1815]393  $tags = get_common_tags($page['cat_elements_id'], -1);
[1310]394
[2249]395  $template->assign(
[1120]396    array(
397      'DEL_TAG_SELECTION' => get_html_tag_selection($tags, 'del_tags'),
398      )
399    );
400}
[2249]401
[762]402// creation date
403$day =
404empty($_POST['date_creation_day']) ? date('j') : $_POST['date_creation_day'];
405
[2249]406$month =
407empty($_POST['date_creation_month']) ? date('n') : $_POST['date_creation_month'];
[762]408
[2249]409$year =
410empty($_POST['date_creation_year']) ? date('Y') : $_POST['date_creation_year'];
[762]411
[2249]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
[2084]423// image level options
[2249]424$tpl_options = array();
[2084]425foreach ($conf['available_permission_levels'] as $level)
426{
[2249]427  $tpl_options[$level] = l10n( sprintf('Level %d', $level) );
428}
429$template->assign(
[2084]430    array(
[2249]431      'level_options'=> $tpl_options,
432    )
433  );
[2084]434
[755]435// +-----------------------------------------------------------------------+
436// |                        global mode thumbnails                         |
437// +-----------------------------------------------------------------------+
438
[875]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
[764]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'],
[1084]462    $page['nb_images']
463    );
[2249]464  $template->assign('NAV_BAR', $nav_bar);
[764]465
466  $query = '
[2084]467SELECT id,path,tn_ext,file,filesize,level
[764]468  FROM '.IMAGES_TABLE.'
469  WHERE id IN ('.implode(',', $page['cat_elements_id']).')
[755]470  '.$conf['order_by'].'
[764]471  LIMIT '.$page['start'].', '.$page['nb_images'].'
[755]472;';
[764]473  //echo '<pre>'.$query.'</pre>';
474  $result = pwg_query($query);
[755]475
[764]476  // template thumbnail initialization
[755]477
[1609]478  while ($row = mysql_fetch_assoc($result))
[755]479  {
[1609]480    $src = get_thumbnail_url($row);
[1092]481
[2249]482    $template->append(
483      'thumbnails',
[764]484      array(
485        'ID' => $row['id'],
[2249]486        'TN_SRC' => $src,
487        'FILE' => $row['file'],
488        'TITLE' => get_thumbnail_title($row),
489        'LEVEL' => $row['level']
[764]490        )
491      );
[755]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.