3 * StatusNet, the distributed open-source microblogging tool
5 * Handler for posting new notices
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.
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.
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/>.
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/
33 if (!defined('STATUSNET')) {
38 * Action for posting new notices
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/
48 class NewnoticeAction extends FormAction
53 * Note that this usually doesn't get called unless something went wrong
55 * @return string page title
59 // TRANS: Page title for sending a new notice.
60 return _m('TITLE','New notice');
64 * This handlePost saves a new notice, based on arguments
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.
69 * Also, if the notice input looks like a command, it will run the
70 * command and show the results -- again, possibly ajaxy.
74 protected function handlePost()
78 assert($this->scoped); // XXX: maybe an error instead...
79 $user = $this->scoped->getUser();
80 $content = $this->trimmed('status_textarea');
82 Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
85 // TRANS: Client error displayed trying to send a notice without content.
86 $this->clientError(_('No content!'));
89 $inter = new CommandInterpreter();
91 $cmd = $inter->handle_command($user, $content);
94 if (StatusNet::isAjax()) {
95 $cmd->execute(new AjaxWebChannel($this));
97 $cmd->execute(new WebChannel($this));
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()));
112 $replyto = intval($this->trimmed('inreplyto'));
114 $options['reply_to'] = $replyto;
118 $upload = MediaFile::fromUpload('attach');
120 if (isset($upload)) {
122 if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
123 $content_shortened .= ' ' . $upload->shortUrl();
125 Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
127 if (Notice::contentTooLong($content_shortened)) {
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()));
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'),
147 $locOptions = Notice::locationOptions(null,
154 $options = array_merge($options, $locOptions);
157 $author_id = $this->scoped->id;
158 $text = $content_shortened;
160 // Does the heavy-lifting for getting "To:" information
162 ToSelector::fillOptions($this, $options);
164 if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
166 $notice = Notice::saveNew($this->scoped->id, $content_shortened, 'web', $options);
168 if (isset($upload)) {
169 $upload->attachToNotice($notice);
172 Event::handle('EndNoticeSaveWeb', array($this, $notice));
174 Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
176 if (StatusNet::isAjax()) {
177 $this->startHTML('text/xml;charset=utf-8');
178 $this->elementStart('head');
179 // TRANS: Page title after sending a notice.
180 $this->element('title', null, _('Notice posted'));
181 $this->elementEnd('head');
182 $this->elementStart('body');
183 $this->showNotice($notice);
184 $this->elementEnd('body');
188 $returnto = $this->trimmed('returnto');
191 $url = common_local_url($returnto,
192 array('nickname' => $this->scoped->nickname));
194 $url = common_local_url('shownotice',
195 array('notice' => $notice->id));
197 common_redirect($url, 303);
202 * Show an Ajax-y error message
204 * Goes back to the browser, where it's shown in a popup.
206 * @param string $msg Message to show
210 function ajaxErrorMsg($msg)
212 $this->startHTML('text/xml;charset=utf-8', true);
213 $this->elementStart('head');
214 // TRANS: Page title after an AJAX error occurs on the send notice page.
215 $this->element('title', null, _('Ajax Error'));
216 $this->elementEnd('head');
217 $this->elementStart('body');
218 $this->element('p', array('id' => 'error'), $msg);
219 $this->elementEnd('body');
220 $this->elementEnd('html');
224 * Show an Ajax-y notice form
226 * Goes back to the browser, where it's shown in a popup.
228 * @param string $msg Message to show
232 function ajaxShowForm()
234 $this->startHTML('text/xml;charset=utf-8', true);
235 $this->elementStart('head');
236 // TRANS: Title for form to send a new notice.
237 $this->element('title', null, _m('TITLE','New notice'));
238 $this->elementEnd('head');
239 $this->elementStart('body');
241 $form = new NoticeForm($this);
244 $this->elementEnd('body');
245 $this->elementEnd('html');
249 * Formerly page output
251 * This used to be the whole page output; now that's been largely
252 * subsumed by showPage. So this just stores an error message, if
253 * it was passed, and calls showPage.
255 * Note that since we started doing Ajax output, this page is rarely
258 * @param string $msg An error/info message, if any
259 * @param boolean $success false for error indication, true for info
263 function showForm($msg=null, $success=false)
265 if (StatusNet::isAjax()) {
267 $this->ajaxErrorMsg($msg);
269 $this->ajaxShowForm();
274 parent::showForm($msg, $success);
278 * // XXX: Should we be showing the notice form with microapps here?
280 * Overload for replies or bad results
282 * We show content in the notice form if there were replies or results.
286 function showNoticeForm()
288 $content = $this->trimmed('status_textarea');
290 $replyto = $this->trimmed('replyto');
291 $inreplyto = $this->trimmed('inreplyto');
292 $profile = Profile::getKV('nickname', $replyto);
294 $content = '@' . $profile->nickname . ' ';
297 // @fixme most of these bits above aren't being passed on above
301 $this->elementStart('div', 'input_forms');
305 'id' => 'input_form_status',
306 'class' => 'input_form current nonav'
310 $notice_form = new NoticeForm(
313 'content' => $content,
314 'inreplyto' => $inreplyto
318 $notice_form->show();
320 $this->elementEnd('div');
321 $this->elementEnd('div');
325 * Show an error message
327 * Shows an error message if there is one.
331 * @todo maybe show some instructions?
333 function showPageNotice()
336 $this->element('p', array('id' => 'error'), $this->msg);
343 * Used to generate the notice code for Ajax results.
345 * @param Notice $notice Notice that was saved
349 function showNotice($notice)
351 $nli = new NoticeListItem($notice, $this);