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 _('This method requires a POST.'),
94 if (!in_array($this->format, array('xml', 'json'))) {
96 // TRANS: Client error displayed when using an unsupported API format.
97 _('API method not found.'),
104 if (empty($this->user)) {
105 // TRANS: Client error displayed when not providing a user or an invalid user.
106 $this->clientError(_('No such user.'), 404, $this->format);
110 if (empty($this->group)) {
111 // TRANS: Client error displayed when not providing a group or an invalid group.
112 $this->clientError(_('Group not found.'), 404, $this->format);
116 if (!$this->user->isAdmin($this->group)) {
117 // TRANS: Client error displayed when trying to edit a group without being an admin.
118 $this->clientError(_('You must be an admin to edit the group.'), 403);
122 $this->group->query('BEGIN');
124 $orig = clone($this->group);
128 if (!empty($this->nickname)) {
129 if ($this->validateNickname()) {
130 $this->group->nickname = $this->nickname;
131 $this->group->mainpage = common_local_url(
133 array('nickname' => $this->nickname)
138 if (!empty($this->fullname)) {
139 $this->validateFullname();
140 $this->group->fullname = $this->fullname;
143 if (!empty($this->homepage)) {
144 $this->validateHomepage();
145 $this->group->homepage = $this->hompage;
148 if (!empty($this->description)) {
149 $this->validateDescription();
150 $this->group->description = $this->decription;
153 if (!empty($this->location)) {
154 $this->validateLocation();
155 $this->group->location = $this->location;
158 } catch (ApiValidationException $ave) {
167 $result = $this->group->update($orig);
170 common_log_db_error($this->group, 'UPDATE', __FILE__);
171 // TRANS: Server error displayed when group update fails.
172 $this->serverError(_('Could not update group.'));
178 if (!empty($this->aliasstring)) {
179 $aliases = $this->validateAliases();
182 } catch (ApiValidationException $ave) {
191 $result = $this->group->setAliases($aliases);
194 // TRANS: Server error displayed when adding group aliases fails.
195 $this->serverError(_('Could not create aliases.'));
198 if (!empty($this->nickname) && ($this->nickname != $orig->nickname)) {
199 common_log(LOG_INFO, "Saving local group info.");
200 $local = Local_group::staticGet('group_id', $this->group->id);
201 $local->setNickname($this->nickname);
204 $this->group->query('COMMIT');
206 switch($this->format) {
208 $this->showSingleXmlGroup($this->group);
211 $this->showSingleJsonGroup($this->group);
214 // TRANS: Client error displayed when using an unsupported API format.
215 $this->clientError(_('API method not found.'), 404, $this->format);
220 function nicknameExists($nickname)
222 $group = Local_group::staticGet('nickname', $nickname);
224 if (!empty($group) &&
225 $group->group_id != $this->group->id) {
229 $alias = Group_alias::staticGet('alias', $nickname);
231 if (!empty($alias) &&
232 $alias->group_id != $this->group->id) {
239 function validateNickname()
241 if (!Validate::string(
242 $this->nickname, array(
245 'format' => NICKNAME_FMT
249 throw new ApiValidationException(
250 // TRANS: API validation exception thrown when nickname does not validate.
251 _('Nickname must have only lowercase letters and numbers and no spaces.')
253 } else if ($this->nicknameExists($this->nickname)) {
254 throw new ApiValidationException(
255 // TRANS: API validation exception thrown when nickname is already used.
256 _('Nickname already in use. Try another one.')
258 } else if (!User_group::allowedNickname($this->nickname)) {
259 throw new ApiValidationException(
260 // TRANS: API validation exception thrown when nickname does not validate.
261 _('Not a valid nickname.')
268 function validateHomepage()
270 if (!is_null($this->homepage)
271 && (strlen($this->homepage) > 0)
274 array('allowed_schemes' => array('http', 'https')
278 throw new ApiValidationException(
279 // TRANS: API validation exception thrown when homepage URL does not validate.
280 _('Homepage is not a valid URL.')
285 function validateFullname()
287 if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
288 throw new ApiValidationException(
289 // TRANS: API validation exception thrown when full name does not validate.
290 _('Full name is too long (maximum 255 characters).')
295 function validateDescription()
297 if (User_group::descriptionTooLong($this->description)) {
298 // TRANS: API validation exception thrown when description does not validate.
299 // TRANS: %d is the maximum description length and used for plural.
300 throw new ApiValidationException(sprintf(_m('Description is too long (maximum %d character).',
301 'Description is too long (maximum %d characters).',
302 User_group::maxDescription()),
303 User_group::maxDescription()));
307 function validateLocation()
309 if (!is_null($this->location) && mb_strlen($this->location) > 255) {
310 throw new ApiValidationException(
311 // TRANS: API validation exception thrown when location does not validate.
312 _('Location is too long (maximum 255 characters).')
317 function validateAliases()
319 $aliases = array_map(
320 'common_canonical_nickname',
322 preg_split('/[\s,]+/',
328 if (count($aliases) > common_config('group', 'maxaliases')) {
329 // TRANS: API validation exception thrown when aliases do not validate.
330 // TRANS: %d is the maximum number of aliases and used for plural.
331 throw new ApiValidationException(sprintf(_m('Too many aliases! Maximum %d allowed.',
332 'Too many aliases! Maximum %d allowed.',
333 common_config('group', 'maxaliases')),
334 common_config('group', 'maxaliases')));
337 foreach ($aliases as $alias) {
338 if (!Validate::string(
342 'format' => NICKNAME_FMT)
345 throw new ApiValidationException(
347 // TRANS: API validation exception thrown when aliases does not validate.
348 // TRANS: %s is the invalid alias.
349 _('Invalid alias: "%s".'),
355 if ($this->nicknameExists($alias)) {
356 throw new ApiValidationException(
358 // TRANS: API validation exception thrown when aliases is already used.
359 // TRANS: %s is the already used alias.
360 _('Alias "%s" already in use. Try another one.'),
365 // XXX assumes alphanum nicknames
366 if (strcmp($alias, $this->nickname) == 0) {
367 throw new ApiValidationException(
368 // TRANS: API validation exception thrown when alias is the same as nickname.
369 _('Alias cannot be the same as nickname.')