source: branches/2.4/admin/tags.php @ 17971

Last change on this file since 17971 was 17766, checked in by rvelices, 12 years ago

merge-r17765 from trunk to branch 2.4 feature 2737: improve tag administration screen
show for every tag

  • the number of photos
  • link to public index page
  • link to batch manager edit

add an event for extended description multi language strings (used for autocompletion and shown in the tag admin screen) instead of hard coded in the core [lang=..

  • Property svn:eol-style set to LF
File size: 11.1 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
335// tag counters
336$query = '
337SELECT tag_id, COUNT(image_id) AS counter
338  FROM '.IMAGE_TAG_TABLE.'
339  GROUP BY tag_id';
340$tag_counters = simple_hash_from_query($query, 'tag_id', 'counter');
341
342// all tags
343$query = '
344SELECT *
345  FROM '.TAGS_TABLE.'
346;';
347$result = pwg_query($query);
348$all_tags = array();
349while ($tag = pwg_db_fetch_assoc($result))
350{
351  $raw_name = $tag['name'];
352  $tag['name'] = trigger_event('render_tag_name', $raw_name);
353  $tag['counter'] = intval(@$tag_counters[ $tag['id'] ]);
354  $tag['U_VIEW'] = make_index_url(array('tags'=>array($tag)));
355  $tag['U_EDIT'] = 'admin.php?page=batch_manager&amp;cat=tag-'.$tag['id'];
356
357  $alt_names = trigger_event('get_tag_alt_names', array(), $raw_name);
358  $alt_names = array_diff( array_unique($alt_names), array($tag['name']) );
359  if (count($alt_names))
360  {
361    $tag['alt_names'] = implode(', ', $alt_names);
362  }
363  $all_tags[] = $tag;
364}
365usort($all_tags, 'tag_alpha_compare');
366
367
368
369$template->assign(
370  array(
371    'all_tags' => $all_tags,
372    )
373  );
374
375if ((isset($_POST['edit']) or isset($_POST['merge'])) and isset($_POST['tags']))
376{
377  $list_name = 'EDIT_TAGS_LIST';
378  if (isset($_POST['merge']))
379  {
380    $list_name = 'MERGE_TAGS_LIST';
381  }
382
383  $template->assign(
384    array(
385      $list_name => implode(',', $_POST['tags']),
386      )
387    );
388
389  $query = '
390SELECT id, name
391  FROM '.TAGS_TABLE.'
392  WHERE id IN ('.implode(',', $_POST['tags']).')
393;';
394  $result = pwg_query($query);
395  while ($row = pwg_db_fetch_assoc($result))
396  {
397    $name_of[ $row['id'] ] = $row['name'];
398  }
399
400  foreach ($_POST['tags'] as $tag_id)
401  {
402    $template->append(
403      'tags',
404      array(
405        'ID' => $tag_id,
406        'NAME' => $name_of[$tag_id],
407        )
408      );
409  }
410}
411
412// +-----------------------------------------------------------------------+
413// |                           sending html code                           |
414// +-----------------------------------------------------------------------+
415
416$template->assign_var_from_handle('ADMIN_CONTENT', 'tags');
417
418?>
Note: See TracBrowser for help on using the repository browser.