]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showstream.php
close parens in showstream
[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                 common_element('link', array('rel' => 'meta',
77                                                                          'href' => common_local_url('foaf', array('nickname' =>
78                                                                                                                                                           $user->nickname)),
79                                                                          'type' => 'application/rdf+xml',
80                                                                          'title' => 'FOAF'));
81                 # for remote subscriptions etc.
82                 common_element('meta', array('http-equiv' => 'X-XRDS-Location',
83                                                                          'content' => common_local_url('xrds', array('nickname' =>
84                                                                                                                                                            $user->nickname))));
85         }
86
87         function no_such_user() {
88                 common_user_error('No such user');
89         }
90
91         function show_sidebar($profile) {
92
93                 common_element_start('div', 'sidebar width33 floatRight greenBg');
94
95                 $this->show_profile($profile);
96
97                 $this->show_last_notice($profile);
98
99                 $cur = common_current_user();
100
101                 if ($cur) {
102                         if ($cur->id != $profile->id) {
103                                 if ($cur->isSubscribed($profile)) {
104                                         $this->show_unsubscribe_form($profile);
105                                 } else {
106                                         $this->show_subscribe_form($profile);
107                                 }
108                         }
109                 } else {
110                         $this->show_remote_subscribe_form($profile);
111                 }
112
113                 $this->show_statistics($profile);
114
115                 $this->show_subscriptions($profile);
116
117                 common_element_end('div');
118         }
119
120         function show_profile($profile) {
121                 common_element_start('div', 'profile');
122
123                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
124                 if ($avatar) {
125                         common_element('img', array('src' => $avatar->url,
126                                                                                 'class' => 'avatar profile',
127                                                                                 'width' => AVATAR_PROFILE_SIZE,
128                                                                                 'height' => AVATAR_PROFILE_SIZE,
129                                                                                 'alt' => $profile->nickname));
130                 }
131                 if ($profile->fullname) {
132                         common_element_start('div', 'fullname');
133                         if ($profile->homepage) {
134                                 common_element('a', array('href' => $profile->homepage),
135                                                            $profile->fullname);
136                         } else {
137                                 common_text($profile->fullname);
138                         }
139                         common_element_end('div');
140                 }
141                 if ($profile->location) {
142                         common_element('div', 'location', $profile->location);
143                 }
144                 if ($profile->bio) {
145                         common_element('div', 'bio', $profile->bio);
146                 }
147                 common_element_end('div');
148         }
149
150         function show_subscribe_form($profile) {
151                 common_element_start('form', array('id' => 'subscribe', 'method' => 'POST',
152                                                                                    'action' => common_local_url('subscribe')));
153                 common_element('input', array('id' => 'subscribeto',
154                                                                           'name' => 'subscribeto',
155                                                                           'type' => 'hidden',
156                                                                           'value' => $profile->nickname));
157                 common_element('input', array('type' => 'submit',
158                                                                           'class' => 'button',
159                                                                           'value' => _t('Subscribe')));
160                 common_element_end('form');
161         }
162
163         function show_remote_subscribe_form($profile) {
164                 common_element_start('form', array('id' => 'remotesubscribe',
165                                                                                    'method' => 'POST',
166                                                                                    'action' => common_local_url('remotesubscribe')));
167                 common_hidden('nickname', $profile->nickname);
168                 common_element('input', array('name' => 'profile',
169                                                                           'type' => 'text',
170                                                                           'id' => 'profile',
171                                                                           'size' => '15'));
172                 common_element('input', array('type' => 'submit',
173                                                                           'id' => 'submit',
174                                                                           'name' => 'submit',
175                                                                           'value' => _t('Subscribe'),
176                                                                           'class' => 'button'));
177                 common_element_end('form');
178         }
179
180         function show_unsubscribe_form($profile) {
181                 common_element_start('form', array('id' => 'unsubscribe', 'method' => 'POST',
182                                                                                    'action' => common_local_url('unsubscribe')));
183                 common_element('input', array('id' => 'unsubscribeto',
184                                                                           'name' => 'unsubscribeto',
185                                                                           'type' => 'hidden',
186                                                                           'value' => $profile->nickname));
187                 common_element('input', array('type' => 'submit',
188                                                                           'class' => 'button',
189                                                                           'value' => _t('Unsubscribe')));
190                 common_element_end('form');
191         }
192
193         function show_subscriptions($profile) {
194                 global $config;
195
196                 # XXX: add a limit
197                 $subs = DB_DataObject::factory('subscription');
198                 $subs->subscriber = $profile->id;
199
200                 # We ask for an extra one to know if we need to do another page
201
202                 $subs->limit(0, SUBSCRIPTIONS);
203
204                 $subs_count = $subs->find();
205
206                 common_element_start('div', 'subscriptions');
207
208                 common_element('h2', 'subscriptions', _t('Subscriptions'));
209
210                 $idx = 0;
211
212                 while ($subs->fetch()) {
213                         $idx++;
214                         if ($idx % SUBSCRIPTIONS_PER_ROW == 1) {
215                                 common_element_start('div', 'row');
216                         }
217
218                         $other = Profile::staticGet($subs->subscribed);
219
220                         common_element_start('a', array('title' => ($other->fullname) ?
221                                                                                         $other->fullname :
222                                                                                         $other->nickname,
223                                                                                         'href' => $other->profileurl,
224                                                                                         'class' => 'subscription'));
225                         $avatar = $other->getAvatar(AVATAR_MINI_SIZE);
226                         common_element('img', array('src' => (($avatar) ? $avatar->url :  common_default_avatar(AVATAR_MINI_SIZE)),
227                                                                                 'width' => AVATAR_MINI_SIZE,
228                                                                                 'height' => AVATAR_MINI_SIZE,
229                                                                                 'class' => 'avatar mini',
230                                                                                 'alt' =>  ($other->fullname) ?
231                                                                                 $other->fullname :
232                                                                                 $other->nickname));
233                         common_element_end('a');
234
235                         if ($idx % SUBSCRIPTIONS_PER_ROW == 0) {
236                                 common_element_end('div');
237                         }
238
239                         if ($idx == SUBSCRIPTIONS) {
240                                 break;
241                         }
242                 }
243
244                 # close any unclosed row
245                 if ($idx % SUBSCRIPTIONS_PER_ROW != 0) {
246                         common_element_end('div');
247                 }
248
249                 common_element('a', array('href' => common_local_url('subscriptions',
250                                                                                                                          array('nickname' => $profile->nickname)),
251                                                                   'class' => 'moresubscriptions'),
252                                            _t('All subscriptions'));
253
254                 common_element_end('div');
255         }
256
257         function show_statistics($profile) {
258
259                 // XXX: WORM cache this
260                 $subs = DB_DataObject::factory('subscription');
261                 $subs->subscriber = $profile->id;
262                 $subs_count = (int) $subs->count();
263
264                 $subbed = DB_DataObject::factory('subscription');
265                 $subbed->subscribed = $profile->id;
266                 $subbed_count = (int) $subbed->count();
267
268                 $notices = DB_DataObject::factory('notice');
269                 $notices->profile_id = $profile->id;
270                 $notice_count = (int) $notices->count();
271
272                 common_element_start('div', 'statistics');
273                 common_element('h2', 'statistics', _t('Statistics'));
274
275                 # Other stats...?
276                 common_element_start('dl', 'statistics');
277                 common_element('dt', 'subscriptions', _t('Subscriptions'));
278                 common_element('dd', 'subscriptions', $subs_count);
279                 common_element('dt', 'subscribers', _t('Subscribers'));
280                 common_element('dd', 'subscribers', $subbed_count);
281                 common_element('dt', 'notices', _t('Notices'));
282                 common_element('dd', 'notices', $notice_count);
283                 common_element_end('dl');
284
285                 common_element_end('div');
286         }
287
288         function show_notices($profile) {
289
290                 $notice = DB_DataObject::factory('notice');
291                 $notice->profile_id = $profile->id;
292
293                 $notice->orderBy('created DESC');
294
295                 $page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
296
297                 $notice->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
298
299                 $cnt = $notice->find();
300
301                 common_element_start('div', 'notices width66 floatLeft');
302
303                 for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
304                         if ($notice->fetch()) {
305                                 $this->show_notice($notice);
306                         } else {
307                                 // shouldn't happen!
308                                 break;
309                         }
310                 }
311
312                 if ($page > 1) {
313                         common_element_start('span', 'floatLeft width25');
314                         common_element('a', array('href' => common_local_url('showstream',
315                                                                                                                                  array('nickname' => $profile->nickname,
316                                                                                                                                            'page' => $page-1)),
317                                                                           'class' => 'newer'),
318                                                    _t('Newer'));
319                         common_element_end('span');
320                 }
321
322                 if ($cnt > NOTICES_PER_PAGE) {
323                         common_element_start('span', 'floatRight width25');
324                         common_element('a', array('href' => common_local_url('showstream',
325                                                                                                                                  array('nickname' => $profile->nickname,
326                                                                                                                                            'page' => $page+1)),
327                                                                           'class' => 'older'),
328                                                    _t('Older'));
329                         common_element_end('span');
330                 }
331
332                 # XXX: show a link for the next page
333                 common_element_end('div');
334         }
335
336         function show_last_notice($profile) {
337
338                 common_element_start('div', 'lastnotice');
339                 common_element('h2', 'lastnotice', _t('Currently'));
340
341                 $notice = DB_DataObject::factory('notice');
342                 $notice->profile_id = $profile->id;
343                 $notice->orderBy('created DESC');
344                 $notice->limit(0, 1);
345
346                 if ($notice->find(true)) {
347                         # FIXME: URL, image, video, audio
348                         common_element_start('span', array('class' => 'content'));
349                         common_raw(common_render_content($notice->content, $notice));
350                         common_element_end('span');
351                 }
352
353                 common_element_end('div');
354         }
355
356         function show_notice($notice) {
357                 $profile = $notice->getProfile();
358                 # XXX: RDFa
359                 common_element_start('div', array('class' => 'notice',
360                                                                                   'id' => 'notice-' . $notice->id));
361                 $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
362                 # FIXME: URL, image, video, audio
363                 common_element_start('span', array('class' => 'content'));
364                 common_raw(common_render_content($notice->content, $notice));
365                 common_element_end('span');
366                 common_element('a', array('class' => 'notice',
367                                                                   'href' => $noticeurl),
368                                            common_date_string($notice->created));
369                 common_element_end('div');
370         }
371 }