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

Last change on this file since 11741 was 11741, checked in by nikrou, 13 years ago

Make plugin works as expected using webservice.

File size: 4.5 KB
Line 
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
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(),
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 = '
44SELECT
45    id AS tag_id,
46    name AS tag_name
47  FROM '.TAGS_TABLE.'
48;';
49   
50    $tagslist = $this->__makeTagsList($query);
51    unset($tagslist['__associative_tags']);
52    $cmp = create_function('$a,$b', 'return strcasecmp($a[\'name\'], $b[\'name\']);');
53    usort($tagslist, $cmp);
54   
55    return $tagslist;
56  }
57
58  public function updateTags($params, &$service) {
59    if (!$service->isPost()) {
60      return new PwgError(405, "This method requires HTTP POST");
61    }
62
63    if (!t4u_Config::getInstance()->hasPermission('add') && !t4u_Config::getInstance()->hasPermission('delete')) { 
64      return array('error' => l10n('You are not allowed to add nor delete tags'));
65    }
66
67    if (empty($params['tags'])) {
68      $params['tags'] = array();
69    }
70    $message = '';
71
72    $query = '
73SELECT
74    tag_id,
75    name AS tag_name
76  FROM '.IMAGE_TAG_TABLE.' AS it
77    JOIN '.TAGS_TABLE.' AS t ON t.id = it.tag_id
78  WHERE image_id = '.(int) $params['image_id'].'
79;';
80   
81    $current_tags = $this->__makeTagsList($query);
82    $current_tags_ids = array_keys($current_tags['__associative_tags']);
83    if (empty($params['tags'])) {
84      $tags_to_associate = array();
85    } else {
86      $tags_to_associate = explode(',', $params['tags']);
87    }
88
89    $removed_tags = array_diff($current_tags_ids, $tags_to_associate);
90    $new_tags = array_diff($tags_to_associate, $current_tags_ids);
91
92    if (count($removed_tags)>0) {
93      if (!t4u_Config::getInstance()->hasPermission('delete')) { 
94        $message['error'][] = l10n('You are not allowed to delete tags');
95      } else {
96        $message['info'] = l10n('Tags updated');
97      }
98    }
99    if (count($new_tags)>0) {
100      if (!t4u_Config::getInstance()->hasPermission('add')) { 
101        $message['error'][] = l10n('You are not allowed to add tags');
102        $tags_to_associate = array_diff($tags_to_associate, $new_tags);
103      } else {
104        $message['info'] = l10n('Tags updated');
105      }
106    } 
107
108    if (!empty($tags_to_associate)) {
109      $tag_ids = get_tag_ids(implode(',', $tags_to_associate));
110      set_tags($tag_ids, $params['image_id']); 
111    }
112   
113    return $message;
114  }
115
116  private function __makeTagsList($query) {
117    $result = pwg_query($query);
118   
119    $tagslist = array();
120    $associative_tags = array();
121    while ($row = pwg_db_fetch_assoc($result)) {
122      $associative_tags['~~'.$row['tag_id'].'~~'] = $row['tag_name'];
123      $tagslist[] = array('id' => '~~'.$row['tag_id'].'~~',
124                          'name' => $row['tag_name']
125                          );
126     
127    }
128    $tagslist['__associative_tags'] = $associative_tags;
129   
130    return $tagslist;
131  }
132}
Note: See TracBrowser for help on using the repository browser.