]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showstream.php
b54aad855789616212ec9e3a7789e526ef9be422
[quix0rs-gnu-social.git] / actions / showstream.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/stream.php');
23
24 define('SUBSCRIPTIONS_PER_ROW', 4);
25 define('SUBSCRIPTIONS', 80);
26
27 class ShowstreamAction extends StreamAction {
28
29         function handle($args) {
30
31                 parent::handle($args);
32
33                 $nickname = common_canonical_nickname($this->arg('nickname'));
34                 $user = User::staticGet('nickname', $nickname);
35
36                 if (!$user) {
37                         $this->no_such_user();
38                         return;
39                 }
40
41                 $profile = $user->getProfile();
42
43                 if (!$profile) {
44                         common_server_error(_t('User record exists without profile.'));
45                         return;
46                 }
47
48                 # Looks like we're good; start output
49
50                 # For YADIS discovery, we also have a <meta> tag
51
52                 header('X-XRDS-Location: '. common_local_url('xrds', array('nickname' =>
53                                                                                                                                    $user->nickname)));
54
55                 common_show_header($profile->nickname, array($this, 'show_header'), $user);
56
57                 $cur = common_current_user();
58
59                 if ($cur && $profile->id == $cur->id) {
60                         common_notice_form();
61                 }
62
63                 $this->show_sidebar($profile);
64
65                 $this->show_notices($profile);
66
67                 common_show_footer();
68         }
69
70         function show_header($user) {
71                 common_element('link', array('rel' => 'alternate',
72                                                                          'href' => common_local_url('userrss', array('nickname' =>
73                                                                                                                                                            $user->nickname)),
74                                                                          'type' => 'application/rss+xml',
75                                                                          'title' => _t('Notice feed for ') . $user->nickname));
76                 # for remote subscriptions etc.
77                 common_element('meta', array('http-equiv' => 'X-XRDS-Location',
78                                                                          'content' => common_local_url('xrds', array('nickname' =>
79                                                                                                                                                            $user->nickname))));
80         }
81
82         function no_such_user() {
83                 common_user_error('No such user');
84         }
85
86         function show_sidebar($profile) {
87
88                 common_element_start('div', 'sidebar width33 floatRight greenBg');
89
90                 $this->show_profile($profile);
91
92                 $this->show_last_notice($profile);
93
94                 $cur = common_current_user();
95
96                 if ($cur) {
97                         if ($cur->id != $profile->id) {
98                                 if ($cur->isSubscribed($profile)) {
99                                         $this->show_unsubscribe_form($profile);
100                                 } else {
101                                         $this->show_subscribe_form($profile);
102                                 }
103                         }
104                 } else {
105                         $this->show_remote_subscribe_form($profile);
106                 }
107
108                 $this->show_statistics($profile);
109
110                 $this->show_subscriptions($profile);
111
112                 common_element_end('div');
113         }
114
115         function show_profile($profile) {
116                 common_element_start('div', 'profile');
117
118                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
119                 if ($avatar) {
120                         common_element('img', array('src' => $avatar->url,
121                                                                                 'class' => 'avatar profile',
122                                                                                 'width' => AVATAR_PROFILE_SIZE,
123                                                                                 'height' => AVATAR_PROFILE_SIZE,
124                                                                                 'alt' => $profile->nickname));
125                 }
126                 if ($profile->fullname) {
127                         common_element_start('div', 'fullname');
128                         if ($profile->homepage) {
129                                 common_element('a', array('href' => $profile->homepage),
130                                                            $profile->fullname);
131                         } else {
132                                 common_text($profile->fullname);
133                         }
134                         common_element_end('div');
135                 }
136                 if ($profile->location) {
137                         common_element('div', 'location', $profile->location);
138                 }
139                 if ($profile->bio) {
140                         common_element('div', 'bio', $profile->bio);
141                 }
142                 common_element_end('div');
143         }
144
145         function show_subscribe_form($profile) {
146                 common_element_start('form', array('id' => 'subscribe', 'method' => 'POST',
147                                                                                    'action' => common_local_url('subscribe')));
148                 common_element('input', array('id' => 'subscribeto',
149                                                                           'name' => 'subscribeto',
150                                                                           'type' => 'hidden',
151                                                                           'value' => $profile->nickname));
152                 common_element('input', array('type' => 'submit',
153                                                                           'class' => 'button',
154                                                                           'value' => _t('Subscribe')));
155                 common_element_end('form');
156         }
157
158         function show_remote_subscribe_form($profile) {
159                 common_element_start('form', array('id' => 'remotesubscribe',
160                                                                                    'method' => 'POST',
161                                                                                    'action' => common_local_url('remotesubscribe')));
162                 common_hidden('nickname', $profile->nickname);
163                 common_element('input', array('name' => 'profile',
164                                                                           'type' => 'text',
165                                                                           'id' => 'profile',
166                                                                           'size' => '15'));
167                 common_element('input', array('type' => 'submit',
168                                                                           'id' => 'submit',
169                                                                           'name' => 'submit',
170                                                                           'value' => _t('Subscribe'),
171                                                                           'class' => 'button'));
172                 common_element_end('form');
173         }
174
175         function show_unsubscribe_form($profile) {
176                 common_element_start('form', array('id' => 'unsubscribe', 'method' => 'POST',
177                                                                                    'action' => common_local_url('unsubscribe')));
178                 common_element('input', array('id' => 'unsubscribeto',
179                                                                           'name' => 'unsubscribeto',
180                                                                           'type' => 'hidden',
181                                                                           'value' => $profile->nickname));
182                 common_element('input', array('type' => 'submit',
183                                                                           'class' => 'button',
184                                                                           'value' => _t('Unsubscribe')));
185                 common_element_end('form');
186         }
187
188         function show_subscriptions($profile) {
189                 global $config;
190
191                 # XXX: add a limit
192                 $subs = DB_DataObject::factory('subscription');
193                 $subs->subscriber = $profile->id;
194
195                 # We ask for an extra one to know if we need to do another page
196
197                 $subs->limit(0, SUBSCRIPTIONS);
198
199                 $subs_count = $subs->find();
200
201                 common_element_start('div', 'subscriptions');
202
203                 common_element('h2', 'subscriptions', _t('Subscriptions'));
204
205                 $idx = 0;
206
207                 while ($subs->fetch()) {
208                         $idx++;
209                         if ($idx % SUBSCRIPTIONS_PER_ROW == 1) {
210                                 common_element_start('div', 'row');
211                         }
212
213                         $other = Profile::staticGet($subs->subscribed);
214
215                         common_element_start('a', array('title' => ($other->fullname) ?
216                                                                                         $other->fullname :
217                                                                                         $other->nickname,
218                                                                                         'href' => $other->profileurl,
219                                                                                         'class' => 'subscription'));
220                         $avatar = $other->getAvatar(AVATAR_MINI_SIZE);
221                         common_element('img', array('src' => (($avatar) ? $avatar->url :  common_default_avatar(AVATAR_MINI_SIZE)),
222                                                                                 'width' => AVATAR_MINI_SIZE,
223                                                                                 'height' => AVATAR_MINI_SIZE,
224                                                                                 'class' => 'avatar mini',
225                                                                                 'alt' =>  ($other->fullname) ?
226                                                                                 $other->fullname :
227                                                                                 $other->nickname));
228                         common_element_end('a');
229
230                         if ($idx % SUBSCRIPTIONS_PER_ROW == 0) {
231                                 common_element_end('div');
232                         }
233
234                         if ($idx == SUBSCRIPTIONS) {
235                                 break;
236                         }
237                 }
238
239                 # close any unclosed row
240                 if ($idx % SUBSCRIPTIONS_PER_ROW != 0) {
241                         common_element_end('div');
242                 }
243
244                 common_element('a', array('href' => common_local_url('subscriptions',
245                                                                                                                          array('nickname' => $profile->nickname)),
246                                                                   'class' => 'moresubscriptions'),
247                                            _t('All subscriptions'));
248
249                 common_element_end('div');
250         }
251
252         function show_statistics($profile) {
253
254                 // XXX: WORM cache this
255                 $subs = DB_DataObject::factory('subscription');
256                 $subs->subscriber = $profile->id;
257                 $subs_count = (int) $subs->count();
258
259                 $subbed = DB_DataObject::factory('subscription');
260                 $subbed->subscribed = $profile->id;
261                 $subbed_count = (int) $subbed->count();
262
263                 $notices = DB_DataObject::factory('notice');
264                 $notices->profile_id = $profile->id;
265                 $notice_count = (int) $notices->count();
266
267                 common_element_start('div', 'statistics');
268                 common_element('h2', 'statistics', _t('Statistics'));
269
270                 # Other stats...?
271                 common_element_start('dl', 'statistics');
272                 common_element('dt', 'subscriptions', _t('Subscriptions'));
273                 common_element('dd', 'subscriptions', $subs_count);
274                 common_element('dt', 'subscribers', _t('Subscribers'));
275                 common_element('dd', 'subscribers', $subbed_count);
276                 common_element('dt', 'notices', _t('Notices'));
277                 common_element('dd', 'notices', $notice_count);
278                 common_element_end('dl');
279
280                 common_element_end('div');
281         }
282
283         function show_notices($profile) {
284
285                 $notice = DB_DataObject::factory('notice');
286                 $notice->profile_id = $profile->id;
287
288                 $notice->orderBy('created DESC');
289
290                 $page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
291
292                 $notice->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
293
294                 $cnt = $notice->find();
295
296                 common_element_start('div', 'notices width66 floatLeft');
297
298                 for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
299                         if ($notice->fetch()) {
300                                 $this->show_notice($notice);
301                         } else {
302                                 // shouldn't happen!
303                                 break;
304                         }
305                 }
306
307                 if ($page > 1) {
308                         common_element_start('span', 'floatLeft width25');
309                         common_element('a', array('href' => common_local_url('showstream',
310                                                                                                                                  array('nickname' => $profile->nickname,
311                                                                                                                                            'page' => $page-1)),
312                                                                           'class' => 'newer'),
313                                                    _t('Newer'));
314                         common_element_end('span');
315                 }
316
317                 if ($cnt > NOTICES_PER_PAGE) {
318                         common_element_start('span', 'floatRight width25');
319                         common_element('a', array('href' => common_local_url('showstream',
320                                                                                                                                  array('nickname' => $profile->nickname,
321                                                                                                                                            'page' => $page+1)),
322                                                                           'class' => 'older'),
323                                                    _t('Older'));
324                         common_element_end('span');
325                 }
326
327                 # XXX: show a link for the next page
328                 common_element_end('div');
329         }
330
331         function show_last_notice($profile) {
332
333                 common_element_start('div', 'lastnotice');
334                 common_element('h2', 'lastnotice', _t('Currently'));
335
336                 $notice = DB_DataObject::factory('notice');
337                 $notice->profile_id = $profile->id;
338                 $notice->orderBy('created DESC');
339                 $notice->limit(0, 1);
340
341                 if ($notice->find(true)) {
342                         # FIXME: URL, image, video, audio
343                         common_element_start('span', array('class' => 'content'));
344                         common_raw(common_render_content($notice->content, $notice));
345                         common_element_end('span');
346                 }
347
348                 common_element_end('div');
349         }
350
351         function show_notice($notice) {
352                 $profile = $notice->getProfile();
353                 # XXX: RDFa
354                 common_element_start('div', array('class' => 'notice',
355                                                                                   'id' => 'notice-' . $notice->id));
356                 $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
357                 # FIXME: URL, image, video, audio
358                 common_element_start('span', array('class' => 'content'));
359                 common_raw(common_render_content($notice->content, $notice));
360                 common_element_end('span');
361                 common_element('a', array('class' => 'notice',
362                                                                   'href' => $noticeurl),
363                                            common_date_string($notice->created));
364                 common_element_end('div');
365         }
366 }