]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
NewnoticeAction now uses Notice::saveActivity(...)
[quix0rs-gnu-social.git] / actions / newnotice.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Handler for posting new notices
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Personal
23  * @package   StatusNet
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/
31  */
32
33 if (!defined('GNUSOCIAL')) { exit(1); }
34
35 /**
36  * Action for posting new notices
37  *
38  * @category Personal
39  * @package  StatusNet
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/
45  */
46 class NewnoticeAction extends FormAction
47 {
48     protected $form = 'Notice';
49
50     /**
51      * Title of the page
52      *
53      * Note that this usually doesn't get called unless something went wrong
54      *
55      * @return string page title
56      */
57     function title()
58     {
59         if ($this->getInfo() && $this->stored instanceof Notice) {
60             // TRANS: Page title after sending a notice.
61             return _('Notice posted');
62         }
63         if ($this->int('inreplyto')) {
64             return _m('TITLE', 'New reply');
65         }
66         // TRANS: Page title for sending a new notice.
67         return _m('TITLE','New notice');
68     }
69
70     protected function doPreparation()
71     {
72         foreach(array('inreplyto') as $opt) {
73             if ($this->trimmed($opt)) {
74                 $this->formOpts[$opt] = $this->trimmed($opt);
75             }
76         }
77
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');
81     }
82
83     /**
84      * This doPost saves a new notice, based on arguments
85      *
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.
88      *
89      * Also, if the notice input looks like a command, it will run the
90      * command and show the results -- again, possibly ajaxy.
91      *
92      * @return void
93      */
94     protected function doPost()
95     {
96         assert($this->scoped instanceof Profile); // XXX: maybe an error instead...
97         $user = $this->scoped->getUser();
98         $content = $this->trimmed('status_textarea');
99         $options = array('source' => 'web');
100         Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
101
102         if (empty($content)) {
103             // TRANS: Client error displayed trying to send a notice without content.
104             $this->clientError(_('No content!'));
105         }
106
107         $inter = new CommandInterpreter();
108
109         $cmd = $inter->handle_command($user, $content);
110
111         if ($cmd) {
112             if (GNUsocial::isAjax()) {
113                 $cmd->execute(new AjaxWebChannel($this));
114             } else {
115                 $cmd->execute(new WebChannel($this));
116             }
117             return;
118         }
119
120         if ($this->int('inreplyto')) {
121             // Throws exception if the inreplyto Notice is given but not found.
122             $parent = Notice::getByID($this->int('inreplyto'));
123         } else {
124             $parent = null;
125         }
126
127         $act = new Activity();
128         $act->verb = ActivityVerb::POST;
129         $act->time = time();
130         $act->actor = $this->scoped->asActivityObject();
131
132         $content = $this->scoped->shortenLinks($content);
133
134         $upload = null;
135         try {
136             // throws exception on failure
137             $upload = MediaFile::fromUpload('attach', $this->scoped);
138             if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content, &$options))) {
139                 $content .= ' ' . $upload->shortUrl();
140             }
141             Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content, &$options));
142
143             if (Notice::contentTooLong($content)) {
144                 $upload->delete();
145                 // TRANS: Client error displayed exceeding the maximum notice length.
146                 // TRANS: %d is the maximum length for a notice.
147                 $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.',
148                                               'Maximum notice size is %d characters, including attachment URL.',
149                                               Notice::maxContent()),
150                                            Notice::maxContent()));
151             }
152
153             $act->enclosures[] = $upload->getEnclosure();
154         } catch (NoUploadedMediaException $e) {
155             // simply no attached media to the new notice
156         }
157
158         $actobj = new ActivityObject();
159         $actobj->type = ActivityObject::NOTE;
160         $actobj->content = common_render_content($content, $this->scoped, $parent);
161
162         $act->objects[] = $actobj;
163
164
165         $act->context = new ActivityContext();
166
167         if ($parent instanceof Notice) {
168             $act->context->replyToID = $parent->getUri();
169             $act->context->replyToUrl = $parent->getUrl(true);  // maybe we don't have to send true here to force a URL?
170         }
171
172         if ($this->scoped->shareLocation()) {
173             // use browser data if checked; otherwise profile data
174             if ($this->arg('notice_data-geo')) {
175                 $locOptions = Notice::locationOptions($this->trimmed('lat'),
176                                                       $this->trimmed('lon'),
177                                                       $this->trimmed('location_id'),
178                                                       $this->trimmed('location_ns'),
179                                                       $this->scoped);
180             } else {
181                 $locOptions = Notice::locationOptions(null,
182                                                       null,
183                                                       null,
184                                                       null,
185                                                       $this->scoped);
186             }
187
188             $act->context->location = Location::fromOptions($locOptions);
189         }
190
191         $author_id = $this->scoped->id;
192         $text      = $content;
193
194         // Does the heavy-lifting for getting "To:" information
195
196         ToSelector::fillOptions($this, $options);
197
198         // FIXME: Make sure NoticeTitle plugin gets a change to add the title to our activityobject!
199         if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
200
201             $this->stored = Notice::saveActivity($act, $this->scoped, $options);
202
203             if ($upload instanceof MediaFile) {
204                 $upload->attachToNotice($this->stored);
205             }
206
207             Event::handle('EndNoticeSaveWeb', array($this, $this->stored));
208         }
209
210         Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content, &$options));
211
212         if (!GNUsocial::isAjax()) {
213             $url = common_local_url('shownotice', array('notice' => $this->stored->id));
214             common_redirect($url, 303);
215         }
216
217         return _('Saved the notice!');
218     }
219
220     protected function showContent()
221     {
222         if ($this->getInfo() && $this->stored instanceof Notice) {
223             $this->showNotice($this->stored);
224         } elseif (!$this->getError()) {
225             parent::showContent();
226         }
227     }
228
229     /**
230      * Output a notice
231      *
232      * Used to generate the notice code for Ajax results.
233      *
234      * @param Notice $notice Notice that was saved
235      *
236      * @return void
237      */
238     function showNotice(Notice $notice)
239     {
240         $nli = new NoticeListItem($notice, $this);
241         $nli->show();
242     }
243
244     public function showNoticeForm()
245     {
246         // pass
247     }
248 }