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

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

Fix issue. Search for tag with capitalize letter didn't work.

(Thanks one more time to Tim Mohr for reporting issue)

File size: 5.2 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 LOWER(name) like \'%%%s%%\'', strtolower(pwg_db_real_escape_string($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 = 'SELECT tag_id, name AS tag_name';
71    $query .= ' FROM '.IMAGE_TAG_TABLE.' AS it';
72    $query .= ' JOIN '.TAGS_TABLE.' AS t ON t.id = it.tag_id';
73    $query .= sprintf(' WHERE image_id = %s', pwg_db_real_escape_string($params['image_id']));
74   
75    $current_tags = $this->__makeTagsList($query);
76    $current_tags_ids = array_keys($current_tags['__associative_tags']);
77    if (empty($params['tags'])) {
78      $tags_to_associate = array();
79    } else {
80      $tags_to_associate = explode(',', $params['tags']);
81    }
82
83    $removed_tags = array_diff($current_tags_ids, $tags_to_associate);
84    $new_tags = array_diff($tags_to_associate, $current_tags_ids);
85
86    if (count($removed_tags)>0) {
87      if (!t4u_Config::getInstance()->hasPermission('delete')) { 
88        $message['error'][] = l10n('You are not allowed to delete tags');
89      } else {
90        $message['info'] = l10n('Tags updated');
91      }
92    }
93    if (count($new_tags)>0) {
94      if (!t4u_Config::getInstance()->hasPermission('add')) { 
95        $message['error'][] = l10n('You are not allowed to add tags');
96        $tags_to_associate = array_diff($tags_to_associate, $new_tags);
97      } else {
98        $message['info'] = l10n('Tags updated');
99      }
100    } 
101
102    if (empty($message['error'])) {
103      if (empty($tags_to_associate)) { // remove all tags for an image
104        $query = 'DELETE FROM '.IMAGE_TAG_TABLE;
105        $query .= sprintf(' WHERE image_id = %d', pwg_db_real_escape_string($params['image_id']));
106        pwg_query($query);
107      } else {
108        $tag_ids = get_tag_ids(implode(',', $tags_to_associate));
109        set_tags($tag_ids, $params['image_id']); 
110      }
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.