source: trunk/admin/tags.php @ 4325

Last change on this file since 4325 was 4325, checked in by nikrou, 14 years ago

Feature 1244 resolved
Replace all mysql functions in core code by ones independant of database engine

Fix small php code synxtax : hash must be accessed with [ ] and not { }.

  • Property svn:eol-style set to LF
File size: 6.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 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
32// +-----------------------------------------------------------------------+
33// |                                edit tags                              |
34// +-----------------------------------------------------------------------+
35
36if (isset($_POST['submit']) and !is_adviser())
37{
38  $query = '
39SELECT name
40  FROM '.TAGS_TABLE.'
41;';
42  $existing_names = array_from_query($query, 'name');
43 
44
45  $current_name_of = array();
46  $query = '
47SELECT id, name
48  FROM '.TAGS_TABLE.'
49  WHERE id IN ('.$_POST['edit_list'].')
50;';
51  $result = pwg_query($query);
52  while ($row = pwg_db_fetch_assoc($result))
53  {
54    $current_name_of[ $row['id'] ] = $row['name'];
55  }
56 
57  $updates = array();
58  // we must not rename tag with an already existing name
59  foreach (explode(',', $_POST['edit_list']) as $tag_id)
60  {
61    $tag_name = stripslashes($_POST['tag_name-'.$tag_id]);
62
63    if ($tag_name != $current_name_of[$tag_id])
64    {
65      if (in_array($tag_name, $existing_names))
66      {
67        array_push(
68          $page['errors'],
69          sprintf(
70            l10n('Tag "%s" already exists'),
71            $tag_name
72            )
73          );
74      }
75      else if (!empty($tag_name))
76      {
77        array_push(
78          $updates,
79          array(
80            'id' => $tag_id,
81            'name' => addslashes($tag_name),
82            'url_name' => str2url($tag_name),
83            )
84          );
85      }
86    }
87  }
88  mass_updates(
89    TAGS_TABLE,
90    array(
91      'primary' => array('id'),
92      'update' => array('name', 'url_name'),
93      ),
94    $updates
95    );
96}
97
98// +-----------------------------------------------------------------------+
99// |                               delete tags                             |
100// +-----------------------------------------------------------------------+
101
102if (isset($_POST['delete']) and isset($_POST['tags']) and !is_adviser())
103{
104  $query = '
105SELECT name
106  FROM '.TAGS_TABLE.'
107  WHERE id IN ('.implode(',', $_POST['tags']).')
108;';
109  $tag_names = array_from_query($query, 'name');
110 
111  $query = '
112DELETE
113  FROM '.IMAGE_TAG_TABLE.'
114  WHERE tag_id IN ('.implode(',', $_POST['tags']).')
115;';
116  pwg_query($query);
117 
118  $query = '
119DELETE
120  FROM '.TAGS_TABLE.'
121  WHERE id IN ('.implode(',', $_POST['tags']).')
122;';
123  pwg_query($query);
124 
125  array_push(
126    $page['infos'],
127    l10n_dec(
128      'The %d following tag were deleted', 
129      'The %d following tags were deleted',
130      count($tag_names)).' : '.
131      implode(', ', $tag_names)
132    );
133}
134
135// +-----------------------------------------------------------------------+
136// |                               add a tag                               |
137// +-----------------------------------------------------------------------+
138
139if (isset($_POST['add']) and !empty($_POST['add_tag']) and !is_adviser())
140{
141  $tag_name = $_POST['add_tag'];
142
143  // does the tag already exists?
144  $query = '
145SELECT id
146  FROM '.TAGS_TABLE.'
147  WHERE name = \''.$tag_name.'\'
148;';
149  $existing_tags = array_from_query($query, 'id');
150
151  if (count($existing_tags) == 0)
152  {
153    mass_inserts(
154      TAGS_TABLE,
155      array('name', 'url_name'),
156      array(
157        array(
158          'name' => $tag_name,
159          'url_name' => str2url($tag_name),
160          )
161        )
162      );
163   
164    array_push(
165      $page['infos'],
166      sprintf(
167        l10n('Tag "%s" was added'),
168        stripslashes($tag_name)
169        )
170      );
171  }
172  else
173  {
174    array_push(
175      $page['errors'],
176      sprintf(
177        l10n('Tag "%s" already exists'),
178        stripslashes($tag_name)
179        )
180      );
181  }
182}
183
184// +-----------------------------------------------------------------------+
185// |                             template init                             |
186// +-----------------------------------------------------------------------+
187
188$template->set_filenames(array('tags' => 'tags.tpl'));
189
190$template->assign(
191  array(
192    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=tags'
193    )
194  );
195
196// +-----------------------------------------------------------------------+
197// |                             form creation                             |
198// +-----------------------------------------------------------------------+
199
200$template->assign(
201  array(
202    'TAG_SELECTION' => get_html_tag_selection(
203      get_all_tags(),
204      'tags'
205      ),
206    )
207  );
208
209if (isset($_POST['edit']) and isset($_POST['tags']))
210{
211  $template->assign(
212    array(
213      'EDIT_TAGS_LIST' => implode(',', $_POST['tags']),
214      )
215    );
216
217  $query = '
218SELECT id, name
219  FROM '.TAGS_TABLE.'
220  WHERE id IN ('.implode(',', $_POST['tags']).')
221;';
222  $result = pwg_query($query);
223  while ($row = pwg_db_fetch_assoc($result))
224  {
225    $name_of[ $row['id'] ] = $row['name'];
226  }
227
228  foreach ($_POST['tags'] as $tag_id)
229  {
230    $template->append(
231      'tags',
232      array(
233        'ID' => $tag_id,
234        'NAME' => $name_of[$tag_id],
235        )
236      );
237  }
238}
239
240// +-----------------------------------------------------------------------+
241// |                           sending html code                           |
242// +-----------------------------------------------------------------------+
243
244$template->assign_var_from_handle('ADMIN_CONTENT', 'tags');
245
246?>
Note: See TracBrowser for help on using the repository browser.