source: branches/2.3/admin/tags.php @ 12555

Last change on this file since 12555 was 12555, checked in by patdenice, 12 years ago

merge r12553 from trunk to branch 2.3
feature:2322
feature:2493
Add a trigger for multi language tags in quick search and url

  • Property svn:eol-style set to LF
File size: 10.3 KB
RevLine 
[1119]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[8728]5// | Copyright(C) 2008-2011 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// +-----------------------------------------------------------------------+
[1119]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
[5195]32if (!empty($_POST))
33{
34  check_pwg_token();
35}
36
[1119]37// +-----------------------------------------------------------------------+
38// |                                edit tags                              |
39// +-----------------------------------------------------------------------+
40
[8126]41if (isset($_POST['submit']))
[1119]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);
[4325]57  while ($row = pwg_db_fetch_assoc($result))
[1119]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  {
[2098]66    $tag_name = stripslashes($_POST['tag_name-'.$tag_id]);
[1119]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(
[5036]75            l10n('Tag "%s" already exists'),
[1119]76            $tag_name
77            )
78          );
79      }
80      else if (!empty($tag_name))
81      {
82        array_push(
83          $updates,
84          array(
85            'id' => $tag_id,
[2098]86            'name' => addslashes($tag_name),
[12555]87            'url_name' => trigger_event('render_tag_url', $tag_name),
[1119]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// +-----------------------------------------------------------------------+
[12032]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// +-----------------------------------------------------------------------+
[1119]206// |                               delete tags                             |
207// +-----------------------------------------------------------------------+
208
[8126]209if (isset($_POST['delete']) and isset($_POST['tags']))
[1119]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');
[12032]217
218  delete_tags($_POST['tags']);
[1119]219 
220  array_push(
221    $page['infos'],
[1932]222    l10n_dec(
[5036]223      'The following tag was deleted', 
[1932]224      'The %d following tags were deleted',
225      count($tag_names)).' : '.
[1119]226      implode(', ', $tag_names)
227    );
228}
229
230// +-----------------------------------------------------------------------+
[8762]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// +-----------------------------------------------------------------------+
[1119]244// |                               add a tag                               |
245// +-----------------------------------------------------------------------+
246
[8126]247if (isset($_POST['add']) and !empty($_POST['add_tag']))
[1119]248{
[2098]249  $tag_name = $_POST['add_tag'];
[1119]250
[1452]251  // does the tag already exists?
[1119]252  $query = '
253SELECT id
254  FROM '.TAGS_TABLE.'
[1717]255  WHERE name = \''.$tag_name.'\'
[1119]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(
[1717]266          'name' => $tag_name,
[12555]267          'url_name' => trigger_event('render_tag_url', $tag_name),
[1119]268          )
269        )
270      );
271   
272    array_push(
273      $page['infos'],
274      sprintf(
[5036]275        l10n('Tag "%s" was added'),
[1717]276        stripslashes($tag_name)
[1119]277        )
278      );
279  }
280  else
281  {
282    array_push(
283      $page['errors'],
284      sprintf(
[5036]285        l10n('Tag "%s" already exists'),
[1717]286        stripslashes($tag_name)
[1119]287        )
288      );
289  }
290}
291
292// +-----------------------------------------------------------------------+
293// |                             template init                             |
294// +-----------------------------------------------------------------------+
295
[2530]296$template->set_filenames(array('tags' => 'tags.tpl'));
[1119]297
[2277]298$template->assign(
[1119]299  array(
[5195]300    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=tags',
301    'PWG_TOKEN' => get_pwg_token(),
[1119]302    )
303  );
304
305// +-----------------------------------------------------------------------+
[8762]306// |                              orphan tags                              |
307// +-----------------------------------------------------------------------+
308
309$orphan_tags = get_orphan_tags();
310
311$orphan_tag_names = array();
312foreach ($orphan_tags as $tag)
313{
[11487]314  array_push($orphan_tag_names, trigger_event('render_tag_name', $tag['name']));
[8762]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// +-----------------------------------------------------------------------+
[1119]331// |                             form creation                             |
332// +-----------------------------------------------------------------------+
333
[2277]334$template->assign(
[1119]335  array(
336    'TAG_SELECTION' => get_html_tag_selection(
337      get_all_tags(),
338      'tags'
339      ),
340    )
341  );
342
[12032]343if ((isset($_POST['edit']) or isset($_POST['merge'])) and isset($_POST['tags']))
[1119]344{
[12032]345  $list_name = 'EDIT_TAGS_LIST';
346  if (isset($_POST['merge']))
347  {
348    $list_name = 'MERGE_TAGS_LIST';
349  }
350 
[2277]351  $template->assign(
[1119]352    array(
[12032]353      $list_name => implode(',', $_POST['tags']),
[1119]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);
[4325]363  while ($row = pwg_db_fetch_assoc($result))
[1119]364  {
365    $name_of[ $row['id'] ] = $row['name'];
366  }
367
368  foreach ($_POST['tags'] as $tag_id)
369  {
[2277]370    $template->append(
371      'tags',
[1119]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.