]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/editgroup.php
Merge branch 'ATOM-priority" from Alexandre Alapetite into HEAD
[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') && !defined('GNUSOCIAL')) {
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  * @author   Alexei Sorokin <sor.alexei@meowr.ru>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://status.net/
48  */
49 class EditgroupAction extends GroupAction
50 {
51     public $message = null;
52     public $success = null;
53     protected $canPost = true;
54
55     public function title()
56     {
57         // TRANS: Title for form to edit a group. %s is a group nickname.
58         return sprintf(_('Edit %s group'), $this->group->nickname);
59     }
60
61     public function showContent()
62     {
63         $form = new GroupEditForm($this, $this->group);
64         $form->show();
65     }
66
67     public function showPageNoticeBlock()
68     {
69         parent::showPageNoticeBlock();
70
71         if ($this->message) {
72             $this->element(
73                 'p',
74                 ($this->success) ? 'success' : 'error',
75                 $this->message
76             );
77         } else {
78             $this->element(
79                 'p',
80                 'instructions',
81                 // TRANS: Form instructions for group edit form.
82                 _('Use this form to edit the group.')
83             );
84         }
85     }
86
87     public function showScripts()
88     {
89         parent::showScripts();
90         $this->autofocus('fullname');
91     }
92
93     /**
94      * Prepare to run
95      * @param array $args
96      * @return bool
97      * @throws ClientException
98      * @throws NicknameException
99      */
100
101     protected function prepare(array $args = [])
102     {
103         parent::prepare($args);
104
105         if (!common_logged_in()) {
106             // TRANS: Client error displayed trying to edit a group while not logged in.
107             $this->clientError(_('You must be logged in to create a group.'));
108         }
109
110         $nickname_arg = $this->trimmed('nickname');
111         $nickname = common_canonical_nickname($nickname_arg);
112
113         // Permanent redirect on non-canonical nickname
114
115         if ($nickname_arg != $nickname) {
116             $args = ['nickname' => $nickname];
117             common_redirect(common_local_url('editgroup', $args), 301);
118         }
119
120         if (!$nickname) {
121             // TRANS: Client error displayed trying to edit a group while not proving a nickname for the group to edit.
122             $this->clientError(_('No nickname.'), 404);
123         }
124
125         $groupid = $this->trimmed('groupid');
126
127         if ($groupid) {
128             $this->group = User_group::getKV('id', $groupid);
129         } else {
130             $local = Local_group::getKV('nickname', $nickname);
131             if ($local) {
132                 $this->group = User_group::getKV('id', $local->group_id);
133             }
134         }
135
136         if (!$this->group) {
137             // TRANS: Client error displayed trying to edit a non-existing group.
138             $this->clientError(_('No such group.'), 404);
139         }
140
141         $cur = common_current_user();
142
143         if (!$cur->isAdmin($this->group)) {
144             // TRANS: Client error displayed trying to edit a group while not being a group admin.
145             $this->clientError(_('You must be an admin to edit the group.'), 403);
146         }
147
148         return true;
149     }
150
151     protected function handlePost()
152     {
153         parent::handlePost();
154
155         $cur = common_current_user();
156         if (!$cur->isAdmin($this->group)) {
157             // TRANS: Client error displayed trying to edit a group while not being a group admin.
158             $this->clientError(_('You must be an admin to edit the group.'), 403);
159         }
160
161         if (Event::handle('StartGroupSaveForm', [$this])) {
162
163             // $nickname will only be set if this changenick value is true.
164             $nickname = null;
165             if (common_config('profile', 'changenick') == true) {
166                 try {
167                     $nickname = Nickname::normalize($this->trimmed('newnickname'), true);
168                 } catch (NicknameTakenException $e) {
169                     // Abort only if the nickname is occupied by _another_ group
170                     if ($e->profile->id != $this->group->profile_id) {
171                         $this->setMessage($e->getMessage(), true);
172                         return;
173                     }
174                     $nickname = Nickname::normalize($this->trimmed('newnickname')); // without in-use check this time
175                 } catch (NicknameException $e) {
176                     $this->setMessage($e->getMessage(), true);
177                     return;
178                 }
179             }
180
181             $fullname = $this->trimmed('fullname');
182             $homepage = $this->trimmed('homepage');
183             $description = $this->trimmed('description');
184             $location = $this->trimmed('location');
185             $aliasstring = $this->trimmed('aliases');
186             $private = $this->boolean('private');
187
188             if ($private) {
189                 $force_scope = 1;
190                 $join_policy = User_group::JOIN_POLICY_MODERATE;
191             } else {
192                 $force_scope = 0;
193                 $join_policy = User_group::JOIN_POLICY_OPEN;
194             }
195
196             if (!is_null($homepage) && (strlen($homepage) > 0) &&
197                 !common_valid_http_url($homepage)) {
198                 // TRANS: Group edit form validation error.
199                 $this->setMessage(_('Homepage is not a valid URL.'), true);
200                 return;
201             } elseif (!is_null($fullname) && mb_strlen($fullname) > 255) {
202                 // TRANS: Group edit form validation error.
203                 $this->setMessage(_('Full name is too long (maximum 255 characters).'), true);
204                 return;
205             } elseif (User_group::descriptionTooLong($description)) {
206                 $this->setMessage(sprintf(
207                 // TRANS: Group edit form validation error.
208                     _m(
209                         'Description is too long (maximum %d character).',
210                         'Description is too long (maximum %d characters).',
211                         User_group::maxDescription()
212                     ),
213                     User_group::maxDescription()
214                 ), true);
215                 return;
216             } elseif (!is_null($location) && mb_strlen($location) > 255) {
217                 // TRANS: Group edit form validation error.
218                 $this->setMessage(_('Location is too long (maximum 255 characters).'), true);
219                 return;
220             }
221
222             if (!empty($aliasstring)) {
223                 $aliases = array_map(
224                     ['Nickname', 'normalize'],
225                     array_unique(preg_split('/[\s,]+/', $aliasstring))
226                 );
227             } else {
228                 $aliases = [];
229             }
230
231             if (count($aliases) > common_config('group', 'maxaliases')) {
232                 // TRANS: Group edit form validation error.
233                 // TRANS: %d is the maximum number of allowed aliases.
234                 $this->setMessage(sprintf(
235                     _m(
236                         'Too many aliases! Maximum %d allowed.',
237                         'Too many aliases! Maximum %d allowed.',
238                         common_config('group', 'maxaliases')
239                     ),
240                     common_config('group', 'maxaliases')
241                 ), true);
242                 return;
243             }
244
245             $this->group->query('BEGIN');
246
247             $orig = clone($this->group);
248
249             if (common_config('profile', 'changenick') == true && $this->group->nickname !== $nickname) {
250                 assert(Nickname::normalize($nickname) === $nickname);
251                 common_debug("Changing group nickname from '{$this->group->nickname}' to '{$nickname}'.");
252                 $this->group->nickname = $nickname;
253                 $this->group->mainpage = common_local_url('showgroup', ['nickname' => $this->group->nickname]);
254             }
255             $this->group->fullname = $fullname;
256             $this->group->homepage = $homepage;
257             $this->group->description = $description;
258             $this->group->location = $location;
259             $this->group->join_policy = $join_policy;
260             $this->group->force_scope = $force_scope;
261
262             $result = $this->group->update($orig);
263
264             if ($result === false) {
265                 common_log_db_error($this->group, 'UPDATE', __FILE__);
266                 // TRANS: Server error displayed when editing a group fails.
267                 $this->serverError(_('Could not update group.'));
268             }
269
270             $result = $this->group->setAliases($aliases);
271
272             if (!$result) {
273                 // TRANS: Server error displayed when group aliases could not be added.
274                 $this->serverError(_('Could not create aliases.'));
275             }
276
277             $this->group->query('COMMIT');
278
279             Event::handle('EndGroupSaveForm', [$this]);
280
281             if ($this->group->nickname != $orig->nickname) {
282                 common_redirect(common_local_url('editgroup', ['nickname' => $this->group->nickname]), 303);
283             }
284         }
285
286         // TRANS: Group edit form success message.
287         $this->setMessage(_('Options saved.'));
288     }
289
290     public function setMessage($msg, $error = false)
291     {
292         $this->message = $msg;
293         $this->success = !$error;
294     }
295 }