]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/deletegroup.php
Give NoticeListItem its own class
[quix0rs-gnu-social.git] / actions / deletegroup.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Delete a 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    Brion Vibber <brion@status.net>
26  * @copyright 2008-2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 /**
36  * Delete a group
37  *
38  * This is the action for deleting a group.
39  *
40  * @category Group
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @author   Brion Vibber <brion@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  * @fixme merge more of this code with related variants
47  */
48 class DeletegroupAction extends RedirectingAction
49 {
50     var $group = null;
51
52     /**
53      * Prepare to run
54      *
55      * @fixme merge common setup code with other group actions
56      * @fixme allow group admins to delete their own groups
57      */
58     function prepare($args)
59     {
60         parent::prepare($args);
61
62         if (!common_logged_in()) {
63             // TRANS: Client error when trying to delete group while not logged in.
64             $this->clientError(_('You must be logged in to delete a group.'));
65             return false;
66         }
67
68         $nickname_arg = $this->trimmed('nickname');
69         $id = intval($this->arg('id'));
70         if ($id) {
71             $this->group = User_group::staticGet('id', $id);
72         } else if ($nickname_arg) {
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('leavegroup', $args), 301);
80                 return false;
81             }
82
83             $local = Local_group::staticGet('nickname', $nickname);
84
85             if (!$local) {
86                 // TRANS: Client error when trying to delete a non-local group.
87                 $this->clientError(_('No such group.'), 404);
88                 return false;
89             }
90
91             $this->group = User_group::staticGet('id', $local->group_id);
92         } else {
93             // TRANS: Client error when trying to delete a group without providing a nickname or ID for the group.
94             $this->clientError(_('No nickname or ID.'), 404);
95             return false;
96         }
97
98         if (!$this->group) {
99             // TRANS: Client error when trying to delete a non-existing group.
100             $this->clientError(_('No such group.'), 404);
101             return false;
102         }
103
104         $cur = common_current_user();
105         if (!$cur->hasRight(Right::DELETEGROUP)) {
106             // TRANS: Client error when trying to delete a group without having the rights to delete it.
107             $this->clientError(_('You are not allowed to delete this group.'), 403);
108             return false;
109         }
110
111         return true;
112     }
113
114     /**
115      * Handle the request
116      *
117      * On POST, delete the group.
118      *
119      * @param array $args unused
120      *
121      * @return void
122      */
123     function handle($args)
124     {
125         parent::handle($args);
126         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
127             if ($this->arg('no')) {
128                 $this->returnToPrevious();
129                 return;
130             } elseif ($this->arg('yes')) {
131                 $this->handlePost();
132                 return;
133             }
134         }
135         $this->showPage();
136     }
137
138     function handlePost()
139     {
140         $cur = common_current_user();
141
142         try {
143             if (Event::handle('StartDeleteGroup', array($this->group))) {
144                 $this->group->delete();
145                 Event::handle('EndDeleteGroup', array($this->group));
146             }
147         } catch (Exception $e) {
148             // TRANS: Server error displayed if a group could not be deleted.
149             // TRANS: %s is the name of the group that could not be deleted.
150             $this->serverError(sprintf(_('Could not delete group %s.'),
151                                        $this->group->nickname));
152         }
153
154         if ($this->boolean('ajax')) {
155             $this->startHTML('text/xml;charset=utf-8');
156             $this->elementStart('head');
157             // TRANS: Message given after deleting a group.
158             // TRANS: %s is the deleted group's name.
159             $this->element('title', null, sprintf(_('Deleted group %s'),
160                                                   $this->group->nickname));
161             $this->elementEnd('head');
162             $this->elementStart('body');
163             // @fixme add a sensible AJAX response form!
164             $this->elementEnd('body');
165             $this->elementEnd('html');
166         } else {
167             // @fixme if we could direct to the page on which this group
168             // would have shown... that would be awesome
169             common_redirect(common_local_url('groups'),
170                             303);
171         }
172     }
173
174     function title() {
175         // TRANS: Title of delete group page.
176         return _('Delete group');
177     }
178
179     function showContent() {
180         $this->areYouSureForm();
181     }
182
183     /**
184      * Confirm with user.
185      * Ripped from DeleteuserAction
186      *
187      * Shows a confirmation form.
188      *
189      * @fixme refactor common code for things like this
190      * @return void
191      */
192     function areYouSureForm()
193     {
194         $id = $this->group->id;
195         $this->elementStart('form', array('id' => 'deletegroup-' . $id,
196                                            'method' => 'post',
197                                            'class' => 'form_settings form_entity_block',
198                                            'action' => common_local_url('deletegroup', array('id' => $this->group->id))));
199         $this->elementStart('fieldset');
200         $this->hidden('token', common_session_token());
201         // TRANS: Form legend for deleting a group.
202         $this->element('legend', _('Delete group'));
203         if (Event::handle('StartDeleteGroupForm', array($this, $this->group))) {
204             $this->element('p', null,
205                            // TRANS: Warning in form for deleleting a group.
206                            _('Are you sure you want to delete this group? '.
207                              'This will clear all data about the group from the '.
208                              'database, without a backup. ' .
209                              'Public posts to this group will still appear in ' .
210                              'individual timelines.'));
211             foreach ($this->args as $k => $v) {
212                 if (substr($k, 0, 9) == 'returnto-') {
213                     $this->hidden($k, $v);
214                 }
215             }
216             Event::handle('EndDeleteGroupForm', array($this, $this->group));
217         }
218         $this->submit('form_action-no',
219                       // TRANS: Button label on the delete group form.
220                       _m('BUTTON','No'),
221                       'submit form_action-primary',
222                       'no',
223                       // TRANS: Submit button title for 'No' when deleting a group.
224                       _('Do not delete this group.'));
225         $this->submit('form_action-yes',
226                       // TRANS: Button label on the delete group form.
227                       _m('BUTTON','Yes'),
228                       'submit form_action-secondary',
229                       'yes',
230                       // TRANS: Submit button title for 'Yes' when deleting a group.
231                       _('Delete this group.'));
232         $this->elementEnd('fieldset');
233         $this->elementEnd('form');
234     }
235 }