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 = new User_group();
122 $group->query('BEGIN');
124 $group->nickname = $this->nickname;
125 $group->fullname = $this->fullname;
126 $group->homepage = $this->homepage;
127 $group->description = $this->description;
128 $group->location = $this->location;
129 $group->created = common_sql_now();
131 $result = $group->insert();
134 common_log_db_error($group, 'INSERT', __FILE__);
136 _('Could not create group.'),
143 $result = $group->setAliases($this->aliases);
147 _('Could not create aliases.'),
154 $member = new Group_member();
156 $member->group_id = $group->id;
157 $member->profile_id = $this->user->id;
158 $member->is_admin = 1;
159 $member->created = $group->created;
161 $result = $member->insert();
164 common_log_db_error($member, 'INSERT', __FILE__);
166 _('Could not set group membership.'),
173 $group->query('COMMIT');
175 switch($this->format) {
177 $this->showSingleXmlGroup($group);
180 $this->showSingleJsonGroup($group);
184 _('API method not found!'),
194 * Validate params for the new group
199 function validateParams()
201 $valid = Validate::string(
202 $this->nickname, array(
205 'format' => NICKNAME_FMT
212 'Nickname must have only lowercase letters ' .
213 'and numbers and no spaces.'
219 } elseif ($this->groupNicknameExists($this->nickname)) {
221 _('Nickname already in use. Try another one.'),
226 } else if (!User_group::allowedNickname($this->nickname)) {
228 _('Not a valid nickname.'),
235 !is_null($this->homepage)
236 && strlen($this->homepage) > 0
238 $this->homepage, array(
240 array('http', 'https')
244 _('Homepage is not a valid URL.'),
250 !is_null($this->fullname)
251 && mb_strlen($this->fullname) > 255) {
253 _('Full name is too long (max 255 chars).'),
258 } elseif (User_group::descriptionTooLong($this->description)) {
261 _('Description is too long (max %d chars).'),
262 User_group::maxDescription()
269 !is_null($this->location)
270 && mb_strlen($this->location) > 255) {
272 _('Location is too long (max 255 chars).'),
279 if (!empty($this->aliasstring)) {
280 $this->aliases = array_map(
281 'common_canonical_nickname',
282 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
285 $this->aliases = array();
288 if (count($this->aliases) > common_config('group', 'maxaliases')) {
291 _('Too many aliases! Maximum %d.'),
292 common_config('group', 'maxaliases')
300 foreach ($this->aliases as $alias) {
302 $valid = Validate::string(
306 'format' => NICKNAME_FMT
312 sprintf(_('Invalid alias: "%s"'), $alias),
318 if ($this->groupNicknameExists($alias)) {
321 _('Alias "%s" already in use. Try another one.'),
330 // XXX assumes alphanum nicknames
332 if (strcmp($alias, $this->nickname) == 0) {
334 _('Alias can\'t be the same as nickname.'),
342 // Evarything looks OK
348 * Check to see whether a nickname is already in use by a group
350 * @param String $nickname The nickname in question
352 * @return boolean true or false
355 function groupNicknameExists($nickname)
357 $group = User_group::staticGet('nickname', $nickname);
359 if (!empty($group)) {
363 $alias = Group_alias::staticGet('alias', $nickname);
365 if (!empty($alias)) {