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 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
30 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
31 * @link http://status.net/
34 if (!defined('STATUSNET')) {
38 require_once INSTALLDIR . '/lib/apiauth.php';
41 * Make a new group. Sets the authenticated user as the administrator of the group.
45 * @author Craig Andrews <candrews@integralblue.com>
46 * @author Evan Prodromou <evan@status.net>
47 * @author Jeffery To <jeffery.to@gmail.com>
48 * @author Zach Copley <zach@status.net>
49 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50 * @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
70 function prepare($args)
72 parent::prepare($args);
74 $this->user = $this->auth_user;
76 $this->nickname = Nickname::normalize($this->arg('nickname'));
77 $this->fullname = $this->arg('full_name');
78 $this->homepage = $this->arg('homepage');
79 $this->description = $this->arg('description');
80 $this->location = $this->arg('location');
81 $this->aliasstring = $this->arg('aliases');
91 * @param array $args $_REQUEST data (unused)
95 function handle($args)
97 parent::handle($args);
99 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
101 // TRANS: Client error. POST is a HTTP command. It should not be translated.
102 _('This method requires a POST.'),
109 if (empty($this->user)) {
110 // TRANS: Client error given when a user was not found (404).
111 $this->clientError(_('No such user.'), 404, $this->format);
115 if ($this->validateParams() == false) {
119 $group = User_group::register(array('nickname' => $this->nickname,
120 'fullname' => $this->fullname,
121 'homepage' => $this->homepage,
122 'description' => $this->description,
123 'location' => $this->location,
124 'aliases' => $this->aliases,
125 'userid' => $this->user->id,
128 switch($this->format) {
130 $this->showSingleXmlGroup($group);
133 $this->showSingleJsonGroup($group);
137 // TRANS: Client error given when an API method was not found (404).
138 _('API method not found.'),
147 * Validate params for the new group
151 function validateParams()
153 if ($this->groupNicknameExists($this->nickname)) {
155 // TRANS: Client error trying to create a group with a nickname this is already in use.
156 _('Nickname already in use. Try another one.'),
161 } else if (!User_group::allowedNickname($this->nickname)) {
163 // TRANS: Client error in form for group creation.
164 _('Not a valid nickname.'),
171 !is_null($this->homepage)
172 && strlen($this->homepage) > 0
174 $this->homepage, array(
176 array('http', 'https')
180 // TRANS: Client error in form for group creation.
181 _('Homepage is not a valid URL.'),
187 !is_null($this->fullname)
188 && mb_strlen($this->fullname) > 255) {
190 // TRANS: Client error in form for group creation.
191 _('Full name is too long (maximum 255 characters).'),
196 } elseif (User_group::descriptionTooLong($this->description)) {
199 // TRANS: Client error shown when providing too long a description during group creation.
200 // TRANS: %d is the maximum number of allowed characters.
201 _m('Description is too long (maximum %d character).',
202 'Description is too long (maximum %d characters).',
203 User_group::maxDescription()),
204 User_group::maxDescription()
211 !is_null($this->location)
212 && mb_strlen($this->location) > 255) {
214 // TRANS: Client error shown when providing too long a location during group creation.
215 _('Location is too long (maximum 255 characters).'),
222 if (!empty($this->aliasstring)) {
223 $this->aliases = array_map(
224 'common_canonical_nickname',
225 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
228 $this->aliases = array();
231 if (count($this->aliases) > common_config('group', 'maxaliases')) {
234 // TRANS: Client error shown when providing too many aliases during group creation.
235 // TRANS: %d is the maximum number of allowed aliases.
236 _m('Too many aliases! Maximum %d allowed.',
237 'Too many aliases! Maximum %d allowed.',
238 common_config('group', 'maxaliases')),
239 common_config('group', 'maxaliases')
247 foreach ($this->aliases as $alias) {
249 if (!Nickname::isValid($alias)) {
251 // TRANS: Client error shown when providing an invalid alias during group creation.
252 // TRANS: %s is the invalid alias.
253 sprintf(_('Invalid alias: "%s".'), $alias),
259 if ($this->groupNicknameExists($alias)) {
262 // TRANS: Client error displayed when trying to use an alias during group creation that is already in use.
263 // TRANS: %s is the alias that is already in use.
264 _('Alias "%s" already in use. Try another one.'),
273 // XXX assumes alphanum nicknames
275 if (strcmp($alias, $this->nickname) == 0) {
277 // TRANS: Client error displayed when trying to use an alias during group creation that is the same as the group's nickname.
278 _('Alias can\'t be the same as nickname.'),
286 // Everything looks OK
292 * Check to see whether a nickname is already in use by a group
294 * @param String $nickname The nickname in question
296 * @return boolean true or false
298 function groupNicknameExists($nickname)
300 $local = Local_group::staticGet('nickname', $nickname);
302 if (!empty($local)) {
306 $alias = Group_alias::staticGet('alias', $nickname);
308 if (!empty($alias)) {