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 Zach Copley <zach@status.net>
26 * @copyright 2009 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
35 require_once INSTALLDIR . '/lib/apiauth.php';
38 * Make a new group. Sets the authenticated user as the administrator of the group.
42 * @author Zach Copley <zach@status.net>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://status.net/
47 class ApiGroupCreateAction extends ApiAuthAction
53 var $description = null;
55 var $aliasstring = null;
59 * Take arguments for running
61 * @param array $args $_REQUEST args
63 * @return boolean success flag
67 function prepare($args)
69 parent::prepare($args);
71 $this->user = $this->auth_user;
73 $this->nickname = $this->arg('nickname');
74 $this->fullname = $this->arg('full_name');
75 $this->homepage = $this->arg('homepage');
76 $this->description = $this->arg('description');
77 $this->location = $this->arg('location');
78 $this->aliasstring = $this->arg('aliases');
88 * @param array $args $_REQUEST data (unused)
93 function handle($args)
95 parent::handle($args);
97 if (!common_config('inboxes', 'enabled')) {
99 _('Inboxes must be enabled for groups to work'),
106 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
108 _('This method requires a POST.'),
115 if (empty($this->user)) {
116 $this->clientError(_('No such user!'), 404, $this->format);
120 if ($this->validateParams() == false) {
124 $group = new User_group();
126 $group->query('BEGIN');
128 $group->nickname = $this->nickname;
129 $group->fullname = $this->fullname;
130 $group->homepage = $this->homepage;
131 $group->description = $this->description;
132 $group->location = $this->location;
133 $group->created = common_sql_now();
135 $result = $group->insert();
138 common_log_db_error($group, 'INSERT', __FILE__);
140 _('Could not create group.'),
147 $result = $group->setAliases($this->aliases);
151 _('Could not create aliases.'),
158 $member = new Group_member();
160 $member->group_id = $group->id;
161 $member->profile_id = $this->user->id;
162 $member->is_admin = 1;
163 $member->created = $group->created;
165 $result = $member->insert();
168 common_log_db_error($member, 'INSERT', __FILE__);
170 _('Could not set group membership.'),
177 $group->query('COMMIT');
179 switch($this->format) {
181 $this->showSingleXmlGroup($group);
184 $this->showSingleJsonGroup($group);
188 _('API method not found!'),
198 * Validate params for the new group
203 function validateParams()
205 $valid = Validate::string(
206 $this->nickname, array(
209 'format' => NICKNAME_FMT
216 'Nickname must have only lowercase letters ' .
217 'and numbers and no spaces.'
223 } elseif ($this->groupNicknameExists($this->nickname)) {
225 _('Nickname already in use. Try another one.'),
230 } else if (!User_group::allowedNickname($this->nickname)) {
232 _('Not a valid nickname.'),
239 !is_null($this->homepage)
240 && strlen($this->homepage) > 0
242 $this->homepage, array(
244 array('http', 'https')
248 _('Homepage is not a valid URL.'),
254 !is_null($this->fullname)
255 && mb_strlen($this->fullname) > 255) {
257 _('Full name is too long (max 255 chars).'),
262 } elseif (User_group::descriptionTooLong($this->description)) {
265 _('Description is too long (max %d chars).'),
266 User_group::maxDescription()
273 !is_null($this->location)
274 && mb_strlen($this->location) > 255) {
276 _('Location is too long (max 255 chars).'),
283 if (!empty($this->aliasstring)) {
284 $this->aliases = array_map(
285 'common_canonical_nickname',
286 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
289 $this->aliases = array();
292 if (count($this->aliases) > common_config('group', 'maxaliases')) {
295 _('Too many aliases! Maximum %d.'),
296 common_config('group', 'maxaliases')
304 foreach ($this->aliases as $alias) {
306 $valid = Validate::string(
310 'format' => NICKNAME_FMT
316 sprintf(_('Invalid alias: "%s"'), $alias),
322 if ($this->groupNicknameExists($alias)) {
325 _('Alias "%s" already in use. Try another one.'),
334 // XXX assumes alphanum nicknames
336 if (strcmp($alias, $this->nickname) == 0) {
338 _('Alias can\'t be the same as nickname.'),
346 // Evarything looks OK
352 * Check to see whether a nickname is already in use by a group
354 * @param String $nickname The nickname in question
356 * @return boolean true or false
359 function groupNicknameExists($nickname)
361 $group = User_group::staticGet('nickname', $nickname);
363 if (!empty($group)) {
367 $alias = Group_alias::staticGet('alias', $nickname);
369 if (!empty($alias)) {