source: trunk/admin/tags.php @ 13018

Last change on this file since 13018 was 12922, checked in by mistic100, 12 years ago

update Piwigo headers to 2012, last change before the expected (or not) apocalypse

  • Property svn:eol-style set to LF
File size: 10.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 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');
30check_status(ACCESS_ADMINISTRATOR);
31
32if (!empty($_POST))
33{
34  check_pwg_token();
35}
36
37// +-----------------------------------------------------------------------+
38// |                                edit tags                              |
39// +-----------------------------------------------------------------------+
40
41if (isset($_POST['submit']))
42{
43  $query = '
44SELECT name
45  FROM '.TAGS_TABLE.'
46;';
47  $existing_names = array_from_query($query, 'name');
48 
49
50  $current_name_of = array();
51  $query = '
52SELECT id, name
53  FROM '.TAGS_TABLE.'
54  WHERE id IN ('.$_POST['edit_list'].')
55;';
56  $result = pwg_query($query);
57  while ($row = pwg_db_fetch_assoc($result))
58  {
59    $current_name_of[ $row['id'] ] = $row['name'];
60  }
61 
62  $updates = array();
63  // we must not rename tag with an already existing name
64  foreach (explode(',', $_POST['edit_list']) as $tag_id)
65  {
66    $tag_name = stripslashes($_POST['tag_name-'.$tag_id]);
67
68    if ($tag_name != $current_name_of[$tag_id])
69    {
70      if (in_array($tag_name, $existing_names))
71      {
72        array_push(
73          $page['errors'],
74          sprintf(
75            l10n('Tag "%s" already exists'),
76            $tag_name
77            )
78          );
79      }
80      else if (!empty($tag_name))
81      {
82        array_push(
83          $updates,
84          array(
85            'id' => $tag_id,
86            'name' => addslashes($tag_name),
87            'url_name' => trigger_event('render_tag_url', $tag_name),
88            )
89          );
90      }
91    }
92  }
93  mass_updates(
94    TAGS_TABLE,
95    array(
96      'primary' => array('id'),
97      'update' => array('name', 'url_name'),
98      ),
99    $updates
100    );
101}
102
103// +-----------------------------------------------------------------------+
104// |                               merge tags                              |
105// +-----------------------------------------------------------------------+
106
107if (isset($_POST['confirm_merge']))
108{
109  if (!isset($_POST['destination_tag']))
110  {
111    array_push(
112      $page['errors'],
113      l10n('No destination tag selected')
114      );
115  }
116  else
117  {
118    $destination_tag_id = $_POST['destination_tag'];
119    $tag_ids = explode(',', $_POST['merge_list']);
120   
121    if (is_array($tag_ids) and count($tag_ids) > 1)
122    {
123      $name_of_tag = array();
124      $query = '
125SELECT
126    id,
127    name
128  FROM '.TAGS_TABLE.'
129  WHERE id IN ('.implode(',', $tag_ids).')
130;';
131      $result = pwg_query($query);
132      while ($row = pwg_db_fetch_assoc($result))
133      {
134        $name_of_tag[ $row['id'] ] = trigger_event('render_tag_name', $row['name']);
135      }
136     
137      $tag_ids_to_delete = array_diff(
138        $tag_ids,
139        array($destination_tag_id)
140        );
141
142      $query = '
143SELECT
144    DISTINCT(image_id)
145  FROM '.IMAGE_TAG_TABLE.'
146  WHERE tag_id IN ('.implode(',', $tag_ids_to_delete).')
147;';
148      $image_ids = array_from_query($query, 'image_id');
149
150      delete_tags($tag_ids_to_delete);
151
152      $query = '
153SELECT
154    image_id
155  FROM '.IMAGE_TAG_TABLE.'
156  WHERE tag_id = '.$destination_tag_id.'
157;';
158      $destination_tag_image_ids = array_from_query($query, 'image_id');
159
160      $image_ids_to_link = array_diff(
161        $image_ids,
162        $destination_tag_image_ids
163        );
164
165      $inserts = array();
166      foreach ($image_ids_to_link as $image_id)
167      {
168        array_push(
169          $inserts,
170          array(
171            'tag_id' => $destination_tag_id,
172            'image_id' => $image_id
173            )
174          );
175      }
176
177      if (count($inserts) > 0)
178      {
179        mass_inserts(
180          IMAGE_TAG_TABLE,
181          array_keys($inserts[0]),
182          $inserts
183          );
184      }
185
186      $tags_deleted = array();
187      foreach ($tag_ids_to_delete as $tag_id)
188      {
189        $tags_deleted[] = $name_of_tag[$tag_id];
190      }
191     
192      array_push(
193        $page['infos'],
194        sprintf(
195          l10n('Tags <em>%s</em> merged into tag <em>%s</em>'),
196          implode(', ', $tags_deleted),
197          $name_of_tag[$destination_tag_id]
198          )
199        );
200    }
201  }
202}
203
204
205// +-----------------------------------------------------------------------+
206// |                               delete tags                             |
207// +-----------------------------------------------------------------------+
208
209if (isset($_POST['delete']) and isset($_POST['tags']))
210{
211  $query = '
212SELECT name
213  FROM '.TAGS_TABLE.'
214  WHERE id IN ('.implode(',', $_POST['tags']).')
215;';
216  $tag_names = array_from_query($query, 'name');
217
218  delete_tags($_POST['tags']);
219 
220  array_push(
221    $page['infos'],
222    l10n_dec(
223      'The following tag was deleted', 
224      'The %d following tags were deleted',
225      count($tag_names)).' : '.
226      implode(', ', $tag_names)
227    );
228}
229
230// +-----------------------------------------------------------------------+
231// |                           delete orphan tags                          |
232// +-----------------------------------------------------------------------+
233
234if (isset($_GET['action']) and 'delete_orphans' == $_GET['action'])
235{
236  check_pwg_token();
237 
238  delete_orphan_tags();
239  $_SESSION['page_infos'] = array(l10n('Orphan tags deleted'));
240  redirect(get_root_url().'admin.php?page=tags');
241}
242
243// +-----------------------------------------------------------------------+
244// |                               add a tag                               |
245// +-----------------------------------------------------------------------+
246
247if (isset($_POST['add']) and !empty($_POST['add_tag']))
248{
249  $tag_name = $_POST['add_tag'];
250
251  // does the tag already exists?
252  $query = '
253SELECT id
254  FROM '.TAGS_TABLE.'
255  WHERE name = \''.$tag_name.'\'
256;';
257  $existing_tags = array_from_query($query, 'id');
258
259  if (count($existing_tags) == 0)
260  {
261    mass_inserts(
262      TAGS_TABLE,
263      array('name', 'url_name'),
264      array(
265        array(
266          'name' => $tag_name,
267          'url_name' => trigger_event('render_tag_url', $tag_name),
268          )
269        )
270      );
271   
272    array_push(
273      $page['infos'],
274      sprintf(
275        l10n('Tag "%s" was added'),
276        stripslashes($tag_name)
277        )
278      );
279  }
280  else
281  {
282    array_push(
283      $page['errors'],
284      sprintf(
285        l10n('Tag "%s" already exists'),
286        stripslashes($tag_name)
287        )
288      );
289  }
290}
291
292// +-----------------------------------------------------------------------+
293// |                             template init                             |
294// +-----------------------------------------------------------------------+
295
296$template->set_filenames(array('tags' => 'tags.tpl'));
297
298$template->assign(
299  array(
300    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=tags',
301    'PWG_TOKEN' => get_pwg_token(),
302    )
303  );
304
305// +-----------------------------------------------------------------------+
306// |                              orphan tags                              |
307// +-----------------------------------------------------------------------+
308
309$orphan_tags = get_orphan_tags();
310
311$orphan_tag_names = array();
312foreach ($orphan_tags as $tag)
313{
314  array_push($orphan_tag_names, trigger_event('render_tag_name', $tag['name']));
315}
316
317if (count($orphan_tag_names) > 0)
318{
319  array_push(
320    $page['warnings'],
321    sprintf(
322      l10n('You have %d orphan tags: %s.').' <a href="%s">'.l10n('Delete orphan tags').'</a>',
323      count($orphan_tag_names),
324      implode(', ', $orphan_tag_names),
325      get_root_url().'admin.php?page=tags&amp;action=delete_orphans&amp;pwg_token='.get_pwg_token()
326      )
327    );
328}
329
330// +-----------------------------------------------------------------------+
331// |                             form creation                             |
332// +-----------------------------------------------------------------------+
333
334$template->assign(
335  array(
336    'TAG_SELECTION' => get_html_tag_selection(
337      get_all_tags(),
338      'tags'
339      ),
340    )
341  );
342
343if ((isset($_POST['edit']) or isset($_POST['merge'])) and isset($_POST['tags']))
344{
345  $list_name = 'EDIT_TAGS_LIST';
346  if (isset($_POST['merge']))
347  {
348    $list_name = 'MERGE_TAGS_LIST';
349  }
350 
351  $template->assign(
352    array(
353      $list_name => implode(',', $_POST['tags']),
354      )
355    );
356
357  $query = '
358SELECT id, name
359  FROM '.TAGS_TABLE.'
360  WHERE id IN ('.implode(',', $_POST['tags']).')
361;';
362  $result = pwg_query($query);
363  while ($row = pwg_db_fetch_assoc($result))
364  {
365    $name_of[ $row['id'] ] = $row['name'];
366  }
367
368  foreach ($_POST['tags'] as $tag_id)
369  {
370    $template->append(
371      'tags',
372      array(
373        'ID' => $tag_id,
374        'NAME' => $name_of[$tag_id],
375        )
376      );
377  }
378}
379
380// +-----------------------------------------------------------------------+
381// |                           sending html code                           |
382// +-----------------------------------------------------------------------+
383
384$template->assign_var_from_handle('ADMIN_CONTENT', 'tags');
385
386?>
Note: See TracBrowser for help on using the repository browser.