]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newgroup.php
fix syntax error introduced in i18n tweaks: newgroup action
[quix0rs-gnu-social.git] / actions / newgroup.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Add a new group
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Group
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 /**
36  * Add a new group
37  *
38  * This is the form for adding a new group
39  *
40  * @category Group
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46
47 class NewgroupAction extends Action
48 {
49     var $msg;
50
51     function title()
52     {
53         return _('New group');
54     }
55
56     /**
57      * Prepare to run
58      */
59
60     function prepare($args)
61     {
62         parent::prepare($args);
63
64         if (!common_logged_in()) {
65             $this->clientError(_('You must be logged in to create a group.'));
66             return false;
67         }
68
69         return true;
70     }
71
72     /**
73      * Handle the request
74      *
75      * On GET, show the form. On POST, try to save the group.
76      *
77      * @param array $args unused
78      *
79      * @return void
80      */
81
82     function handle($args)
83     {
84         parent::handle($args);
85         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
86             $this->trySave();
87         } else {
88             $this->showForm();
89         }
90     }
91
92     function showForm($msg=null)
93     {
94         $this->msg = $msg;
95         $this->showPage();
96     }
97
98     function showContent()
99     {
100         $form = new GroupEditForm($this);
101         $form->show();
102     }
103
104     function showPageNotice()
105     {
106         if ($this->msg) {
107             $this->element('p', 'error', $this->msg);
108         } else {
109             $this->element('p', 'instructions',
110                            _('Use this form to create a new group.'));
111         }
112     }
113
114     function trySave()
115     {
116         $nickname    = $this->trimmed('nickname');
117         $fullname    = $this->trimmed('fullname');
118         $homepage    = $this->trimmed('homepage');
119         $description = $this->trimmed('description');
120         $location    = $this->trimmed('location');
121         $aliasstring = $this->trimmed('aliases');
122
123         if (!Validate::string($nickname, array('min_length' => 1,
124                                                'max_length' => 64,
125                                                'format' => NICKNAME_FMT))) {
126             $this->showForm(_('Nickname must have only lowercase letters '.
127                               'and numbers and no spaces.'));
128             return;
129         } else if ($this->nicknameExists($nickname)) {
130             $this->showForm(_('Nickname already in use. Try another one.'));
131             return;
132         } else if (!User_group::allowedNickname($nickname)) {
133             $this->showForm(_('Not a valid nickname.'));
134             return;
135         } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
136                    !Validate::uri($homepage,
137                                   array('allowed_schemes' =>
138                                         array('http', 'https')))) {
139             $this->showForm(_('Homepage is not a valid URL.'));
140             return;
141         } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
142             $this->showForm(_('Full name is too long (maximum 255 characters).'));
143             return;
144         } else if (User_group::descriptionTooLong($description)) {
145             // TRANS: Form validation error creating a new group because the description is too long.
146             // TRANS: %d is the maximum number of allowed characters.
147             $this->showForm(sprintf(_m('Description is too long (maximum %d character).',
148                                        'Description is too long (maximum %d characters).',
149                                        User_group::maxDescription()),
150                                     User_group::maxDescription()));
151             return;
152         } else if (!is_null($location) && mb_strlen($location) > 255) {
153             $this->showForm(_('Location is too long (maximum 255 characters).'));
154             return;
155         }
156
157         if (!empty($aliasstring)) {
158             $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
159         } else {
160             $aliases = array();
161         }
162
163         if (count($aliases) > common_config('group', 'maxaliases')) {
164             // TRANS: Client error shown when providing too many aliases during group creation.
165             // TRANS: %d is the maximum number of allowed aliases.
166             $this->showForm(sprintf(_m('Too many aliases! Maximum %d allowed.',
167                                        'Too many aliases! Maximum %d allowed.',
168                                        common_config('group', 'maxaliases')),
169                                     common_config('group', 'maxaliases')));
170             return;
171         }
172
173         foreach ($aliases as $alias) {
174             if (!Validate::string($alias, array('min_length' => 1,
175                                                 'max_length' => 64,
176                                                 'format' => NICKNAME_FMT))) {
177                 $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
178                 return;
179             }
180             if ($this->nicknameExists($alias)) {
181                 $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
182                                         $alias));
183                 return;
184             }
185             // XXX assumes alphanum nicknames
186             if (strcmp($alias, $nickname) == 0) {
187                 $this->showForm(_('Alias can\'t be the same as nickname.'));
188                 return;
189             }
190         }
191
192         $mainpage = common_local_url('showgroup', array('nickname' => $nickname));
193
194         $cur = common_current_user();
195
196         // Checked in prepare() above
197
198         assert(!is_null($cur));
199
200         $group = User_group::register(array('nickname' => $nickname,
201                                             'fullname' => $fullname,
202                                             'homepage' => $homepage,
203                                             'description' => $description,
204                                             'location' => $location,
205                                             'aliases'  => $aliases,
206                                             'userid'   => $cur->id,
207                                             'mainpage' => $mainpage,
208                                             'local'    => true));
209
210         common_redirect($group->homeUrl(), 303);
211     }
212
213     function nicknameExists($nickname)
214     {
215         $local = Local_group::staticGet('nickname', $nickname);
216
217         if (!empty($local)) {
218             return true;
219         }
220
221         $alias = Group_alias::staticGet('alias', $nickname);
222
223         if (!empty($alias)) {
224             return true;
225         }
226
227         return false;
228     }
229 }
230