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