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 (!common_config('inboxes', 'enabled')) {
104 _('Inboxes must be enabled for groups to work'),
111 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
113 _('This method requires a POST.'),
120 if (empty($this->user)) {
121 $this->clientError(_('No such user!'), 404, $this->format);
125 if ($this->validateParams() == false) {
129 $group = new User_group();
131 $group->query('BEGIN');
133 $group->nickname = $this->nickname;
134 $group->fullname = $this->fullname;
135 $group->homepage = $this->homepage;
136 $group->description = $this->description;
137 $group->location = $this->location;
138 $group->created = common_sql_now();
140 $result = $group->insert();
143 common_log_db_error($group, 'INSERT', __FILE__);
145 _('Could not create group.'),
152 $result = $group->setAliases($this->aliases);
156 _('Could not create aliases.'),
163 $member = new Group_member();
165 $member->group_id = $group->id;
166 $member->profile_id = $this->user->id;
167 $member->is_admin = 1;
168 $member->created = $group->created;
170 $result = $member->insert();
173 common_log_db_error($member, 'INSERT', __FILE__);
175 _('Could not set group membership.'),
182 $group->query('COMMIT');
184 switch($this->format) {
186 $this->showSingleXmlGroup($group);
189 $this->showSingleJsonGroup($group);
193 _('API method not found!'),
203 * Validate params for the new group
208 function validateParams()
210 $valid = Validate::string(
211 $this->nickname, array(
214 'format' => NICKNAME_FMT
221 'Nickname must have only lowercase letters ' .
222 'and numbers and no spaces.'
228 } elseif ($this->groupNicknameExists($this->nickname)) {
230 _('Nickname already in use. Try another one.'),
235 } else if (!User_group::allowedNickname($this->nickname)) {
237 _('Not a valid nickname.'),
244 !is_null($this->homepage)
245 && strlen($this->homepage) > 0
247 $this->homepage, array(
249 array('http', 'https')
253 _('Homepage is not a valid URL.'),
259 !is_null($this->fullname)
260 && mb_strlen($this->fullname) > 255) {
262 _('Full name is too long (max 255 chars).'),
267 } elseif (User_group::descriptionTooLong($this->description)) {
270 _('Description is too long (max %d chars).'),
271 User_group::maxDescription()
278 !is_null($this->location)
279 && mb_strlen($this->location) > 255) {
281 _('Location is too long (max 255 chars).'),
288 if (!empty($this->aliasstring)) {
289 $this->aliases = array_map(
290 'common_canonical_nickname',
291 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
294 $this->aliases = array();
297 if (count($this->aliases) > common_config('group', 'maxaliases')) {
300 _('Too many aliases! Maximum %d.'),
301 common_config('group', 'maxaliases')
309 foreach ($this->aliases as $alias) {
311 $valid = Validate::string(
315 'format' => NICKNAME_FMT
321 sprintf(_('Invalid alias: "%s"'), $alias),
327 if ($this->groupNicknameExists($alias)) {
330 _('Alias "%s" already in use. Try another one.'),
339 // XXX assumes alphanum nicknames
341 if (strcmp($alias, $this->nickname) == 0) {
343 _('Alias can\'t be the same as nickname.'),
351 // Evarything looks OK
357 * Check to see whether a nickname is already in use by a group
359 * @param String $nickname The nickname in question
361 * @return boolean true or false
364 function groupNicknameExists($nickname)
366 $group = User_group::staticGet('nickname', $nickname);
368 if (!empty($group)) {
372 $alias = Group_alias::staticGet('alias', $nickname);
374 if (!empty($alias)) {