source: trunk/admin/tags.php @ 26650

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

change behavior of reset/cancel button on tags manager + reorganize code

  • Property svn:eol-style set to LF
File size: 12.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 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['edit_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        $page['errors'][] = l10n('Tag "%s" already exists', $tag_name);
73      }
74      else if (!empty($tag_name))
75      {
76        $updates[] = array(
77          'id' => $tag_id,
78          'name' => addslashes($tag_name),
79          'url_name' => trigger_event('render_tag_url', $tag_name),
80          );
81      }
82    }
83  }
84  mass_updates(
85    TAGS_TABLE,
86    array(
87      'primary' => array('id'),
88      'update' => array('name', 'url_name'),
89      ),
90    $updates
91    );
92}
93// +-----------------------------------------------------------------------+
94// |                            dulicate tags                              |
95// +-----------------------------------------------------------------------+
96
97if (isset($_POST['duplic_submit']))
98{
99  $query = '
100SELECT name
101  FROM '.TAGS_TABLE.'
102;';
103  $existing_names = array_from_query($query, 'name');
104
105
106  $current_name_of = array();
107  $query = '
108SELECT id, name
109  FROM '.TAGS_TABLE.'
110  WHERE id IN ('.$_POST['edit_list'].')
111;';
112  $result = pwg_query($query);
113  while ($row = pwg_db_fetch_assoc($result))
114  {
115    $current_name_of[ $row['id'] ] = $row['name'];
116  }
117
118  $updates = array();
119  // we must not rename tag with an already existing name
120  foreach (explode(',', $_POST['edit_list']) as $tag_id)
121  {
122    $tag_name = stripslashes($_POST['tag_name-'.$tag_id]);
123
124    if ($tag_name != $current_name_of[$tag_id])
125    {
126      if (in_array($tag_name, $existing_names))
127      {
128        $page['errors'][] = l10n('Tag "%s" already exists', $tag_name);
129      }
130      else if (!empty($tag_name))
131      {
132        single_insert(
133          TAGS_TABLE,
134          array(
135            'name' => $tag_name,
136            'url_name' => trigger_event('render_tag_url', $tag_name),
137            )
138          );
139
140        $query = '
141        SELECT id
142          FROM '.TAGS_TABLE.'
143          WHERE name = \''.$tag_name.'\'
144        ;';
145        $destination_tag = array_from_query($query, 'id');
146        $destination_tag_id = $destination_tag[0];
147
148        $query = '
149        SELECT
150            image_id
151          FROM '.IMAGE_TAG_TABLE.'
152          WHERE tag_id = '.$tag_id.'
153        ;';
154        $destination_tag_image_ids = array_from_query($query, 'image_id');
155
156        $inserts = array();
157        foreach ($destination_tag_image_ids as $image_id)
158        {
159          $inserts[] = array(
160            'tag_id' => $destination_tag_id,
161            'image_id' => $image_id
162            );
163        }
164
165        if (count($inserts) > 0)
166        {
167          mass_inserts(
168            IMAGE_TAG_TABLE,
169            array_keys($inserts[0]),
170            $inserts
171            );
172        }
173       
174        $page['infos'][] = l10n(
175          'Tag "%s" is now a duplicate of "%s"',
176          stripslashes($tag_name),
177          $current_name_of[$tag_id]
178          );
179      }
180    }
181  }
182
183  mass_updates(
184    TAGS_TABLE,
185    array(
186      'primary' => array('id'),
187      'update' => array('name', 'url_name'),
188      ),
189    $updates
190    );
191}
192
193// +-----------------------------------------------------------------------+
194// |                               merge tags                              |
195// +-----------------------------------------------------------------------+
196
197if (isset($_POST['merge_submit']))
198{
199  if (!isset($_POST['destination_tag']))
200  {
201    $page['errors'][] = l10n('No destination tag selected');
202  }
203  else
204  {
205    $destination_tag_id = $_POST['destination_tag'];
206    $tag_ids = explode(',', $_POST['merge_list']);
207
208    if (is_array($tag_ids) and count($tag_ids) > 1)
209    {
210      $name_of_tag = array();
211      $query = '
212SELECT
213    id,
214    name
215  FROM '.TAGS_TABLE.'
216  WHERE id IN ('.implode(',', $tag_ids).')
217;';
218      $result = pwg_query($query);
219      while ($row = pwg_db_fetch_assoc($result))
220      {
221        $name_of_tag[ $row['id'] ] = trigger_event('render_tag_name', $row['name'], $row);
222      }
223
224      $tag_ids_to_delete = array_diff(
225        $tag_ids,
226        array($destination_tag_id)
227        );
228
229      $query = '
230SELECT
231    DISTINCT(image_id)
232  FROM '.IMAGE_TAG_TABLE.'
233  WHERE tag_id IN ('.implode(',', $tag_ids_to_delete).')
234;';
235      $image_ids = array_from_query($query, 'image_id');
236
237      delete_tags($tag_ids_to_delete);
238
239      $query = '
240SELECT
241    image_id
242  FROM '.IMAGE_TAG_TABLE.'
243  WHERE tag_id = '.$destination_tag_id.'
244;';
245      $destination_tag_image_ids = array_from_query($query, 'image_id');
246
247      $image_ids_to_link = array_diff(
248        $image_ids,
249        $destination_tag_image_ids
250        );
251
252      $inserts = array();
253      foreach ($image_ids_to_link as $image_id)
254      {
255        $inserts[] = array(
256          'tag_id' => $destination_tag_id,
257          'image_id' => $image_id
258          );
259      }
260
261      if (count($inserts) > 0)
262      {
263        mass_inserts(
264          IMAGE_TAG_TABLE,
265          array_keys($inserts[0]),
266          $inserts
267          );
268      }
269
270      $tags_deleted = array();
271      foreach ($tag_ids_to_delete as $tag_id)
272      {
273        $tags_deleted[] = $name_of_tag[$tag_id];
274      }
275
276      $page['infos'][] = l10n(
277        'Tags <em>%s</em> merged into tag <em>%s</em>',
278        implode(', ', $tags_deleted),
279        $name_of_tag[$destination_tag_id]
280        );
281    }
282  }
283}
284
285
286// +-----------------------------------------------------------------------+
287// |                               delete tags                             |
288// +-----------------------------------------------------------------------+
289
290if (isset($_POST['delete']) and isset($_POST['tags']))
291{
292  $query = '
293SELECT name
294  FROM '.TAGS_TABLE.'
295  WHERE id IN ('.implode(',', $_POST['tags']).')
296;';
297  $tag_names = array_from_query($query, 'name');
298
299  delete_tags($_POST['tags']);
300
301  $page['infos'][] = l10n_dec(
302    'The following tag was deleted', 'The %d following tags were deleted',
303    count($tag_names)
304    )
305  .' : '.implode(', ', $tag_names);
306}
307
308// +-----------------------------------------------------------------------+
309// |                           delete orphan tags                          |
310// +-----------------------------------------------------------------------+
311
312if (isset($_GET['action']) and 'delete_orphans' == $_GET['action'])
313{
314  check_pwg_token();
315
316  delete_orphan_tags();
317  $_SESSION['page_infos'] = array(l10n('Orphan tags deleted'));
318  redirect(get_root_url().'admin.php?page=tags');
319}
320
321// +-----------------------------------------------------------------------+
322// |                               add a tag                               |
323// +-----------------------------------------------------------------------+
324
325if (isset($_POST['add']) and !empty($_POST['add_tag']))
326{
327  $ret = create_tag($_POST['add_tag']);
328 
329  if (isset($ret['error']))
330  {
331    $page['errors'][] = $ret['error'];
332  }
333  else
334  {
335    $page['infos'][] = $ret['info'];
336  }
337}
338
339// +-----------------------------------------------------------------------+
340// |                             template init                             |
341// +-----------------------------------------------------------------------+
342
343$template->set_filenames(array('tags' => 'tags.tpl'));
344
345$template->assign(
346  array(
347    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=tags',
348    'PWG_TOKEN' => get_pwg_token(),
349    )
350  );
351
352// +-----------------------------------------------------------------------+
353// |                              orphan tags                              |
354// +-----------------------------------------------------------------------+
355
356$orphan_tags = get_orphan_tags();
357
358$orphan_tag_names = array();
359foreach ($orphan_tags as $tag)
360{
361  $orphan_tag_names[] = trigger_event('render_tag_name', $tag['name'], $tag);
362}
363
364if (count($orphan_tag_names) > 0)
365{
366  $page['warnings'][] = sprintf(
367    l10n('You have %d orphan tags: %s.').' <a href="%s">'.l10n('Delete orphan tags').'</a>',
368    count($orphan_tag_names),
369    implode(', ', $orphan_tag_names),
370    get_root_url().'admin.php?page=tags&amp;action=delete_orphans&amp;pwg_token='.get_pwg_token()
371    );
372}
373
374// +-----------------------------------------------------------------------+
375// |                             form creation                             |
376// +-----------------------------------------------------------------------+
377
378
379// tag counters
380$query = '
381SELECT tag_id, COUNT(image_id) AS counter
382  FROM '.IMAGE_TAG_TABLE.'
383  GROUP BY tag_id';
384$tag_counters = simple_hash_from_query($query, 'tag_id', 'counter');
385
386// all tags
387$query = '
388SELECT *
389  FROM '.TAGS_TABLE.'
390;';
391$result = pwg_query($query);
392$all_tags = array();
393while ($tag = pwg_db_fetch_assoc($result))
394{
395  $raw_name = $tag['name'];
396  $tag['name'] = trigger_event('render_tag_name', $raw_name, $tag);
397  $tag['counter'] = intval(@$tag_counters[ $tag['id'] ]);
398  $tag['U_VIEW'] = make_index_url(array('tags'=>array($tag)));
399  $tag['U_EDIT'] = 'admin.php?page=batch_manager&amp;filter=tag-'.$tag['id'];
400
401  $alt_names = trigger_event('get_tag_alt_names', array(), $raw_name);
402  $alt_names = array_diff( array_unique($alt_names), array($tag['name']) );
403  if (count($alt_names))
404  {
405    $tag['alt_names'] = implode(', ', $alt_names);
406  }
407  $all_tags[] = $tag;
408}
409usort($all_tags, 'tag_alpha_compare');
410
411
412
413$template->assign(
414  array(
415    'all_tags' => $all_tags,
416    )
417  );
418
419if ((isset($_POST['edit']) or isset($_POST['duplicate']) or isset($_POST['merge'])) and isset($_POST['tags']))
420{
421  $list_name = 'EDIT_TAGS_LIST';
422  if (isset($_POST['duplicate']))
423  {
424    $list_name = 'DUPLIC_TAGS_LIST';
425  }
426  elseif (isset($_POST['merge']))
427  {
428    $list_name = 'MERGE_TAGS_LIST';
429  }
430
431  $template->assign($list_name, implode(',', $_POST['tags']));
432
433  $query = '
434SELECT id, name
435  FROM '.TAGS_TABLE.'
436  WHERE id IN ('.implode(',', $_POST['tags']).')
437;';
438  $result = pwg_query($query);
439  while ($row = pwg_db_fetch_assoc($result))
440  {
441    $template->append(
442      'tags',
443      array(
444        'ID' => $row['id'],
445        'NAME' => $row['name'],
446        )
447      );
448  }
449}
450
451// +-----------------------------------------------------------------------+
452// |                           sending html code                           |
453// +-----------------------------------------------------------------------+
454
455$template->assign_var_from_handle('ADMIN_CONTENT', 'tags');
456
457?>
Note: See TracBrowser for help on using the repository browser.