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