]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/deletenotice.php
Added missing isPrivateScope().
[quix0rs-gnu-social.git] / actions / deletenotice.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Class for deleting a notice
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  Personal
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008 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 // @todo FIXME: documentation needed.
36 class DeletenoticeAction extends Action
37 {
38     var $error        = null;
39     var $user         = null;
40     var $notice       = null;
41     var $profile      = null;
42     var $user_profile = null;
43
44     function prepare(array $args=array())
45     {
46         parent::prepare($args);
47
48         $this->user   = common_current_user();
49
50         if (!$this->user) {
51             // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
52             common_user_error(_('Not logged in.'));
53             exit;
54         }
55
56         $notice_id    = $this->trimmed('notice');
57         $this->notice = Notice::getKV($notice_id);
58
59         if (!$this->notice) {
60             // TRANS: Error message displayed trying to delete a non-existing notice.
61             common_user_error(_('No such notice.'));
62             exit;
63         }
64
65         $this->profile      = $this->notice->getProfile();
66         $this->user_profile = $this->user->getProfile();
67
68         return true;
69     }
70
71     function handle(array $args=array())
72     {
73         parent::handle($args);
74
75         if ($this->notice->profile_id != $this->user_profile->id &&
76                    !$this->user->hasRight(Right::DELETEOTHERSNOTICE)) {
77             // TRANS: Error message displayed trying to delete a notice that was not made by the current user.
78             common_user_error(_('Cannot delete this notice.'));
79             exit;
80         }
81         // XXX: Ajax!
82
83         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
84             $this->deleteNotice();
85         } else if ($_SERVER['REQUEST_METHOD'] == 'GET') {
86             $this->showForm();
87         }
88     }
89
90     /**
91      * Show the page notice
92      *
93      * Shows instructions for the page
94      *
95      * @return void
96      */
97     function showPageNotice()
98     {
99         $instr  = $this->getInstructions();
100         $output = common_markup_to_html($instr);
101
102         $this->elementStart('div', 'instructions');
103         $this->raw($output);
104         $this->elementEnd('div');
105     }
106
107     function getInstructions()
108     {
109         // TRANS: Instructions for deleting a notice.
110         return _('You are about to permanently delete a notice. ' .
111                  'Once this is done, it cannot be undone.');
112     }
113
114     function title()
115     {
116         // TRANS: Page title when deleting a notice.
117         return _('Delete notice');
118     }
119
120     /**
121      * Wrapper for showing a page
122      *
123      * Stores an error and shows the page
124      *
125      * @param string $error Error, if any
126      *
127      * @return void
128      */
129     function showForm($error = null)
130     {
131         $this->error = $error;
132         $this->showPage();
133     }
134
135     /**
136      * Insert delete notice form into the content
137      *
138      * @return void
139      */
140     function showContent()
141     {
142         $this->elementStart('form', array('id' => 'form_notice_delete',
143                                           'class' => 'form_settings',
144                                           'method' => 'post',
145                                           'action' => common_local_url('deletenotice')));
146         $this->elementStart('fieldset');
147         // TRANS: Fieldset legend for the delete notice form.
148         $this->element('legend', null, _('Delete notice'));
149         $this->hidden('token', common_session_token());
150         $this->hidden('notice', $this->trimmed('notice'));
151         // TRANS: Message for the delete notice form.
152         $this->element('p', null, _('Are you sure you want to delete this notice?'));
153         $this->submit('form_action-no',
154                       // TRANS: Button label on the delete notice form.
155                       _m('BUTTON','No'),
156                       'submit form_action-primary',
157                       'no',
158                       // TRANS: Submit button title for 'No' when deleting a notice.
159                       _('Do not delete this notice.'));
160         $this->submit('form_action-yes',
161                       // TRANS: Button label on the delete notice form.
162                       _m('BUTTON','Yes'),
163                       'submit form_action-secondary',
164                       'yes',
165                       // TRANS: Submit button title for 'Yes' when deleting a notice.
166                       _('Delete this notice.'));
167         $this->elementEnd('fieldset');
168         $this->elementEnd('form');
169     }
170
171     function deleteNotice()
172     {
173         // CSRF protection
174         $token = $this->trimmed('token');
175
176         if (!$token || $token != common_session_token()) {
177             // TRANS: Client error displayed when the session token does not match or is not given.
178             $this->showForm(_('There was a problem with your session token. ' .
179                               'Try again, please.'));
180             return;
181         }
182
183         if ($this->arg('yes')) {
184             if (Event::handle('StartDeleteOwnNotice', array($this->user, $this->notice))) {
185                 $this->notice->delete();
186                 Event::handle('EndDeleteOwnNotice', array($this->user, $this->notice));
187             }
188         }
189
190         $url = common_get_returnto();
191
192         if ($url) {
193             common_set_returnto(null);
194         } else {
195             $url = common_local_url('public');
196         }
197
198         common_redirect($url, 303);
199     }
200 }