]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
Merge commit 'refs/merge-requests/199' of git://gitorious.org/statusnet/mainline...
[quix0rs-gnu-social.git] / actions / newnotice.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Handler for posting new notices
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    Zach Copley <zach@status.net>
26  * @author    Sarven Capadisli <csarven@status.net>
27  * @copyright 2008-2009 StatusNet, Inc.
28  * @copyright 2013 Free Software Foundation, Inc.
29  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30  * @link      http://status.net/
31  */
32
33 if (!defined('STATUSNET')) {
34     exit(1);
35 }
36
37 /**
38  * Action for posting new notices
39  *
40  * @category Personal
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @author   Zach Copley <zach@status.net>
44  * @author   Sarven Capadisli <csarven@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://status.net/
47  */
48 class NewnoticeAction extends FormAction
49 {
50     /**
51      * Title of the page
52      *
53      * Note that this usually doesn't get called unless something went wrong
54      *
55      * @return string page title
56      */
57     function title()
58     {
59         // TRANS: Page title for sending a new notice.
60         return _m('TITLE','New notice');
61     }
62
63     /**
64      * This handlePost saves a new notice, based on arguments
65      *
66      * If successful, will show the notice, or return an Ajax-y result.
67      * If not, it will show an error message -- possibly Ajax-y.
68      *
69      * Also, if the notice input looks like a command, it will run the
70      * command and show the results -- again, possibly ajaxy.
71      *
72      * @return void
73      */
74     protected function handlePost()
75     {
76         parent::handlePost();
77
78         assert($this->scoped); // XXX: maybe an error instead...
79         $user = $this->scoped->getUser();
80         $content = $this->trimmed('status_textarea');
81         $options = array();
82         Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
83
84         if (!$content) {
85             // TRANS: Client error displayed trying to send a notice without content.
86             $this->clientError(_('No content!'));
87         }
88
89         $inter = new CommandInterpreter();
90
91         $cmd = $inter->handle_command($user, $content);
92
93         if ($cmd) {
94             if (StatusNet::isAjax()) {
95                 $cmd->execute(new AjaxWebChannel($this));
96             } else {
97                 $cmd->execute(new WebChannel($this));
98             }
99             return;
100         }
101
102         $content_shortened = $user->shortenLinks($content);
103         if (Notice::contentTooLong($content_shortened)) {
104             // TRANS: Client error displayed when the parameter "status" is missing.
105             // TRANS: %d is the maximum number of character for a notice.
106             $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.',
107                                           'That\'s too long. Maximum notice size is %d characters.',
108                                           Notice::maxContent()),
109                                        Notice::maxContent()));
110         }
111
112         $replyto = intval($this->trimmed('inreplyto'));
113         if ($replyto) {
114             $options['reply_to'] = $replyto;
115         }
116
117         $upload = MediaFile::fromUpload('attach', $this->scoped);
118
119         if (isset($upload)) {
120
121             if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
122                 $content_shortened .= ' ' . $upload->shortUrl();
123             }
124             Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
125
126             if (Notice::contentTooLong($content_shortened)) {
127                 $upload->delete();
128                 // TRANS: Client error displayed exceeding the maximum notice length.
129                 // TRANS: %d is the maximum length for a notice.
130                 $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.',
131                                               'Maximum notice size is %d characters, including attachment URL.',
132                                               Notice::maxContent()),
133                                            Notice::maxContent()));
134             }
135         }
136
137         if ($this->scoped->shareLocation()) {
138             // use browser data if checked; otherwise profile data
139             if ($this->arg('notice_data-geo')) {
140                 $locOptions = Notice::locationOptions($this->trimmed('lat'),
141                                                       $this->trimmed('lon'),
142                                                       $this->trimmed('location_id'),
143                                                       $this->trimmed('location_ns'),
144                                                       $this->scoped);
145             } else {
146                 $locOptions = Notice::locationOptions(null,
147                                                       null,
148                                                       null,
149                                                       null,
150                                                       $this->scoped);
151             }
152
153             $options = array_merge($options, $locOptions);
154         }
155
156         $author_id = $this->scoped->id;
157         $text      = $content_shortened;
158
159         // Does the heavy-lifting for getting "To:" information
160
161         ToSelector::fillOptions($this, $options);
162
163         if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
164
165             $notice = Notice::saveNew($this->scoped->id, $content_shortened, 'web', $options);
166
167             if (isset($upload)) {
168                 $upload->attachToNotice($notice);
169             }
170
171             Event::handle('EndNoticeSaveWeb', array($this, $notice));
172         }
173         Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
174
175         if (StatusNet::isAjax()) {
176             $this->startHTML('text/xml;charset=utf-8');
177             $this->elementStart('head');
178             // TRANS: Page title after sending a notice.
179             $this->element('title', null, _('Notice posted'));
180             $this->elementEnd('head');
181             $this->elementStart('body');
182             $this->showNotice($notice);
183             $this->elementEnd('body');
184             $this->endHTML();
185             exit;
186         } else {
187             $returnto = $this->trimmed('returnto');
188
189             if ($returnto) {
190                 $url = common_local_url($returnto,
191                                         array('nickname' => $this->scoped->nickname));
192             } else {
193                 $url = common_local_url('shownotice',
194                                         array('notice' => $notice->id));
195             }
196             common_redirect($url, 303);
197         }
198     }
199
200     /**
201      * Show an Ajax-y error message
202      *
203      * Goes back to the browser, where it's shown in a popup.
204      *
205      * @param string $msg Message to show
206      *
207      * @return void
208      */
209     function ajaxErrorMsg($msg)
210     {
211         $this->startHTML('text/xml;charset=utf-8', true);
212         $this->elementStart('head');
213         // TRANS: Page title after an AJAX error occurs on the send notice page.
214         $this->element('title', null, _('Ajax Error'));
215         $this->elementEnd('head');
216         $this->elementStart('body');
217         $this->element('p', array('id' => 'error'), $msg);
218         $this->elementEnd('body');
219         $this->endHTML();
220     }
221
222     /**
223      * Show an Ajax-y notice form
224      *
225      * Goes back to the browser, where it's shown in a popup.
226      *
227      * @param string $msg Message to show
228      *
229      * @return void
230      */
231     function ajaxShowForm()
232     {
233         $this->startHTML('text/xml;charset=utf-8', true);
234         $this->elementStart('head');
235         // TRANS: Title for form to send a new notice.
236         $this->element('title', null, _m('TITLE','New notice'));
237         $this->elementEnd('head');
238         $this->elementStart('body');
239
240         $form = new NoticeForm($this);
241         $form->show();
242
243         $this->elementEnd('body');
244         $this->endHTML();
245     }
246
247     /**
248      * Formerly page output
249      *
250      * This used to be the whole page output; now that's been largely
251      * subsumed by showPage. So this just stores an error message, if
252      * it was passed, and calls showPage.
253      *
254      * Note that since we started doing Ajax output, this page is rarely
255      * seen.
256      *
257      * @param string  $msg     An error/info message, if any
258      * @param boolean $success false for error indication, true for info
259      *
260      * @return void
261      */
262     function showForm($msg=null, $success=false)
263     {
264         if (StatusNet::isAjax()) {
265             if ($msg) {
266                 $this->ajaxErrorMsg($msg);
267             } else {
268                 $this->ajaxShowForm();
269             }
270             return;
271         }
272
273         parent::showForm($msg, $success);
274     }
275
276     /**
277      * // XXX: Should we be showing the notice form with microapps here?
278      *
279      * Overload for replies or bad results
280      *
281      * We show content in the notice form if there were replies or results.
282      *
283      * @return void
284      */
285     function showNoticeForm()
286     {
287         $content = $this->trimmed('status_textarea');
288         if (!$content) {
289             $replyto = $this->trimmed('replyto');
290             $inreplyto = $this->trimmed('inreplyto');
291             $profile = Profile::getKV('nickname', $replyto);
292             if ($profile) {
293                 $content = '@' . $profile->nickname . ' ';
294             }
295         } else {
296             // @fixme most of these bits above aren't being passed on above
297             $inreplyto = null;
298         }
299
300         $this->elementStart('div', 'input_forms');
301         $this->elementStart(
302             'div',
303             array(
304                 'id'    => 'input_form_status',
305                 'class' => 'input_form current nonav'
306             )
307         );
308
309         $notice_form = new NoticeForm(
310             $this,
311             array(
312                 'content' => $content,
313                 'inreplyto' => $inreplyto
314             )
315         );
316
317         $notice_form->show();
318
319         $this->elementEnd('div');
320         $this->elementEnd('div');
321     }
322
323     /**
324      * Show an error message
325      *
326      * Shows an error message if there is one.
327      *
328      * @return void
329      *
330      * @todo maybe show some instructions?
331      */
332     function showPageNotice()
333     {
334         if ($this->msg) {
335             $this->element('p', array('id' => 'error'), $this->msg);
336         }
337     }
338
339     /**
340      * Output a notice
341      *
342      * Used to generate the notice code for Ajax results.
343      *
344      * @param Notice $notice Notice that was saved
345      *
346      * @return void
347      */
348     function showNotice($notice)
349     {
350         $nli = new NoticeListItem($notice, $this);
351         $nli->show();
352     }
353 }