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')) {
38 require_once INSTALLDIR . '/lib/apiauth.php';
41 * Make a new group. Sets the authenticated user as the administrator of the group.
45 * @author Craig Andrews <candrews@integralblue.com>
46 * @author Evan Prodromou <evan@status.net>
47 * @author Jeffery To <jeffery.to@gmail.com>
48 * @author Zach Copley <zach@status.net>
49 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50 * @link http://status.net/
52 class ApiGroupCreateAction extends ApiAuthAction
58 var $description = null;
60 var $aliasstring = null;
64 * Take arguments for running
66 * @param array $args $_REQUEST args
68 * @return boolean success flag
70 function prepare($args)
72 parent::prepare($args);
74 $this->user = $this->auth_user;
76 $this->nickname = $this->arg('nickname');
77 $this->fullname = $this->arg('full_name');
78 $this->homepage = $this->arg('homepage');
79 $this->description = $this->arg('description');
80 $this->location = $this->arg('location');
81 $this->aliasstring = $this->arg('aliases');
91 * @param array $args $_REQUEST data (unused)
95 function handle($args)
97 parent::handle($args);
99 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
101 // TRANS: Client error. POST is a HTTP command. It should not be translated.
102 _('This method requires a POST.'),
109 if (empty($this->user)) {
110 // TRANS: Client error given when a user was not found (404).
111 $this->clientError(_('No such user.'), 404, $this->format);
115 if ($this->validateParams() == false) {
119 $group = User_group::register(array('nickname' => $this->nickname,
120 'fullname' => $this->fullname,
121 'homepage' => $this->homepage,
122 'description' => $this->description,
123 'location' => $this->location,
124 'aliases' => $this->aliases,
125 'userid' => $this->user->id,
128 switch($this->format) {
130 $this->showSingleXmlGroup($group);
133 $this->showSingleJsonGroup($group);
137 // TRANS: Client error given when an API method was not found (404).
138 _('API method not found.'),
147 * Validate params for the new group
151 function validateParams()
153 $valid = Validate::string(
154 $this->nickname, array(
157 'format' => NICKNAME_FMT
163 // TRANS: Validation error in form for group creation.
165 'Nickname must have only lowercase letters ' .
166 'and numbers and no spaces.'
172 } elseif ($this->groupNicknameExists($this->nickname)) {
174 // TRANS: Client error trying to create a group with a nickname this is already in use.
175 _('Nickname already in use. Try another one.'),
180 } else if (!User_group::allowedNickname($this->nickname)) {
182 // TRANS: Client error in form for group creation.
183 _('Not a valid nickname.'),
190 !is_null($this->homepage)
191 && strlen($this->homepage) > 0
193 $this->homepage, array(
195 array('http', 'https')
199 // TRANS: Client error in form for group creation.
200 _('Homepage is not a valid URL.'),
206 !is_null($this->fullname)
207 && mb_strlen($this->fullname) > 255) {
209 // TRANS: Client error in form for group creation.
210 _('Full name is too long (maximum 255 characters).'),
215 } elseif (User_group::descriptionTooLong($this->description)) {
218 // TRANS: Client error shown when providing too long a description during group creation.
219 // TRANS: %d is the maximum number of allowed characters.
220 _m('Description is too long (maximum %d character).',
221 'Description is too long (maximum %d characters).',
222 User_group::maxDescription()),
223 User_group::maxDescription()
230 !is_null($this->location)
231 && mb_strlen($this->location) > 255) {
233 // TRANS: Client error shown when providing too long a location during group creation.
234 _('Location is too long (maximum 255 characters).'),
241 if (!empty($this->aliasstring)) {
242 $this->aliases = array_map(
243 'common_canonical_nickname',
244 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
247 $this->aliases = array();
250 if (count($this->aliases) > common_config('group', 'maxaliases')) {
253 // TRANS: Client error shown when providing too many aliases during group creation.
254 // TRANS: %d is the maximum number of allowed aliases.
255 _m('Too many aliases! Maximum %d allowed.',
256 'Too many aliases! Maximum %d allowed.',
257 common_config('group', 'maxaliases')),
258 common_config('group', 'maxaliases')
266 foreach ($this->aliases as $alias) {
268 $valid = Validate::string(
272 'format' => NICKNAME_FMT
278 // TRANS: Client error shown when providing an invalid alias during group creation.
279 // TRANS: %s is the invalid alias.
280 sprintf(_('Invalid alias: "%s".'), $alias),
286 if ($this->groupNicknameExists($alias)) {
289 // TRANS: Client error displayed when trying to use an alias during group creation that is already in use.
290 // TRANS: %s is the alias that is already in use.
291 _('Alias "%s" already in use. Try another one.'),
300 // XXX assumes alphanum nicknames
302 if (strcmp($alias, $this->nickname) == 0) {
304 // TRANS: Client error displayed when trying to use an alias during group creation that is the same as the group's nickname.
305 _('Alias can\'t be the same as nickname.'),
313 // Everything looks OK
319 * Check to see whether a nickname is already in use by a group
321 * @param String $nickname The nickname in question
323 * @return boolean true or false
325 function groupNicknameExists($nickname)
327 $local = Local_group::staticGet('nickname', $nickname);
329 if (!empty($local)) {
333 $alias = Group_alias::staticGet('alias', $nickname);
335 if (!empty($alias)) {