]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/editgroup.php
Always naming it 'plugin' is not good, it can easily confuse. So better name it
[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     protected function prepare(array $args=array())
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         }
70
71         $nickname_arg = $this->trimmed('nickname');
72         $nickname = common_canonical_nickname($nickname_arg);
73
74         // Permanent redirect on non-canonical nickname
75
76         if ($nickname_arg != $nickname) {
77             $args = array('nickname' => $nickname);
78             common_redirect(common_local_url('editgroup', $args), 301);
79         }
80
81         if (!$nickname) {
82             // TRANS: Client error displayed trying to edit a group while not proving a nickname for the group to edit.
83             $this->clientError(_('No nickname.'), 404);
84         }
85
86         $groupid = $this->trimmed('groupid');
87
88         if ($groupid) {
89             $this->group = User_group::getKV('id', $groupid);
90         } else {
91             $local = Local_group::getKV('nickname', $nickname);
92             if ($local) {
93                 $this->group = User_group::getKV('id', $local->group_id);
94             }
95         }
96
97         if (!$this->group) {
98             // TRANS: Client error displayed trying to edit a non-existing group.
99             $this->clientError(_('No such group.'), 404);
100         }
101
102         $cur = common_current_user();
103
104         if (!$cur->isAdmin($this->group)) {
105             // TRANS: Client error displayed trying to edit a group while not being a group admin.
106             $this->clientError(_('You must be an admin to edit the group.'), 403);
107         }
108
109         return true;
110     }
111
112     /**
113      * Handle the request
114      *
115      * On GET, show the form. On POST, try to save the group.
116      *
117      * @return void
118      */
119     protected function handle()
120     {
121         parent::handle();
122         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
123             $this->trySave();
124         } else {
125             $this->showForm();
126         }
127     }
128
129     function showForm($msg=null)
130     {
131         $this->msg = $msg;
132         $this->showPage();
133     }
134
135     function showContent()
136     {
137         $form = new GroupEditForm($this, $this->group);
138         $form->show();
139     }
140
141     function showPageNotice()
142     {
143         if ($this->msg) {
144             $this->element('p', 'error', $this->msg);
145         } else {
146             $this->element('p', 'instructions',
147                            // TRANS: Form instructions for group edit form.
148                            _('Use this form to edit the group.'));
149         }
150     }
151
152     function showScripts()
153     {
154         parent::showScripts();
155         $this->autofocus('fullname');
156     }
157
158     function trySave()
159     {
160         $cur = common_current_user();
161         if (!$cur->isAdmin($this->group)) {
162             // TRANS: Client error displayed trying to edit a group while not being a group admin.
163             $this->clientError(_('You must be an admin to edit the group.'), 403);
164         }
165
166         if (Event::handle('StartGroupSaveForm', array($this))) {
167
168             // $nickname will only be set if this changenick value is true.
169             if (common_config('profile', 'changenick') == true) {
170                 try {
171                     $nickname = Nickname::normalize($this->trimmed('newnickname'), true);
172                 } catch (NicknameTakenException $e) {
173                     // Abort only if the nickname is occupied by _another_ group
174                     if ($e->profile->id != $this->group->profile_id) {
175                         $this->showForm($e->getMessage());
176                         return;
177                     }
178                     $nickname = Nickname::normalize($this->trimmed('newnickname')); // without in-use check this time
179                 } catch (NicknameException $e) {
180                     $this->showForm($e->getMessage());
181                     return;
182                 }
183             }
184
185             $fullname    = $this->trimmed('fullname');
186             $homepage    = $this->trimmed('homepage');
187             $description = $this->trimmed('description');
188             $location    = $this->trimmed('location');
189             $aliasstring = $this->trimmed('aliases');
190             $private     = $this->boolean('private');
191
192             if ($private) {
193                 $force_scope = 1;
194                 $join_policy = User_group::JOIN_POLICY_MODERATE;
195             } else {
196                 $force_scope = 0;
197                 $join_policy = User_group::JOIN_POLICY_OPEN;
198             }
199
200             if (!is_null($homepage) && (strlen($homepage) > 0) &&
201                        !common_valid_http_url($homepage)) {
202                 // TRANS: Group edit form validation error.
203                 $this->showForm(_('Homepage is not a valid URL.'));
204                 return;
205             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
206                 // TRANS: Group edit form validation error.
207                 $this->showForm(_('Full name is too long (maximum 255 characters).'));
208                 return;
209             } else if (User_group::descriptionTooLong($description)) {
210                 $this->showForm(sprintf(
211                                     // TRANS: Group edit form validation error.
212                                     _m('Description is too long (maximum %d character).',
213                                        'Description is too long (maximum %d characters).',
214                                        User_group::maxDescription()),
215                                     User_group::maxDescription()));
216                 return;
217             } else if (!is_null($location) && mb_strlen($location) > 255) {
218                 // TRANS: Group edit form validation error.
219                 $this->showForm(_('Location is too long (maximum 255 characters).'));
220                 return;
221             }
222
223             if (!empty($aliasstring)) {
224                 $aliases = array_map(array('Nickname', 'normalize'),
225                                      array_unique(preg_split('/[\s,]+/', $aliasstring)));
226             } else {
227                 $aliases = array();
228             }
229
230             if (count($aliases) > common_config('group', 'maxaliases')) {
231                 // TRANS: Group edit form validation error.
232                 // TRANS: %d is the maximum number of allowed aliases.
233                 $this->showForm(sprintf(_m('Too many aliases! Maximum %d allowed.',
234                                            'Too many aliases! Maximum %d allowed.',
235                                            common_config('group', 'maxaliases')),
236                                         common_config('group', 'maxaliases')));
237                 return;
238             }
239
240             $this->group->query('BEGIN');
241
242             $orig = clone($this->group);
243
244             if (common_config('profile', 'changenick') == true && $this->group->nickname !== $nickname) {
245                 assert(Nickname::normalize($nickname)===$nickname);
246                 common_debug("Changing group nickname from '{$profile->nickname}' to '{$nickname}'.");
247                 $this->group->nickname = $nickname;
248                 $this->group->mainpage = common_local_url('showgroup', array('nickname' => $this->group->nickname));
249             }
250             $this->group->fullname    = $fullname;
251             $this->group->homepage    = $homepage;
252             $this->group->description = $description;
253             $this->group->location    = $location;
254             $this->group->join_policy = $join_policy;
255             $this->group->force_scope = $force_scope;
256
257             $result = $this->group->update($orig);
258
259             if ($result === false) {
260                 common_log_db_error($this->group, 'UPDATE', __FILE__);
261                 // TRANS: Server error displayed when editing a group fails.
262                 $this->serverError(_('Could not update group.'));
263             }
264
265             $result = $this->group->setAliases($aliases);
266
267             if (!$result) {
268                 // TRANS: Server error displayed when group aliases could not be added.
269                 $this->serverError(_('Could not create aliases.'));
270             }
271
272             $this->group->query('COMMIT');
273
274             Event::handle('EndGroupSaveForm', array($this));
275         }
276
277         if ($this->group->nickname != $orig->nickname) {
278             common_redirect(common_local_url('editgroup', array('nickname' => $this->group->nickname)), 303);
279         } else {
280             // TRANS: Group edit form success message.
281             $this->showForm(_('Options saved.'));
282         }
283     }
284 }