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;
117 $upload = MediaFile::fromUpload('attach', $this->scoped);
119 if (isset($upload)) {
121 if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
122 $content_shortened .= ' ' . $upload->shortUrl();
124 Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
126 if (Notice::contentTooLong($content_shortened)) {
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()));
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'),
146 $locOptions = Notice::locationOptions(null,
153 $options = array_merge($options, $locOptions);
156 $author_id = $this->scoped->id;
157 $text = $content_shortened;
159 // Does the heavy-lifting for getting "To:" information
161 ToSelector::fillOptions($this, $options);
163 if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
165 $notice = Notice::saveNew($this->scoped->id, $content_shortened, 'web', $options);
167 if (isset($upload)) {
168 $upload->attachToNotice($notice);
171 Event::handle('EndNoticeSaveWeb', array($this, $notice));
174 assert($notice instanceof Notice);
176 Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
178 if (StatusNet::isAjax()) {
179 $this->startHTML('text/xml;charset=utf-8');
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');
190 $returnto = $this->trimmed('returnto');
193 $url = common_local_url($returnto,
194 array('nickname' => $this->scoped->nickname));
196 $url = common_local_url('shownotice',
197 array('notice' => $notice->id));
199 common_redirect($url, 303);
204 * Show an Ajax-y error message
206 * Goes back to the browser, where it's shown in a popup.
208 * @param string $msg Message to show
212 function ajaxErrorMsg($msg)
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');
226 * Show an Ajax-y notice form
228 * Goes back to the browser, where it's shown in a popup.
230 * @param string $msg Message to show
234 function ajaxShowForm()
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');
243 $form = new NoticeForm($this);
246 $this->elementEnd('body');
251 * Formerly page output
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.
257 * Note that since we started doing Ajax output, this page is rarely
260 * @param string $msg An error/info message, if any
261 * @param boolean $success false for error indication, true for info
265 function showForm($msg=null, $success=false)
267 if (StatusNet::isAjax()) {
269 $this->ajaxErrorMsg($msg);
271 $this->ajaxShowForm();
276 parent::showForm($msg, $success);
280 * // XXX: Should we be showing the notice form with microapps here?
282 * Overload for replies or bad results
284 * We show content in the notice form if there were replies or results.
288 function showNoticeForm()
290 $content = $this->trimmed('status_textarea');
292 $replyto = $this->trimmed('replyto');
293 $inreplyto = $this->trimmed('inreplyto');
294 $profile = Profile::getKV('nickname', $replyto);
296 $content = '@' . $profile->nickname . ' ';
299 // @fixme most of these bits above aren't being passed on above
303 $this->elementStart('div', 'input_forms');
307 'id' => 'input_form_status',
308 'class' => 'input_form current nonav'
312 $notice_form = new NoticeForm(
315 'content' => $content,
316 'inreplyto' => $inreplyto
320 $notice_form->show();
322 $this->elementEnd('div');
323 $this->elementEnd('div');
327 * Show an error message
329 * Shows an error message if there is one.
333 * @todo maybe show some instructions?
335 function showPageNotice()
338 $this->element('p', array('id' => 'error'), $this->msg);
345 * Used to generate the notice code for Ajax results.
347 * @param Notice $notice Notice that was saved
351 function showNotice(Notice $notice)
353 $nli = new NoticeListItem($notice, $this);