]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DirectMessage/actions/newmessage.php
NewmessageAction lacked the $form property
[quix0rs-gnu-social.git] / plugins / DirectMessage / actions / newmessage.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Handler for posting new messages
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 direct messages
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
49 class NewmessageAction extends FormAction
50 {
51     var $content = null;
52     var $to = null;
53     var $other = null;
54
55     protected $form = 'message';    // will become MessageForm later
56
57     /**
58      * Title of the page
59      *
60      * Note that this usually doesn't get called unless something went wrong
61      *
62      * @return string page title
63      */
64
65     function title()
66     {
67         // TRANS: Page title for new direct message page.
68         return _('New message');
69     }
70
71     /**
72      * Handle input, produce output
73      *
74      * @param array $args $_REQUEST contents
75      *
76      * @return void
77      */
78
79     protected function prepare(array $args=array())
80     {
81         parent::prepare($args);
82
83         $this->content = $this->trimmed('content');
84         $this->to = $this->trimmed('to');
85
86         if ($this->to) {
87
88             $this->other = Profile::getKV('id', $this->to);
89
90             if (!$this->other instanceof Profile) {
91                 // TRANS: Client error displayed trying to send a direct message to a non-existing user.
92                 $this->clientError(_('No such user.'), 404);
93             }
94
95             if (!$this->other->isLocal()) {
96                 // TRANS: Explains that current federation does not support direct, private messages yet.
97                 $this->clientError(_('You cannot send direct messages to federated users yet.'));
98             }
99
100             if (!$this->scoped->mutuallySubscribed($this->other)) {
101                 // TRANS: Client error displayed trying to send a direct message to a user while sender and
102                 // TRANS: receiver are not subscribed to each other.
103                 $this->clientError(_('You cannot send a message to this user.'), 404);
104             }
105         }
106
107         return true;
108     }
109
110     protected function handlePost()
111     {
112         parent::handlePost();
113
114         assert($this->scoped instanceof Profile); // XXX: maybe an error instead...
115
116         if (!$this->content) {
117             // TRANS: Form validator error displayed trying to send a direct message without content.
118             $this->clientError(_('No content!'));
119         } else {
120             $content_shortened = $this->scoped->shortenLinks($this->content);
121
122             if (Message::contentTooLong($content_shortened)) {
123                 // TRANS: Form validation error displayed when message content is too long.
124                 // TRANS: %d is the maximum number of characters for a message.
125                 $this->clientError(sprintf(_m('That\'s too long. Maximum message size is %d character.',
126                                            'That\'s too long. Maximum message size is %d characters.',
127                                            Message::maxContent()),
128                                         Message::maxContent()));
129             }
130         }
131
132         if (!$this->other) {
133             // TRANS: Form validation error displayed trying to send a direct message without specifying a recipient.
134             $this->clientError(_('No recipient specified.'));
135         } else if (!$this->scoped->mutuallySubscribed($this->other)) {
136             // TRANS: Client error displayed trying to send a direct message to a user while sender and
137             // TRANS: receiver are not subscribed to each other.
138             $this->clientError(_('You cannot send a message to this user.'), 404);
139         } else if ($this->scoped->id == $this->other->id) {
140             // TRANS: Client error displayed trying to send a direct message to self.
141             $this->clientError(_('Do not send a message to yourself; ' .
142                 'just say it to yourself quietly instead.'), 403);
143         }
144
145         $message = Message::saveNew($this->scoped->id, $this->other->id, $this->content, 'web');
146         $message->notify();
147
148         if ($this->boolean('ajax')) {
149             $this->startHTML('text/xml;charset=utf-8');
150             $this->elementStart('head');
151             // TRANS: Page title after sending a direct message.
152             $this->element('title', null, _('Message sent'));
153             $this->elementEnd('head');
154             $this->elementStart('body');
155             $this->element('p', array('id' => 'command_result'),
156                 // TRANS: Confirmation text after sending a direct message.
157                 // TRANS: %s is the direct message recipient.
158                 sprintf(_('Direct message to %s sent.'),
159                     $this->other->nickname));
160             $this->elementEnd('body');
161             $this->endHTML();
162         } else {
163             $url = common_local_url('outbox',
164                 array('nickname' => $this->scoped->nickname));
165             common_redirect($url, 303);
166         }
167     }
168
169     /**
170      * Show an Ajax-y error message
171      *
172      * Goes back to the browser, where it's shown in a popup.
173      *
174      * @param string $msg Message to show
175      *
176      * @return void
177      */
178
179     function ajaxErrorMsg($msg)
180     {
181         $this->startHTML('text/xml;charset=utf-8', true);
182         $this->elementStart('head');
183         // TRANS: Page title after an AJAX error occurred on the "send direct message" page.
184         $this->element('title', null, _('Ajax Error'));
185         $this->elementEnd('head');
186         $this->elementStart('body');
187         $this->element('p', array('id' => 'error'), $msg);
188         $this->elementEnd('body');
189         $this->endHTML();
190     }
191
192     function showForm($msg = null)
193     {
194         if ($msg && $this->boolean('ajax')) {
195             $this->ajaxErrorMsg($msg);
196             return;
197         }
198
199         $this->msg = $msg;
200         if ($this->trimmed('ajax')) {
201             $this->startHTML('text/xml;charset=utf-8');
202             $this->elementStart('head');
203             // TRANS: Page title on page for sending a direct message.
204             $this->element('title', null, _('New message'));
205             $this->elementEnd('head');
206             $this->elementStart('body');
207             $this->showNoticeForm();
208             $this->elementEnd('body');
209             $this->endHTML();
210         }
211         else {
212             $this->showPage();
213         }
214     }
215
216     function showPageNotice()
217     {
218         if ($this->msg) {
219             $this->element('p', 'error', $this->msg);
220         }
221     }
222
223     // Do nothing (override)
224
225     function showNoticeForm()
226     {
227         $message_form = new MessageForm($this, $this->other, $this->content);
228         $message_form->show();
229     }
230 }