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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30 * @link http://status.net/
33 if (!defined('STATUSNET')) {
37 require_once INSTALLDIR . '/lib/apiauth.php';
40 * Make a new group. Sets the authenticated user as the administrator of the group.
44 * @author Craig Andrews <candrews@integralblue.com>
45 * @author Evan Prodromou <evan@status.net>
46 * @author Jeffery To <jeffery.to@gmail.com>
47 * @author Zach Copley <zach@status.net>
48 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
49 * @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
72 function prepare($args)
74 parent::prepare($args);
76 $this->user = $this->auth_user;
78 $this->nickname = $this->arg('nickname');
79 $this->fullname = $this->arg('full_name');
80 $this->homepage = $this->arg('homepage');
81 $this->description = $this->arg('description');
82 $this->location = $this->arg('location');
83 $this->aliasstring = $this->arg('aliases');
93 * @param array $args $_REQUEST data (unused)
98 function handle($args)
100 parent::handle($args);
102 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
104 _('This method requires a POST.'),
111 if (empty($this->user)) {
112 $this->clientError(_('No such user.'), 404, $this->format);
116 if ($this->validateParams() == false) {
120 $group = User_group::register(array('nickname' => $this->nickname,
121 'fullname' => $this->fullname,
122 'homepage' => $this->homepage,
123 'description' => $this->description,
124 'location' => $this->location,
125 'aliases' => $this->aliases,
126 'userid' => $this->user->id));
127 switch($this->format) {
129 $this->showSingleXmlGroup($group);
132 $this->showSingleJsonGroup($group);
136 _('API method not found!'),
146 * Validate params for the new group
151 function validateParams()
153 $valid = Validate::string(
154 $this->nickname, array(
157 'format' => NICKNAME_FMT
164 'Nickname must have only lowercase letters ' .
165 'and numbers and no spaces.'
171 } elseif ($this->groupNicknameExists($this->nickname)) {
173 _('Nickname already in use. Try another one.'),
178 } else if (!User_group::allowedNickname($this->nickname)) {
180 _('Not a valid nickname.'),
187 !is_null($this->homepage)
188 && strlen($this->homepage) > 0
190 $this->homepage, array(
192 array('http', 'https')
196 _('Homepage is not a valid URL.'),
202 !is_null($this->fullname)
203 && mb_strlen($this->fullname) > 255) {
205 _('Full name is too long (max 255 chars).'),
210 } elseif (User_group::descriptionTooLong($this->description)) {
213 _('Description is too long (max %d chars).'),
214 User_group::maxDescription()
221 !is_null($this->location)
222 && mb_strlen($this->location) > 255) {
224 _('Location is too long (max 255 chars).'),
231 if (!empty($this->aliasstring)) {
232 $this->aliases = array_map(
233 'common_canonical_nickname',
234 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
237 $this->aliases = array();
240 if (count($this->aliases) > common_config('group', 'maxaliases')) {
243 _('Too many aliases! Maximum %d.'),
244 common_config('group', 'maxaliases')
252 foreach ($this->aliases as $alias) {
254 $valid = Validate::string(
258 'format' => NICKNAME_FMT
264 sprintf(_('Invalid alias: "%s"'), $alias),
270 if ($this->groupNicknameExists($alias)) {
273 _('Alias "%s" already in use. Try another one.'),
282 // XXX assumes alphanum nicknames
284 if (strcmp($alias, $this->nickname) == 0) {
286 _('Alias can\'t be the same as nickname.'),
294 // Evarything looks OK
300 * Check to see whether a nickname is already in use by a group
302 * @param String $nickname The nickname in question
304 * @return boolean true or false
307 function groupNicknameExists($nickname)
309 $group = User_group::staticGet('nickname', $nickname);
311 if (!empty($group)) {
315 $alias = Group_alias::staticGet('alias', $nickname);
317 if (!empty($alias)) {