source: extensions/user_tags/include/t4u_ws.class.php @ 20251

Last change on this file since 20251 was 20251, checked in by nikrou, 11 years ago

Mise à jour année du copyright

File size: 4.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | User Tags  - a plugin for Piwigo                                      |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2010-2013 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 version 2 as     |
9// | published by 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
22include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
23
24class t4u_Ws
25{
26  public function addMethods($arr) {
27    $service = &$arr[0];
28
29    $service->addMethod(T4U_WS.'list', array($this, 'tagsList'), 
30                        array('q' => array()),
31                        'retrieves a list of tags than can be filtered'
32                        );
33
34    $service->addMethod(T4U_WS.'update', array($this, 'updateTags'), 
35                        array('image_id' => array(),
36                              'tags' => array('default' => array())
37                              ),
38                        'Updates (add or remove) tags associated to an image (POST method only)'
39                        );
40  }
41
42  public function tagsList($params, &$service) {
43    $query = 'SELECT id AS tag_id, name AS tag_name FROM '.TAGS_TABLE;
44    if (!empty($params['q'])) {
45      $query .= sprintf(' WHERE name like \'%%%s%%\'', $params['q']);
46    }
47
48    $tagslist = $this->__makeTagsList($query);
49    unset($tagslist['__associative_tags']);
50    $cmp = create_function('$a,$b', 'return strcasecmp($a[\'name\'], $b[\'name\']);');
51    usort($tagslist, $cmp);
52   
53    return $tagslist;
54  }
55
56  public function updateTags($params, &$service) {
57    if (!$service->isPost()) {
58      return new PwgError(405, "This method requires HTTP POST");
59    }
60
61    if (!t4u_Config::getInstance()->hasPermission('add') && !t4u_Config::getInstance()->hasPermission('delete')) { 
62      return array('error' => l10n('You are not allowed to add nor delete tags'));
63    }
64
65    if (empty($params['tags'])) {
66      $params['tags'] = array();
67    }
68    $message = '';
69
70    $query = '
71SELECT
72    tag_id,
73    name AS tag_name
74  FROM '.IMAGE_TAG_TABLE.' AS it
75    JOIN '.TAGS_TABLE.' AS t ON t.id = it.tag_id
76  WHERE image_id = '.(int) $params['image_id'].'
77;';
78   
79    $current_tags = $this->__makeTagsList($query);
80    $current_tags_ids = array_keys($current_tags['__associative_tags']);
81    if (empty($params['tags'])) {
82      $tags_to_associate = array();
83    } else {
84      $tags_to_associate = explode(',', $params['tags']);
85    }
86
87    $removed_tags = array_diff($current_tags_ids, $tags_to_associate);
88    $new_tags = array_diff($tags_to_associate, $current_tags_ids);
89
90    if (count($removed_tags)>0) {
91      if (!t4u_Config::getInstance()->hasPermission('delete')) { 
92        $message['error'][] = l10n('You are not allowed to delete tags');
93      } else {
94        $message['info'] = l10n('Tags updated');
95      }
96    }
97    if (count($new_tags)>0) {
98      if (!t4u_Config::getInstance()->hasPermission('add')) { 
99        $message['error'][] = l10n('You are not allowed to add tags');
100        $tags_to_associate = array_diff($tags_to_associate, $new_tags);
101      } else {
102        $message['info'] = l10n('Tags updated');
103      }
104    } 
105
106    if (empty($message['error'])) {
107      if (empty($tags_to_associate)) { // remove all tags for an image
108        $query = 'DELETE FROM '.IMAGE_TAG_TABLE;
109        $query .= sprintf(' WHERE image_id = %d', $params['image_id']);
110        pwg_query($query);
111      } else {
112        $tag_ids = get_tag_ids(implode(',', $tags_to_associate));
113        set_tags($tag_ids, $params['image_id']); 
114      }
115    }
116   
117    return $message;
118  }
119
120  private function __makeTagsList($query) {
121    $result = pwg_query($query);
122   
123    $tagslist = array();
124    $associative_tags = array();
125    while ($row = pwg_db_fetch_assoc($result)) {
126      $associative_tags['~~'.$row['tag_id'].'~~'] = $row['tag_name'];
127      $tagslist[] = array('id' => '~~'.$row['tag_id'].'~~',
128                          'name' => $row['tag_name']
129                          );
130     
131    }
132    $tagslist['__associative_tags'] = $associative_tags;
133   
134    return $tagslist;
135  }
136}
Note: See TracBrowser for help on using the repository browser.