]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
Removing unnecessary require_once lines (autoload!)
[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 = null;
118         $upload = MediaFile::fromUpload('attach');
119
120         if (isset($upload)) {
121
122             if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
123                 $content_shortened .= ' ' . $upload->shortUrl();
124             }
125             Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
126
127             if (Notice::contentTooLong($content_shortened)) {
128                 $upload->delete();
129                 // TRANS: Client error displayed exceeding the maximum notice length.
130                 // TRANS: %d is the maximum length for a notice.
131                 $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.',
132                                               'Maximum notice size is %d characters, including attachment URL.',
133                                               Notice::maxContent()),
134                                            Notice::maxContent()));
135             }
136         }
137
138         if ($user->shareLocation()) {
139             // use browser data if checked; otherwise profile data
140             if ($this->arg('notice_data-geo')) {
141                 $locOptions = Notice::locationOptions($this->trimmed('lat'),
142                                                       $this->trimmed('lon'),
143                                                       $this->trimmed('location_id'),
144                                                       $this->trimmed('location_ns'),
145                                                       $this->scoped);
146             } else {
147                 $locOptions = Notice::locationOptions(null,
148                                                       null,
149                                                       null,
150                                                       null,
151                                                       $this->scoped);
152             }
153
154             $options = array_merge($options, $locOptions);
155         }
156
157         $author_id = $this->scoped->id;
158         $text      = $content_shortened;
159
160         // Does the heavy-lifting for getting "To:" information
161
162         ToSelector::fillOptions($this, $options);
163
164         if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
165
166             $notice = Notice::saveNew($this->scoped->id, $content_shortened, 'web', $options);
167
168             if (isset($upload)) {
169                 $upload->attachToNotice($notice);
170             }
171
172             Event::handle('EndNoticeSaveWeb', array($this, $notice));
173         }
174         Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
175
176         if (StatusNet::isAjax()) {
177             header('Content-Type: text/xml;charset=utf-8');
178             $this->xw->startDocument('1.0', 'UTF-8');
179             $this->elementStart('html');
180             $this->elementStart('head');
181             // TRANS: Page title after sending a notice.
182             $this->element('title', null, _('Notice posted'));
183             $this->elementEnd('head');
184             $this->elementStart('body');
185             $this->showNotice($notice);
186             $this->elementEnd('body');
187             $this->elementEnd('html');
188             exit;
189         } else {
190             $returnto = $this->trimmed('returnto');
191
192             if ($returnto) {
193                 $url = common_local_url($returnto,
194                                         array('nickname' => $this->scoped->nickname));
195             } else {
196                 $url = common_local_url('shownotice',
197                                         array('notice' => $notice->id));
198             }
199             common_redirect($url, 303);
200         }
201     }
202
203     /**
204      * Show an Ajax-y error message
205      *
206      * Goes back to the browser, where it's shown in a popup.
207      *
208      * @param string $msg Message to show
209      *
210      * @return void
211      */
212     function ajaxErrorMsg($msg)
213     {
214         $this->startHTML('text/xml;charset=utf-8', true);
215         $this->elementStart('head');
216         // TRANS: Page title after an AJAX error occurs on the send notice page.
217         $this->element('title', null, _('Ajax Error'));
218         $this->elementEnd('head');
219         $this->elementStart('body');
220         $this->element('p', array('id' => 'error'), $msg);
221         $this->elementEnd('body');
222         $this->elementEnd('html');
223     }
224
225     /**
226      * Show an Ajax-y notice form
227      *
228      * Goes back to the browser, where it's shown in a popup.
229      *
230      * @param string $msg Message to show
231      *
232      * @return void
233      */
234     function ajaxShowForm()
235     {
236         $this->startHTML('text/xml;charset=utf-8', true);
237         $this->elementStart('head');
238         // TRANS: Title for form to send a new notice.
239         $this->element('title', null, _m('TITLE','New notice'));
240         $this->elementEnd('head');
241         $this->elementStart('body');
242
243         $form = new NoticeForm($this);
244         $form->show();
245
246         $this->elementEnd('body');
247         $this->elementEnd('html');
248     }
249
250     /**
251      * Formerly page output
252      *
253      * This used to be the whole page output; now that's been largely
254      * subsumed by showPage. So this just stores an error message, if
255      * it was passed, and calls showPage.
256      *
257      * Note that since we started doing Ajax output, this page is rarely
258      * seen.
259      *
260      * @param string  $msg     An error/info message, if any
261      * @param boolean $success false for error indication, true for info
262      *
263      * @return void
264      */
265     function showForm($msg=null, $success=false)
266     {
267         if (StatusNet::isAjax()) {
268             if ($msg) {
269                 $this->ajaxErrorMsg($msg);
270             } else {
271                 $this->ajaxShowForm();
272             }
273             return;
274         }
275
276         parent::showForm($msg, $success);
277     }
278
279     /**
280      * // XXX: Should we be showing the notice form with microapps here?
281      *
282      * Overload for replies or bad results
283      *
284      * We show content in the notice form if there were replies or results.
285      *
286      * @return void
287      */
288     function showNoticeForm()
289     {
290         $content = $this->trimmed('status_textarea');
291         if (!$content) {
292             $replyto = $this->trimmed('replyto');
293             $inreplyto = $this->trimmed('inreplyto');
294             $profile = Profile::getKV('nickname', $replyto);
295             if ($profile) {
296                 $content = '@' . $profile->nickname . ' ';
297             }
298         } else {
299             // @fixme most of these bits above aren't being passed on above
300             $inreplyto = null;
301         }
302
303         $this->elementStart('div', 'input_forms');
304         $this->elementStart(
305             'div',
306             array(
307                 'id'    => 'input_form_status',
308                 'class' => 'input_form current nonav'
309             )
310         );
311
312         $notice_form = new NoticeForm(
313             $this,
314             array(
315                 'content' => $content,
316                 'inreplyto' => $inreplyto
317             )
318         );
319
320         $notice_form->show();
321
322         $this->elementEnd('div');
323         $this->elementEnd('div');
324     }
325
326     /**
327      * Show an error message
328      *
329      * Shows an error message if there is one.
330      *
331      * @return void
332      *
333      * @todo maybe show some instructions?
334      */
335     function showPageNotice()
336     {
337         if ($this->msg) {
338             $this->element('p', array('id' => 'error'), $this->msg);
339         }
340     }
341
342     /**
343      * Output a notice
344      *
345      * Used to generate the notice code for Ajax results.
346      *
347      * @param Notice $notice Notice that was saved
348      *
349      * @return void
350      */
351     function showNotice($notice)
352     {
353         $nli = new NoticeListItem($notice, $this);
354         $nli->show();
355     }
356 }