source: trunk/admin/tags.php @ 25005

Last change on this file since 25005 was 25005, checked in by mistic100, 10 years ago

feature 2978: remove useless sprintf in the core

  • Property svn:eol-style set to LF
File size: 13.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2013 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          l10n(
75            '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// |                            dulicate tags                              |
104// +-----------------------------------------------------------------------+
105
106if (isset($_POST['duplic_submit']))
107{
108  $query = '
109SELECT name
110  FROM '.TAGS_TABLE.'
111;';
112  $existing_names = array_from_query($query, 'name');
113
114
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  }
126
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          l10n(
140            '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        }
182
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          l10n(
194            '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
212// +-----------------------------------------------------------------------+
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']);
229
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      }
245
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      }
300
301      array_push(
302        $page['infos'],
303        l10n(
304          '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// +-----------------------------------------------------------------------+
315// |                               delete tags                             |
316// +-----------------------------------------------------------------------+
317
318if (isset($_POST['delete']) and isset($_POST['tags']))
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');
326
327  delete_tags($_POST['tags']);
328
329  array_push(
330    $page['infos'],
331    l10n_dec(
332      'The following tag was deleted',
333      'The %d following tags were deleted',
334      count($tag_names)).' : '.
335      implode(', ', $tag_names)
336    );
337}
338
339// +-----------------------------------------------------------------------+
340// |                           delete orphan tags                          |
341// +-----------------------------------------------------------------------+
342
343if (isset($_GET['action']) and 'delete_orphans' == $_GET['action'])
344{
345  check_pwg_token();
346
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// +-----------------------------------------------------------------------+
353// |                               add a tag                               |
354// +-----------------------------------------------------------------------+
355
356if (isset($_POST['add']) and !empty($_POST['add_tag']))
357{
358  $tag_name = $_POST['add_tag'];
359
360  // does the tag already exists?
361  $query = '
362SELECT id
363  FROM '.TAGS_TABLE.'
364  WHERE name = \''.$tag_name.'\'
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(
375          'name' => $tag_name,
376          'url_name' => trigger_event('render_tag_url', $tag_name),
377          )
378        )
379      );
380
381    array_push(
382      $page['infos'],
383      l10n(
384        'Tag "%s" was added',
385        stripslashes($tag_name)
386        )
387      );
388  }
389  else
390  {
391    array_push(
392      $page['errors'],
393      l10n(
394        'Tag "%s" already exists',
395        stripslashes($tag_name)
396        )
397      );
398  }
399}
400
401// +-----------------------------------------------------------------------+
402// |                             template init                             |
403// +-----------------------------------------------------------------------+
404
405$template->set_filenames(array('tags' => 'tags.tpl'));
406
407$template->assign(
408  array(
409    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=tags',
410    'PWG_TOKEN' => get_pwg_token(),
411    )
412  );
413
414// +-----------------------------------------------------------------------+
415// |                              orphan tags                              |
416// +-----------------------------------------------------------------------+
417
418$orphan_tags = get_orphan_tags();
419
420$orphan_tag_names = array();
421foreach ($orphan_tags as $tag)
422{
423  array_push($orphan_tag_names, trigger_event('render_tag_name', $tag['name']));
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// +-----------------------------------------------------------------------+
440// |                             form creation                             |
441// +-----------------------------------------------------------------------+
442
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;filter=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
478$template->assign(
479  array(
480    'all_tags' => $all_tags,
481    )
482  );
483
484if ((isset($_POST['edit']) or isset($_POST['duplicate']) or isset($_POST['merge'])) and isset($_POST['tags']))
485{
486  $list_name = 'EDIT_TAGS_LIST';
487  if (isset($_POST['duplicate']))
488  {
489    $list_name = 'DUPLIC_TAGS_LIST';
490  }
491  elseif (isset($_POST['merge']))
492  {
493    $list_name = 'MERGE_TAGS_LIST';
494  }
495
496  $template->assign(
497    array(
498      $list_name => implode(',', $_POST['tags']),
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);
508  while ($row = pwg_db_fetch_assoc($result))
509  {
510    $name_of[ $row['id'] ] = $row['name'];
511  }
512
513  foreach ($_POST['tags'] as $tag_id)
514  {
515    $template->append(
516      'tags',
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.