source: trunk/admin/tags.php @ 1119

Last change on this file since 1119 was 1119, checked in by plg, 18 years ago

improvement: tags replace keywords. Better data model, less
limitations. Each image can be associated to as many tag as needed. Tags can
contain non ASCII characters. Oriented navigation with tags by association.

File size: 7.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2006-03-09 23:46:28 +0100 (jeu, 09 mar 2006) $
10// | last modifier : $Author: rub $
11// | revision      : $Revision: 1072 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28if( !defined("PHPWG_ROOT_PATH") )
29{
30  die ("Hacking attempt!");
31}
32
33include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
34check_status(ACCESS_ADMINISTRATOR);
35
36// +-----------------------------------------------------------------------+
37// |                                edit tags                              |
38// +-----------------------------------------------------------------------+
39
40if (isset($_POST['submit']))
41{
42  $query = '
43SELECT name
44  FROM '.TAGS_TABLE.'
45;';
46  $existing_names = array_from_query($query, 'name');
47 
48
49  $current_name_of = array();
50  $query = '
51SELECT id, name
52  FROM '.TAGS_TABLE.'
53  WHERE id IN ('.$_POST['edit_list'].')
54;';
55  $result = pwg_query($query);
56  while ($row = mysql_fetch_array($result))
57  {
58    $current_name_of[ $row['id'] ] = $row['name'];
59  }
60 
61  $updates = array();
62  // we must not rename tag with an already existing name
63  foreach (explode(',', $_POST['edit_list']) as $tag_id)
64  {
65    if (function_exists('mysql_real_escape_string'))
66    {
67      $tag_name = mysql_real_escape_string($_POST['tag_name-'.$tag_id]);
68    }
69    else
70    {
71      $tag_name = mysql_escape_string($_POST['tag_name-'.$tag_id]);
72    }
73
74    if ($tag_name != $current_name_of[$tag_id])
75    {
76      if (in_array($tag_name, $existing_names))
77      {
78        array_push(
79          $page['errors'],
80          sprintf(
81            l10n('Tag "%s" already exist'),
82            $tag_name
83            )
84          );
85      }
86      else if (!empty($tag_name))
87      {
88        array_push(
89          $updates,
90          array(
91            'id' => $tag_id,
92            'name' => $tag_name,
93            'url_name' => str2url($tag_name),
94            )
95          );
96      }
97    }
98  }
99  mass_updates(
100    TAGS_TABLE,
101    array(
102      'primary' => array('id'),
103      'update' => array('name', 'url_name'),
104      ),
105    $updates
106    );
107}
108
109// +-----------------------------------------------------------------------+
110// |                               delete tags                             |
111// +-----------------------------------------------------------------------+
112
113if (isset($_POST['delete']) and isset($_POST['tags']))
114{
115  $query = '
116SELECT name
117  FROM '.TAGS_TABLE.'
118  WHERE id IN ('.implode(',', $_POST['tags']).')
119;';
120  $tag_names = array_from_query($query, 'name');
121 
122  $query = '
123DELETE
124  FROM '.IMAGE_TAG_TABLE.'
125  WHERE tag_id IN ('.implode(',', $_POST['tags']).')
126;';
127  pwg_query($query);
128 
129  $query = '
130DELETE
131  FROM '.TAGS_TABLE.'
132  WHERE id IN ('.implode(',', $_POST['tags']).')
133;';
134  pwg_query($query);
135 
136  array_push(
137    $page['infos'],
138    sprintf(
139      l10n('The %d following tags were deleted : %s'),
140      count($tag_names),
141      implode(', ', $tag_names)
142      )
143    );
144}
145
146// +-----------------------------------------------------------------------+
147// |                               add a tag                               |
148// +-----------------------------------------------------------------------+
149
150if (isset($_POST['add_tag']) and !empty($_POST['add_tag']))
151{
152  if (function_exists('mysql_real_escape_string'))
153  {
154    $tag_name = mysql_real_escape_string($_POST['add_tag']);
155  }
156  else
157  {
158    $tag_name = mysql_escape_string($_POST['add_tag']);
159  }
160
161  // does the tag already exist?
162  $query = '
163SELECT id
164  FROM '.TAGS_TABLE.'
165  WHERE name = \''.$tag_name.'\'
166;';
167  $existing_tags = array_from_query($query, 'id');
168
169  if (count($existing_tags) == 0)
170  {
171    mass_inserts(
172      TAGS_TABLE,
173      array('name', 'url_name'),
174      array(
175        array(
176          'name' => $tag_name,
177          'url_name' => str2url($tag_name),
178          )
179        )
180      );
181   
182    array_push(
183      $page['infos'],
184      sprintf(
185        l10n('Tag "%s" was added'),
186        $tag_name
187        )
188      );
189  }
190  else
191  {
192    array_push(
193      $page['errors'],
194      sprintf(
195        l10n('Tag "%s" already exist'),
196        $tag_name
197        )
198      );
199  }
200}
201
202// +-----------------------------------------------------------------------+
203// |                             template init                             |
204// +-----------------------------------------------------------------------+
205
206$template->set_filenames(array('tags' => 'admin/tags.tpl'));
207
208$template->assign_vars(
209  array(
210    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=tags'
211    )
212  );
213
214// +-----------------------------------------------------------------------+
215// |                             form creation                             |
216// +-----------------------------------------------------------------------+
217
218$template->assign_vars(
219  array(
220    'TAG_SELECTION' => get_html_tag_selection(
221      get_all_tags(),
222      'tags'
223      ),
224    )
225  );
226
227if (isset($_POST['edit']) and isset($_POST['tags']))
228{
229  $template->assign_block_vars(
230    'edit_tags',
231    array(
232      'LIST' => implode(',', $_POST['tags']),
233      )
234    );
235
236  $query = '
237SELECT id, name
238  FROM '.TAGS_TABLE.'
239  WHERE id IN ('.implode(',', $_POST['tags']).')
240;';
241  $result = pwg_query($query);
242  while ($row = mysql_fetch_array($result))
243  {
244    $name_of[ $row['id'] ] = $row['name'];
245  }
246
247  foreach ($_POST['tags'] as $tag_id)
248  {
249    $template->assign_block_vars(
250      'edit_tags.tag',
251      array(
252        'ID' => $tag_id,
253        'NAME' => $name_of[$tag_id],
254        )
255      );
256  }
257}
258
259// +-----------------------------------------------------------------------+
260// |                           sending html code                           |
261// +-----------------------------------------------------------------------+
262
263$template->assign_var_from_handle('ADMIN_CONTENT', 'tags');
264
265?>
Note: See TracBrowser for help on using the repository browser.