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')) {
39 * Make a new group. Sets the authenticated user as the administrator of the group.
43 * @author Craig Andrews <candrews@integralblue.com>
44 * @author Evan Prodromou <evan@status.net>
45 * @author Jeffery To <jeffery.to@gmail.com>
46 * @author Zach Copley <zach@status.net>
47 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
48 * @link http://status.net/
50 class ApiGroupCreateAction extends ApiAuthAction
56 var $description = null;
58 var $aliasstring = null;
62 * Take arguments for running
64 * @param array $args $_REQUEST args
66 * @return boolean success flag
68 function prepare($args)
70 parent::prepare($args);
72 $this->user = $this->auth_user;
74 $this->nickname = Nickname::normalize($this->arg('nickname'));
75 $this->fullname = $this->arg('full_name');
76 $this->homepage = $this->arg('homepage');
77 $this->description = $this->arg('description');
78 $this->location = $this->arg('location');
79 $this->aliasstring = $this->arg('aliases');
89 * @param array $args $_REQUEST data (unused)
93 function handle($args)
95 parent::handle($args);
97 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
99 // TRANS: Client error. POST is a HTTP command. It should not be translated.
100 _('This method requires a POST.'),
107 if (empty($this->user)) {
108 // TRANS: Client error given when a user was not found (404).
109 $this->clientError(_('No such user.'), 404, $this->format);
113 if ($this->validateParams() == false) {
117 $group = User_group::register(array('nickname' => $this->nickname,
118 'fullname' => $this->fullname,
119 'homepage' => $this->homepage,
120 'description' => $this->description,
121 'location' => $this->location,
122 'aliases' => $this->aliases,
123 'userid' => $this->user->id,
126 switch($this->format) {
128 $this->showSingleXmlGroup($group);
131 $this->showSingleJsonGroup($group);
135 // TRANS: Client error displayed when coming across a non-supported API method.
136 _('API method not found.'),
145 * Validate params for the new group
149 function validateParams()
151 if ($this->groupNicknameExists($this->nickname)) {
153 // TRANS: Client error trying to create a group with a nickname this is already in use.
154 _('Nickname already in use. Try another one.'),
159 } else if (!User_group::allowedNickname($this->nickname)) {
161 // TRANS: Client error in form for group creation.
162 _('Not a valid nickname.'),
168 } elseif (!is_null($this->homepage)
169 && strlen($this->homepage) > 0
170 && !common_valid_http_url($this->homepage)) {
172 // TRANS: Client error in form for group creation.
173 _('Homepage is not a valid URL.'),
179 !is_null($this->fullname)
180 && mb_strlen($this->fullname) > 255) {
182 // TRANS: Client error in form for group creation.
183 _('Full name is too long (maximum 255 characters).'),
188 } elseif (User_group::descriptionTooLong($this->description)) {
191 // TRANS: Client error shown when providing too long a description during group creation.
192 // TRANS: %d is the maximum number of allowed characters.
193 _m('Description is too long (maximum %d character).',
194 'Description is too long (maximum %d characters).',
195 User_group::maxDescription()),
196 User_group::maxDescription()
203 !is_null($this->location)
204 && mb_strlen($this->location) > 255) {
206 // TRANS: Client error shown when providing too long a location during group creation.
207 _('Location is too long (maximum 255 characters).'),
214 if (!empty($this->aliasstring)) {
215 $this->aliases = array_map(
216 'common_canonical_nickname',
217 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
220 $this->aliases = array();
223 if (count($this->aliases) > common_config('group', 'maxaliases')) {
226 // TRANS: Client error shown when providing too many aliases during group creation.
227 // TRANS: %d is the maximum number of allowed aliases.
228 _m('Too many aliases! Maximum %d allowed.',
229 'Too many aliases! Maximum %d allowed.',
230 common_config('group', 'maxaliases')),
231 common_config('group', 'maxaliases')
239 foreach ($this->aliases as $alias) {
241 if (!Nickname::isValid($alias)) {
243 // TRANS: Client error shown when providing an invalid alias during group creation.
244 // TRANS: %s is the invalid alias.
245 sprintf(_('Invalid alias: "%s".'), $alias),
251 if ($this->groupNicknameExists($alias)) {
254 // TRANS: Client error displayed when trying to use an alias during group creation that is already in use.
255 // TRANS: %s is the alias that is already in use.
256 _('Alias "%s" already in use. Try another one.'),
265 // XXX assumes alphanum nicknames
267 if (strcmp($alias, $this->nickname) == 0) {
269 // TRANS: Client error displayed when trying to use an alias during group creation that is the same as the group's nickname.
270 _('Alias can\'t be the same as nickname.'),
278 // Everything looks OK
284 * Check to see whether a nickname is already in use by a group
286 * @param String $nickname The nickname in question
288 * @return boolean true or false
290 function groupNicknameExists($nickname)
292 $local = Local_group::getKV('nickname', $nickname);
294 if (!empty($local)) {
298 $alias = Group_alias::getKV('alias', $nickname);
300 if (!empty($alias)) {