]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/deletegroup.php
Merge branch '2828' into 0.9.x
[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
49 class DeletegroupAction extends RedirectingAction
50 {
51     var $group = null;
52
53     /**
54      * Prepare to run
55      *
56      * @fixme merge common setup code with other group actions
57      * @fixme allow group admins to delete their own groups
58      */
59
60     function prepare($args)
61     {
62         parent::prepare($args);
63
64         if (!common_logged_in()) {
65             $this->clientError(_('You must be logged in to delete a group.'));
66             return false;
67         }
68
69         $nickname_arg = $this->trimmed('nickname');
70         $id = intval($this->arg('id'));
71         if ($id) {
72             $this->group = User_group::staticGet('id', $id);
73         } else if ($nickname_arg) {
74             $nickname = common_canonical_nickname($nickname_arg);
75
76             // Permanent redirect on non-canonical nickname
77
78             if ($nickname_arg != $nickname) {
79                 $args = array('nickname' => $nickname);
80                 common_redirect(common_local_url('leavegroup', $args), 301);
81                 return false;
82             }
83
84             $local = Local_group::staticGet('nickname', $nickname);
85
86             if (!$local) {
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             $this->clientError(_('No nickname or ID.'), 404);
94             return false;
95         }
96
97         if (!$this->group) {
98             $this->clientError(_('No such group.'), 404);
99             return false;
100         }
101
102         $cur = common_current_user();
103         if (!$cur->hasRight(Right::DELETEGROUP)) {
104             $this->clientError(_('You are not allowed to delete this group.'), 403);
105             return false;
106         }
107
108         return true;
109     }
110
111     /**
112      * Handle the request
113      *
114      * On POST, delete the group.
115      *
116      * @param array $args unused
117      *
118      * @return void
119      */
120
121     function handle($args)
122     {
123         parent::handle($args);
124         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
125             if ($this->arg('no')) {
126                 $this->returnToPrevious();
127                 return;
128             } elseif ($this->arg('yes')) {
129                 $this->handlePost();
130                 return;
131             }
132         }
133         $this->showPage();
134     }
135
136     function handlePost()
137     {
138         $cur = common_current_user();
139
140         try {
141             if (Event::handle('StartDeleteGroup', array($this->group))) {
142                 $this->group->delete();
143                 Event::handle('EndDeleteGroup', array($this->group));
144             }
145         } catch (Exception $e) {
146             $this->serverError(sprintf(_('Could not delete group %2$s.'),
147                                        $this->group->nickname));
148         }
149
150         if ($this->boolean('ajax')) {
151             $this->startHTML('text/xml;charset=utf-8');
152             $this->elementStart('head');
153             $this->element('title', null, sprintf(_('Deleted group %2$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->elementEnd('html');
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'),
164                             303);
165         }
166     }
167
168     function title() {
169         return _('Delete group');
170     }
171
172     function showContent() {
173         $this->areYouSureForm();
174     }
175
176     /**
177      * Confirm with user.
178      * Ripped from DeleteuserAction
179      *
180      * Shows a confirmation form.
181      *
182      * @fixme refactor common code for things like this
183      * @return void
184      */
185     function areYouSureForm()
186     {
187         $id = $this->group->id;
188         $this->elementStart('form', array('id' => 'deletegroup-' . $id,
189                                            'method' => 'post',
190                                            'class' => 'form_settings form_entity_block',
191                                            'action' => common_local_url('deletegroup', array('id' => $this->group->id))));
192         $this->elementStart('fieldset');
193         $this->hidden('token', common_session_token());
194         $this->element('legend', _('Delete group'));
195         if (Event::handle('StartDeleteGroupForm', array($this, $this->group))) {
196             $this->element('p', null,
197                            _('Are you sure you want to delete this group? '.
198                              'This will clear all data about the group from the '.
199                              'database, without a backup. ' .
200                              'Public posts to this group will still appear in ' .
201                              'individual timelines.'));
202             foreach ($this->args as $k => $v) {
203                 if (substr($k, 0, 9) == 'returnto-') {
204                     $this->hidden($k, $v);
205                 }
206             }
207             Event::handle('EndDeleteGroupForm', array($this, $this->group));
208         }
209         $this->submit('form_action-no',
210                       // TRANS: Button label on the delete group form.
211                       _m('BUTTON','No'),
212                       'submit form_action-primary',
213                       'no',
214                       // TRANS: Submit button title for 'No' when deleting a group.
215                       _('Do not delete this group'));
216         $this->submit('form_action-yes',
217                       // TRANS: Button label on the delete group form.
218                       _m('BUTTON','Yes'),
219                       'submit form_action-secondary',
220                       'yes',
221                       // TRANS: Submit button title for 'Yes' when deleting a group.
222                       _('Delete this group'));
223         $this->elementEnd('fieldset');
224         $this->elementEnd('form');
225     }
226 }