source: trunk/admin/tags.php @ 17765

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

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: 13.9 KB
RevLine 
[1119]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[12922]5// | Copyright(C) 2008-2012 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
[17765]49
[1119]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  }
[17765]61
[1119]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),
[12553]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}
[16526]102// +-----------------------------------------------------------------------+
103// |                            dulicate tags                              |
104// +-----------------------------------------------------------------------+
[1119]105
[16526]106if (isset($_POST['duplic_submit']))
107{
108  $query = '
109SELECT name
110  FROM '.TAGS_TABLE.'
111;';
112  $existing_names = array_from_query($query, 'name');
113
[17765]114
[16526]115  $current_name_of = array();
116  $query = '
117SELECT id, name
118  FROM '.TAGS_TABLE.'
119  WHERE id IN ('.$_POST['edit_list'].')
120;';
121  $result = pwg_query($query);
122  while ($row = pwg_db_fetch_assoc($result))
123  {
124    $current_name_of[ $row['id'] ] = $row['name'];
125  }
[17765]126
[16526]127  $updates = array();
128  // we must not rename tag with an already existing name
129  foreach (explode(',', $_POST['edit_list']) as $tag_id)
130  {
131    $tag_name = stripslashes($_POST['tag_name-'.$tag_id]);
132
133    if ($tag_name != $current_name_of[$tag_id])
134    {
135      if (in_array($tag_name, $existing_names))
136      {
137        array_push(
138          $page['errors'],
139          sprintf(
140            l10n('Tag "%s" already exists'),
141            $tag_name
142            )
143          );
144      }
145      else if (!empty($tag_name))
146      {
147        mass_inserts(
148          TAGS_TABLE,
149          array('name', 'url_name'),
150          array(
151            array(
152              'name' => $tag_name,
153              'url_name' => trigger_event('render_tag_url', $tag_name),
154              )
155            )
156          );
157        $query = '
158        SELECT id
159          FROM '.TAGS_TABLE.'
160          WHERE name = \''.$tag_name.'\'
161        ;';
162        $destination_tag = array_from_query($query, 'id');
163        $destination_tag_id = $destination_tag[0];
164        $query = '
165        SELECT
166            image_id
167          FROM '.IMAGE_TAG_TABLE.'
168          WHERE tag_id = '.$tag_id.'
169        ;';
170        $destination_tag_image_ids = array_from_query($query, 'image_id');
171        $inserts = array();
172        foreach ($destination_tag_image_ids as $image_id)
173        {
174          array_push(
175            $inserts,
176            array(
177              'tag_id' => $destination_tag_id,
178              'image_id' => $image_id
179              )
180            );
181        }
[17765]182
[16526]183        if (count($inserts) > 0)
184        {
185          mass_inserts(
186            IMAGE_TAG_TABLE,
187            array_keys($inserts[0]),
188            $inserts
189            );
190        }
191        array_push(
192          $page['infos'],
193          sprintf(
194            l10n('Tag "%s" is now a duplicate of "%s"'),
195            stripslashes($tag_name),
196            $current_name_of[$tag_id]
197            )
198          );
199      }
200    }
201  }
202  mass_updates(
203    TAGS_TABLE,
204    array(
205      'primary' => array('id'),
206      'update' => array('name', 'url_name'),
207      ),
208    $updates
209    );
210}
211
[1119]212// +-----------------------------------------------------------------------+
[12032]213// |                               merge tags                              |
214// +-----------------------------------------------------------------------+
215
216if (isset($_POST['confirm_merge']))
217{
218  if (!isset($_POST['destination_tag']))
219  {
220    array_push(
221      $page['errors'],
222      l10n('No destination tag selected')
223      );
224  }
225  else
226  {
227    $destination_tag_id = $_POST['destination_tag'];
228    $tag_ids = explode(',', $_POST['merge_list']);
[17765]229
[12032]230    if (is_array($tag_ids) and count($tag_ids) > 1)
231    {
232      $name_of_tag = array();
233      $query = '
234SELECT
235    id,
236    name
237  FROM '.TAGS_TABLE.'
238  WHERE id IN ('.implode(',', $tag_ids).')
239;';
240      $result = pwg_query($query);
241      while ($row = pwg_db_fetch_assoc($result))
242      {
243        $name_of_tag[ $row['id'] ] = trigger_event('render_tag_name', $row['name']);
244      }
[17765]245
[12032]246      $tag_ids_to_delete = array_diff(
247        $tag_ids,
248        array($destination_tag_id)
249        );
250
251      $query = '
252SELECT
253    DISTINCT(image_id)
254  FROM '.IMAGE_TAG_TABLE.'
255  WHERE tag_id IN ('.implode(',', $tag_ids_to_delete).')
256;';
257      $image_ids = array_from_query($query, 'image_id');
258
259      delete_tags($tag_ids_to_delete);
260
261      $query = '
262SELECT
263    image_id
264  FROM '.IMAGE_TAG_TABLE.'
265  WHERE tag_id = '.$destination_tag_id.'
266;';
267      $destination_tag_image_ids = array_from_query($query, 'image_id');
268
269      $image_ids_to_link = array_diff(
270        $image_ids,
271        $destination_tag_image_ids
272        );
273
274      $inserts = array();
275      foreach ($image_ids_to_link as $image_id)
276      {
277        array_push(
278          $inserts,
279          array(
280            'tag_id' => $destination_tag_id,
281            'image_id' => $image_id
282            )
283          );
284      }
285
286      if (count($inserts) > 0)
287      {
288        mass_inserts(
289          IMAGE_TAG_TABLE,
290          array_keys($inserts[0]),
291          $inserts
292          );
293      }
294
295      $tags_deleted = array();
296      foreach ($tag_ids_to_delete as $tag_id)
297      {
298        $tags_deleted[] = $name_of_tag[$tag_id];
299      }
[17765]300
[12032]301      array_push(
302        $page['infos'],
303        sprintf(
304          l10n('Tags <em>%s</em> merged into tag <em>%s</em>'),
305          implode(', ', $tags_deleted),
306          $name_of_tag[$destination_tag_id]
307          )
308        );
309    }
310  }
311}
312
313
314// +-----------------------------------------------------------------------+
[1119]315// |                               delete tags                             |
316// +-----------------------------------------------------------------------+
317
[8126]318if (isset($_POST['delete']) and isset($_POST['tags']))
[1119]319{
320  $query = '
321SELECT name
322  FROM '.TAGS_TABLE.'
323  WHERE id IN ('.implode(',', $_POST['tags']).')
324;';
325  $tag_names = array_from_query($query, 'name');
[12032]326
327  delete_tags($_POST['tags']);
[17765]328
[1119]329  array_push(
330    $page['infos'],
[1932]331    l10n_dec(
[17765]332      'The following tag was deleted',
[1932]333      'The %d following tags were deleted',
334      count($tag_names)).' : '.
[1119]335      implode(', ', $tag_names)
336    );
337}
338
339// +-----------------------------------------------------------------------+
[8762]340// |                           delete orphan tags                          |
341// +-----------------------------------------------------------------------+
342
343if (isset($_GET['action']) and 'delete_orphans' == $_GET['action'])
344{
345  check_pwg_token();
[17765]346
[8762]347  delete_orphan_tags();
348  $_SESSION['page_infos'] = array(l10n('Orphan tags deleted'));
349  redirect(get_root_url().'admin.php?page=tags');
350}
351
352// +-----------------------------------------------------------------------+
[1119]353// |                               add a tag                               |
354// +-----------------------------------------------------------------------+
355
[8126]356if (isset($_POST['add']) and !empty($_POST['add_tag']))
[1119]357{
[2098]358  $tag_name = $_POST['add_tag'];
[1119]359
[1452]360  // does the tag already exists?
[1119]361  $query = '
362SELECT id
363  FROM '.TAGS_TABLE.'
[1717]364  WHERE name = \''.$tag_name.'\'
[1119]365;';
366  $existing_tags = array_from_query($query, 'id');
367
368  if (count($existing_tags) == 0)
369  {
370    mass_inserts(
371      TAGS_TABLE,
372      array('name', 'url_name'),
373      array(
374        array(
[1717]375          'name' => $tag_name,
[12553]376          'url_name' => trigger_event('render_tag_url', $tag_name),
[1119]377          )
378        )
379      );
[17765]380
[1119]381    array_push(
382      $page['infos'],
383      sprintf(
[5036]384        l10n('Tag "%s" was added'),
[1717]385        stripslashes($tag_name)
[1119]386        )
387      );
388  }
389  else
390  {
391    array_push(
392      $page['errors'],
393      sprintf(
[5036]394        l10n('Tag "%s" already exists'),
[1717]395        stripslashes($tag_name)
[1119]396        )
397      );
398  }
399}
400
401// +-----------------------------------------------------------------------+
402// |                             template init                             |
403// +-----------------------------------------------------------------------+
404
[2530]405$template->set_filenames(array('tags' => 'tags.tpl'));
[1119]406
[2277]407$template->assign(
[1119]408  array(
[5195]409    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=tags',
410    'PWG_TOKEN' => get_pwg_token(),
[1119]411    )
412  );
413
414// +-----------------------------------------------------------------------+
[8762]415// |                              orphan tags                              |
416// +-----------------------------------------------------------------------+
417
418$orphan_tags = get_orphan_tags();
419
420$orphan_tag_names = array();
421foreach ($orphan_tags as $tag)
422{
[11487]423  array_push($orphan_tag_names, trigger_event('render_tag_name', $tag['name']));
[8762]424}
425
426if (count($orphan_tag_names) > 0)
427{
428  array_push(
429    $page['warnings'],
430    sprintf(
431      l10n('You have %d orphan tags: %s.').' <a href="%s">'.l10n('Delete orphan tags').'</a>',
432      count($orphan_tag_names),
433      implode(', ', $orphan_tag_names),
434      get_root_url().'admin.php?page=tags&amp;action=delete_orphans&amp;pwg_token='.get_pwg_token()
435      )
436    );
437}
438
439// +-----------------------------------------------------------------------+
[1119]440// |                             form creation                             |
441// +-----------------------------------------------------------------------+
442
[17765]443
444// tag counters
445$query = '
446SELECT tag_id, COUNT(image_id) AS counter
447  FROM '.IMAGE_TAG_TABLE.'
448  GROUP BY tag_id';
449$tag_counters = simple_hash_from_query($query, 'tag_id', 'counter');
450
451// all tags
452$query = '
453SELECT *
454  FROM '.TAGS_TABLE.'
455;';
456$result = pwg_query($query);
457$all_tags = array();
458while ($tag = pwg_db_fetch_assoc($result))
459{
460  $raw_name = $tag['name'];
461  $tag['name'] = trigger_event('render_tag_name', $raw_name);
462  $tag['counter'] = intval(@$tag_counters[ $tag['id'] ]);
463  $tag['U_VIEW'] = make_index_url(array('tags'=>array($tag)));
464  $tag['U_EDIT'] = 'admin.php?page=batch_manager&amp;cat=tag-'.$tag['id'];
465
466  $alt_names = trigger_event('get_tag_alt_names', array(), $raw_name);
467  $alt_names = array_diff( array_unique($alt_names), array($tag['name']) );
468  if (count($alt_names))
469  {
470    $tag['alt_names'] = implode(', ', $alt_names);
471  }
472  $all_tags[] = $tag;
473}
474usort($all_tags, 'tag_alpha_compare');
475
476
477
[2277]478$template->assign(
[1119]479  array(
[17765]480    'all_tags' => $all_tags,
[1119]481    )
482  );
483
[16526]484if ((isset($_POST['edit']) or isset($_POST['duplicate']) or isset($_POST['merge'])) and isset($_POST['tags']))
[1119]485{
[12032]486  $list_name = 'EDIT_TAGS_LIST';
[16526]487  if (isset($_POST['duplicate']))
[12032]488  {
[16526]489    $list_name = 'DUPLIC_TAGS_LIST';
490  }
491  elseif (isset($_POST['merge']))
492  {
[12032]493    $list_name = 'MERGE_TAGS_LIST';
494  }
[17765]495
[2277]496  $template->assign(
[1119]497    array(
[12032]498      $list_name => implode(',', $_POST['tags']),
[1119]499      )
500    );
501
502  $query = '
503SELECT id, name
504  FROM '.TAGS_TABLE.'
505  WHERE id IN ('.implode(',', $_POST['tags']).')
506;';
507  $result = pwg_query($query);
[4325]508  while ($row = pwg_db_fetch_assoc($result))
[1119]509  {
510    $name_of[ $row['id'] ] = $row['name'];
511  }
512
513  foreach ($_POST['tags'] as $tag_id)
514  {
[2277]515    $template->append(
516      'tags',
[1119]517      array(
518        'ID' => $tag_id,
519        'NAME' => $name_of[$tag_id],
520        )
521      );
522  }
523}
524
525// +-----------------------------------------------------------------------+
526// |                           sending html code                           |
527// +-----------------------------------------------------------------------+
528
529$template->assign_var_from_handle('ADMIN_CONTENT', 'tags');
530
531?>
Note: See TracBrowser for help on using the repository browser.