]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/editgroup.php
Merge branch '0.7.x' of git@gitorious.org:laconica/dev into 0.7.x
[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         $groupid = $this->trimmed('groupid');
92         if ($groupid) {
93             $this->group = User_group::staticGet('id', $groupid);
94         } else {
95             $this->group = User_group::staticGet('nickname', $nickname);
96         }
97
98         if (!$this->group) {
99             $this->clientError(_('No such group'), 404);
100             return false;
101         }
102
103         $cur = common_current_user();
104
105         if (!$cur->isAdmin($this->group)) {
106             $this->clientError(_('You must be an admin to edit the group'), 403);
107             return false;
108         }
109
110         return true;
111     }
112
113     /**
114      * Handle the request
115      *
116      * On GET, show the form. On POST, try to save the group.
117      *
118      * @param array $args unused
119      *
120      * @return void
121      */
122
123     function handle($args)
124     {
125         parent::handle($args);
126         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
127             $this->trySave();
128         } else {
129             $this->showForm();
130         }
131     }
132
133     function showForm($msg=null)
134     {
135         $this->msg = $msg;
136         $this->showPage();
137     }
138
139     function showLocalNav()
140     {
141         $nav = new GroupNav($this, $this->group);
142         $nav->show();
143     }
144
145     function showContent()
146     {
147         $form = new GroupEditForm($this, $this->group);
148         $form->show();
149     }
150
151     function showPageNotice()
152     {
153         if ($this->msg) {
154             $this->element('p', 'error', $this->msg);
155         } else {
156             $this->element('p', 'instructions',
157                            _('Use this form to edit the group.'));
158         }
159     }
160
161     function trySave()
162     {
163         $cur = common_current_user();
164         if (!$cur->isAdmin($this->group)) {
165             $this->clientError(_('You must be an admin to edit the group'), 403);
166             return;
167         }
168
169
170         $nickname    = common_canonical_nickname($this->trimmed('nickname'));
171         $fullname    = $this->trimmed('fullname');
172         $homepage    = $this->trimmed('homepage');
173         $description = $this->trimmed('description');
174         $location    = $this->trimmed('location');
175
176         if (!Validate::string($nickname, array('min_length' => 1,
177                                                'max_length' => 64,
178                                                'format' => NICKNAME_FMT))) {
179             $this->showForm(_('Nickname must have only lowercase letters '.
180                               'and numbers and no spaces.'));
181             return;
182         } else if ($this->nicknameExists($nickname)) {
183             $this->showForm(_('Nickname already in use. Try another one.'));
184             return;
185         } else if (!User_group::allowedNickname($nickname)) {
186             $this->showForm(_('Not a valid nickname.'));
187             return;
188         } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
189                    !Validate::uri($homepage,
190                                   array('allowed_schemes' =>
191                                         array('http', 'https')))) {
192             $this->showForm(_('Homepage is not a valid URL.'));
193             return;
194         } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
195             $this->showForm(_('Full name is too long (max 255 chars).'));
196             return;
197         } else if (!is_null($description) && mb_strlen($description) > 140) {
198             $this->showForm(_('description is too long (max 140 chars).'));
199             return;
200         } else if (!is_null($location) && mb_strlen($location) > 255) {
201             $this->showForm(_('Location is too long (max 255 chars).'));
202             return;
203         }
204
205         $orig = clone($this->group);
206
207         $this->group->nickname    = $nickname;
208         $this->group->fullname    = $fullname;
209         $this->group->homepage    = $homepage;
210         $this->group->description = $description;
211         $this->group->location    = $location;
212         $this->group->created     = common_sql_now();
213
214         $result = $this->group->update($orig);
215
216         if (!$result) {
217             common_log_db_error($this->group, 'UPDATE', __FILE__);
218             $this->serverError(_('Could not update group.'));
219         }
220
221         if ($this->group->nickname != $orig->nickname) {
222             common_redirect(common_local_url('editgroup',
223                                              array('nickname' => $nickname)),
224                             307);
225         } else {
226             $this->showForm(_('Options saved.'));
227         }
228     }
229
230     function nicknameExists($nickname)
231     {
232         $group = User_group::staticGet('nickname', $nickname);
233         return (!is_null($group) &&
234                 $group != false &&
235                 $group->id != $this->group->id);
236     }
237 }
238