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