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
45 protected $needPost = true;
47 * Take arguments for running
49 * @param array $args $_REQUEST args
51 * @return boolean success flag
54 protected function prepare($args)
56 parent::prepare($args);
58 $this->nickname = common_canonical_nickname($this->trimmed('nickname'));
60 $this->fullname = $this->trimmed('fullname');
61 $this->homepage = $this->trimmed('homepage');
62 $this->description = $this->trimmed('description');
63 $this->location = $this->trimmed('location');
64 $this->aliasstring = $this->trimmed('aliases');
66 $this->user = $this->auth_user;
67 $this->group = $this->getTargetGroup($this->arg('id'));
75 * See which request params have been set, and update the profile
79 protected function handle()
83 if (!in_array($this->format, array('xml', 'json'))) {
84 // TRANS: Client error displayed when coming across a non-supported API method.
85 $this->clientError(_('API method not found.'), 404);
88 if (empty($this->user)) {
89 // TRANS: Client error displayed when not providing a user or an invalid user.
90 $this->clientError(_('No such user.'), 404);
93 if (empty($this->group)) {
94 // TRANS: Client error displayed when not providing a group or an invalid group.
95 $this->clientError(_('Group not found.'), 404);
98 if (!$this->user->isAdmin($this->group)) {
99 // TRANS: Client error displayed when trying to edit a group without being an admin.
100 $this->clientError(_('You must be an admin to edit the group.'), 403);
103 $this->group->query('BEGIN');
105 $orig = clone($this->group);
109 if (!empty($this->nickname)) {
110 if ($this->validateNickname()) {
111 $this->group->nickname = $this->nickname;
112 $this->group->mainpage = common_local_url(
114 array('nickname' => $this->nickname)
119 if (!empty($this->fullname)) {
120 $this->validateFullname();
121 $this->group->fullname = $this->fullname;
124 if (!empty($this->homepage)) {
125 $this->validateHomepage();
126 $this->group->homepage = $this->hompage;
129 if (!empty($this->description)) {
130 $this->validateDescription();
131 $this->group->description = $this->decription;
134 if (!empty($this->location)) {
135 $this->validateLocation();
136 $this->group->location = $this->location;
139 } catch (ApiValidationException $ave) {
140 $this->clientError($ave->getMessage(), 403);
143 $result = $this->group->update($orig);
146 common_log_db_error($this->group, 'UPDATE', __FILE__);
147 // TRANS: Server error displayed when group update fails.
148 $this->serverError(_('Could not update group.'));
154 if (!empty($this->aliasstring)) {
155 $aliases = $this->validateAliases();
158 } catch (ApiValidationException $ave) {
159 $this->clientError($ave->getMessage(), 403);
162 $result = $this->group->setAliases($aliases);
165 // TRANS: Server error displayed when adding group aliases fails.
166 $this->serverError(_('Could not create aliases.'));
169 if (!empty($this->nickname) && ($this->nickname != $orig->nickname)) {
170 common_log(LOG_INFO, "Saving local group info.");
171 $local = Local_group::getKV('group_id', $this->group->id);
172 $local->setNickname($this->nickname);
175 $this->group->query('COMMIT');
177 switch($this->format) {
179 $this->showSingleXmlGroup($this->group);
182 $this->showSingleJsonGroup($this->group);
185 // TRANS: Client error displayed when coming across a non-supported API method.
186 $this->clientError(_('API method not found.'), 404);
190 function nicknameExists($nickname)
192 $group = Local_group::getKV('nickname', $nickname);
194 if (!empty($group) &&
195 $group->group_id != $this->group->id) {
199 $alias = Group_alias::getKV('alias', $nickname);
201 if (!empty($alias) &&
202 $alias->group_id != $this->group->id) {
209 function validateNickname()
211 if (!Validate::string(
212 $this->nickname, array(
215 'format' => NICKNAME_FMT
219 throw new ApiValidationException(
220 // TRANS: API validation exception thrown when nickname does not validate.
221 _('Nickname must have only lowercase letters and numbers and no spaces.')
223 } else if ($this->nicknameExists($this->nickname)) {
224 throw new ApiValidationException(
225 // TRANS: API validation exception thrown when nickname is already used.
226 _('Nickname already in use. Try another one.')
228 } else if (!User_group::allowedNickname($this->nickname)) {
229 throw new ApiValidationException(
230 // TRANS: API validation exception thrown when nickname does not validate.
231 _('Not a valid nickname.')
238 function validateHomepage()
240 if (!is_null($this->homepage)
241 && (strlen($this->homepage) > 0)
242 && !common_valid_http_url($this->homepage)) {
243 throw new ApiValidationException(
244 // TRANS: API validation exception thrown when homepage URL does not validate.
245 _('Homepage is not a valid URL.')
250 function validateFullname()
252 if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
253 throw new ApiValidationException(
254 // TRANS: API validation exception thrown when full name does not validate.
255 _('Full name is too long (maximum 255 characters).')
260 function validateDescription()
262 if (User_group::descriptionTooLong($this->description)) {
263 // TRANS: API validation exception thrown when description does not validate.
264 // TRANS: %d is the maximum description length and used for plural.
265 throw new ApiValidationException(sprintf(_m('Description is too long (maximum %d character).',
266 'Description is too long (maximum %d characters).',
267 User_group::maxDescription()),
268 User_group::maxDescription()));
272 function validateLocation()
274 if (!is_null($this->location) && mb_strlen($this->location) > 255) {
275 throw new ApiValidationException(
276 // TRANS: API validation exception thrown when location does not validate.
277 _('Location is too long (maximum 255 characters).')
282 function validateAliases()
284 $aliases = array_map(
285 'common_canonical_nickname',
287 preg_split('/[\s,]+/',
293 if (count($aliases) > common_config('group', 'maxaliases')) {
294 // TRANS: API validation exception thrown when aliases do not validate.
295 // TRANS: %d is the maximum number of aliases and used for plural.
296 throw new ApiValidationException(sprintf(_m('Too many aliases! Maximum %d allowed.',
297 'Too many aliases! Maximum %d allowed.',
298 common_config('group', 'maxaliases')),
299 common_config('group', 'maxaliases')));
302 foreach ($aliases as $alias) {
303 if (!Validate::string(
307 'format' => NICKNAME_FMT)
310 throw new ApiValidationException(
312 // TRANS: API validation exception thrown when aliases does not validate.
313 // TRANS: %s is the invalid alias.
314 _('Invalid alias: "%s".'),
320 if ($this->nicknameExists($alias)) {
321 throw new ApiValidationException(
323 // TRANS: API validation exception thrown when aliases is already used.
324 // TRANS: %s is the already used alias.
325 _('Alias "%s" already in use. Try another one.'),
330 // XXX assumes alphanum nicknames
331 if (strcmp($alias, $this->nickname) == 0) {
332 throw new ApiValidationException(
333 // TRANS: API validation exception thrown when alias is the same as nickname.
334 _('Alias cannot be the same as nickname.')