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 = Nickname::normalize($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 (common_config('profile', 'changenick') == true && $this->group->nickname !== $this->nickname) {
111 $this->group->nickname = Nickname::normalize($this->nickname, true);
112 } catch (NicknameException $e) {
113 throw new ApiValidationException($e->getMessage());
115 $this->group->mainpage = common_local_url('showgroup',
116 array('nickname' => $this->group->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->homepage;
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(), 400);
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();
157 } catch (ApiValidationException $ave) {
158 $this->clientError($ave->getMessage(), 403);
161 $result = $this->group->setAliases($aliases);
164 // TRANS: Server error displayed when adding group aliases fails.
165 $this->serverError(_('Could not create aliases.'));
168 $this->group->query('COMMIT');
170 switch($this->format) {
172 $this->showSingleXmlGroup($this->group);
175 $this->showSingleJsonGroup($this->group);
178 // TRANS: Client error displayed when coming across a non-supported API method.
179 $this->clientError(_('API method not found.'), 404);
183 function validateHomepage()
185 if (!is_null($this->homepage)
186 && (strlen($this->homepage) > 0)
187 && !common_valid_http_url($this->homepage)) {
188 throw new ApiValidationException(
189 // TRANS: API validation exception thrown when homepage URL does not validate.
190 _('Homepage is not a valid URL.')
195 function validateFullname()
197 if (!is_null($this->fullname) && mb_strlen($this->fullname) > 255) {
198 throw new ApiValidationException(
199 // TRANS: API validation exception thrown when full name does not validate.
200 _('Full name is too long (maximum 255 characters).')
205 function validateDescription()
207 if (User_group::descriptionTooLong($this->description)) {
208 // TRANS: API validation exception thrown when description does not validate.
209 // TRANS: %d is the maximum description length and used for plural.
210 throw new ApiValidationException(sprintf(_m('Description is too long (maximum %d character).',
211 'Description is too long (maximum %d characters).',
212 User_group::maxDescription()),
213 User_group::maxDescription()));
217 function validateLocation()
219 if (!is_null($this->location) && mb_strlen($this->location) > 255) {
220 throw new ApiValidationException(
221 // TRANS: API validation exception thrown when location does not validate.
222 _('Location is too long (maximum 255 characters).')
227 function validateAliases()
230 $aliases = array_map(array('Nickname', 'normalize'),
231 array_unique(preg_split('/[\s,]+/', $this->aliasstring)));
232 } catch (NicknameException $e) {
233 throw new ApiValidationException(sprintf('Error processing aliases: %s', $e->getMessage()));
236 if (count($aliases) > common_config('group', 'maxaliases')) {
237 // TRANS: API validation exception thrown when aliases do not validate.
238 // TRANS: %d is the maximum number of aliases and used for plural.
239 throw new ApiValidationException(sprintf(_m('Too many aliases! Maximum %d allowed.',
240 'Too many aliases! Maximum %d allowed.',
241 common_config('group', 'maxaliases')),
242 common_config('group', 'maxaliases')));