]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
jQuery cookie plugin updated
[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('STATUSNET')) {
34     exit(1);
35 }
36
37 /**
38  * Action for posting new notices
39  *
40  * @category Personal
41  * @package  StatusNet
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/
47  */
48 class NewnoticeAction extends FormAction
49 {
50     protected $form = 'Notice';
51
52     /**
53      * Title of the page
54      *
55      * Note that this usually doesn't get called unless something went wrong
56      *
57      * @return string page title
58      */
59     function title()
60     {
61         if ($this->getInfo() && $this->stored instanceof Notice) {
62             // TRANS: Page title after sending a notice.
63             return _('Notice posted');
64         }
65         // TRANS: Page title for sending a new notice.
66         return _m('TITLE','New notice');
67     }
68
69     protected function doPreparation()
70     {
71         foreach(array('inreplyto') as $opt) {
72             if ($this->trimmed($opt)) {
73                 $this->formOpts[$opt] = $this->trimmed($opt);
74             }
75         }
76
77         // Backwards compatibility for "share this" widget things.
78         // If no 'content', use 'status_textarea'
79         $this->formOpts['content'] = $this->trimmed('content') ?: $this->trimmed('status_textarea');
80     }
81
82     /**
83      * This handlePost saves a new notice, based on arguments
84      *
85      * If successful, will show the notice, or return an Ajax-y result.
86      * If not, it will show an error message -- possibly Ajax-y.
87      *
88      * Also, if the notice input looks like a command, it will run the
89      * command and show the results -- again, possibly ajaxy.
90      *
91      * @return void
92      */
93     protected function handlePost()
94     {
95         parent::handlePost();
96
97         assert($this->scoped); // XXX: maybe an error instead...
98         $user = $this->scoped->getUser();
99         $content = $this->trimmed('status_textarea');
100         $options = array();
101         Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
102
103         if (!$content) {
104             // TRANS: Client error displayed trying to send a notice without content.
105             $this->clientError(_('No content!'));
106         }
107
108         $inter = new CommandInterpreter();
109
110         $cmd = $inter->handle_command($user, $content);
111
112         if ($cmd) {
113             if (GNUsocial::isAjax()) {
114                 $cmd->execute(new AjaxWebChannel($this));
115             } else {
116                 $cmd->execute(new WebChannel($this));
117             }
118             return;
119         }
120
121         $content_shortened = $user->shortenLinks($content);
122         if (Notice::contentTooLong($content_shortened)) {
123             // TRANS: Client error displayed when the parameter "status" is missing.
124             // TRANS: %d is the maximum number of character for a notice.
125             $this->clientError(sprintf(_m('That\'s too long. Maximum notice size is %d character.',
126                                           'That\'s too long. Maximum notice size is %d characters.',
127                                           Notice::maxContent()),
128                                        Notice::maxContent()));
129         }
130
131         $replyto = intval($this->trimmed('inreplyto'));
132         if ($replyto) {
133             $options['reply_to'] = $replyto;
134         }
135
136         $upload = null;
137         try {
138             // throws exception on failure
139             $upload = MediaFile::fromUpload('attach', $this->scoped);
140             if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
141                 $content_shortened .= ' ' . $upload->shortUrl();
142             }
143             Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options));
144
145             if (Notice::contentTooLong($content_shortened)) {
146                 $upload->delete();
147                 // TRANS: Client error displayed exceeding the maximum notice length.
148                 // TRANS: %d is the maximum length for a notice.
149                 $this->clientError(sprintf(_m('Maximum notice size is %d character, including attachment URL.',
150                                               'Maximum notice size is %d characters, including attachment URL.',
151                                               Notice::maxContent()),
152                                            Notice::maxContent()));
153             }
154         } catch (NoUploadedMediaException $e) {
155             // simply no attached media to the new notice
156         }
157
158
159         if ($this->scoped->shareLocation()) {
160             // use browser data if checked; otherwise profile data
161             if ($this->arg('notice_data-geo')) {
162                 $locOptions = Notice::locationOptions($this->trimmed('lat'),
163                                                       $this->trimmed('lon'),
164                                                       $this->trimmed('location_id'),
165                                                       $this->trimmed('location_ns'),
166                                                       $this->scoped);
167             } else {
168                 $locOptions = Notice::locationOptions(null,
169                                                       null,
170                                                       null,
171                                                       null,
172                                                       $this->scoped);
173             }
174
175             $options = array_merge($options, $locOptions);
176         }
177
178         $author_id = $this->scoped->id;
179         $text      = $content_shortened;
180
181         // Does the heavy-lifting for getting "To:" information
182
183         ToSelector::fillOptions($this, $options);
184
185         if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) {
186
187             $this->stored = Notice::saveNew($this->scoped->id, $content_shortened, 'web', $options);
188
189             if ($upload instanceof MediaFile) {
190                 $upload->attachToNotice($this->stored);
191             }
192
193             Event::handle('EndNoticeSaveWeb', array($this, $this->stored));
194         }
195
196         Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content_shortened, &$options));
197
198         if (!GNUsocial::isAjax()) {
199             $url = common_local_url('shownotice', array('notice' => $this->stored->id));
200             common_redirect($url, 303);
201         }
202
203         return _('Saved the notice!');
204     }
205
206     protected function showContent()
207     {
208         if ($this->getInfo() && $this->stored instanceof Notice) {
209             $this->showNotice($this->stored);
210         } elseif (!$this->getError()) {
211             parent::showContent();
212         }
213     }
214
215     /**
216      * Output a notice
217      *
218      * Used to generate the notice code for Ajax results.
219      *
220      * @param Notice $notice Notice that was saved
221      *
222      * @return void
223      */
224     function showNotice(Notice $notice)
225     {
226         $nli = new NoticeListItem($notice, $this);
227         $nli->show();
228     }
229
230     public function showNoticeForm()
231     {
232         // pass
233     }
234 }