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
50 protected $form = 'Notice';
55 * Note that this usually doesn't get called unless something went wrong
57 * @return string page title
61 // TRANS: Page title for sending a new notice.
62 return _m('TITLE','New notice');
66 * This handlePost saves a new notice, based on arguments
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.
71 * Also, if the notice input looks like a command, it will run the
72 * command and show the results -- again, possibly ajaxy.
76 protected function handlePost()
80 assert($this->scoped); // XXX: maybe an error instead...
81 $user = $this->scoped->getUser();
82 $content = $this->trimmed('status_textarea');
84 Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
87 // TRANS: Client error displayed trying to send a notice without content.
88 $this->clientError(_('No content!'));
91 $inter = new CommandInterpreter();
93 $cmd = $inter->handle_command($user, $content);
96 if (StatusNet::isAjax()) {
97 $cmd->execute(new AjaxWebChannel($this));
99 $cmd->execute(new WebChannel($this));
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()));
114 $replyto = intval($this->trimmed('inreplyto'));
116 $options['reply_to'] = $replyto;
119 $upload = MediaFile::fromUpload('attach', $this->scoped);
121 if (isset($upload)) {
123 if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
124 $content_shortened .= ' ' . $upload->shortUrl();
126 Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
128 if (Notice::contentTooLong($content_shortened)) {
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()));
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'),
148 $locOptions = Notice::locationOptions(null,
155 $options = array_merge($options, $locOptions);
158 $author_id = $this->scoped->id;
159 $text = $content_shortened;
161 // Does the heavy-lifting for getting "To:" information
163 ToSelector::fillOptions($this, $options);
165 if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
167 $notice = Notice::saveNew($this->scoped->id, $content_shortened, 'web', $options);
169 if (isset($upload)) {
170 $upload->attachToNotice($notice);
173 Event::handle('EndNoticeSaveWeb', array($this, $notice));
176 assert($notice instanceof Notice);
178 Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
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');
192 $returnto = $this->trimmed('returnto');
195 $url = common_local_url($returnto,
196 array('nickname' => $this->scoped->nickname));
198 $url = common_local_url('shownotice',
199 array('notice' => $notice->id));
201 common_redirect($url, 303);
206 * Show an Ajax-y error message
208 * Goes back to the browser, where it's shown in a popup.
210 * @param string $msg Message to show
214 function ajaxErrorMsg($msg)
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');
228 * Show an Ajax-y notice form
230 * Goes back to the browser, where it's shown in a popup.
232 * @param string $msg Message to show
236 function ajaxShowForm()
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');
245 $form = new NoticeForm($this);
248 $this->elementEnd('body');
253 * Formerly page output
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.
259 * Note that since we started doing Ajax output, this page is rarely
262 * @param string $msg An error/info message, if any
263 * @param boolean $success false for error indication, true for info
267 function showForm($msg=null, $success=false)
269 if (StatusNet::isAjax()) {
271 $this->ajaxErrorMsg($msg);
273 $this->ajaxShowForm();
278 parent::showForm($msg, $success);
284 * Used to generate the notice code for Ajax results.
286 * @param Notice $notice Notice that was saved
290 function showNotice(Notice $notice)
292 $nli = new NoticeListItem($notice, $this);