3 * StatusNet, the distributed open-source microblogging tool
5 * Update a group's profile
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Zach Copley <zach@status.net>
25 * @copyright 2010 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET')) {
34 require_once INSTALLDIR . '/lib/apiauth.php';
37 * API analog to the group edit page
41 * @author Zach Copley <zach@status.net>
42 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43 * @link http://status.net/
45 class ApiGroupProfileUpdateAction extends ApiAuthAction
48 * Take arguments for running
50 * @param array $args $_REQUEST args
52 * @return boolean success flag
55 function prepare($args)
57 parent::prepare($args);
59 $this->nickname = common_canonical_nickname($this->trimmed('nickname'));
61 $this->fullname = $this->trimmed('fullname');
62 $this->homepage = $this->trimmed('homepage');
63 $this->description = $this->trimmed('description');
64 $this->location = $this->trimmed('location');
65 $this->aliasstring = $this->trimmed('aliases');
67 $this->user = $this->auth_user;
68 $this->group = $this->getTargetGroup($this->arg('id'));
76 * See which request params have been set, and update the profile
78 * @param array $args $_REQUEST data (unused)
82 function handle($args)
84 parent::handle($args);
86 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
88 // TRANS: Client error message. POST is a HTTP command. It should not be translated.
89 _('This method requires a POST.'),
95 if (!in_array($this->format, array('xml', 'json'))) {
97 // TRANS: Client error displayed when coming across a non-supported API method.
98 _('API method not found.'),
105 if (empty($this->user)) {
106 // TRANS: Client error displayed when not providing a user or an invalid user.
107 $this->clientError(_('No such user.'), 404, $this->format);
111 if (empty($this->group)) {
112 // TRANS: Client error displayed when not providing a group or an invalid group.
113 $this->clientError(_('Group not found.'), 404, $this->format);
117 if (!$this->user->isAdmin($this->group)) {
118 // TRANS: Client error displayed when trying to edit a group without being an admin.
119 $this->clientError(_('You must be an admin to edit the group.'), 403);
123 $this->group->query('BEGIN');
125 $orig = clone($this->group);
129 if (!empty($this->nickname)) {
130 if ($this->validateNickname()) {
131 $this->group->nickname = $this->nickname;
132 $this->group->mainpage = common_local_url(
134 array('nickname' => $this->nickname)
139 if (!empty($this->fullname)) {
140 $this->validateFullname();
141 $this->group->fullname = $this->fullname;
144 if (!empty($this->homepage)) {
145 $this->validateHomepage();
146 $this->group->homepage = $this->hompage;
149 if (!empty($this->description)) {
150 $this->validateDescription();
151 $this->group->description = $this->decription;
154 if (!empty($this->location)) {
155 $this->validateLocation();
156 $this->group->location = $this->location;
159 } catch (ApiValidationException $ave) {
168 $result = $this->group->update($orig);
171 common_log_db_error($this->group, 'UPDATE', __FILE__);
172 // TRANS: Server error displayed when group update fails.
173 $this->serverError(_('Could not update group.'));
179 if (!empty($this->aliasstring)) {
180 $aliases = $this->validateAliases();
183 } catch (ApiValidationException $ave) {
192 $result = $this->group->setAliases($aliases);
195 // TRANS: Server error displayed when adding group aliases fails.
196 $this->serverError(_('Could not create aliases.'));
199 if (!empty($this->nickname) && ($this->nickname != $orig->nickname)) {
200 common_log(LOG_INFO, "Saving local group info.");
201 $local = Local_group::staticGet('group_id', $this->group->id);
202 $local->setNickname($this->nickname);
205 $this->group->query('COMMIT');
207 switch($this->format) {
209 $this->showSingleXmlGroup($this->group);
212 $this->showSingleJsonGroup($this->group);
215 // TRANS: Client error displayed when coming across a non-supported API method.
216 $this->clientError(_('API method not found.'), 404, $this->format);
221 function nicknameExists($nickname)
223 $group = Local_group::staticGet('nickname', $nickname);
225 if (!empty($group) &&
226 $group->group_id != $this->group->id) {
230 $alias = Group_alias::staticGet('alias', $nickname);
232 if (!empty($alias) &&
233 $alias->group_id != $this->group->id) {
240 function validateNickname()
242 if (!Validate::string(
243 $this->nickname, array(
246 'format' => NICKNAME_FMT
250 throw new ApiValidationException(
251 // TRANS: API validation exception thrown when nickname does not validate.
252 _('Nickname must have only lowercase letters and numbers and no spaces.')
254 } else if ($this->nicknameExists($this->nickname)) {
255 throw new ApiValidationException(
256 // TRANS: API validation exception thrown when nickname is already used.
257 _('Nickname already in use. Try another one.')
259 } else if (!User_group::allowedNickname($this->nickname)) {
260 throw new ApiValidationException(
261 // TRANS: API validation exception thrown when nickname does not validate.
262 _('Not a valid nickname.')
269 function validateHomepage()
271 if (!is_null($this->homepage)
272 && (strlen($this->homepage) > 0)
275 array('allowed_schemes' => array('http', 'https')
279 throw new ApiValidationException(
280 // TRANS: API validation exception thrown when homepage URL does not validate.
281 _('Homepage is not a valid URL.')
286 function validateFullname()
288 if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
289 throw new ApiValidationException(
290 // TRANS: API validation exception thrown when full name does not validate.
291 _('Full name is too long (maximum 255 characters).')
296 function validateDescription()
298 if (User_group::descriptionTooLong($this->description)) {
299 // TRANS: API validation exception thrown when description does not validate.
300 // TRANS: %d is the maximum description length and used for plural.
301 throw new ApiValidationException(sprintf(_m('Description is too long (maximum %d character).',
302 'Description is too long (maximum %d characters).',
303 User_group::maxDescription()),
304 User_group::maxDescription()));
308 function validateLocation()
310 if (!is_null($this->location) && mb_strlen($this->location) > 255) {
311 throw new ApiValidationException(
312 // TRANS: API validation exception thrown when location does not validate.
313 _('Location is too long (maximum 255 characters).')
318 function validateAliases()
320 $aliases = array_map(
321 'common_canonical_nickname',
323 preg_split('/[\s,]+/',
329 if (count($aliases) > common_config('group', 'maxaliases')) {
330 // TRANS: API validation exception thrown when aliases do not validate.
331 // TRANS: %d is the maximum number of aliases and used for plural.
332 throw new ApiValidationException(sprintf(_m('Too many aliases! Maximum %d allowed.',
333 'Too many aliases! Maximum %d allowed.',
334 common_config('group', 'maxaliases')),
335 common_config('group', 'maxaliases')));
338 foreach ($aliases as $alias) {
339 if (!Validate::string(
343 'format' => NICKNAME_FMT)
346 throw new ApiValidationException(
348 // TRANS: API validation exception thrown when aliases does not validate.
349 // TRANS: %s is the invalid alias.
350 _('Invalid alias: "%s".'),
356 if ($this->nicknameExists($alias)) {
357 throw new ApiValidationException(
359 // TRANS: API validation exception thrown when aliases is already used.
360 // TRANS: %s is the already used alias.
361 _('Alias "%s" already in use. Try another one.'),
366 // XXX assumes alphanum nicknames
367 if (strcmp($alias, $this->nickname) == 0) {
368 throw new ApiValidationException(
369 // TRANS: API validation exception thrown when alias is the same as nickname.
370 _('Alias cannot be the same as nickname.')