1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2012 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 | |
---|
24 | if( !defined("PHPWG_ROOT_PATH") ) |
---|
25 | { |
---|
26 | die ("Hacking attempt!"); |
---|
27 | } |
---|
28 | |
---|
29 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
30 | check_status(ACCESS_ADMINISTRATOR); |
---|
31 | |
---|
32 | if (!empty($_POST)) |
---|
33 | { |
---|
34 | check_pwg_token(); |
---|
35 | } |
---|
36 | |
---|
37 | // +-----------------------------------------------------------------------+ |
---|
38 | // | edit tags | |
---|
39 | // +-----------------------------------------------------------------------+ |
---|
40 | |
---|
41 | if (isset($_POST['submit'])) |
---|
42 | { |
---|
43 | $query = ' |
---|
44 | SELECT name |
---|
45 | FROM '.TAGS_TABLE.' |
---|
46 | ;'; |
---|
47 | $existing_names = array_from_query($query, 'name'); |
---|
48 | |
---|
49 | |
---|
50 | $current_name_of = array(); |
---|
51 | $query = ' |
---|
52 | SELECT 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 | sprintf( |
---|
75 | l10n('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 | |
---|
106 | if (isset($_POST['duplic_submit'])) |
---|
107 | { |
---|
108 | $query = ' |
---|
109 | SELECT name |
---|
110 | FROM '.TAGS_TABLE.' |
---|
111 | ;'; |
---|
112 | $existing_names = array_from_query($query, 'name'); |
---|
113 | |
---|
114 | |
---|
115 | $current_name_of = array(); |
---|
116 | $query = ' |
---|
117 | SELECT 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 | sprintf( |
---|
140 | l10n('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 | sprintf( |
---|
194 | l10n('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 | |
---|
216 | if (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 = ' |
---|
234 | SELECT |
---|
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 = ' |
---|
252 | SELECT |
---|
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 = ' |
---|
262 | SELECT |
---|
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 | sprintf( |
---|
304 | l10n('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 | |
---|
318 | if (isset($_POST['delete']) and isset($_POST['tags'])) |
---|
319 | { |
---|
320 | $query = ' |
---|
321 | SELECT 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 | |
---|
343 | if (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 | |
---|
356 | if (isset($_POST['add']) and !empty($_POST['add_tag'])) |
---|
357 | { |
---|
358 | $tag_name = $_POST['add_tag']; |
---|
359 | |
---|
360 | // does the tag already exists? |
---|
361 | $query = ' |
---|
362 | SELECT 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 | sprintf( |
---|
384 | l10n('Tag "%s" was added'), |
---|
385 | stripslashes($tag_name) |
---|
386 | ) |
---|
387 | ); |
---|
388 | } |
---|
389 | else |
---|
390 | { |
---|
391 | array_push( |
---|
392 | $page['errors'], |
---|
393 | sprintf( |
---|
394 | l10n('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(); |
---|
421 | foreach ($orphan_tags as $tag) |
---|
422 | { |
---|
423 | array_push($orphan_tag_names, trigger_event('render_tag_name', $tag['name'])); |
---|
424 | } |
---|
425 | |
---|
426 | if (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&action=delete_orphans&pwg_token='.get_pwg_token() |
---|
435 | ) |
---|
436 | ); |
---|
437 | } |
---|
438 | |
---|
439 | // +-----------------------------------------------------------------------+ |
---|
440 | // | form creation | |
---|
441 | // +-----------------------------------------------------------------------+ |
---|
442 | |
---|
443 | $template->assign( |
---|
444 | array( |
---|
445 | 'TAG_SELECTION' => get_html_tag_selection( |
---|
446 | get_all_tags(), |
---|
447 | 'tags' |
---|
448 | ), |
---|
449 | ) |
---|
450 | ); |
---|
451 | |
---|
452 | if ((isset($_POST['edit']) or isset($_POST['duplicate']) or isset($_POST['merge'])) and isset($_POST['tags'])) |
---|
453 | { |
---|
454 | $list_name = 'EDIT_TAGS_LIST'; |
---|
455 | if (isset($_POST['duplicate'])) |
---|
456 | { |
---|
457 | $list_name = 'DUPLIC_TAGS_LIST'; |
---|
458 | } |
---|
459 | elseif (isset($_POST['merge'])) |
---|
460 | { |
---|
461 | $list_name = 'MERGE_TAGS_LIST'; |
---|
462 | } |
---|
463 | |
---|
464 | $template->assign( |
---|
465 | array( |
---|
466 | $list_name => implode(',', $_POST['tags']), |
---|
467 | ) |
---|
468 | ); |
---|
469 | |
---|
470 | $query = ' |
---|
471 | SELECT id, name |
---|
472 | FROM '.TAGS_TABLE.' |
---|
473 | WHERE id IN ('.implode(',', $_POST['tags']).') |
---|
474 | ;'; |
---|
475 | $result = pwg_query($query); |
---|
476 | while ($row = pwg_db_fetch_assoc($result)) |
---|
477 | { |
---|
478 | $name_of[ $row['id'] ] = $row['name']; |
---|
479 | } |
---|
480 | |
---|
481 | foreach ($_POST['tags'] as $tag_id) |
---|
482 | { |
---|
483 | $template->append( |
---|
484 | 'tags', |
---|
485 | array( |
---|
486 | 'ID' => $tag_id, |
---|
487 | 'NAME' => $name_of[$tag_id], |
---|
488 | ) |
---|
489 | ); |
---|
490 | } |
---|
491 | } |
---|
492 | |
---|
493 | // +-----------------------------------------------------------------------+ |
---|
494 | // | sending html code | |
---|
495 | // +-----------------------------------------------------------------------+ |
---|
496 | |
---|
497 | $template->assign_var_from_handle('ADMIN_CONTENT', 'tags'); |
---|
498 | |
---|
499 | ?> |
---|