]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/editgroup.php
Merge branch 'master' into groups
[quix0rs-gnu-social.git] / actions / editgroup.php
1 <?php
2 /**
3  * Laconica, 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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @author    Sarven Capadisli <csarven@controlyourself.ca>
26  * @copyright 2008-2009 Control Yourself, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://laconi.ca/
29  */
30
31 if (!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  Laconica
42  * @author   Evan Prodromou <evan@controlyourself.ca>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://laconi.ca/
45  */
46
47 class EditgroupAction extends Action
48 {
49     var $msg;
50     var $group = null;
51
52     function title()
53     {
54         return sprintf(_('Edit %s group'), $this->group->nickname);
55     }
56
57     /**
58      * Prepare to run
59      */
60
61     function prepare($args)
62     {
63         parent::prepare($args);
64
65         if (!common_config('inboxes','enabled')) {
66             $this->serverError(_('Inboxes must be enabled for groups to work'));
67             return false;
68         }
69
70         if (!common_logged_in()) {
71             $this->clientError(_('You must be logged in to create a group.'));
72             return false;
73         }
74
75         $nickname_arg = $this->trimmed('nickname');
76         $nickname = common_canonical_nickname($nickname_arg);
77
78         // Permanent redirect on non-canonical nickname
79
80         if ($nickname_arg != $nickname) {
81             $args = array('nickname' => $nickname);
82             common_redirect(common_local_url('editgroup', $args), 301);
83             return false;
84         }
85
86         if (!$nickname) {
87             $this->clientError(_('No nickname'), 404);
88             return false;
89         }
90
91         $this->group = User_group::staticGet('nickname', $nickname);
92
93         if (!$this->group) {
94             $this->clientError(_('No such group'), 404);
95             return false;
96         }
97
98         $cur = common_current_user();
99
100         if (!$cur->isAdmin($group)) {
101             $this->clientError(_('You must be an admin to edit the group'), 403);
102             return false;
103         }
104
105         return true;
106     }
107
108     /**
109      * Handle the request
110      *
111      * On GET, show the form. On POST, try to save the group.
112      *
113      * @param array $args unused
114      *
115      * @return void
116      */
117
118     function handle($args)
119     {
120         parent::handle($args);
121         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
122             $this->trySave();
123         } else {
124             $this->showForm();
125         }
126     }
127
128     function showForm($msg=null)
129     {
130         $this->msg = $msg;
131         $this->showPage();
132     }
133
134     function showLocalNav()
135     {
136         $nav = new GroupNav($this, $this->group);
137         $nav->show();
138     }
139
140     function showContent()
141     {
142         $form = new GroupEditForm($this, $this->group);
143         $form->show();
144     }
145
146     function showPageNotice()
147     {
148         if ($this->msg) {
149             $this->element('p', 'error', $this->msg);
150         } else {
151             $this->element('p', 'instructions',
152                            _('Use this form to edit the group.'));
153         }
154     }
155
156     function trySave()
157     {
158         $nickname    = common_canonical_nickname($this->trimmed('nickname'));
159         $fullname    = $this->trimmed('fullname');
160         $homepage    = $this->trimmed('homepage');
161         $description = $this->trimmed('description');
162         $location    = $this->trimmed('location');
163
164         if (!Validate::string($nickname, array('min_length' => 1,
165                                                'max_length' => 64,
166                                                'format' => NICKNAME_FMT))) {
167             $this->showForm(_('Nickname must have only lowercase letters '.
168                               'and numbers and no spaces.'));
169             return;
170         } else if ($this->nicknameExists($nickname)) {
171             $this->showForm(_('Nickname already in use. Try another one.'));
172             return;
173         } else if (!User_group::allowedNickname($nickname)) {
174             $this->showForm(_('Not a valid nickname.'));
175             return;
176         } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
177                    !Validate::uri($homepage,
178                                   array('allowed_schemes' =>
179                                         array('http', 'https')))) {
180             $this->showForm(_('Homepage is not a valid URL.'));
181             return;
182         } else if (!is_null($fullname) && strlen($fullname) > 255) {
183             $this->showForm(_('Full name is too long (max 255 chars).'));
184             return;
185         } else if (!is_null($description) && strlen($description) > 140) {
186             $this->showForm(_('description is too long (max 140 chars).'));
187             return;
188         } else if (!is_null($location) && strlen($location) > 255) {
189             $this->showForm(_('Location is too long (max 255 chars).'));
190             return;
191         }
192
193         $orig = clone($this->group);
194
195         $this->group->nickname    = $nickname;
196         $this->group->fullname    = $fullname;
197         $this->group->homepage    = $homepage;
198         $this->group->description = $description;
199         $this->group->location    = $location;
200         $this->group->created     = common_sql_now();
201
202         $result = $this->group->update($orig);
203
204         if (!$result) {
205             common_log_db_error($this->group, 'UPDATE', __FILE__);
206             $this->serverError(_('Could not update group.'));
207         }
208
209         if ($this->group->nickname != $orig->nickname) {
210             common_redirect(common_local_url('editgroup',
211                                              array('nickname' => $nickname)),
212                             307);
213         } else {
214             $this->showForm(_('Options saved.'));
215         }
216     }
217
218     function nicknameExists($nickname)
219     {
220         $group = User_group::staticGet('nickname', $nickname);
221         return (!is_null($group) &&
222                 $group != false &&
223                 $group->id != $this->group->id);
224     }
225 }