source: trunk/admin/element_set_global.php @ 3049

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

Administration: happy new year 2009, all PHP headers updated.

  • 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
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// +-----------------------------------------------------------------------+
128// |                       global mode form submission                     |
129// +-----------------------------------------------------------------------+
130
131if (isset($_POST['submit']))
132{
133  $collection = array();
134
135//   echo '<pre>';
136//   print_r($_POST);
137//   echo '</pre>';
138//   exit();
139
140  switch ($_POST['target'])
141  {
142    case 'all' :
143    {
144      $collection = $page['cat_elements_id'];
145      break;
146    }
147    case 'selection' :
148    {
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      }
157      break;
158    }
159  }
160
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  }
176
177  if ($_POST['associate'] != 0 and count($collection) > 0)
178  {
179    associate_images_to_categories(
180      $collection,
181      array($_POST['associate'])
182      );
183  }
184
185  if ($_POST['dissociate'] != 0 and count($collection) > 0)
186  {
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".
189    $query = '
190SELECT id
191  FROM '.IMAGE_CATEGORY_TABLE.'
192    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
193  WHERE category_id = '.$_POST['dissociate'].'
194    AND id IN ('.implode(',', $collection).')
195    AND (
196      category_id != storage_category_id
197      OR storage_category_id IS NULL
198    )
199;';
200    $dissociables = array_from_query($query, 'id');
201
202    if (!empty($dissociables))
203    {
204      $query = '
205DELETE
206  FROM '.IMAGE_CATEGORY_TABLE.'
207  WHERE category_id = '.$_POST['dissociate'].'
208    AND image_id IN ('.implode(',', $dissociables).')
209';
210      pwg_query($query);
211
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      }
221    }
222
223    update_category($_POST['dissociate']);
224  }
225
226  $datas = array();
227  $dbfields = array('primary' => array('id'), 'update' => array());
228
229  $formfields = array('author', 'name', 'date_creation', 'level');
230  foreach ($formfields as $formfield)
231  {
232    if ($_POST[$formfield.'_action'] != 'leave')
233    {
234      array_push($dbfields['update'], $formfield);
235    }
236  }
237
238  // updating elements is useful only if needed...
239  if (count($dbfields['update']) > 0 and count($collection) > 0)
240  {
241    $query = '
242SELECT id
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'];
252
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      }
279
280      if ('set' == $_POST['level_action'])
281      {
282        $data['level'] = $_POST['level'];
283      }
284
285      array_push($datas, $data);
286    }
287    // echo '<pre>'; print_r($datas); echo '</pre>';
288    mass_updates(IMAGES_TABLE, $dbfields, $datas);
289  }
290}
291
292// +-----------------------------------------------------------------------+
293// |                             template init                             |
294// +-----------------------------------------------------------------------+
295$template->set_filenames(
296  array('element_set_global' => 'element_set_global.tpl'));
297
298$base_url = get_root_url().'admin.php';
299
300// $form_action = $base_url.'?page=element_set_global';
301
302$template->assign(
303  array(
304    'CATEGORIES_NAV'=>$page['title'],
305
306    'U_DISPLAY'=>$base_url.get_query_string_diff(array('display')),
307
308    'U_UNIT_MODE'
309    =>
310    $base_url
311    .get_query_string_diff(array('mode','display'))
312    .'&amp;mode=unit',
313
314    'F_ACTION'=>$base_url.get_query_string_diff(array()),
315   )
316 );
317
318// +-----------------------------------------------------------------------+
319// |                            caddie options                             |
320// +-----------------------------------------------------------------------+
321
322$template->assign('IN_CADDIE', 'caddie' == $_GET['cat'] ? true : false );
323
324// +-----------------------------------------------------------------------+
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// +-----------------------------------------------------------------------+
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;';
357display_select_cat_wrapper($query, array(), 'associate_options', true);
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
362if (count($page['cat_elements_id']) > 0)
363{
364  $query = '
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
373  WHERE ic.image_id IN ('.implode(',', $page['cat_elements_id']).')
374    AND (
375      ic.category_id != i.storage_category_id
376      OR i.storage_category_id IS NULL
377    )
378;';
379  display_select_cat_wrapper($query, array(), 'dissociate_options', true);
380}
381
382$all_tags = get_all_tags();
383
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      )
393    );
394}
395
396if (count($page['cat_elements_id']) > 0)
397{
398  // remove tags
399  $tags = get_common_tags($page['cat_elements_id'], -1);
400
401  $template->assign(
402    array(
403      'DEL_TAG_SELECTION' => get_html_tag_selection($tags, 'del_tags'),
404      )
405    );
406}
407
408// creation date
409$day =
410empty($_POST['date_creation_day']) ? date('j') : $_POST['date_creation_day'];
411
412$month =
413empty($_POST['date_creation_month']) ? date('n') : $_POST['date_creation_month'];
414
415$year =
416empty($_POST['date_creation_year']) ? date('Y') : $_POST['date_creation_year'];
417
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
429// image level options
430$tpl_options = array();
431foreach ($conf['available_permission_levels'] as $level)
432{
433  $tpl_options[$level] = l10n( sprintf('Level %d', $level) );
434}
435$template->assign(
436    array(
437      'level_options'=> $tpl_options,
438    )
439  );
440
441// +-----------------------------------------------------------------------+
442// |                        global mode thumbnails                         |
443// +-----------------------------------------------------------------------+
444
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
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'],
468    $page['nb_images']
469    );
470  $template->assign('NAV_BAR', $nav_bar);
471
472  $query = '
473SELECT id,path,tn_ext,file,filesize,level
474  FROM '.IMAGES_TABLE.'
475  WHERE id IN ('.implode(',', $page['cat_elements_id']).')
476  '.$conf['order_by'].'
477  LIMIT '.$page['start'].', '.$page['nb_images'].'
478;';
479  //echo '<pre>'.$query.'</pre>';
480  $result = pwg_query($query);
481
482  // template thumbnail initialization
483
484  while ($row = mysql_fetch_assoc($result))
485  {
486    $src = get_thumbnail_url($row);
487
488    $template->append(
489      'thumbnails',
490      array(
491        'ID' => $row['id'],
492        'TN_SRC' => $src,
493        'FILE' => $row['file'],
494        'TITLE' => get_thumbnail_title($row),
495        'LEVEL' => $row['level']
496        )
497      );
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.