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(array $args=array())
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)) {
111 $this->group->nickname = Nickname::normalize($this->nickname, true);
112 } catch (NicknameTakenException $e) {
113 // Abort only if the nickname is occupied by _another_ local group
114 if ($e->profile->id != $this->group->id) {
115 throw new ApiValidationException($e->getMessage());
117 } catch (NicknameException $e) {
118 throw new ApiValidationException($e->getMessage());
120 $this->group->mainpage = common_local_url('showgroup',
121 array('nickname' => $this->group->nickname));
124 if (!empty($this->fullname)) {
125 $this->validateFullname();
126 $this->group->fullname = $this->fullname;
129 if (!empty($this->homepage)) {
130 $this->validateHomepage();
131 $this->group->homepage = $this->homepage;
134 if (!empty($this->description)) {
135 $this->validateDescription();
136 $this->group->description = $this->decription;
139 if (!empty($this->location)) {
140 $this->validateLocation();
141 $this->group->location = $this->location;
144 } catch (ApiValidationException $ave) {
145 $this->clientError($ave->getMessage(), 400);
148 $result = $this->group->update($orig);
151 common_log_db_error($this->group, 'UPDATE', __FILE__);
152 // TRANS: Server error displayed when group update fails.
153 $this->serverError(_('Could not update group.'));
159 if (!empty($this->aliasstring)) {
160 $aliases = $this->validateAliases();
162 } catch (ApiValidationException $ave) {
163 $this->clientError($ave->getMessage(), 403);
166 $result = $this->group->setAliases($aliases);
169 // TRANS: Server error displayed when adding group aliases fails.
170 $this->serverError(_('Could not create aliases.'));
173 $this->group->query('COMMIT');
175 switch($this->format) {
177 $this->showSingleXmlGroup($this->group);
180 $this->showSingleJsonGroup($this->group);
183 // TRANS: Client error displayed when coming across a non-supported API method.
184 $this->clientError(_('API method not found.'), 404);
188 function validateHomepage()
190 if (!is_null($this->homepage)
191 && (strlen($this->homepage) > 0)
192 && !common_valid_http_url($this->homepage)) {
193 throw new ApiValidationException(
194 // TRANS: API validation exception thrown when homepage URL does not validate.
195 _('Homepage is not a valid URL.')
200 function validateFullname()
202 if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
203 throw new ApiValidationException(
204 // TRANS: API validation exception thrown when full name does not validate.
205 _('Full name is too long (maximum 255 characters).')
210 function validateDescription()
212 if (User_group::descriptionTooLong($this->description)) {
213 // TRANS: API validation exception thrown when description does not validate.
214 // TRANS: %d is the maximum description length and used for plural.
215 throw new ApiValidationException(sprintf(_m('Description is too long (maximum %d character).',
216 'Description is too long (maximum %d characters).',
217 User_group::maxDescription()),
218 User_group::maxDescription()));
222 function validateLocation()
224 if (!is_null($this->location) && mb_strlen($this->location) > 255) {
225 throw new ApiValidationException(
226 // TRANS: API validation exception thrown when location does not validate.
227 _('Location is too long (maximum 255 characters).')
232 function validateAliases()
235 $aliases = array_map(array('Nickname', 'normalize'),
236 array_unique(preg_split('/[\s,]+/', $this->aliasstring)));
237 } catch (NicknameException $e) {
238 throw new ApiValidationException(sprintf('Error processing aliases: %s', $e->getMessage()));
241 if (count($aliases) > common_config('group', 'maxaliases')) {
242 // TRANS: API validation exception thrown when aliases do not validate.
243 // TRANS: %d is the maximum number of aliases and used for plural.
244 throw new ApiValidationException(sprintf(_m('Too many aliases! Maximum %d allowed.',
245 'Too many aliases! Maximum %d allowed.',
246 common_config('group', 'maxaliases')),
247 common_config('group', 'maxaliases')));