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
52 protected $needPost = true;
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 protected function prepare(array $args=array())
72 parent::prepare($args);
74 $this->nickname = Nickname::normalize($this->arg('nickname'), true);
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');
91 protected function handle()
95 if (empty($this->user)) {
96 // TRANS: Client error given when a user was not found (404).
97 $this->clientError(_('No such user.'), 404);
100 if ($this->validateParams() == false) {
104 $group = User_group::register(array('nickname' => $this->nickname,
105 'fullname' => $this->fullname,
106 'homepage' => $this->homepage,
107 'description' => $this->description,
108 'location' => $this->location,
109 'aliases' => $this->aliases,
110 'userid' => $this->user->id,
113 switch($this->format) {
115 $this->showSingleXmlGroup($group);
118 $this->showSingleJsonGroup($group);
121 // TRANS: Client error displayed when coming across a non-supported API method.
122 $this->clientError(_('API method not found.'), 404);
127 * Validate params for the new group
131 function validateParams()
133 if (!is_null($this->homepage)
134 && strlen($this->homepage) > 0
135 && !common_valid_http_url($this->homepage)) {
136 // TRANS: Client error in form for group creation.
137 $this->clientError(_('Homepage is not a valid URL.'), 403);
139 } elseif (!is_null($this->fullname)
140 && mb_strlen($this->fullname) > 255) {
141 // TRANS: Client error in form for group creation.
142 $this->clientError(_('Full name is too long (maximum 255 characters).'), 403);
144 } elseif (User_group::descriptionTooLong($this->description)) {
145 // TRANS: Client error shown when providing too long a description during group creation.
146 // TRANS: %d is the maximum number of allowed characters.
147 $this->clientError(sprintf(_m('Description is too long (maximum %d character).',
148 'Description is too long (maximum %d characters).',
149 User_group::maxDescription()), User_group::maxDescription()), 403);
151 } elseif (!is_null($this->location)
152 && mb_strlen($this->location) > 255) {
153 // TRANS: Client error shown when providing too long a location during group creation.
154 $this->clientError(_('Location is too long (maximum 255 characters).'), 403);
157 if (!empty($this->aliasstring)) {
158 $this->aliases = array_map(
159 array('Nickname', 'normalize'), // static call to Nickname::normalize
160 array_unique(preg_split('/[\s,]+/', $this->aliasstring))
163 $this->aliases = array();
166 if (count($this->aliases) > common_config('group', 'maxaliases')) {
167 $this->clientError(sprintf(
168 // TRANS: Client error shown when providing too many aliases during group creation.
169 // TRANS: %d is the maximum number of allowed aliases.
170 _m('Too many aliases! Maximum %d allowed.',
171 'Too many aliases! Maximum %d allowed.',
172 common_config('group', 'maxaliases')),
173 common_config('group', 'maxaliases')),
177 // Everything looks OK