]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
FormAction updates, also fixing NoticeForm CSS
[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     protected $form = 'Notice';
51
52     /**
53      * Title of the page
54      *
55      * Note that this usually doesn't get called unless something went wrong
56      *
57      * @return string page title
58      */
59     function title()
60     {
61         // TRANS: Page title for sending a new notice.
62         return _m('TITLE','New notice');
63     }
64
65     /**
66      * This handlePost saves a new notice, based on arguments
67      *
68      * If successful, will show the notice, or return an Ajax-y result.
69      * If not, it will show an error message -- possibly Ajax-y.
70      *
71      * Also, if the notice input looks like a command, it will run the
72      * command and show the results -- again, possibly ajaxy.
73      *
74      * @return void
75      */
76     protected function handlePost()
77     {
78         parent::handlePost();
79
80         assert($this->scoped); // XXX: maybe an error instead...
81         $user = $this->scoped->getUser();
82         $content = $this->trimmed('status_textarea');
83         $options = array();
84         Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
85
86         if (!$content) {
87             // TRANS: Client error displayed trying to send a notice without content.
88             $this->clientError(_('No content!'));
89         }
90
91         $inter = new CommandInterpreter();
92
93         $cmd = $inter->handle_command($user, $content);
94
95         if ($cmd) {
96             if (StatusNet::isAjax()) {
97                 $cmd->execute(new AjaxWebChannel($this));
98             } else {
99                 $cmd->execute(new WebChannel($this));
100             }
101             return;
102         }
103
104         $content_shortened = $user->shortenLinks($content);
105         if (Notice::contentTooLong($content_shortened)) {
106             // TRANS: Client error displayed when the parameter "status" is missing.
107             // TRANS: %d is the maximum number of character for a notice.
108             $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.',
109                                           'That\'s too long. Maximum notice size is %d characters.',
110                                           Notice::maxContent()),
111                                        Notice::maxContent()));
112         }
113
114         $replyto = intval($this->trimmed('inreplyto'));
115         if ($replyto) {
116             $options['reply_to'] = $replyto;
117         }
118
119         $upload = MediaFile::fromUpload('attach', $this->scoped);
120
121         if (isset($upload)) {
122
123             if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
124                 $content_shortened .= ' ' . $upload->shortUrl();
125             }
126             Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
127
128             if (Notice::contentTooLong($content_shortened)) {
129                 $upload->delete();
130                 // TRANS: Client error displayed exceeding the maximum notice length.
131                 // TRANS: %d is the maximum length for a notice.
132                 $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.',
133                                               'Maximum notice size is %d characters, including attachment URL.',
134                                               Notice::maxContent()),
135                                            Notice::maxContent()));
136             }
137         }
138
139         if ($this->scoped->shareLocation()) {
140             // use browser data if checked; otherwise profile data
141             if ($this->arg('notice_data-geo')) {
142                 $locOptions = Notice::locationOptions($this->trimmed('lat'),
143                                                       $this->trimmed('lon'),
144                                                       $this->trimmed('location_id'),
145                                                       $this->trimmed('location_ns'),
146                                                       $this->scoped);
147             } else {
148                 $locOptions = Notice::locationOptions(null,
149                                                       null,
150                                                       null,
151                                                       null,
152                                                       $this->scoped);
153             }
154
155             $options = array_merge($options, $locOptions);
156         }
157
158         $author_id = $this->scoped->id;
159         $text      = $content_shortened;
160
161         // Does the heavy-lifting for getting "To:" information
162
163         ToSelector::fillOptions($this, $options);
164
165         if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
166
167             $notice = Notice::saveNew($this->scoped->id, $content_shortened, 'web', $options);
168
169             if (isset($upload)) {
170                 $upload->attachToNotice($notice);
171             }
172
173             Event::handle('EndNoticeSaveWeb', array($this, $notice));
174         }
175
176         assert($notice instanceof Notice);
177
178         Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
179
180         if (StatusNet::isAjax()) {
181             $this->startHTML('text/xml;charset=utf-8');
182             $this->elementStart('head');
183             // TRANS: Page title after sending a notice.
184             $this->element('title', null, _('Notice posted'));
185             $this->elementEnd('head');
186             $this->elementStart('body');
187             $this->showNotice($notice);
188             $this->elementEnd('body');
189             $this->endHTML();
190             exit;
191         } else {
192             $returnto = $this->trimmed('returnto');
193
194             if ($returnto) {
195                 $url = common_local_url($returnto,
196                                         array('nickname' => $this->scoped->nickname));
197             } else {
198                 $url = common_local_url('shownotice',
199                                         array('notice' => $notice->id));
200             }
201             common_redirect($url, 303);
202         }
203     }
204
205     /**
206      * Show an Ajax-y error message
207      *
208      * Goes back to the browser, where it's shown in a popup.
209      *
210      * @param string $msg Message to show
211      *
212      * @return void
213      */
214     function ajaxErrorMsg($msg)
215     {
216         $this->startHTML('text/xml;charset=utf-8', true);
217         $this->elementStart('head');
218         // TRANS: Page title after an AJAX error occurs on the send notice page.
219         $this->element('title', null, _('Ajax Error'));
220         $this->elementEnd('head');
221         $this->elementStart('body');
222         $this->element('p', array('id' => 'error'), $msg);
223         $this->elementEnd('body');
224         $this->endHTML();
225     }
226
227     /**
228      * Show an Ajax-y notice form
229      *
230      * Goes back to the browser, where it's shown in a popup.
231      *
232      * @param string $msg Message to show
233      *
234      * @return void
235      */
236     function ajaxShowForm()
237     {
238         $this->startHTML('text/xml;charset=utf-8', true);
239         $this->elementStart('head');
240         // TRANS: Title for form to send a new notice.
241         $this->element('title', null, _m('TITLE','New notice'));
242         $this->elementEnd('head');
243         $this->elementStart('body');
244
245         $form = new NoticeForm($this);
246         $form->show();
247
248         $this->elementEnd('body');
249         $this->endHTML();
250     }
251
252     /**
253      * Formerly page output
254      *
255      * This used to be the whole page output; now that's been largely
256      * subsumed by showPage. So this just stores an error message, if
257      * it was passed, and calls showPage.
258      *
259      * Note that since we started doing Ajax output, this page is rarely
260      * seen.
261      *
262      * @param string  $msg     An error/info message, if any
263      * @param boolean $success false for error indication, true for info
264      *
265      * @return void
266      */
267     function showForm($msg=null, $success=false)
268     {
269         if (StatusNet::isAjax()) {
270             if ($msg) {
271                 $this->ajaxErrorMsg($msg);
272             } else {
273                 $this->ajaxShowForm();
274             }
275             return;
276         }
277
278         parent::showForm($msg, $success);
279     }
280
281     /**
282      * Output a notice
283      *
284      * Used to generate the notice code for Ajax results.
285      *
286      * @param Notice $notice Notice that was saved
287      *
288      * @return void
289      */
290     function showNotice(Notice $notice)
291     {
292         $nli = new NoticeListItem($notice, $this);
293         $nli->show();
294     }
295 }