]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newgroup.php
Merge branch 'master' of gitorious.org:statusnet/mainline into 0.9.x
[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 class NewgroupAction extends Action
47 {
48     var $msg;
49
50     function title()
51     {
52         // TRANS: Title for form to create a group.
53         return _('New group');
54     }
55
56     /**
57      * Prepare to run
58      */
59     function prepare($args)
60     {
61         parent::prepare($args);
62
63         if (!common_logged_in()) {
64             // TRANS: Client error displayed trying to create a group while not 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     function handle($args)
82     {
83         parent::handle($args);
84         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
85             $this->trySave();
86         } else {
87             $this->showForm();
88         }
89     }
90
91     function showForm($msg=null)
92     {
93         $this->msg = $msg;
94         $this->showPage();
95     }
96
97     function showContent()
98     {
99         $form = new GroupEditForm($this);
100         $form->show();
101     }
102
103     function showPageNotice()
104     {
105         if ($this->msg) {
106             $this->element('p', 'error', $this->msg);
107         } else {
108             $this->element('p', 'instructions',
109                            // TRANS: Form instructions for group create form.
110                            _('Use this form to create a new group.'));
111         }
112     }
113
114     function trySave()
115     {
116         try {
117             $nickname = Nickname::normalize($this->trimmed('nickname'));
118         } catch (NicknameException $e) {
119             $this->showForm($e->getMessage());
120         }
121         $fullname    = $this->trimmed('fullname');
122         $homepage    = $this->trimmed('homepage');
123         $description = $this->trimmed('description');
124         $location    = $this->trimmed('location');
125         $aliasstring = $this->trimmed('aliases');
126
127         if ($this->nicknameExists($nickname)) {
128             // TRANS: Group create form validation error.
129             $this->showForm(_('Nickname already in use. Try another one.'));
130             return;
131         } else if (!User_group::allowedNickname($nickname)) {
132             // TRANS: Group create form validation error.
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             // TRANS: Group create form validation error.
140             $this->showForm(_('Homepage is not a valid URL.'));
141             return;
142         } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
143             // TRANS: Group create form validation error.
144             $this->showForm(_('Full name is too long (maximum 255 characters).'));
145             return;
146         } else if (User_group::descriptionTooLong($description)) {
147             // TRANS: Group create form validation error.
148             // TRANS: %d is the maximum number of allowed characters.
149             $this->showForm(sprintf(_m('Description is too long (maximum %d character).',
150                                        'Description is too long (maximum %d characters).',
151                                        User_group::maxDescription()),
152                                     User_group::maxDescription()));
153             return;
154         } else if (!is_null($location) && mb_strlen($location) > 255) {
155             // TRANS: Group create form validation error.
156             $this->showForm(_('Location is too long (maximum 255 characters).'));
157             return;
158         }
159
160         if (!empty($aliasstring)) {
161             $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
162         } else {
163             $aliases = array();
164         }
165
166         if (count($aliases) > common_config('group', 'maxaliases')) {
167             // TRANS: Group create form validation error.
168             // TRANS: %d is the maximum number of allowed aliases.
169             $this->showForm(sprintf(_m('Too many aliases! Maximum %d allowed.',
170                                        'Too many aliases! Maximum %d allowed.',
171                                        common_config('group', 'maxaliases')),
172                                     common_config('group', 'maxaliases')));
173             return;
174         }
175
176         foreach ($aliases as $alias) {
177             if (!Nickname::isValid($alias)) {
178                 // TRANS: Group create form validation error.
179                 $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
180                 return;
181             }
182             if ($this->nicknameExists($alias)) {
183                 // TRANS: Group create form validation error.
184                 $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
185                                         $alias));
186                 return;
187             }
188             // XXX assumes alphanum nicknames
189             if (strcmp($alias, $nickname) == 0) {
190                 // TRANS: Group create form validation error.
191                 $this->showForm(_('Alias can\'t be the same as nickname.'));
192                 return;
193             }
194         }
195
196         $mainpage = common_local_url('showgroup', array('nickname' => $nickname));
197
198         $cur = common_current_user();
199
200         // Checked in prepare() above
201
202         assert(!is_null($cur));
203
204         $group = User_group::register(array('nickname' => $nickname,
205                                             'fullname' => $fullname,
206                                             'homepage' => $homepage,
207                                             'description' => $description,
208                                             'location' => $location,
209                                             'aliases'  => $aliases,
210                                             'userid'   => $cur->id,
211                                             'mainpage' => $mainpage,
212                                             'local'    => true));
213
214         common_redirect($group->homeUrl(), 303);
215     }
216
217     function nicknameExists($nickname)
218     {
219         $local = Local_group::staticGet('nickname', $nickname);
220
221         if (!empty($local)) {
222             return true;
223         }
224
225         $alias = Group_alias::staticGet('alias', $nickname);
226
227         if (!empty($alias)) {
228             return true;
229         }
230
231         return false;
232     }
233 }