source: branches/2.2/admin/tags.php @ 11727

Last change on this file since 11727 was 8762, checked in by plg, 13 years ago

feature 1289 added: easy "delete orphan tags" function. On the "tags"
administration page, a warning message is displayed if you have at least
one orphan tag + direct action to delete them.

  • Property svn:eol-style set to LF
File size: 8.0 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") )
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' => str2url($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// |                               delete tags                             |
105// +-----------------------------------------------------------------------+
106
107if (isset($_POST['delete']) and isset($_POST['tags']))
108{
109  $query = '
110SELECT name
111  FROM '.TAGS_TABLE.'
112  WHERE id IN ('.implode(',', $_POST['tags']).')
113;';
114  $tag_names = array_from_query($query, 'name');
115 
116  $query = '
117DELETE
118  FROM '.IMAGE_TAG_TABLE.'
119  WHERE tag_id IN ('.implode(',', $_POST['tags']).')
120;';
121  pwg_query($query);
122 
123  $query = '
124DELETE
125  FROM '.TAGS_TABLE.'
126  WHERE id IN ('.implode(',', $_POST['tags']).')
127;';
128  pwg_query($query);
129 
130  array_push(
131    $page['infos'],
132    l10n_dec(
133      'The following tag was deleted', 
134      'The %d following tags were deleted',
135      count($tag_names)).' : '.
136      implode(', ', $tag_names)
137    );
138}
139
140// +-----------------------------------------------------------------------+
141// |                           delete orphan tags                          |
142// +-----------------------------------------------------------------------+
143
144if (isset($_GET['action']) and 'delete_orphans' == $_GET['action'])
145{
146  check_pwg_token();
147 
148  delete_orphan_tags();
149  $_SESSION['page_infos'] = array(l10n('Orphan tags deleted'));
150  redirect(get_root_url().'admin.php?page=tags');
151}
152
153// +-----------------------------------------------------------------------+
154// |                               add a tag                               |
155// +-----------------------------------------------------------------------+
156
157if (isset($_POST['add']) and !empty($_POST['add_tag']))
158{
159  $tag_name = $_POST['add_tag'];
160
161  // does the tag already exists?
162  $query = '
163SELECT id
164  FROM '.TAGS_TABLE.'
165  WHERE name = \''.$tag_name.'\'
166;';
167  $existing_tags = array_from_query($query, 'id');
168
169  if (count($existing_tags) == 0)
170  {
171    mass_inserts(
172      TAGS_TABLE,
173      array('name', 'url_name'),
174      array(
175        array(
176          'name' => $tag_name,
177          'url_name' => str2url($tag_name),
178          )
179        )
180      );
181   
182    array_push(
183      $page['infos'],
184      sprintf(
185        l10n('Tag "%s" was added'),
186        stripslashes($tag_name)
187        )
188      );
189  }
190  else
191  {
192    array_push(
193      $page['errors'],
194      sprintf(
195        l10n('Tag "%s" already exists'),
196        stripslashes($tag_name)
197        )
198      );
199  }
200}
201
202// +-----------------------------------------------------------------------+
203// |                             template init                             |
204// +-----------------------------------------------------------------------+
205
206$template->set_filenames(array('tags' => 'tags.tpl'));
207
208$template->assign(
209  array(
210    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=tags',
211    'PWG_TOKEN' => get_pwg_token(),
212    )
213  );
214
215// +-----------------------------------------------------------------------+
216// |                              orphan tags                              |
217// +-----------------------------------------------------------------------+
218
219$orphan_tags = get_orphan_tags();
220
221$orphan_tag_names = array();
222foreach ($orphan_tags as $tag)
223{
224  array_push($orphan_tag_names, $tag['name']);
225}
226
227if (count($orphan_tag_names) > 0)
228{
229  array_push(
230    $page['warnings'],
231    sprintf(
232      l10n('You have %d orphan tags: %s.').' <a href="%s">'.l10n('Delete orphan tags').'</a>',
233      count($orphan_tag_names),
234      implode(', ', $orphan_tag_names),
235      get_root_url().'admin.php?page=tags&amp;action=delete_orphans&amp;pwg_token='.get_pwg_token()
236      )
237    );
238}
239
240// +-----------------------------------------------------------------------+
241// |                             form creation                             |
242// +-----------------------------------------------------------------------+
243
244$template->assign(
245  array(
246    'TAG_SELECTION' => get_html_tag_selection(
247      get_all_tags(),
248      'tags'
249      ),
250    )
251  );
252
253if (isset($_POST['edit']) and isset($_POST['tags']))
254{
255  $template->assign(
256    array(
257      'EDIT_TAGS_LIST' => implode(',', $_POST['tags']),
258      )
259    );
260
261  $query = '
262SELECT id, name
263  FROM '.TAGS_TABLE.'
264  WHERE id IN ('.implode(',', $_POST['tags']).')
265;';
266  $result = pwg_query($query);
267  while ($row = pwg_db_fetch_assoc($result))
268  {
269    $name_of[ $row['id'] ] = $row['name'];
270  }
271
272  foreach ($_POST['tags'] as $tag_id)
273  {
274    $template->append(
275      'tags',
276      array(
277        'ID' => $tag_id,
278        'NAME' => $name_of[$tag_id],
279        )
280      );
281  }
282}
283
284// +-----------------------------------------------------------------------+
285// |                           sending html code                           |
286// +-----------------------------------------------------------------------+
287
288$template->assign_var_from_handle('ADMIN_CONTENT', 'tags');
289
290?>
Note: See TracBrowser for help on using the repository browser.