]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/deletegroup.php
Stronger typing and fixes in RequireValidatedEmail
[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         }
66
67         $nickname_arg = $this->trimmed('nickname');
68         $id = intval($this->arg('id'));
69         if ($id) {
70             $this->group = User_group::getKV('id', $id);
71         } else if ($nickname_arg) {
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('leavegroup', $args), 301);
79             }
80
81             $local = Local_group::getKV('nickname', $nickname);
82
83             if (!$local) {
84                 // TRANS: Client error when trying to delete a non-local group.
85                 $this->clientError(_('No such group.'), 404);
86             }
87
88             $this->group = User_group::getKV('id', $local->group_id);
89         } else {
90             // TRANS: Client error when trying to delete a group without providing a nickname or ID for the group.
91             $this->clientError(_('No nickname or ID.'), 404);
92         }
93
94         if (!$this->group) {
95             // TRANS: Client error when trying to delete a non-existing group.
96             $this->clientError(_('No such group.'), 404);
97         }
98
99         $cur = common_current_user();
100         if (!$cur->hasRight(Right::DELETEGROUP)) {
101             // TRANS: Client error when trying to delete a group without having the rights to delete it.
102             $this->clientError(_('You are not allowed to delete this group.'), 403);
103         }
104
105         return true;
106     }
107
108     /**
109      * Handle the request
110      *
111      * On POST, delete the group.
112      *
113      * @param array $args unused
114      *
115      * @return void
116      */
117     function handle($args)
118     {
119         parent::handle($args);
120         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
121             if ($this->arg('no')) {
122                 $this->returnToPrevious();
123                 return;
124             } elseif ($this->arg('yes')) {
125                 $this->handlePost();
126                 return;
127             }
128         }
129         $this->showPage();
130     }
131
132     function handlePost()
133     {
134         $cur = common_current_user();
135
136         try {
137             if (Event::handle('StartDeleteGroup', array($this->group))) {
138                 $this->group->delete();
139                 Event::handle('EndDeleteGroup', array($this->group));
140             }
141         } catch (Exception $e) {
142             // TRANS: Server error displayed if a group could not be deleted.
143             // TRANS: %s is the name of the group that could not be deleted.
144             $this->serverError(sprintf(_('Could not delete group %s.'),
145                                        $this->group->nickname));
146         }
147
148         if ($this->boolean('ajax')) {
149             $this->startHTML('text/xml;charset=utf-8');
150             $this->elementStart('head');
151             // TRANS: Message given after deleting a group.
152             // TRANS: %s is the deleted group's name.
153             $this->element('title', null, sprintf(_('Deleted group %s'),
154                                                   $this->group->nickname));
155             $this->elementEnd('head');
156             $this->elementStart('body');
157             // @fixme add a sensible AJAX response form!
158             $this->elementEnd('body');
159             $this->endHTML();
160         } else {
161             // @fixme if we could direct to the page on which this group
162             // would have shown... that would be awesome
163             common_redirect(common_local_url('groups'), 303);
164         }
165     }
166
167     function title() {
168         // TRANS: Title of delete group page.
169         return _('Delete group');
170     }
171
172     function showContent() {
173         $this->areYouSureForm();
174         $block = new GroupProfileBlock($this, $this->group);
175         $block->show();
176     }
177
178     /**
179      * Confirm with user.
180      * Ripped from DeleteuserAction
181      *
182      * Shows a confirmation form.
183      *
184      * @fixme refactor common code for things like this
185      * @return void
186      */
187     function areYouSureForm()
188     {
189         $id = $this->group->id;
190         $this->elementStart('form', array('id' => 'deletegroup-' . $id,
191                                            'method' => 'post',
192                                            'class' => 'form_settings form_entity_block',
193                                            'action' => common_local_url('deletegroup', array('id' => $this->group->id))));
194         $this->elementStart('fieldset');
195         $this->hidden('token', common_session_token());
196         // TRANS: Form legend for deleting a group.
197         $this->element('legend', _('Delete group'));
198         if (Event::handle('StartDeleteGroupForm', array($this, $this->group))) {
199             $this->element('p', null,
200                            // TRANS: Warning in form for deleleting a group.
201                            _('Are you sure you want to delete this group? '.
202                              'This will clear all data about the group from the '.
203                              'database, without a backup. ' .
204                              'Public posts to this group will still appear in ' .
205                              'individual timelines.'));
206             foreach ($this->args as $k => $v) {
207                 if (substr($k, 0, 9) == 'returnto-') {
208                     $this->hidden($k, $v);
209                 }
210             }
211             Event::handle('EndDeleteGroupForm', array($this, $this->group));
212         }
213         $this->submit('form_action-no',
214                       // TRANS: Button label on the delete group form.
215                       _m('BUTTON','No'),
216                       'submit form_action-primary',
217                       'no',
218                       // TRANS: Submit button title for 'No' when deleting a group.
219                       _('Do not delete this group.'));
220         $this->submit('form_action-yes',
221                       // TRANS: Button label on the delete group form.
222                       _m('BUTTON','Yes'),
223                       'submit form_action-secondary',
224                       'yes',
225                       // TRANS: Submit button title for 'Yes' when deleting a group.
226                       _('Delete this group.'));
227         $this->elementEnd('fieldset');
228         $this->elementEnd('form');
229     }
230 }