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 if ($this->getInfo() && $this->stored instanceof Notice) {
62 // TRANS: Page title after sending a notice.
63 return _('Notice posted');
65 if ($this->int('inreplyto')) {
66 return _m('TITLE', 'New reply');
68 // TRANS: Page title for sending a new notice.
69 return _m('TITLE','New notice');
72 protected function doPreparation()
74 foreach(array('inreplyto') as $opt) {
75 if ($this->trimmed($opt)) {
76 $this->formOpts[$opt] = $this->trimmed($opt);
80 // Backwards compatibility for "share this" widget things.
81 // If no 'content', use 'status_textarea'
82 $this->formOpts['content'] = $this->trimmed('content') ?: $this->trimmed('status_textarea');
86 * This handlePost saves a new notice, based on arguments
88 * If successful, will show the notice, or return an Ajax-y result.
89 * If not, it will show an error message -- possibly Ajax-y.
91 * Also, if the notice input looks like a command, it will run the
92 * command and show the results -- again, possibly ajaxy.
96 protected function handlePost()
100 assert($this->scoped); // XXX: maybe an error instead...
101 $user = $this->scoped->getUser();
102 $content = $this->trimmed('status_textarea');
104 Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
107 // TRANS: Client error displayed trying to send a notice without content.
108 $this->clientError(_('No content!'));
111 $inter = new CommandInterpreter();
113 $cmd = $inter->handle_command($user, $content);
116 if (GNUsocial::isAjax()) {
117 $cmd->execute(new AjaxWebChannel($this));
119 $cmd->execute(new WebChannel($this));
124 $content_shortened = $user->shortenLinks($content);
125 if (Notice::contentTooLong($content_shortened)) {
126 // TRANS: Client error displayed when the parameter "status" is missing.
127 // TRANS: %d is the maximum number of character for a notice.
128 $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.',
129 'That\'s too long. Maximum notice size is %d characters.',
130 Notice::maxContent()),
131 Notice::maxContent()));
134 $replyto = $this->int('inreplyto');
136 $options['reply_to'] = $replyto;
141 // throws exception on failure
142 $upload = MediaFile::fromUpload('attach', $this->scoped);
143 if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
144 $content_shortened .= ' ' . $upload->shortUrl();
146 Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
148 if (Notice::contentTooLong($content_shortened)) {
150 // TRANS: Client error displayed exceeding the maximum notice length.
151 // TRANS: %d is the maximum length for a notice.
152 $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.',
153 'Maximum notice size is %d characters, including attachment URL.',
154 Notice::maxContent()),
155 Notice::maxContent()));
157 } catch (NoUploadedMediaException $e) {
158 // simply no attached media to the new notice
162 if ($this->scoped->shareLocation()) {
163 // use browser data if checked; otherwise profile data
164 if ($this->arg('notice_data-geo')) {
165 $locOptions = Notice::locationOptions($this->trimmed('lat'),
166 $this->trimmed('lon'),
167 $this->trimmed('location_id'),
168 $this->trimmed('location_ns'),
171 $locOptions = Notice::locationOptions(null,
178 $options = array_merge($options, $locOptions);
181 $author_id = $this->scoped->id;
182 $text = $content_shortened;
184 // Does the heavy-lifting for getting "To:" information
186 ToSelector::fillOptions($this, $options);
188 if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
190 $this->stored = Notice::saveNew($this->scoped->id, $content_shortened, 'web', $options);
192 if ($upload instanceof MediaFile) {
193 $upload->attachToNotice($this->stored);
196 Event::handle('EndNoticeSaveWeb', array($this, $this->stored));
199 Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
201 if (!GNUsocial::isAjax()) {
202 $url = common_local_url('shownotice', array('notice' => $this->stored->id));
203 common_redirect($url, 303);
206 return _('Saved the notice!');
209 protected function showContent()
211 if ($this->getInfo() && $this->stored instanceof Notice) {
212 $this->showNotice($this->stored);
213 } elseif (!$this->getError()) {
214 parent::showContent();
221 * Used to generate the notice code for Ajax results.
223 * @param Notice $notice Notice that was saved
227 function showNotice(Notice $notice)
229 $nli = new NoticeListItem($notice, $this);
233 public function showNoticeForm()