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
RevLine 
[1119]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[26461]5// | Copyright(C) 2008-2014 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
[26650]41if (isset($_POST['edit_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      {
[25018]72        $page['errors'][] = l10n('Tag "%s" already exists', $tag_name);
[1119]73      }
74      else if (!empty($tag_name))
75      {
[25018]76        $updates[] = array(
77          'id' => $tag_id,
78          'name' => addslashes($tag_name),
79          'url_name' => trigger_event('render_tag_url', $tag_name),
[1119]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}
[16526]93// +-----------------------------------------------------------------------+
94// |                            dulicate tags                              |
95// +-----------------------------------------------------------------------+
[1119]96
[16526]97if (isset($_POST['duplic_submit']))
98{
99  $query = '
100SELECT name
101  FROM '.TAGS_TABLE.'
102;';
103  $existing_names = array_from_query($query, 'name');
104
[17765]105
[16526]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  }
[17765]117
[16526]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      {
[25018]128        $page['errors'][] = l10n('Tag "%s" already exists', $tag_name);
[16526]129      }
130      else if (!empty($tag_name))
131      {
[25019]132        single_insert(
[16526]133          TAGS_TABLE,
134          array(
[25019]135            'name' => $tag_name,
136            'url_name' => trigger_event('render_tag_url', $tag_name),
[16526]137            )
138          );
[25019]139
[16526]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];
[25019]147
[16526]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');
[25019]155
[16526]156        $inserts = array();
157        foreach ($destination_tag_image_ids as $image_id)
158        {
[25018]159          $inserts[] = array(
160            'tag_id' => $destination_tag_id,
161            'image_id' => $image_id
[16526]162            );
163        }
[17765]164
[16526]165        if (count($inserts) > 0)
166        {
167          mass_inserts(
168            IMAGE_TAG_TABLE,
169            array_keys($inserts[0]),
170            $inserts
171            );
172        }
[25018]173       
174        $page['infos'][] = l10n(
175          'Tag "%s" is now a duplicate of "%s"',
176          stripslashes($tag_name),
177          $current_name_of[$tag_id]
[16526]178          );
179      }
180    }
181  }
[25019]182
[16526]183  mass_updates(
184    TAGS_TABLE,
185    array(
186      'primary' => array('id'),
187      'update' => array('name', 'url_name'),
188      ),
189    $updates
190    );
191}
192
[1119]193// +-----------------------------------------------------------------------+
[12032]194// |                               merge tags                              |
195// +-----------------------------------------------------------------------+
196
[26650]197if (isset($_POST['merge_submit']))
[12032]198{
199  if (!isset($_POST['destination_tag']))
200  {
[25018]201    $page['errors'][] = l10n('No destination tag selected');
[12032]202  }
203  else
204  {
205    $destination_tag_id = $_POST['destination_tag'];
206    $tag_ids = explode(',', $_POST['merge_list']);
[17765]207
[12032]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      {
[26649]221        $name_of_tag[ $row['id'] ] = trigger_event('render_tag_name', $row['name'], $row);
[12032]222      }
[17765]223
[12032]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      {
[25018]255        $inserts[] = array(
256          'tag_id' => $destination_tag_id,
257          'image_id' => $image_id
[12032]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      }
[17765]275
[25018]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]
[12032]280        );
281    }
282  }
283}
284
285
286// +-----------------------------------------------------------------------+
[1119]287// |                               delete tags                             |
288// +-----------------------------------------------------------------------+
289
[8126]290if (isset($_POST['delete']) and isset($_POST['tags']))
[1119]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');
[12032]298
299  delete_tags($_POST['tags']);
[17765]300
[25018]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);
[1119]306}
307
308// +-----------------------------------------------------------------------+
[8762]309// |                           delete orphan tags                          |
310// +-----------------------------------------------------------------------+
311
312if (isset($_GET['action']) and 'delete_orphans' == $_GET['action'])
313{
314  check_pwg_token();
[17765]315
[8762]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// +-----------------------------------------------------------------------+
[1119]322// |                               add a tag                               |
323// +-----------------------------------------------------------------------+
324
[8126]325if (isset($_POST['add']) and !empty($_POST['add_tag']))
[1119]326{
[25019]327  $ret = create_tag($_POST['add_tag']);
328 
329  if (isset($ret['error']))
[1119]330  {
[25019]331    $page['errors'][] = $ret['error'];
[1119]332  }
333  else
334  {
[25019]335    $page['infos'][] = $ret['info'];
[1119]336  }
337}
338
339// +-----------------------------------------------------------------------+
340// |                             template init                             |
341// +-----------------------------------------------------------------------+
342
[2530]343$template->set_filenames(array('tags' => 'tags.tpl'));
[1119]344
[2277]345$template->assign(
[1119]346  array(
[5195]347    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=tags',
348    'PWG_TOKEN' => get_pwg_token(),
[1119]349    )
350  );
351
352// +-----------------------------------------------------------------------+
[8762]353// |                              orphan tags                              |
354// +-----------------------------------------------------------------------+
355
356$orphan_tags = get_orphan_tags();
357
358$orphan_tag_names = array();
359foreach ($orphan_tags as $tag)
360{
[26649]361  $orphan_tag_names[] = trigger_event('render_tag_name', $tag['name'], $tag);
[8762]362}
363
364if (count($orphan_tag_names) > 0)
365{
[25018]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()
[8762]371    );
372}
373
374// +-----------------------------------------------------------------------+
[1119]375// |                             form creation                             |
376// +-----------------------------------------------------------------------+
377
[17765]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'];
[26649]396  $tag['name'] = trigger_event('render_tag_name', $raw_name, $tag);
[17765]397  $tag['counter'] = intval(@$tag_counters[ $tag['id'] ]);
398  $tag['U_VIEW'] = make_index_url(array('tags'=>array($tag)));
[24834]399  $tag['U_EDIT'] = 'admin.php?page=batch_manager&amp;filter=tag-'.$tag['id'];
[17765]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
[2277]413$template->assign(
[1119]414  array(
[17765]415    'all_tags' => $all_tags,
[1119]416    )
417  );
418
[16526]419if ((isset($_POST['edit']) or isset($_POST['duplicate']) or isset($_POST['merge'])) and isset($_POST['tags']))
[1119]420{
[12032]421  $list_name = 'EDIT_TAGS_LIST';
[16526]422  if (isset($_POST['duplicate']))
[12032]423  {
[16526]424    $list_name = 'DUPLIC_TAGS_LIST';
425  }
426  elseif (isset($_POST['merge']))
427  {
[12032]428    $list_name = 'MERGE_TAGS_LIST';
429  }
[17765]430
[26650]431  $template->assign($list_name, implode(',', $_POST['tags']));
[1119]432
433  $query = '
434SELECT id, name
435  FROM '.TAGS_TABLE.'
436  WHERE id IN ('.implode(',', $_POST['tags']).')
437;';
438  $result = pwg_query($query);
[4325]439  while ($row = pwg_db_fetch_assoc($result))
[1119]440  {
[2277]441    $template->append(
442      'tags',
[1119]443      array(
[26650]444        'ID' => $row['id'],
445        'NAME' => $row['name'],
[1119]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.