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