3 * Laconica, the distributed open-source microblogging tool
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 Evan Prodromou <evan@controlyourself.ca>
25 * @author Sarven Capadisli <csarven@controlyourself.ca>
26 * @copyright 2008-2009 Control Yourself, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://laconi.ca/
31 if (!defined('LACONICA')) {
38 * This is the form for adding a new group
42 * @author Evan Prodromou <evan@controlyourself.ca>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://laconi.ca/
47 class NewgroupAction extends Action
53 return _('New group');
60 function prepare($args)
62 parent::prepare($args);
64 if (!common_config('inboxes','enabled')) {
65 $this->serverError(_('Inboxes must be enabled for groups to work'));
69 if (!common_logged_in()) {
70 $this->clientError(_('You must be logged in to create a group.'));
80 * On GET, show the form. On POST, try to save the group.
82 * @param array $args unused
87 function handle($args)
89 parent::handle($args);
90 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
97 function showForm($msg=null)
103 function showContent()
105 $form = new GroupEditForm($this);
109 function showPageNotice()
112 $this->element('p', 'error', $this->msg);
114 $this->element('p', 'instructions',
115 _('Use this form to create a new group.'));
121 $nickname = $this->trimmed('nickname');
122 $fullname = $this->trimmed('fullname');
123 $homepage = $this->trimmed('homepage');
124 $description = $this->trimmed('description');
125 $location = $this->trimmed('location');
127 if (!Validate::string($nickname, array('min_length' => 1,
129 'format' => NICKNAME_FMT))) {
130 $this->showForm(_('Nickname must have only lowercase letters '.
131 'and numbers and no spaces.'));
133 } else if ($this->nicknameExists($nickname)) {
134 $this->showForm(_('Nickname already in use. Try another one.'));
136 } else if (!User_group::allowedNickname($nickname)) {
137 $this->showForm(_('Not a valid nickname.'));
139 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
140 !Validate::uri($homepage,
141 array('allowed_schemes' =>
142 array('http', 'https')))) {
143 $this->showForm(_('Homepage is not a valid URL.'));
145 } else if (!is_null($fullname) && strlen($fullname) > 255) {
146 $this->showForm(_('Full name is too long (max 255 chars).'));
148 } else if (!is_null($description) && strlen($description) > 140) {
149 $this->showForm(_('description is too long (max 140 chars).'));
151 } else if (!is_null($location) && strlen($location) > 255) {
152 $this->showForm(_('Location is too long (max 255 chars).'));
156 $cur = common_current_user();
158 // Checked in prepare() above
160 assert(!is_null($cur));
162 $group = new User_group();
164 $group->query('BEGIN');
166 $group->nickname = $nickname;
167 $group->fullname = $fullname;
168 $group->homepage = $homepage;
169 $group->description = $description;
170 $group->location = $location;
171 $group->created = common_sql_now();
173 $result = $group->insert();
176 common_log_db_error($group, 'INSERT', __FILE__);
177 $this->serverError(_('Could not create group.'));
180 $member = new Group_member();
182 $member->group_id = $group->id;
183 $member->profile_id = $cur->id;
184 $member->is_admin = 1;
185 $member->created = $group->created;
187 $result = $member->insert();
190 common_log_db_error($member, 'INSERT', __FILE__);
191 $this->serverError(_('Could not set group membership.'));
194 $group->query('COMMIT');
196 common_redirect($group->homeUrl(), 307);
199 function nicknameExists($nickname)
201 $group = User_group::staticGet('nickname', $nickname);
202 return (!is_null($group) && $group != false);