3 * StatusNet, the distributed open-source microblogging tool
5 * Create a group via the API
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 Craig Andrews <candrews@integralblue.com>
25 * @author Evan Prodromou <evan@status.net>
26 * @author Jeffery To <jeffery.to@gmail.com>
27 * @author Zach Copley <zach@status.net>
28 * @copyright 2009 StatusNet, Inc.
29 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
30 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
31 * @link http://status.net/
34 if (!defined('STATUSNET')) {
39 * Make a new group. Sets the authenticated user as the administrator of the group.
43 * @author Craig Andrews <candrews@integralblue.com>
44 * @author Evan Prodromou <evan@status.net>
45 * @author Jeffery To <jeffery.to@gmail.com>
46 * @author Zach Copley <zach@status.net>
47 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
48 * @link http://status.net/
50 class ApiGroupCreateAction extends ApiAuthAction
56 var $description = null;
58 var $aliasstring = null;
62 * Take arguments for running
64 * @param array $args $_REQUEST args
66 * @return boolean success flag
68 function prepare($args)
70 parent::prepare($args);
72 $this->user = $this->auth_user;
74 $this->nickname = Nickname::normalize($this->arg('nickname'));
75 $this->fullname = $this->arg('full_name');
76 $this->homepage = $this->arg('homepage');
77 $this->description = $this->arg('description');
78 $this->location = $this->arg('location');
79 $this->aliasstring = $this->arg('aliases');
89 * @param array $args $_REQUEST data (unused)
93 function handle($args)
95 parent::handle($args);
97 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
99 // TRANS: Client error. POST is a HTTP command. It should not be translated.
100 _('This method requires a POST.'),
107 if (empty($this->user)) {
108 // TRANS: Client error given when a user was not found (404).
109 $this->clientError(_('No such user.'), 404, $this->format);
113 if ($this->validateParams() == false) {
117 $group = User_group::register(array('nickname' => $this->nickname,
118 'fullname' => $this->fullname,
119 'homepage' => $this->homepage,
120 'description' => $this->description,
121 'location' => $this->location,
122 'aliases' => $this->aliases,
123 'userid' => $this->user->id,
126 switch($this->format) {
128 $this->showSingleXmlGroup($group);
131 $this->showSingleJsonGroup($group);
135 // TRANS: Client error displayed when coming across a non-supported API method.
136 _('API method not found.'),
145 * Validate params for the new group
149 function validateParams()
151 if ($this->groupNicknameExists($this->nickname)) {
153 // TRANS: Client error trying to create a group with a nickname this is already in use.
154 _('Nickname already in use. Try another one.'),
159 } else if (!User_group::allowedNickname($this->nickname)) {
161 // TRANS: Client error in form for group creation.
162 _('Not a valid nickname.'),
169 !is_null($this->homepage)
170 && strlen($this->homepage) > 0
172 $this->homepage, array(
174 array('http', 'https')
178 // TRANS: Client error in form for group creation.
179 _('Homepage is not a valid URL.'),
185 !is_null($this->fullname)
186 && mb_strlen($this->fullname) > 255) {
188 // TRANS: Client error in form for group creation.
189 _('Full name is too long (maximum 255 characters).'),
194 } elseif (User_group::descriptionTooLong($this->description)) {
197 // TRANS: Client error shown when providing too long a description during group creation.
198 // TRANS: %d is the maximum number of allowed characters.
199 _m('Description is too long (maximum %d character).',
200 'Description is too long (maximum %d characters).',
201 User_group::maxDescription()),
202 User_group::maxDescription()
209 !is_null($this->location)
210 && mb_strlen($this->location) > 255) {
212 // TRANS: Client error shown when providing too long a location during group creation.
213 _('Location is too long (maximum 255 characters).'),
220 if (!empty($this->aliasstring)) {
221 $this->aliases = array_map(
222 'common_canonical_nickname',
223 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
226 $this->aliases = array();
229 if (count($this->aliases) > common_config('group', 'maxaliases')) {
232 // TRANS: Client error shown when providing too many aliases during group creation.
233 // TRANS: %d is the maximum number of allowed aliases.
234 _m('Too many aliases! Maximum %d allowed.',
235 'Too many aliases! Maximum %d allowed.',
236 common_config('group', 'maxaliases')),
237 common_config('group', 'maxaliases')
245 foreach ($this->aliases as $alias) {
247 if (!Nickname::isValid($alias)) {
249 // TRANS: Client error shown when providing an invalid alias during group creation.
250 // TRANS: %s is the invalid alias.
251 sprintf(_('Invalid alias: "%s".'), $alias),
257 if ($this->groupNicknameExists($alias)) {
260 // TRANS: Client error displayed when trying to use an alias during group creation that is already in use.
261 // TRANS: %s is the alias that is already in use.
262 _('Alias "%s" already in use. Try another one.'),
271 // XXX assumes alphanum nicknames
273 if (strcmp($alias, $this->nickname) == 0) {
275 // TRANS: Client error displayed when trying to use an alias during group creation that is the same as the group's nickname.
276 _('Alias can\'t be the same as nickname.'),
284 // Everything looks OK
290 * Check to see whether a nickname is already in use by a group
292 * @param String $nickname The nickname in question
294 * @return boolean true or false
296 function groupNicknameExists($nickname)
298 $local = Local_group::getKV('nickname', $nickname);
300 if (!empty($local)) {
304 $alias = Group_alias::getKV('alias', $nickname);
306 if (!empty($alias)) {