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')) {
35 * API analog to the group edit page
39 * @author Zach Copley <zach@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://status.net/
43 class ApiGroupProfileUpdateAction extends ApiAuthAction
46 * Take arguments for running
48 * @param array $args $_REQUEST args
50 * @return boolean success flag
53 function prepare($args)
55 parent::prepare($args);
57 $this->nickname = common_canonical_nickname($this->trimmed('nickname'));
59 $this->fullname = $this->trimmed('fullname');
60 $this->homepage = $this->trimmed('homepage');
61 $this->description = $this->trimmed('description');
62 $this->location = $this->trimmed('location');
63 $this->aliasstring = $this->trimmed('aliases');
65 $this->user = $this->auth_user;
66 $this->group = $this->getTargetGroup($this->arg('id'));
74 * See which request params have been set, and update the profile
76 * @param array $args $_REQUEST data (unused)
80 function handle($args)
82 parent::handle($args);
84 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
86 // TRANS: Client error message. POST is a HTTP command. It should not be translated.
87 _('This method requires a POST.'),
93 if (!in_array($this->format, array('xml', 'json'))) {
95 // TRANS: Client error displayed when coming across a non-supported API method.
96 _('API method not found.'),
103 if (empty($this->user)) {
104 // TRANS: Client error displayed when not providing a user or an invalid user.
105 $this->clientError(_('No such user.'), 404, $this->format);
109 if (empty($this->group)) {
110 // TRANS: Client error displayed when not providing a group or an invalid group.
111 $this->clientError(_('Group not found.'), 404, $this->format);
115 if (!$this->user->isAdmin($this->group)) {
116 // TRANS: Client error displayed when trying to edit a group without being an admin.
117 $this->clientError(_('You must be an admin to edit the group.'), 403);
121 $this->group->query('BEGIN');
123 $orig = clone($this->group);
127 if (!empty($this->nickname)) {
128 if ($this->validateNickname()) {
129 $this->group->nickname = $this->nickname;
130 $this->group->mainpage = common_local_url(
132 array('nickname' => $this->nickname)
137 if (!empty($this->fullname)) {
138 $this->validateFullname();
139 $this->group->fullname = $this->fullname;
142 if (!empty($this->homepage)) {
143 $this->validateHomepage();
144 $this->group->homepage = $this->hompage;
147 if (!empty($this->description)) {
148 $this->validateDescription();
149 $this->group->description = $this->decription;
152 if (!empty($this->location)) {
153 $this->validateLocation();
154 $this->group->location = $this->location;
157 } catch (ApiValidationException $ave) {
166 $result = $this->group->update($orig);
169 common_log_db_error($this->group, 'UPDATE', __FILE__);
170 // TRANS: Server error displayed when group update fails.
171 $this->serverError(_('Could not update group.'));
177 if (!empty($this->aliasstring)) {
178 $aliases = $this->validateAliases();
181 } catch (ApiValidationException $ave) {
190 $result = $this->group->setAliases($aliases);
193 // TRANS: Server error displayed when adding group aliases fails.
194 $this->serverError(_('Could not create aliases.'));
197 if (!empty($this->nickname) && ($this->nickname != $orig->nickname)) {
198 common_log(LOG_INFO, "Saving local group info.");
199 $local = Local_group::getKV('group_id', $this->group->id);
200 $local->setNickname($this->nickname);
203 $this->group->query('COMMIT');
205 switch($this->format) {
207 $this->showSingleXmlGroup($this->group);
210 $this->showSingleJsonGroup($this->group);
213 // TRANS: Client error displayed when coming across a non-supported API method.
214 $this->clientError(_('API method not found.'), 404, $this->format);
219 function nicknameExists($nickname)
221 $group = Local_group::getKV('nickname', $nickname);
223 if (!empty($group) &&
224 $group->group_id != $this->group->id) {
228 $alias = Group_alias::getKV('alias', $nickname);
230 if (!empty($alias) &&
231 $alias->group_id != $this->group->id) {
238 function validateNickname()
240 if (!Validate::string(
241 $this->nickname, array(
244 'format' => NICKNAME_FMT
248 throw new ApiValidationException(
249 // TRANS: API validation exception thrown when nickname does not validate.
250 _('Nickname must have only lowercase letters and numbers and no spaces.')
252 } else if ($this->nicknameExists($this->nickname)) {
253 throw new ApiValidationException(
254 // TRANS: API validation exception thrown when nickname is already used.
255 _('Nickname already in use. Try another one.')
257 } else if (!User_group::allowedNickname($this->nickname)) {
258 throw new ApiValidationException(
259 // TRANS: API validation exception thrown when nickname does not validate.
260 _('Not a valid nickname.')
267 function validateHomepage()
269 if (!is_null($this->homepage)
270 && (strlen($this->homepage) > 0)
273 array('allowed_schemes' => array('http', 'https')
277 throw new ApiValidationException(
278 // TRANS: API validation exception thrown when homepage URL does not validate.
279 _('Homepage is not a valid URL.')
284 function validateFullname()
286 if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
287 throw new ApiValidationException(
288 // TRANS: API validation exception thrown when full name does not validate.
289 _('Full name is too long (maximum 255 characters).')
294 function validateDescription()
296 if (User_group::descriptionTooLong($this->description)) {
297 // TRANS: API validation exception thrown when description does not validate.
298 // TRANS: %d is the maximum description length and used for plural.
299 throw new ApiValidationException(sprintf(_m('Description is too long (maximum %d character).',
300 'Description is too long (maximum %d characters).',
301 User_group::maxDescription()),
302 User_group::maxDescription()));
306 function validateLocation()
308 if (!is_null($this->location) && mb_strlen($this->location) > 255) {
309 throw new ApiValidationException(
310 // TRANS: API validation exception thrown when location does not validate.
311 _('Location is too long (maximum 255 characters).')
316 function validateAliases()
318 $aliases = array_map(
319 'common_canonical_nickname',
321 preg_split('/[\s,]+/',
327 if (count($aliases) > common_config('group', 'maxaliases')) {
328 // TRANS: API validation exception thrown when aliases do not validate.
329 // TRANS: %d is the maximum number of aliases and used for plural.
330 throw new ApiValidationException(sprintf(_m('Too many aliases! Maximum %d allowed.',
331 'Too many aliases! Maximum %d allowed.',
332 common_config('group', 'maxaliases')),
333 common_config('group', 'maxaliases')));
336 foreach ($aliases as $alias) {
337 if (!Validate::string(
341 'format' => NICKNAME_FMT)
344 throw new ApiValidationException(
346 // TRANS: API validation exception thrown when aliases does not validate.
347 // TRANS: %s is the invalid alias.
348 _('Invalid alias: "%s".'),
354 if ($this->nicknameExists($alias)) {
355 throw new ApiValidationException(
357 // TRANS: API validation exception thrown when aliases is already used.
358 // TRANS: %s is the already used alias.
359 _('Alias "%s" already in use. Try another one.'),
364 // XXX assumes alphanum nicknames
365 if (strcmp($alias, $this->nickname) == 0) {
366 throw new ApiValidationException(
367 // TRANS: API validation exception thrown when alias is the same as nickname.
368 _('Alias cannot be the same as nickname.')