source: extensions/Icy_Picture_Modify/icy_picture_modify.php @ 11611

Last change on this file since 11611 was 11610, checked in by icy, 13 years ago

Merge branch 'master' into svn

File size: 14.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 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
24if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
25if (!defined('ICY_PICTURE_MODIFY_PATH')) die('Hacking attempt!');
26
27include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
28include_once(ICY_PICTURE_MODIFY_PATH.'include/functions_icy_picture_modify.inc.php');
29
30global $template, $conf, $user, $page, $lang, $cache;
31
32// redirect users to the index page if 'image_id' isn't provided
33if (!isset($_GET['image_id']))
34{
35  if (isset($_GET['cat_id']))
36  {
37    redirect_http(get_root_url().'?/category/'.$_GET['cat_id']);
38  }
39  else
40  {
41    redirect_http(make_index_url());
42  }
43}
44
45check_input_parameter('cat_id', $_GET, false, PATTERN_ID);
46check_input_parameter('image_id', $_GET, false, PATTERN_ID);
47
48// make sure the image is editable by current user
49if (!icy_check_image_owner($_GET['image_id'], $user['id']))
50{
51  $url = make_picture_url(
52      array(
53        'image_id' => $_GET['image_id'],
54        'cat_id' => $_GET['cat_id'],
55      )
56    );
57  redirect_http($url);
58}
59
60// <admin.php>
61$page['errors'] = array();
62$page['infos']  = array();
63$page['warnings']  = array();
64
65if (isset($_SESSION['page_infos']))
66{
67  $page['infos'] = array_merge($page['infos'], $_SESSION['page_infos']);
68  unset($_SESSION['page_infos']);
69}
70// </admin.php>
71
72// +-----------------------------------------------------------------------+
73// |                             delete photo                              |
74// +-----------------------------------------------------------------------+
75
76if (isset($_GET['delete']))
77{
78  check_pwg_token();
79
80  delete_elements(array($_GET['image_id']), true);
81
82  // where to redirect the user now?
83  //
84  // 1. if a category is available in the URL, use it
85  // 2. else use the first reachable linked category
86  // 3. redirect to gallery root
87
88  if (isset($_GET['cat_id']) and !empty($_GET['cat_id']))
89  {
90    redirect(
91      make_index_url(
92        array(
93          'category' => get_cat_info($_GET['cat_id'])
94          )
95        )
96      );
97  }
98
99  $query = '
100SELECT category_id
101  FROM '.IMAGE_CATEGORY_TABLE.'
102  WHERE image_id = '.$_GET['image_id'].'
103;';
104
105  $authorizeds = array_diff(
106    array_from_query($query, 'category_id'),
107    explode(',', calculate_permissions($user['id'], $user['status']))
108    );
109
110  foreach ($authorizeds as $category_id)
111  {
112    redirect(
113      make_index_url(
114        array(
115          'category' => get_cat_info($category_id)
116          )
117        )
118      );
119  }
120
121  redirect(make_index_url());
122}
123
124// +-----------------------------------------------------------------------+
125// |                          synchronize metadata                         |
126// +-----------------------------------------------------------------------+
127
128if (isset($_GET['sync_metadata']))
129{
130  $query = '
131SELECT path
132  FROM '.IMAGES_TABLE.'
133  WHERE id = '.$_GET['image_id'].'
134;';
135  list($path) = pwg_db_fetch_row(pwg_query($query));
136  update_metadata(array($_GET['image_id'] => $path));
137
138  array_push($page['infos'], l10n('Metadata synchronized from file'));
139}
140
141//--------------------------------------------------------- update informations
142
143// first, we verify whether there is a mistake on the given creation date
144if (isset($_POST['date_creation_action'])
145    and 'set' == $_POST['date_creation_action'])
146{
147  if (!is_numeric($_POST['date_creation_year'])
148    or !checkdate(
149          $_POST['date_creation_month'],
150          $_POST['date_creation_day'],
151          $_POST['date_creation_year'])
152    )
153  {
154    array_push($page['errors'], l10n('wrong date'));
155  }
156}
157
158if (isset($_POST['submit']) and count($page['errors']) == 0)
159{
160  $data = array();
161  $data{'id'} = $_GET['image_id'];
162  $data{'name'} = $_POST['name'];
163  $data{'author'} = $_POST['author'];
164  $data['level'] = $_POST['level'];
165
166  if ($conf['allow_html_descriptions'])
167  {
168    $data{'comment'} = @$_POST['description'];
169  }
170  else
171  {
172    $data{'comment'} = strip_tags(@$_POST['description']);
173  }
174
175  if (isset($_POST['date_creation_action']))
176  {
177    if ('set' == $_POST['date_creation_action'])
178    {
179      $data{'date_creation'} = $_POST['date_creation_year']
180                                 .'-'.$_POST['date_creation_month']
181                                 .'-'.$_POST['date_creation_day'];
182    }
183    else if ('unset' == $_POST['date_creation_action'])
184    {
185      $data{'date_creation'} = '';
186    }
187  }
188
189  mass_updates(
190    IMAGES_TABLE,
191    array(
192      'primary' => array('id'),
193      'update' => array_diff(array_keys($data), array('id'))
194      ),
195    array($data)
196    );
197
198  // time to deal with tags
199  $tag_ids = array();
200  if (!empty($_POST['tags']))
201  {
202    $tag_ids = get_tag_ids($_POST['tags']);
203  }
204  set_tags($tag_ids, $_GET['image_id']);
205
206  array_push($page['infos'], l10n('Photo informations updated'));
207}
208// associate the element to other categories than its storage category
209if (isset($_POST['associate'])
210    and isset($_POST['cat_dissociated'])
211    and count($_POST['cat_dissociated']) > 0
212  )
213{
214  associate_images_to_categories(
215    array($_GET['image_id']),
216    $_POST['cat_dissociated']
217    );
218}
219// dissociate the element from categories (but not from its storage category)
220if (isset($_POST['dissociate'])
221    and isset($_POST['cat_associated'])
222    and count($_POST['cat_associated']) > 0
223  )
224{
225  $query = '
226DELETE FROM '.IMAGE_CATEGORY_TABLE.'
227  WHERE image_id = '.$_GET['image_id'].'
228    AND category_id IN ('.implode(',', $_POST['cat_associated']).')
229';
230  pwg_query($query);
231
232  update_category($_POST['cat_associated']);
233}
234// elect the element to represent the given categories
235if (isset($_POST['elect'])
236    and isset($_POST['cat_dismissed'])
237    and count($_POST['cat_dismissed']) > 0
238  )
239{
240  $datas = array();
241  foreach ($_POST['cat_dismissed'] as $category_id)
242  {
243    array_push($datas,
244               array('id' => $category_id,
245                     'representative_picture_id' => $_GET['image_id']));
246  }
247  $fields = array('primary' => array('id'),
248                  'update' => array('representative_picture_id'));
249  mass_updates(CATEGORIES_TABLE, $fields, $datas);
250}
251// dismiss the element as representant of the given categories
252if (isset($_POST['dismiss'])
253    and isset($_POST['cat_elected'])
254    and count($_POST['cat_elected']) > 0
255  )
256{
257  set_random_representant($_POST['cat_elected']);
258}
259
260// tags
261$query = '
262SELECT
263    tag_id,
264    name AS tag_name
265  FROM '.IMAGE_TAG_TABLE.' AS it
266    JOIN '.TAGS_TABLE.' AS t ON t.id = it.tag_id
267  WHERE image_id = '.$_GET['image_id'].'
268;';
269$tag_selection = get_taglist($query);
270
271$query = '
272SELECT
273    id AS tag_id,
274    name AS tag_name
275  FROM '.TAGS_TABLE.'
276;';
277$tags = get_taglist($query);
278
279// retrieving direct information about picture
280$query = '
281SELECT *
282  FROM '.IMAGES_TABLE.'
283  WHERE id = '.$_GET['image_id'].'
284;';
285$row = pwg_db_fetch_assoc(pwg_query($query));
286
287$storage_category_id = null;
288if (!empty($row['storage_category_id']))
289{
290  $storage_category_id = $row['storage_category_id'];
291}
292
293$image_file = $row['file'];
294
295// +-----------------------------------------------------------------------+
296// |                             template init                             |
297// +-----------------------------------------------------------------------+
298
299$template->set_template_dir(ICY_PICTURE_MODIFY_PATH.'template/');
300$template->set_filenames(array('icy_picture_modify' => 'icy_picture_modify.tpl'));
301
302$admin_url_start = get_root_url().'index.php?/icy_picture_modify';
303$admin_url_start.= '&amp;image_id='.$_GET['image_id'];
304$admin_url_start.= isset($_GET['cat_id']) ? '&amp;cat_id='.$_GET['cat_id'] : '';
305
306$template->assign(
307  array(
308    'ICY_PICTURE_MODIFY_PATH' => ICY_PICTURE_MODIFY_PATH,
309    'ICY_ROOT_PATH' => realpath(dirname(PHPWG_PLUGINS_PATH)),
310    'tag_selection' => $tag_selection,
311    'tags' => $tags,
312    'U_SYNC' => $admin_url_start.'&amp;sync_metadata=1',
313    'U_DELETE' => $admin_url_start.'&amp;delete=1&amp;pwg_token='.get_pwg_token(),
314
315    'PATH'=>$row['path'],
316
317    'TN_SRC' => get_thumbnail_url($row),
318
319    'NAME' =>
320      isset($_POST['name']) ?
321        stripslashes($_POST['name']) : @$row['name'],
322
323    'DIMENSIONS' => @$row['width'].' * '.@$row['height'],
324
325    'FILESIZE' => @$row['filesize'].' KB',
326
327    'REGISTRATION_DATE' => format_date($row['date_available']),
328
329    'AUTHOR' => htmlspecialchars(
330      isset($_POST['author'])
331        ? stripslashes($_POST['author'])
332        : @$row['author']
333      ),
334
335    'DESCRIPTION' =>
336      htmlspecialchars( isset($_POST['description']) ?
337        stripslashes($_POST['description']) : @$row['comment'] ),
338
339    'F_ACTION' =>
340        get_root_url() # .'index.php?/icy_picture_modify'
341        .get_query_string_diff(array('sync_metadata'))
342    )
343  );
344
345if ($row['has_high'] == 'true')
346{
347  $template->assign(
348    'HIGH_FILESIZE',
349    isset($row['high_filesize'])
350        ? $row['high_filesize'].' KB'
351        : l10n('unknown')
352    );
353}
354
355// image level options
356$selected_level = isset($_POST['level']) ? $_POST['level'] : $row['level'];
357$template->assign(
358    array(
359      'level_options'=> get_privacy_level_options(),
360      'level_options_selected' => array($selected_level)
361    )
362  );
363
364// creation date
365unset($day, $month, $year);
366
367if (isset($_POST['date_creation_action'])
368    and 'set' == $_POST['date_creation_action'])
369{
370  foreach (array('day', 'month', 'year') as $varname)
371  {
372    $$varname = $_POST['date_creation_'.$varname];
373  }
374}
375else if (isset($row['date_creation']) and !empty($row['date_creation']))
376{
377  list($year, $month, $day) = explode('-', $row['date_creation']);
378}
379else
380{
381  list($year, $month, $day) = array('', 0, 0);
382}
383
384
385$month_list = $lang['month'];
386$month_list[0]='------------';
387ksort($month_list);
388
389$template->assign(
390    array(
391      'DATE_CREATION_DAY_VALUE' => $day,
392      'DATE_CREATION_MONTH_VALUE' => $month,
393      'DATE_CREATION_YEAR_VALUE' => $year,
394      'month_list' => $month_list,
395      )
396    );
397
398$query = '
399SELECT category_id, uppercats
400  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
401    INNER JOIN '.CATEGORIES_TABLE.' AS c
402      ON c.id = ic.category_id
403  WHERE image_id = '.$_GET['image_id'].'
404;';
405$result = pwg_query($query);
406
407while ($row = pwg_db_fetch_assoc($result))
408{
409  $name =
410    get_cat_display_name_cache(
411      $row['uppercats'],
412      get_root_url().'index.php?/icy_picture_modify&amp;cat_id=',
413      false
414      );
415
416  if ($row['category_id'] == $storage_category_id)
417  {
418    $template->assign('STORAGE_CATEGORY', $name);
419  }
420  else
421  {
422    $template->append('related_categories', $name);
423  }
424}
425
426// jump to link
427//
428// 1. find all linked categories that are reachable for the current user.
429// 2. if a category is available in the URL, use it if reachable
430// 3. if URL category not available or reachable, use the first reachable
431//    linked category
432// 4. if no category reachable, no jumpto link
433
434$query = '
435SELECT category_id
436  FROM '.IMAGE_CATEGORY_TABLE.'
437  WHERE image_id = '.$_GET['image_id'].'
438;';
439
440$authorizeds = array_diff(
441  array_from_query($query, 'category_id'),
442  explode(
443    ',',
444    calculate_permissions($user['id'], $user['status'])
445    )
446  );
447
448if (isset($_GET['cat_id'])
449    and in_array($_GET['cat_id'], $authorizeds))
450{
451  $url_img = make_picture_url(
452    array(
453      'image_id' => $_GET['image_id'],
454      'image_file' => $image_file,
455      'category' => $cache['cat_names'][ $_GET['cat_id'] ],
456      )
457    );
458}
459else
460{
461  foreach ($authorizeds as $category)
462  {
463    $url_img = make_picture_url(
464      array(
465        'image_id' => $_GET['image_id'],
466        'image_file' => $image_file,
467        'category' => $cache['cat_names'][ $category ],
468        )
469      );
470    break;
471  }
472}
473
474if (isset($url_img))
475{
476  $template->assign( 'U_JUMPTO', $url_img );
477}
478
479// associate to another category ?
480$query = '
481SELECT id,name,uppercats,global_rank
482  FROM '.CATEGORIES_TABLE.'
483    INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = category_id
484  WHERE image_id = '.$_GET['image_id'];
485if (isset($storage_category_id))
486{
487  $query.= '
488    AND id != '.$storage_category_id;
489}
490$query.= '
491;';
492display_select_cat_wrapper($query, array(), 'associated_options');
493
494$result = pwg_query($query);
495$associateds = array(-1);
496if (isset($storage_category_id))
497{
498  array_push($associateds, $storage_category_id);
499}
500while ($row = pwg_db_fetch_assoc($result))
501{
502  array_push($associateds, $row['id']);
503}
504$query = '
505SELECT id,name,uppercats,global_rank
506  FROM '.CATEGORIES_TABLE.'
507  WHERE id NOT IN ('.implode(',', $associateds).')
508;';
509display_select_cat_wrapper($query, array(), 'dissociated_options');
510
511// representing
512$query = '
513SELECT id,name,uppercats,global_rank
514  FROM '.CATEGORIES_TABLE.'
515  WHERE representative_picture_id = '.$_GET['image_id'].'
516;';
517display_select_cat_wrapper($query, array(), 'elected_options');
518
519$query = '
520SELECT id,name,uppercats,global_rank
521  FROM '.CATEGORIES_TABLE.'
522  WHERE representative_picture_id != '.$_GET['image_id'].'
523    OR representative_picture_id IS NULL
524;';
525display_select_cat_wrapper($query, array(), 'dismissed_options');
526
527//----------------------------------------------------------- sending html code
528
529$template->assign_var_from_handle('PLUGIN_INDEX_CONTENT_BEGIN', 'icy_picture_modify');
530
531?>
Note: See TracBrowser for help on using the repository browser.