]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/editgroup.php
Merge branch 'i18n-work' into i18n-0.9.x
[quix0rs-gnu-social.git] / actions / editgroup.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Edit an existing 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  * @author   Zach Copley <zach@status.net>
27  * @copyright 2008-2009 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('STATUSNET') && !defined('LACONICA')) {
33     exit(1);
34 }
35
36 /**
37  * Add a new group
38  *
39  * This is the form for adding a new group
40  *
41  * @category Group
42  * @package  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @author   Zach Copley <zach@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://status.net/
47  */
48
49 class EditgroupAction extends GroupDesignAction
50 {
51
52     var $msg;
53
54     function title()
55     {
56         return sprintf(_('Edit %s group'), $this->group->nickname);
57     }
58
59     /**
60      * Prepare to run
61      */
62
63     function prepare($args)
64     {
65         parent::prepare($args);
66
67         if (!common_logged_in()) {
68             $this->clientError(_('You must be logged in to create a group.'));
69             return false;
70         }
71
72         $nickname_arg = $this->trimmed('nickname');
73         $nickname = common_canonical_nickname($nickname_arg);
74
75         // Permanent redirect on non-canonical nickname
76
77         if ($nickname_arg != $nickname) {
78             $args = array('nickname' => $nickname);
79             common_redirect(common_local_url('editgroup', $args), 301);
80             return false;
81         }
82
83         if (!$nickname) {
84             $this->clientError(_('No nickname'), 404);
85             return false;
86         }
87
88         $groupid = $this->trimmed('groupid');
89         if ($groupid) {
90             $this->group = User_group::staticGet('id', $groupid);
91         } else {
92             $this->group = User_group::staticGet('nickname', $nickname);
93         }
94
95         if (!$this->group) {
96             $this->clientError(_('No such group'), 404);
97             return false;
98         }
99
100         $cur = common_current_user();
101
102         if (!$cur->isAdmin($this->group)) {
103             $this->clientError(_('You must be an admin to edit the group'), 403);
104             return false;
105         }
106
107         return true;
108     }
109
110     /**
111      * Handle the request
112      *
113      * On GET, show the form. On POST, try to save the group.
114      *
115      * @param array $args unused
116      *
117      * @return void
118      */
119
120     function handle($args)
121     {
122         parent::handle($args);
123         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
124             $this->trySave();
125         } else {
126             $this->showForm();
127         }
128     }
129
130     function showForm($msg=null)
131     {
132         $this->msg = $msg;
133         $this->showPage();
134     }
135
136     function showLocalNav()
137     {
138         $nav = new GroupNav($this, $this->group);
139         $nav->show();
140     }
141
142     function showContent()
143     {
144         $form = new GroupEditForm($this, $this->group);
145         $form->show();
146     }
147
148     function showPageNotice()
149     {
150         if ($this->msg) {
151             $this->element('p', 'error', $this->msg);
152         } else {
153             $this->element('p', 'instructions',
154                            _('Use this form to edit the group.'));
155         }
156     }
157
158     function showScripts()
159     {
160         parent::showScripts();
161         $this->autofocus('nickname');
162     }
163
164     function trySave()
165     {
166         $cur = common_current_user();
167         if (!$cur->isAdmin($this->group)) {
168             $this->clientError(_('You must be an admin to edit the group'), 403);
169             return;
170         }
171
172         $nickname    = common_canonical_nickname($this->trimmed('nickname'));
173         $fullname    = $this->trimmed('fullname');
174         $homepage    = $this->trimmed('homepage');
175         $description = $this->trimmed('description');
176         $location    = $this->trimmed('location');
177         $aliasstring = $this->trimmed('aliases');
178
179         if (!Validate::string($nickname, array('min_length' => 1,
180                                                'max_length' => 64,
181                                                'format' => NICKNAME_FMT))) {
182             $this->showForm(_('Nickname must have only lowercase letters '.
183                               'and numbers and no spaces.'));
184             return;
185         } else if ($this->nicknameExists($nickname)) {
186             $this->showForm(_('Nickname already in use. Try another one.'));
187             return;
188         } else if (!User_group::allowedNickname($nickname)) {
189             $this->showForm(_('Not a valid nickname.'));
190             return;
191         } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
192                    !Validate::uri($homepage,
193                                   array('allowed_schemes' =>
194                                         array('http', 'https')))) {
195             $this->showForm(_('Homepage is not a valid URL.'));
196             return;
197         } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
198             $this->showForm(_('Full name is too long (max 255 chars).'));
199             return;
200         } else if (User_group::descriptionTooLong($description)) {
201             $this->showForm(sprintf(_('description is too long (max %d chars).'), User_group::maxDescription()));
202             return;
203         } else if (!is_null($location) && mb_strlen($location) > 255) {
204             $this->showForm(_('Location is too long (max 255 chars).'));
205             return;
206         }
207
208         if (!empty($aliasstring)) {
209             $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
210         } else {
211             $aliases = array();
212         }
213
214         if (count($aliases) > common_config('group', 'maxaliases')) {
215             $this->showForm(sprintf(_('Too many aliases! Maximum %d.'),
216                                     common_config('group', 'maxaliases')));
217             return;
218         }
219
220         foreach ($aliases as $alias) {
221             if (!Validate::string($alias, array('min_length' => 1,
222                                                 'max_length' => 64,
223                                                 'format' => NICKNAME_FMT))) {
224                 $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
225                 return;
226             }
227             if ($this->nicknameExists($alias)) {
228                 $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
229                                         $alias));
230                 return;
231             }
232             // XXX assumes alphanum nicknames
233             if (strcmp($alias, $nickname) == 0) {
234                 $this->showForm(_('Alias can\'t be the same as nickname.'));
235                 return;
236             }
237         }
238
239         $this->group->query('BEGIN');
240
241         $orig = clone($this->group);
242
243         $this->group->nickname    = $nickname;
244         $this->group->fullname    = $fullname;
245         $this->group->homepage    = $homepage;
246         $this->group->description = $description;
247         $this->group->location    = $location;
248
249         $result = $this->group->update($orig);
250
251         if (!$result) {
252             common_log_db_error($this->group, 'UPDATE', __FILE__);
253             $this->serverError(_('Could not update group.'));
254         }
255
256         $result = $this->group->setAliases($aliases);
257
258         if (!$result) {
259             $this->serverError(_('Could not create aliases.'));
260         }
261
262         $this->group->query('COMMIT');
263
264         if ($this->group->nickname != $orig->nickname) {
265             common_redirect(common_local_url('editgroup',
266                                              array('nickname' => $nickname)),
267                             303);
268         } else {
269             $this->showForm(_('Options saved.'));
270         }
271     }
272
273     function nicknameExists($nickname)
274     {
275         $group = User_group::staticGet('nickname', $nickname);
276
277         if (!empty($group) &&
278             $group->id != $this->group->id) {
279             return true;
280         }
281
282         $alias = Group_alias::staticGet('alias', $nickname);
283
284         if (!empty($alias) &&
285             $alias->group_id != $this->group->id) {
286             return true;
287         }
288
289         return false;
290     }
291 }
292