]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/editgroup.php
Merge activity plugin into mainline
[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-2011 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 class EditgroupAction extends GroupAction
49 {
50     var $msg;
51
52     function title()
53     {
54         // TRANS: Title for form to edit a group. %s is a group nickname.
55         return sprintf(_('Edit %s group'), $this->group->nickname);
56     }
57
58     /**
59      * Prepare to run
60      */
61
62     function prepare($args)
63     {
64         parent::prepare($args);
65
66         if (!common_logged_in()) {
67             // TRANS: Client error displayed trying to edit a group while not 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             // TRANS: Client error displayed trying to edit a group while not proving a nickname for the group to edit.
85             $this->clientError(_('No nickname.'), 404);
86             return false;
87         }
88
89         $groupid = $this->trimmed('groupid');
90
91         if ($groupid) {
92             $this->group = User_group::staticGet('id', $groupid);
93         } else {
94             $local = Local_group::staticGet('nickname', $nickname);
95             if ($local) {
96                 $this->group = User_group::staticGet('id', $local->group_id);
97             }
98         }
99
100         if (!$this->group) {
101             // TRANS: Client error displayed trying to edit a non-existing group.
102             $this->clientError(_('No such group.'), 404);
103             return false;
104         }
105
106         $cur = common_current_user();
107
108         if (!$cur->isAdmin($this->group)) {
109             // TRANS: Client error displayed trying to edit a group while not being a group admin.
110             $this->clientError(_('You must be an admin to edit the group.'), 403);
111             return false;
112         }
113
114         return true;
115     }
116
117     /**
118      * Handle the request
119      *
120      * On GET, show the form. On POST, try to save the group.
121      *
122      * @param array $args unused
123      *
124      * @return void
125      */
126     function handle($args)
127     {
128         parent::handle($args);
129         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
130             $this->trySave();
131         } else {
132             $this->showForm();
133         }
134     }
135
136     function showForm($msg=null)
137     {
138         $this->msg = $msg;
139         $this->showPage();
140     }
141
142     function showObjectNav()
143     {
144         $nav = new GroupNav($this, $this->group);
145         $nav->show();
146     }
147
148     function showContent()
149     {
150         $form = new GroupEditForm($this, $this->group);
151         $form->show();
152     }
153
154     function showPageNotice()
155     {
156         if ($this->msg) {
157             $this->element('p', 'error', $this->msg);
158         } else {
159             $this->element('p', 'instructions',
160                            // TRANS: Form instructions for group edit form.
161                            _('Use this form to edit the group.'));
162         }
163     }
164
165     function showScripts()
166     {
167         parent::showScripts();
168         $this->autofocus('nickname');
169     }
170
171     function trySave()
172     {
173         $cur = common_current_user();
174         if (!$cur->isAdmin($this->group)) {
175             // TRANS: Client error displayed trying to edit a group while not being a group admin.
176             $this->clientError(_('You must be an admin to edit the group.'), 403);
177             return;
178         }
179
180         if (Event::handle('StartGroupSaveForm', array($this))) {
181
182             $nickname    = Nickname::normalize($this->trimmed('nickname'));
183             $fullname    = $this->trimmed('fullname');
184             $homepage    = $this->trimmed('homepage');
185             $description = $this->trimmed('description');
186             $location    = $this->trimmed('location');
187             $aliasstring = $this->trimmed('aliases');
188             $private     = $this->boolean('private');
189
190             if ($private) {
191                 $force_scope = 1;
192                 $join_policy = User_group::JOIN_POLICY_MODERATE;
193             } else {
194                 $force_scope = 0;
195                 $join_policy = User_group::JOIN_POLICY_OPEN;
196             }
197
198             if ($this->nicknameExists($nickname)) {
199                 // TRANS: Group edit form validation error.
200                 $this->showForm(_('Nickname already in use. Try another one.'));
201                 return;
202             } else if (!User_group::allowedNickname($nickname)) {
203                 // TRANS: Group edit form validation error.
204                 $this->showForm(_('Not a valid nickname.'));
205                 return;
206             } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
207                        !Validate::uri($homepage,
208                                       array('allowed_schemes' =>
209                                             array('http', 'https')))) {
210                 // TRANS: Group edit form validation error.
211                 $this->showForm(_('Homepage is not a valid URL.'));
212                 return;
213             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
214                 // TRANS: Group edit form validation error.
215                 $this->showForm(_('Full name is too long (maximum 255 characters).'));
216                 return;
217             } else if (User_group::descriptionTooLong($description)) {
218                 $this->showForm(sprintf(
219                                     // TRANS: Group edit form validation error.
220                                     _m('Description is too long (maximum %d character).',
221                                        'Description is too long (maximum %d characters).',
222                                        User_group::maxDescription()),
223                                     User_group::maxDescription()));
224                 return;
225             } else if (!is_null($location) && mb_strlen($location) > 255) {
226                 // TRANS: Group edit form validation error.
227                 $this->showForm(_('Location is too long (maximum 255 characters).'));
228                 return;
229             }
230
231             if (!empty($aliasstring)) {
232                 $aliases = array_map('common_canonical_nickname', array_unique(preg_split('/[\s,]+/', $aliasstring)));
233             } else {
234                 $aliases = array();
235             }
236
237             if (count($aliases) > common_config('group', 'maxaliases')) {
238                 // TRANS: Group edit form validation error.
239                 // TRANS: %d is the maximum number of allowed aliases.
240                 $this->showForm(sprintf(_m('Too many aliases! Maximum %d allowed.',
241                                            'Too many aliases! Maximum %d allowed.',
242                                            common_config('group', 'maxaliases')),
243                                         common_config('group', 'maxaliases')));
244                 return;
245             }
246
247             foreach ($aliases as $alias) {
248                 if (!Nickname::isValid($alias)) {
249                     // TRANS: Group edit form validation error.
250                     $this->showForm(sprintf(_('Invalid alias: "%s"'), $alias));
251                     return;
252                 }
253                 if ($this->nicknameExists($alias)) {
254                     // TRANS: Group edit form validation error.
255                     $this->showForm(sprintf(_('Alias "%s" already in use. Try another one.'),
256                                             $alias));
257                     return;
258                 }
259                 // XXX assumes alphanum nicknames
260                 if (strcmp($alias, $nickname) == 0) {
261                     // TRANS: Group edit form validation error.
262                     $this->showForm(_('Alias can\'t be the same as nickname.'));
263                     return;
264                 }
265             }
266
267             $this->group->query('BEGIN');
268
269             $orig = clone($this->group);
270
271             $this->group->nickname    = $nickname;
272             $this->group->fullname    = $fullname;
273             $this->group->homepage    = $homepage;
274             $this->group->description = $description;
275             $this->group->location    = $location;
276             $this->group->mainpage    = common_local_url('showgroup', array('nickname' => $nickname));
277             $this->group->join_policy = $join_policy;
278             $this->group->force_scope = $force_scope;
279
280             $result = $this->group->update($orig);
281
282             if (!$result) {
283                 common_log_db_error($this->group, 'UPDATE', __FILE__);
284                 // TRANS: Server error displayed when editing a group fails.
285                 $this->serverError(_('Could not update group.'));
286             }
287
288             $result = $this->group->setAliases($aliases);
289
290             if (!$result) {
291                 // TRANS: Server error displayed when group aliases could not be added.
292                 $this->serverError(_('Could not create aliases.'));
293             }
294
295             if ($nickname != $orig->nickname) {
296                 common_log(LOG_INFO, "Saving local group info.");
297                 $local = Local_group::staticGet('group_id', $this->group->id);
298                 $local->setNickname($nickname);
299             }
300
301             $this->group->query('COMMIT');
302
303             Event::handle('EndGroupSaveForm', array($this));
304         }
305
306         if ($this->group->nickname != $orig->nickname) {
307             common_redirect(common_local_url('editgroup',
308                                              array('nickname' => $nickname)),
309                             303);
310         } else {
311             // TRANS: Group edit form success message.
312             $this->showForm(_('Options saved.'));
313         }
314     }
315
316     function nicknameExists($nickname)
317     {
318         $group = Local_group::staticGet('nickname', $nickname);
319
320         if (!empty($group) &&
321             $group->group_id != $this->group->id) {
322             return true;
323         }
324
325         $alias = Group_alias::staticGet('alias', $nickname);
326
327         if (!empty($alias) &&
328             $alias->group_id != $this->group->id) {
329             return true;
330         }
331
332         return false;
333     }
334 }