1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | User Tags - a plugin for Piwigo | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2010-2011 Nicolas Roudaire http://www.nikrou.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | This program is free software; you can redistribute it and/or modify | |
---|
8 | // | it under the terms of the GNU General Public License as published by | |
---|
9 | // | the Free Software Foundation | |
---|
10 | // | | |
---|
11 | // | This program is distributed in the hope that it will be useful, but | |
---|
12 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
13 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
14 | // | General Public License for more details. | |
---|
15 | // | | |
---|
16 | // | You should have received a copy of the GNU General Public License | |
---|
17 | // | along with this program; if not, write to the Free Software | |
---|
18 | // | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
---|
19 | // | MA 02110-1301 USA. | |
---|
20 | // +-----------------------------------------------------------------------+ |
---|
21 | |
---|
22 | if (!defined('PHPWG_ROOT_PATH')) { |
---|
23 | die('Hacking attempt!'); |
---|
24 | } |
---|
25 | |
---|
26 | if (!empty($_GET['action']) && ($_GET['action']=='add') |
---|
27 | && isset($_POST['tags']) && $me->getPermission('add')) { |
---|
28 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
29 | |
---|
30 | if (empty($_POST['tags'])) { |
---|
31 | $_POST['tags'] = array(); |
---|
32 | } |
---|
33 | $tag_ids = __get_tag_ids($_POST['tags']); |
---|
34 | set_tags($tag_ids, $_POST['image_id']); |
---|
35 | |
---|
36 | if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) { |
---|
37 | header("Content-Type: application/json"); |
---|
38 | $message['info'] = l10n('Tags updated'); |
---|
39 | |
---|
40 | echo json_encode($message); |
---|
41 | exit(); |
---|
42 | } else { |
---|
43 | redirect(get_root_url().$_POST['referer']); |
---|
44 | } |
---|
45 | } elseif (!empty($_GET['action']) && $_GET['action']=='get' && $me->getPermission('add')) { |
---|
46 | $query = ' |
---|
47 | SELECT |
---|
48 | id AS tag_id, |
---|
49 | name AS tag_name |
---|
50 | FROM '.TAGS_TABLE; |
---|
51 | |
---|
52 | if (!empty($_GET['q'])) { |
---|
53 | $query .= ' WHERE url_name like \'%'.pwg_db_real_escape_string($_GET['q']).'%\';'; |
---|
54 | } else { |
---|
55 | $query .= ';'; |
---|
56 | } |
---|
57 | header("Content-Type: application/json"); |
---|
58 | echo json_encode(__get_taglist($query)); |
---|
59 | exit(); |
---|
60 | } |
---|
61 | |
---|
62 | /* |
---|
63 | * temporary functions before piwigo 2.3 |
---|
64 | * See admin/include/functions.php in piwigo core |
---|
65 | */ |
---|
66 | function __get_taglist($query) { |
---|
67 | $result = pwg_query($query); |
---|
68 | |
---|
69 | $taglist = array(); |
---|
70 | while ($row = pwg_db_fetch_assoc($result)) { |
---|
71 | $taglist[] = array('name' => $row['tag_name'], |
---|
72 | 'id' => '~~'.$row['tag_id'].'~~' |
---|
73 | ); |
---|
74 | } |
---|
75 | |
---|
76 | $cmp = create_function('$a,$b', 'return strcasecmp($a[\'name\'], $b[\'name\']);'); |
---|
77 | usort($taglist, $cmp); |
---|
78 | |
---|
79 | return $taglist; |
---|
80 | } |
---|
81 | |
---|
82 | function __get_tag_ids($raw_tags) { |
---|
83 | $tag_ids = array(); |
---|
84 | $raw_tags = explode(',',$raw_tags); |
---|
85 | |
---|
86 | foreach ($raw_tags as $raw_tag) { |
---|
87 | if (preg_match('/^~~(\d+)~~$/', $raw_tag, $matches)) { |
---|
88 | $tag_ids[] = $matches[1]; |
---|
89 | } else { |
---|
90 | $tag_ids[] = tag_id_from_tag_name($raw_tag); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | return $tag_ids; |
---|
95 | } |
---|
96 | ?> |
---|