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

Last change on this file since 25281 was 25281, checked in by mistic100, 10 years ago

splits ws_functions.inc.php in 8 files + comments + code cleaning

File size: 7.4 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  return new PwgNamedArray($groupnames, 'group_deleted');
153}
154
155/**
156 * API method
157 * Updates a group
158 * @param mixed[] $params
159 *    @option int group_id
160 *    @option string name (optional)
161 *    @option bool is_default (optional)
162 */
163function ws_groups_setInfo($params, &$service)
164{
165  $updates = array();
166
167  // does the group exist ?
168  $query = '
169SELECT COUNT(*)
170  FROM '. GROUPS_TABLE .'
171  WHERE id = '. $params['group_id'] .'
172;';
173  list($count) = pwg_db_fetch_row(pwg_query($query));
174  if ($count == 0)
175  {
176    return new PwgError(WS_ERR_INVALID_PARAM, 'This group does not exist.');
177  }
178
179  if (!empty($params['name']))
180  {
181    $params['name'] = pwg_db_real_escape_string($params['name']);
182
183    // is the name not already used ?
184    $query = '
185SELECT COUNT(*)
186  FROM '. GROUPS_TABLE .'
187  WHERE name = \''. $params['name'] .'\'
188;';
189    list($count) = pwg_db_fetch_row(pwg_query($query));
190    if ($count != 0)
191    {
192      return new PwgError(WS_ERR_INVALID_PARAM, 'This name is already used by another group.');
193    }
194
195    $updates['name'] = $params['name'];
196  }
197
198  if (!empty($params['is_default']) or @$params['is_default']===false)
199  {
200    $updates['is_default'] = boolean_to_string($params['is_default']);
201  }
202
203  single_update(
204    GROUPS_TABLE,
205    $updates,
206    array('id' => $params['group_id'])
207    );
208
209  return $service->invoke('pwg.groups.getList', array('group_id' => $params['group_id']));
210}
211
212/**
213 * API method
214 * Adds user(s) to a group
215 * @param mixed[] $params
216 *    @option int group_id
217 *    @option int[] user_id
218 */
219function ws_groups_addUser($params, &$service)
220{
221  // does the group exist ?
222  $query = '
223SELECT COUNT(*)
224  FROM '. GROUPS_TABLE .'
225  WHERE id = '. $params['group_id'] .'
226;';
227  list($count) = pwg_db_fetch_row(pwg_query($query));
228  if ($count == 0)
229  {
230    return new PwgError(WS_ERR_INVALID_PARAM, 'This group does not exist.');
231  }
232
233  $inserts = array();
234  foreach ($params['user_id'] as $user_id)
235  {
236    $inserts[] = array(
237      'group_id' => $params['group_id'],
238      'user_id' => $user_id,
239      );
240  }
241
242  mass_inserts(
243    USER_GROUP_TABLE,
244    array('group_id', 'user_id'),
245    $inserts,
246    array('ignore'=>true)
247    );
248
249  return $service->invoke('pwg.groups.getList', array('group_id' => $params['group_id']));
250}
251
252/**
253 * API method
254 * Removes user(s) from a group
255 * @param mixed[] $params
256 *    @option int group_id
257 *    @option int[] user_id
258 */
259function ws_groups_deleteUser($params, &$service)
260{
261  // does the group exist ?
262  $query = '
263SELECT COUNT(*)
264  FROM '. GROUPS_TABLE .'
265  WHERE id = '. $params['group_id'] .'
266;';
267  list($count) = pwg_db_fetch_row(pwg_query($query));
268  if ($count == 0)
269  {
270    return new PwgError(WS_ERR_INVALID_PARAM, 'This group does not exist.');
271  }
272
273  $query = '
274DELETE FROM '. USER_GROUP_TABLE .'
275  WHERE
276    group_id = '. $params['group_id'] .'
277    AND user_id IN('. implode(',', $params['user_id']) .')
278;';
279  pwg_query($query);
280
281  return $service->invoke('pwg.groups.getList', array('group_id' => $params['group_id']));
282}
283
284?>
Note: See TracBrowser for help on using the repository browser.