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));
173 Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
175 if (StatusNet::isAjax()) {
176 $this->startHTML('text/xml;charset=utf-8');
177 $this->elementStart('head');
178 // TRANS: Page title after sending a notice.
179 $this->element('title', null, _('Notice posted'));
180 $this->elementEnd('head');
181 $this->elementStart('body');
182 $this->showNotice($notice);
183 $this->elementEnd('body');
187 $returnto = $this->trimmed('returnto');
190 $url = common_local_url($returnto,
191 array('nickname' => $this->scoped->nickname));
193 $url = common_local_url('shownotice',
194 array('notice' => $notice->id));
196 common_redirect($url, 303);
201 * Show an Ajax-y error message
203 * Goes back to the browser, where it's shown in a popup.
205 * @param string $msg Message to show
209 function ajaxErrorMsg($msg)
211 $this->startHTML('text/xml;charset=utf-8', true);
212 $this->elementStart('head');
213 // TRANS: Page title after an AJAX error occurs on the send notice page.
214 $this->element('title', null, _('Ajax Error'));
215 $this->elementEnd('head');
216 $this->elementStart('body');
217 $this->element('p', array('id' => 'error'), $msg);
218 $this->elementEnd('body');
223 * Show an Ajax-y notice form
225 * Goes back to the browser, where it's shown in a popup.
227 * @param string $msg Message to show
231 function ajaxShowForm()
233 $this->startHTML('text/xml;charset=utf-8', true);
234 $this->elementStart('head');
235 // TRANS: Title for form to send a new notice.
236 $this->element('title', null, _m('TITLE','New notice'));
237 $this->elementEnd('head');
238 $this->elementStart('body');
240 $form = new NoticeForm($this);
243 $this->elementEnd('body');
248 * Formerly page output
250 * This used to be the whole page output; now that's been largely
251 * subsumed by showPage. So this just stores an error message, if
252 * it was passed, and calls showPage.
254 * Note that since we started doing Ajax output, this page is rarely
257 * @param string $msg An error/info message, if any
258 * @param boolean $success false for error indication, true for info
262 function showForm($msg=null, $success=false)
264 if (StatusNet::isAjax()) {
266 $this->ajaxErrorMsg($msg);
268 $this->ajaxShowForm();
273 parent::showForm($msg, $success);
277 * // XXX: Should we be showing the notice form with microapps here?
279 * Overload for replies or bad results
281 * We show content in the notice form if there were replies or results.
285 function showNoticeForm()
287 $content = $this->trimmed('status_textarea');
289 $replyto = $this->trimmed('replyto');
290 $inreplyto = $this->trimmed('inreplyto');
291 $profile = Profile::getKV('nickname', $replyto);
293 $content = '@' . $profile->nickname . ' ';
296 // @fixme most of these bits above aren't being passed on above
300 $this->elementStart('div', 'input_forms');
304 'id' => 'input_form_status',
305 'class' => 'input_form current nonav'
309 $notice_form = new NoticeForm(
312 'content' => $content,
313 'inreplyto' => $inreplyto
317 $notice_form->show();
319 $this->elementEnd('div');
320 $this->elementEnd('div');
324 * Show an error message
326 * Shows an error message if there is one.
330 * @todo maybe show some instructions?
332 function showPageNotice()
335 $this->element('p', array('id' => 'error'), $this->msg);
342 * Used to generate the notice code for Ajax results.
344 * @param Notice $notice Notice that was saved
348 function showNotice($notice)
350 $nli = new NoticeListItem($notice, $this);