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 if (!Validate::string(
206 $this->nickname, array(
209 'format' => NICKNAME_FMT)
215 'Nickname must have only lowercase letters ' .
216 'and numbers and no spaces.'
222 } elseif ($this->groupNicknameExists($this->nickname)) {
224 _('Nickname already in use. Try another one.'),
229 } else if (!User_group::allowedNickname($this->nickname)) {
231 _('Not a valid nickname.'),
237 } elseif (!is_null($this->homepage)
238 && strlen($this->homepage) > 0
240 $this->homepage, array(
242 array('http', 'https')
247 _('Homepage is not a valid URL.'),
252 } elseif (!is_null($this->fullname)
253 && mb_strlen($this->fullname) > 255)
256 _('Full name is too long (max 255 chars).'),
261 } elseif (User_group::descriptionTooLong($this->description)) {
262 $this->clientError(sprintf(
263 _('Description is too long (max %d chars).'),
264 User_group::maxDescription()),
269 } elseif (!is_null($this->location)
270 && mb_strlen($this->location) > 255)
273 _('Location is too long (max 255 chars).'),
280 if (!empty($this->aliasstring)) {
281 $this->aliases = array_map(
282 'common_canonical_nickname',
283 array_unique(preg_split('/[\s,]+/',
288 $this->aliases = array();
291 if (count($this->aliases) > common_config('group', 'maxaliases')) {
293 sprintf(_('Too many aliases! Maximum %d.'),
294 common_config('group', 'maxaliases')),
301 foreach ($this->aliases as $alias) {
302 if (!Validate::string($alias, array(
305 'format' => NICKNAME_FMT
310 sprintf(_('Invalid alias: "%s"'), $alias),
316 if ($this->groupNicknameExists($alias)) {
318 sprintf(_('Alias "%s" already in use. Try another one.'),
326 // XXX assumes alphanum nicknames
328 if (strcmp($alias, $this->nickname) == 0) {
330 _('Alias can\'t be the same as nickname.'),
338 // Evarything looks OK
343 function groupNicknameExists($nickname)
345 $group = User_group::staticGet('nickname', $nickname);
347 if (!empty($group)) {
351 $alias = Group_alias::staticGet('alias', $nickname);
353 if (!empty($alias)) {