]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/mailbox.php
Add a menuItem method to Action
[quix0rs-gnu-social.git] / lib / mailbox.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * common superclass for direct messages inbox and outbox
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  Message
23  * @package   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2008 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/personal.php';
35
36 define('MESSAGES_PER_PAGE', 20);
37
38 /**
39  * common superclass for direct messages inbox and outbox
40  *
41  * @category Message
42  * @package  Laconica
43  * @author   Evan Prodromou <evan@controlyourself.ca>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://laconi.ca/
46  * @see      InboxAction
47  * @see      OutboxAction
48  */
49
50 class MailboxAction extends PersonalAction
51 {
52     /**
53      * output page based on arguments
54      *
55      * @param array $args HTTP arguments (from $_REQUEST)
56      *
57      * @return void
58      */
59
60     function handle($args)
61     {
62         parent::handle($args);
63
64         $nickname = common_canonical_nickname($this->arg('nickname'));
65
66         $user = User::staticGet('nickname', $nickname);
67
68         if (!$user) {
69             $this->client_error(_('No such user.'), 404);
70             return;
71         }
72
73         $cur = common_current_user();
74
75         if (!$cur || $cur->id != $user->id) {
76             $this->client_error(_('Only the user can read their own mailboxes.'),
77                                 403);
78             return;
79         }
80
81         $profile = $user->getProfile();
82
83         if (!$profile) {
84             $this->server_error(_('User has no profile.'));
85             return;
86         }
87
88         $page = $this->trimmed('page');
89
90         if (!$page) {
91             $page = 1;
92         }
93
94         $this->showPage($user, $page);
95     }
96
97     /**
98      * returns the title of the page
99      *
100      * @param User $user current user
101      * @param int  $page current page
102      *
103      * @return string localised title of the page
104      */
105
106     function getTitle($user, $page)
107     {
108         return '';
109     }
110
111     /**
112      * instructions for using this page
113      *
114      * @return string localised instructions for using the page
115      */
116
117     function getInstructions()
118     {
119         return '';
120     }
121
122     /**
123      * do structured output for the "instructions" are of the page
124      *
125      * @return void
126      */
127
128     function showTop()
129     {
130         $cur = common_current_user();
131
132         common_message_form(null, $cur, null);
133
134         $this->views_menu();
135     }
136
137     /**
138      * show a full page of output
139      *
140      * @param User $user The current user
141      * @param int  $page The page the user is on
142      *
143      * @return void
144      */
145
146     function showPage($user, $page)
147     {
148         common_show_header($this->getTitle($user, $page),
149                            null, null,
150                            array($this, 'showTop'));
151
152         $this->showBox($user, $page);
153
154         common_show_footer();
155     }
156
157     /**
158      * retrieve the messages appropriate for this mailbox
159      *
160      * Does a query for the right messages
161      *
162      * @param User $user The current user
163      * @param int  $page The page the user is on
164      *
165      * @return Message data object with stream for messages
166      */
167
168     function getMessages($user, $page)
169     {
170         return null;
171     }
172
173     /**
174      * show the messages for a mailbox in list format
175      *
176      * Includes the pagination links (before, after).
177      *
178      * @param User $user The current user
179      * @param int  $page The page the user is on
180      *
181      * @return void
182      */
183
184     function showBox($user, $page)
185     {
186         $message = $this->getMessages($user, $page);
187
188         if ($message) {
189
190             $cnt = 0;
191             common_element_start('ul', array('id' => 'messages'));
192
193             while ($message->fetch() && $cnt <= MESSAGES_PER_PAGE) {
194                 $cnt++;
195
196                 if ($cnt > MESSAGES_PER_PAGE) {
197                     break;
198                 }
199
200                 $this->showMessage($message);
201             }
202
203             common_element_end('ul');
204
205             common_pagination($page > 1, $cnt > MESSAGES_PER_PAGE,
206                               $page, $this->trimmed('action'),
207                               array('nickname' => $user->nickname));
208
209             $message->free();
210             unset($message);
211         }
212     }
213
214     /**
215      * returns the profile we want to show with the message
216      *
217      * For inboxes, we show the sender; for outboxes, the recipient.
218      *
219      * @param Message $message The message to get the profile for
220      *
221      * @return Profile The profile that matches the message
222      */
223
224     function getMessageProfile($message)
225     {
226         return null;
227     }
228
229     /**
230      * show a single message in the list format
231      *
232      * @param Message $message the message to show
233      *
234      * @return void
235      */
236
237     function showMessage($message)
238     {
239         common_element_start('li', array('class' => 'message_single',
240                                          'id' => 'message-' . $message->id));
241
242         $profile = $this->getMessageProfile($message);
243
244         $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
245         common_element_start('a', array('href' => $profile->profileurl));
246         common_element('img', array('src' => ($avatar) ?
247                                     common_avatar_display_url($avatar) :
248                                     common_default_avatar(AVATAR_STREAM_SIZE),
249                                     'class' => 'avatar stream',
250                                     'width' => AVATAR_STREAM_SIZE,
251                                     'height' => AVATAR_STREAM_SIZE,
252                                     'alt' =>
253                                     ($profile->fullname) ? $profile->fullname :
254                                     $profile->nickname));
255         common_element_end('a');
256         common_element('a', array('href' => $profile->profileurl,
257                                   'class' => 'nickname'),
258                        $profile->nickname);
259         // FIXME: URL, image, video, audio
260         common_element_start('p', array('class' => 'content'));
261         common_raw($message->rendered);
262         common_element_end('p');
263
264         $messageurl = common_local_url('showmessage',
265                                        array('message' => $message->id));
266
267         // XXX: we need to figure this out better. Is this right?
268         if (strcmp($message->uri, $messageurl) != 0 &&
269             preg_match('/^http/', $message->uri)) {
270             $messageurl = $message->uri;
271         }
272         common_element_start('p', 'time');
273         common_element('a', array('class' => 'permalink',
274                                   'href' => $messageurl,
275                                   'title' => common_exact_date($message->created)),
276                        common_date_string($message->created));
277         if ($message->source) {
278             common_text(_(' from '));
279             $this->source_link($message->source);
280         }
281
282         common_element_end('p');
283
284         common_element_end('li');
285     }
286 }