source: trunk/include/ws_functions/pwg.groups.php @ 25975

Last change on this file since 25975 was 25975, checked in by rvelices, 11 years ago

invalidate_user_cache fix in web service method + do not invalidate user cache on every page hit on album list, photo etc ...

File size: 7.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2013 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/**
25 * API method
26 * Returns the list of groups
27 * @param mixed[] $params
28 *    @option int[] group_id (optional)
29 *    @option string name (optional)
30 */
31function ws_groups_getList($params, &$service)
32{
33  $where_clauses = array('1=1');
34
35  if (!empty($params['name']))
36  {
37    $where_clauses[] = 'LOWER(name) LIKE \''. pwg_db_real_escape_string($params['name']) .'\'';
38  }
39
40  if (!empty($params['group_id']))
41  {
42    $where_clauses[] = 'id IN('. implode(',', $params['group_id']) .')';
43  }
44
45  $query = '
46SELECT
47    g.*, COUNT(user_id) AS nb_users
48  FROM '. GROUPS_TABLE .' AS g
49    LEFT JOIN '. USER_GROUP_TABLE .' AS ug
50    ON ug.group_id = g.id
51  WHERE '. implode(' AND ', $where_clauses) .'
52  GROUP BY id
53  ORDER BY '. $params['order'] .'
54  LIMIT '. $params['per_page'] .'
55  OFFSET '. ($params['per_page']*$params['page']) .'
56;';
57
58  $groups = array_from_query($query);
59
60  return array(
61    'paging' => new PwgNamedStruct(array(
62      'page' => $params['page'],
63      'per_page' => $params['per_page'],
64      'count' => count($groups)
65      )),
66    'groups' => new PwgNamedArray($groups, 'group')
67    );
68}
69
70/**
71 * API method
72 * Adds a group
73 * @param mixed[] $params
74 *    @option string name
75 *    @option bool is_default
76 */
77function ws_groups_add($params, &$service)
78{
79  $params['name'] = pwg_db_real_escape_string($params['name']);
80
81  // is the name not already used ?
82  $query = '
83SELECT COUNT(*)
84  FROM '.GROUPS_TABLE.'
85  WHERE name = \''.$params['name'].'\'
86;';
87  list($count) = pwg_db_fetch_row(pwg_query($query));
88  if ($count != 0)
89  {
90    return new PwgError(WS_ERR_INVALID_PARAM, 'This name is already used by another group.');
91  }
92
93  // creating the group
94  single_insert(
95    GROUPS_TABLE,
96    array(
97      'name' => $params['name'],
98      'is_default' => boolean_to_string($params['is_default']),
99      )
100    );
101
102  return $service->invoke('pwg.groups.getList', array('group_id' => pwg_db_insert_id()));
103}
104
105/**
106 * API method
107 * Deletes a group
108 * @param mixed[] $params
109 *    @option int[] group_id
110 *    @option string pwg_token
111 */
112function ws_groups_delete($params, &$service)
113{
114  if (get_pwg_token() != $params['pwg_token'])
115  {
116    return new PwgError(403, 'Invalid security token');
117  }
118
119  $group_id_string = implode(',', $params['group_id']);
120
121  // destruction of the access linked to the group
122  $query = '
123DELETE
124  FROM '. GROUP_ACCESS_TABLE .'
125  WHERE group_id IN('. $group_id_string  .')
126;';
127  pwg_query($query);
128
129  // destruction of the users links for this group
130  $query = '
131DELETE
132  FROM '. USER_GROUP_TABLE .'
133  WHERE group_id IN('. $group_id_string  .')
134;';
135  pwg_query($query);
136
137  $query = '
138SELECT name
139  FROM '. GROUPS_TABLE .'
140  WHERE id IN('. $group_id_string  .')
141;';
142  $groupnames = array_from_query($query, 'name');
143
144  // destruction of the group
145  $query = '
146DELETE
147  FROM '. GROUPS_TABLE .'
148  WHERE id IN('. $group_id_string  .')
149;';
150  pwg_query($query);
151
152  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
153  invalidate_user_cache();
154
155  return new PwgNamedArray($groupnames, 'group_deleted');
156}
157
158/**
159 * API method
160 * Updates a group
161 * @param mixed[] $params
162 *    @option int group_id
163 *    @option string name (optional)
164 *    @option bool is_default (optional)
165 */
166function ws_groups_setInfo($params, &$service)
167{
168  $updates = array();
169
170  // does the group exist ?
171  $query = '
172SELECT COUNT(*)
173  FROM '. GROUPS_TABLE .'
174  WHERE id = '. $params['group_id'] .'
175;';
176  list($count) = pwg_db_fetch_row(pwg_query($query));
177  if ($count == 0)
178  {
179    return new PwgError(WS_ERR_INVALID_PARAM, 'This group does not exist.');
180  }
181
182  if (!empty($params['name']))
183  {
184    $params['name'] = pwg_db_real_escape_string($params['name']);
185
186    // is the name not already used ?
187    $query = '
188SELECT COUNT(*)
189  FROM '. GROUPS_TABLE .'
190  WHERE name = \''. $params['name'] .'\'
191;';
192    list($count) = pwg_db_fetch_row(pwg_query($query));
193    if ($count != 0)
194    {
195      return new PwgError(WS_ERR_INVALID_PARAM, 'This name is already used by another group.');
196    }
197
198    $updates['name'] = $params['name'];
199  }
200
201  if (!empty($params['is_default']) or @$params['is_default']===false)
202  {
203    $updates['is_default'] = boolean_to_string($params['is_default']);
204  }
205
206  single_update(
207    GROUPS_TABLE,
208    $updates,
209    array('id' => $params['group_id'])
210    );
211
212  return $service->invoke('pwg.groups.getList', array('group_id' => $params['group_id']));
213}
214
215/**
216 * API method
217 * Adds user(s) to a group
218 * @param mixed[] $params
219 *    @option int group_id
220 *    @option int[] user_id
221 */
222function ws_groups_addUser($params, &$service)
223{
224  // does the group exist ?
225  $query = '
226SELECT COUNT(*)
227  FROM '. GROUPS_TABLE .'
228  WHERE id = '. $params['group_id'] .'
229;';
230  list($count) = pwg_db_fetch_row(pwg_query($query));
231  if ($count == 0)
232  {
233    return new PwgError(WS_ERR_INVALID_PARAM, 'This group does not exist.');
234  }
235
236  $inserts = array();
237  foreach ($params['user_id'] as $user_id)
238  {
239    $inserts[] = array(
240      'group_id' => $params['group_id'],
241      'user_id' => $user_id,
242      );
243  }
244
245  mass_inserts(
246    USER_GROUP_TABLE,
247    array('group_id', 'user_id'),
248    $inserts,
249    array('ignore'=>true)
250    );
251
252  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
253  invalidate_user_cache();
254
255  return $service->invoke('pwg.groups.getList', array('group_id' => $params['group_id']));
256}
257
258/**
259 * API method
260 * Removes user(s) from a group
261 * @param mixed[] $params
262 *    @option int group_id
263 *    @option int[] user_id
264 */
265function ws_groups_deleteUser($params, &$service)
266{
267  // does the group exist ?
268  $query = '
269SELECT COUNT(*)
270  FROM '. GROUPS_TABLE .'
271  WHERE id = '. $params['group_id'] .'
272;';
273  list($count) = pwg_db_fetch_row(pwg_query($query));
274  if ($count == 0)
275  {
276    return new PwgError(WS_ERR_INVALID_PARAM, 'This group does not exist.');
277  }
278
279  $query = '
280DELETE FROM '. USER_GROUP_TABLE .'
281  WHERE
282    group_id = '. $params['group_id'] .'
283    AND user_id IN('. implode(',', $params['user_id']) .')
284;';
285  pwg_query($query);
286
287  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
288  invalidate_user_cache();
289
290  return $service->invoke('pwg.groups.getList', array('group_id' => $params['group_id']));
291}
292
293?>
Note: See TracBrowser for help on using the repository browser.