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('GNUSOCIAL')) { exit(1); }
36 * Action for posting new notices
40 * @author Evan Prodromou <evan@status.net>
41 * @author Zach Copley <zach@status.net>
42 * @author Sarven Capadisli <csarven@status.net>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://status.net/
46 class NewnoticeAction extends FormAction
48 protected $form = 'Notice';
53 * Note that this usually doesn't get called unless something went wrong
55 * @return string page title
59 if ($this->getInfo() && $this->stored instanceof Notice) {
60 // TRANS: Page title after sending a notice.
61 return _('Notice posted');
63 if ($this->int('inreplyto')) {
64 return _m('TITLE', 'New reply');
66 // TRANS: Page title for sending a new notice.
67 return _m('TITLE','New notice');
70 protected function doPreparation()
72 foreach(array('inreplyto') as $opt) {
73 if ($this->trimmed($opt)) {
74 $this->formOpts[$opt] = $this->trimmed($opt);
78 // Backwards compatibility for "share this" widget things.
79 // If no 'content', use 'status_textarea'
80 $this->formOpts['content'] = $this->trimmed('content') ?: $this->trimmed('status_textarea');
84 * This doPost saves a new notice, based on arguments
86 * If successful, will show the notice, or return an Ajax-y result.
87 * If not, it will show an error message -- possibly Ajax-y.
89 * Also, if the notice input looks like a command, it will run the
90 * command and show the results -- again, possibly ajaxy.
94 protected function doPost()
96 assert($this->scoped instanceof Profile); // XXX: maybe an error instead...
97 $user = $this->scoped->getUser();
98 $content = $this->trimmed('status_textarea');
100 Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
102 if (empty($content)) {
103 // TRANS: Client error displayed trying to send a notice without content.
104 $this->clientError(_('No content!'));
107 $inter = new CommandInterpreter();
109 $cmd = $inter->handle_command($user, $content);
112 if (GNUsocial::isAjax()) {
113 $cmd->execute(new AjaxWebChannel($this));
115 $cmd->execute(new WebChannel($this));
120 $content_shortened = $user->shortenLinks($content);
121 if (Notice::contentTooLong($content_shortened)) {
122 // TRANS: Client error displayed when the parameter "status" is missing.
123 // TRANS: %d is the maximum number of character for a notice.
124 $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.',
125 'That\'s too long. Maximum notice size is %d characters.',
126 Notice::maxContent()),
127 Notice::maxContent()));
130 $replyto = $this->int('inreplyto');
132 $options['reply_to'] = $replyto;
137 // throws exception on failure
138 $upload = MediaFile::fromUpload('attach', $this->scoped);
139 if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
140 $content_shortened .= ' ' . $upload->shortUrl();
142 Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
144 if (Notice::contentTooLong($content_shortened)) {
146 // TRANS: Client error displayed exceeding the maximum notice length.
147 // TRANS: %d is the maximum length for a notice.
148 $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.',
149 'Maximum notice size is %d characters, including attachment URL.',
150 Notice::maxContent()),
151 Notice::maxContent()));
153 } catch (NoUploadedMediaException $e) {
154 // simply no attached media to the new notice
158 if ($this->scoped->shareLocation()) {
159 // use browser data if checked; otherwise profile data
160 if ($this->arg('notice_data-geo')) {
161 $locOptions = Notice::locationOptions($this->trimmed('lat'),
162 $this->trimmed('lon'),
163 $this->trimmed('location_id'),
164 $this->trimmed('location_ns'),
167 $locOptions = Notice::locationOptions(null,
174 $options = array_merge($options, $locOptions);
177 $author_id = $this->scoped->id;
178 $text = $content_shortened;
180 // Does the heavy-lifting for getting "To:" information
182 ToSelector::fillOptions($this, $options);
184 if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
186 $this->stored = Notice::saveNew($this->scoped->id, $content_shortened, 'web', $options);
188 if ($upload instanceof MediaFile) {
189 $upload->attachToNotice($this->stored);
192 Event::handle('EndNoticeSaveWeb', array($this, $this->stored));
195 Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
197 if (!GNUsocial::isAjax()) {
198 $url = common_local_url('shownotice', array('notice' => $this->stored->id));
199 common_redirect($url, 303);
202 return _('Saved the notice!');
205 protected function showContent()
207 if ($this->getInfo() && $this->stored instanceof Notice) {
208 $this->showNotice($this->stored);
209 } elseif (!$this->getError()) {
210 parent::showContent();
217 * Used to generate the notice code for Ajax results.
219 * @param Notice $notice Notice that was saved
223 function showNotice(Notice $notice)
225 $nli = new NoticeListItem($notice, $this);
229 public function showNoticeForm()