source: trunk/admin/picture_modify.php @ 5090

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

feature 724: improved "add tags" form. Instead of a big list of checkboxes,
displays a dynamic list of tags with jQuery, with suggestions based on
existing tags and the ability to create new tags on the fly.

The change was applied only on admin/picture_modify.php for test purpose.

Note : FCBKcomplete 2.7 had a bug on "remote tag" click, and the bug was fixed
on 2.7.1. But the suggestions were not working with 2.7.1. So I took the 2.7
and applied the tiny change to make the "remove tag" click work.

  • Property svn:eol-style set to LF
File size: 12.6 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
24if(!defined("PHPWG_ROOT_PATH"))
25{
26  die('Hacking attempt!');
27}
28
29include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
30
31// +-----------------------------------------------------------------------+
32// | Check Access and exit when user status is not ok                      |
33// +-----------------------------------------------------------------------+
34check_status(ACCESS_ADMINISTRATOR);
35
36// +-----------------------------------------------------------------------+
37// |                          synchronize metadata                         |
38// +-----------------------------------------------------------------------+
39
40if (isset($_GET['sync_metadata']) and !is_adviser())
41{
42  $query = '
43SELECT path
44  FROM '.IMAGES_TABLE.'
45  WHERE id = '.$_GET['image_id'].'
46;';
47  list($path) = pwg_db_fetch_row(pwg_query($query));
48  update_metadata(array($_GET['image_id'] => $path));
49
50  array_push($page['infos'], l10n('Metadata synchronized from file'));
51}
52
53//--------------------------------------------------------- update informations
54
55// first, we verify whether there is a mistake on the given creation date
56if (isset($_POST['date_creation_action'])
57    and 'set' == $_POST['date_creation_action'])
58{
59  if (!checkdate(
60        $_POST['date_creation_month'],
61        $_POST['date_creation_day'],
62        $_POST['date_creation_year'])
63    )
64  {
65    array_push($page['errors'], l10n('wrong date'));
66  }
67}
68
69if (isset($_POST['submit']) and count($page['errors']) == 0 and !is_adviser())
70{
71  $data = array();
72  $data{'id'} = $_GET['image_id'];
73  $data{'name'} = $_POST['name'];
74  $data{'author'} = $_POST['author'];
75  $data['level'] = $_POST['level'];
76
77  if ($conf['allow_html_descriptions'])
78  {
79    $data{'comment'} = @$_POST['description'];
80  }
81  else
82  {
83    $data{'comment'} = strip_tags(@$_POST['description']);
84  }
85
86  if (isset($_POST['date_creation_action']))
87  {
88    if ('set' == $_POST['date_creation_action'])
89    {
90      $data{'date_creation'} = $_POST['date_creation_year']
91                                 .'-'.$_POST['date_creation_month']
92                                 .'-'.$_POST['date_creation_day'];
93    }
94    else if ('unset' == $_POST['date_creation_action'])
95    {
96      $data{'date_creation'} = '';
97    }
98  }
99
100  mass_updates(
101    IMAGES_TABLE,
102    array(
103      'primary' => array('id'),
104      'update' => array_diff(array_keys($data), array('id'))
105      ),
106    array($data)
107    );
108
109  // In $_POST[tags] we receive something like array('~~6~~', '~~59~~', 'New
110  // tag', 'Another new tag') The ~~34~~ means that it is an existing
111  // tag. I've added the surrounding ~~ to permit creation of tags like "10"
112  // or "1234" (numeric characters only)
113  $tag_ids = array();
114  if (isset($_POST['tags']))
115  {
116    foreach ($_POST['tags'] as $raw_tag)
117    {
118      if (preg_match('/^~~(\d+)~~$/', $raw_tag, $matches))
119      {
120        array_push($tag_ids, $matches[1]);
121      }
122      else
123      {
124        // we have to create a new tag
125        array_push(
126          $tag_ids, 
127          tag_id_from_tag_name($raw_tag)
128          );
129      }
130    }
131  }
132 
133  set_tags(
134    $tag_ids,
135    $_GET['image_id']
136    );
137
138  array_push($page['infos'], l10n('Picture informations updated'));
139}
140// associate the element to other categories than its storage category
141if (isset($_POST['associate'])
142    and isset($_POST['cat_dissociated'])
143    and count($_POST['cat_dissociated']) > 0
144    and !is_adviser()
145  )
146{
147  associate_images_to_categories(
148    array($_GET['image_id']),
149    $_POST['cat_dissociated']
150    );
151}
152// dissociate the element from categories (but not from its storage category)
153if (isset($_POST['dissociate'])
154    and isset($_POST['cat_associated'])
155    and count($_POST['cat_associated']) > 0
156    and !is_adviser()
157  )
158{
159  $query = '
160DELETE FROM '.IMAGE_CATEGORY_TABLE.'
161  WHERE image_id = '.$_GET['image_id'].'
162    AND category_id IN ('.implode(',', $_POST['cat_associated']).')
163';
164  pwg_query($query);
165
166  update_category($_POST['cat_associated']);
167}
168// elect the element to represent the given categories
169if (isset($_POST['elect'])
170    and isset($_POST['cat_dismissed'])
171    and count($_POST['cat_dismissed']) > 0
172    and !is_adviser()
173  )
174{
175  $datas = array();
176  foreach ($_POST['cat_dismissed'] as $category_id)
177  {
178    array_push($datas,
179               array('id' => $category_id,
180                     'representative_picture_id' => $_GET['image_id']));
181  }
182  $fields = array('primary' => array('id'),
183                  'update' => array('representative_picture_id'));
184  mass_updates(CATEGORIES_TABLE, $fields, $datas);
185}
186// dismiss the element as representant of the given categories
187if (isset($_POST['dismiss'])
188    and isset($_POST['cat_elected'])
189    and count($_POST['cat_elected']) > 0
190    and !is_adviser()
191  )
192{
193  set_random_representant($_POST['cat_elected']);
194}
195
196// tags
197$tags = array();
198
199$query = '
200SELECT
201    tag_id,
202    name
203  FROM '.IMAGE_TAG_TABLE.' AS it
204    JOIN '.TAGS_TABLE.' AS t ON t.id = it.tag_id
205  WHERE image_id = '.$_GET['image_id'].'
206;';
207$result = pwg_query($query);
208while ($row = pwg_db_fetch_assoc($result))
209{
210  array_push(
211    $tags,
212    array(
213      'value' => '~~'.$row['tag_id'].'~~',
214      'caption' => $row['name'],
215      )
216    );
217}
218
219// retrieving direct information about picture
220$query = '
221SELECT *
222  FROM '.IMAGES_TABLE.'
223  WHERE id = '.$_GET['image_id'].'
224;';
225$row = pwg_db_fetch_assoc(pwg_query($query));
226
227$storage_category_id = null;
228if (!empty($row['storage_category_id']))
229{
230  $storage_category_id = $row['storage_category_id'];
231}
232
233$image_file = $row['file'];
234
235// +-----------------------------------------------------------------------+
236// |                             template init                             |
237// +-----------------------------------------------------------------------+
238
239$template->set_filenames(
240  array(
241    'picture_modify' => 'picture_modify.tpl'
242    )
243  );
244
245$template->assign(
246  array(
247    'tags' => $tags,
248    'U_SYNC' =>
249        get_root_url().'admin.php?page=picture_modify'.
250        '&amp;image_id='.$_GET['image_id'].
251        (isset($_GET['cat_id']) ? '&amp;cat_id='.$_GET['cat_id'] : '').
252        '&amp;sync_metadata=1',
253
254    'PATH'=>$row['path'],
255
256    'TN_SRC' => get_thumbnail_url($row),
257
258    'NAME' =>
259      isset($_POST['name']) ?
260        stripslashes($_POST['name']) : @$row['name'],
261
262    'DIMENSIONS' => @$row['width'].' * '.@$row['height'],
263
264    'FILESIZE' => @$row['filesize'].' KB',
265
266    'REGISTRATION_DATE' => format_date($row['date_available']),
267
268    'AUTHOR' => isset($_POST['author']) ? $_POST['author'] : @$row['author'],
269
270    'DESCRIPTION' =>
271      htmlspecialchars( isset($_POST['description']) ?
272        stripslashes($_POST['description']) : @$row['comment'] ),
273
274    'F_ACTION' =>
275        get_root_url().'admin.php'
276        .get_query_string_diff(array('sync_metadata'))
277    )
278  );
279
280if ($row['has_high'] == 'true')
281{
282  $template->assign(
283    'HIGH_FILESIZE',
284    isset($row['high_filesize'])
285        ? $row['high_filesize'].' KB'
286        : l10n('unknown')
287    );
288}
289
290// image level options
291$tpl_options = array();
292foreach ($conf['available_permission_levels'] as $level)
293{
294  $tpl_options[$level] = l10n( sprintf('Level %d', $level) ).' ('.$level.')';
295}
296$selected_level = isset($_POST['level']) ? $_POST['level'] : $row['level'];
297$template->assign(
298    array(
299      'level_options'=> $tpl_options,
300      'level_options_selected' => array($selected_level)
301    )
302  );
303
304// creation date
305unset($day, $month, $year);
306
307if (isset($_POST['date_creation_action'])
308    and 'set' == $_POST['date_creation_action'])
309{
310  foreach (array('day', 'month', 'year') as $varname)
311  {
312    $$varname = $_POST['date_creation_'.$varname];
313  }
314}
315else if (isset($row['date_creation']) and !empty($row['date_creation']))
316{
317  list($year, $month, $day) = explode('-', $row['date_creation']);
318}
319else
320{
321  list($year, $month, $day) = array('', 0, 0);
322}
323
324
325$month_list = $lang['month'];
326$month_list[0]='------------';
327ksort($month_list);
328
329$template->assign(
330    array(
331      'DATE_CREATION_DAY_VALUE' => $day,
332      'DATE_CREATION_MONTH_VALUE' => $month,
333      'DATE_CREATION_YEAR_VALUE' => $year,
334      'month_list' => $month_list,
335      )
336    );
337
338$query = '
339SELECT category_id, uppercats
340  FROM '.IMAGE_CATEGORY_TABLE.' AS ic
341    INNER JOIN '.CATEGORIES_TABLE.' AS c
342      ON c.id = ic.category_id
343  WHERE image_id = '.$_GET['image_id'].'
344;';
345$result = pwg_query($query);
346
347while ($row = pwg_db_fetch_assoc($result))
348{
349  $name =
350    get_cat_display_name_cache(
351      $row['uppercats'],
352      get_root_url().'admin.php?page=cat_modify&amp;cat_id=',
353      false
354      );
355
356  if ($row['category_id'] == $storage_category_id)
357  {
358    $template->assign('STORAGE_CATEGORY', $name);
359  }
360  else
361  {
362    $template->append('related_categories', $name);
363  }
364}
365
366// jump to link
367//
368// 1. find all linked categories that are reachable for the current user.
369// 2. if a category is available in the URL, use it if reachable
370// 3. if URL category not available or reachable, use the first reachable
371//    linked category
372// 4. if no category reachable, no jumpto link
373
374$query = '
375SELECT category_id
376  FROM '.IMAGE_CATEGORY_TABLE.'
377  WHERE image_id = '.$_GET['image_id'].'
378;';
379
380$authorizeds = array_diff(
381  array_from_query($query, 'category_id'),
382  explode(
383    ',',
384    calculate_permissions($user['id'], $user['status'])
385    )
386  );
387
388if (isset($_GET['cat_id'])
389    and in_array($_GET['cat_id'], $authorizeds))
390{
391  $url_img = make_picture_url(
392    array(
393      'image_id' => $_GET['image_id'],
394      'image_file' => $image_file,
395      'category' => $cache['cat_names'][ $_GET['cat_id'] ],
396      )
397    );
398}
399else
400{
401  foreach ($authorizeds as $category)
402  {
403    $url_img = make_picture_url(
404      array(
405        'image_id' => $_GET['image_id'],
406        'image_file' => $image_file,
407        'category' => $cache['cat_names'][ $category ],
408        )
409      );
410    break;
411  }
412}
413
414if (isset($url_img))
415{
416  $template->assign( 'U_JUMPTO', $url_img );
417}
418
419// associate to another category ?
420$query = '
421SELECT id,name,uppercats,global_rank
422  FROM '.CATEGORIES_TABLE.'
423    INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = category_id
424  WHERE image_id = '.$_GET['image_id'];
425if (isset($storage_category_id))
426{
427  $query.= '
428    AND id != '.$storage_category_id;
429}
430$query.= '
431;';
432display_select_cat_wrapper($query, array(), 'associated_options');
433
434$result = pwg_query($query);
435$associateds = array(-1);
436if (isset($storage_category_id))
437{
438  array_push($associateds, $storage_category_id);
439}
440while ($row = pwg_db_fetch_assoc($result))
441{
442  array_push($associateds, $row['id']);
443}
444$query = '
445SELECT id,name,uppercats,global_rank
446  FROM '.CATEGORIES_TABLE.'
447  WHERE id NOT IN ('.implode(',', $associateds).')
448;';
449display_select_cat_wrapper($query, array(), 'dissociated_options');
450
451// representing
452$query = '
453SELECT id,name,uppercats,global_rank
454  FROM '.CATEGORIES_TABLE.'
455  WHERE representative_picture_id = '.$_GET['image_id'].'
456;';
457display_select_cat_wrapper($query, array(), 'elected_options');
458
459$query = '
460SELECT id,name,uppercats,global_rank
461  FROM '.CATEGORIES_TABLE.'
462  WHERE representative_picture_id != '.$_GET['image_id'].'
463    OR representative_picture_id IS NULL
464;';
465display_select_cat_wrapper($query, array(), 'dismissed_options');
466
467//----------------------------------------------------------- sending html code
468
469$template->assign_var_from_handle('ADMIN_CONTENT', 'picture_modify');
470?>
Note: See TracBrowser for help on using the repository browser.