]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showstream.php
e411f77246a83eb437b5ecf53c247eed82ed020a
[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, 
56                                                    array($this, 'show_header'), $user,
57                                                    array($this, 'show_top'));
58
59                 $this->show_profile($profile);
60
61                 $this->show_notices($profile);
62
63                 common_show_footer();
64         }
65
66         function show_top($user) {
67                 
68                 $cur = common_current_user();
69                 
70                 if ($cur && $cur->id == $user->id) {
71                         common_notice_form();
72                 }
73                 
74                 $this->views_menu();
75         }
76         
77         function show_header($user) {
78                 common_element('link', array('rel' => 'alternate',
79                                                                          'href' => common_local_url('userrss', array('nickname' =>
80                                                                                                                                                            $user->nickname)),
81                                                                          'type' => 'application/rss+xml',
82                                                                          'title' => _t('Notice feed for ') . $user->nickname));
83                 common_element('link', array('rel' => 'meta',
84                                                                          'href' => common_local_url('foaf', array('nickname' =>
85                                                                                                                                                           $user->nickname)),
86                                                                          'type' => 'application/rdf+xml',
87                                                                          'title' => 'FOAF'));
88                 # for remote subscriptions etc.
89                 common_element('meta', array('http-equiv' => 'X-XRDS-Location',
90                                                                          'content' => common_local_url('xrds', array('nickname' =>
91                                                                                                                                                            $user->nickname))));
92         }
93
94         function no_such_user() {
95                 common_user_error('No such user');
96         }
97
98         function show_profile($profile) {
99
100                 common_element_start('div', array('id' => 'profile'));
101
102                 $this->show_personal($profile);
103
104                 $this->show_last_notice($profile);
105
106                 $cur = common_current_user();
107
108                 $this->show_subscriptions($profile);
109
110                 common_element_end('div');
111         }
112
113         function show_personal($profile) {
114
115                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
116                 if ($avatar) {
117                         common_element_start('div', array('id' => 'profile_avatar'));
118                         common_element('img', array('src' => $avatar->url,
119                                                                                 'class' => 'avatar profile',
120                                                                                 'width' => AVATAR_PROFILE_SIZE,
121                                                                                 'height' => AVATAR_PROFILE_SIZE,
122                                                                                 'alt' => $profile->nickname));
123                         $cur = common_current_user();
124                         if ($cur) {
125                                 if ($cur->id != $profile->id) {
126                                         if ($cur->isSubscribed($profile)) {
127                                                 $this->show_unsubscribe_form($profile);
128                                         } else {
129                                                 $this->show_subscribe_form($profile);
130                                         }
131                                 }
132                         } else {
133                                 $this->show_remote_subscribe_form($profile);
134                         }
135                         common_element_end('div');
136                 }
137                 common_element_start('div', array('id' => 'profile_information'));
138                 
139                 if ($profile->fullname) {
140                         common_element('h1', NULL, $profile->fullname);
141                 }
142                 if ($profile->location) {
143                         common_element('p', 'location', $profile->location);
144                 }
145                 if ($profile->bio) {
146                         common_element('p', 'description', htmlspecialchars($profile->bio));
147                 }
148                 if ($profile->homepage) {
149                         common_element('p', 'website', $profile->homepage);
150                 }
151                 
152                 $this->show_statistics($profile);
153
154                 common_element_end('div');
155         }
156
157         function show_subscribe_form($profile) {
158                 common_element_start('form', array('id' => 'subscribe', 'method' => 'POST',
159                                                                                    'action' => common_local_url('subscribe')));
160                 common_element('input', array('id' => 'subscribeto',
161                                                                           'name' => 'subscribeto',
162                                                                           'type' => 'hidden',
163                                                                           'value' => $profile->nickname));
164                 common_element('input', array('type' => 'submit',
165                                                                           'class' => 'button',
166                                                                           'value' => _t('Subscribe')));
167                 common_element_end('form');
168         }
169
170         function show_remote_subscribe_form($profile) {
171                 common_element_start('form', array('id' => 'remotesubscribe',
172                                                                                    'method' => 'POST',
173                                                                                    'action' => common_local_url('remotesubscribe')));
174                 common_hidden('nickname', $profile->nickname);
175                 common_element('input', array('name' => 'profile',
176                                                                           'type' => 'text',
177                                                                           'id' => 'profile',
178                                                                           'size' => '15'));
179                 common_element('input', array('type' => 'submit',
180                                                                           'id' => 'submit',
181                                                                           'name' => 'submit',
182                                                                           'value' => _t('Subscribe'),
183                                                                           'class' => 'button'));
184                 common_element_end('form');
185         }
186
187         function show_unsubscribe_form($profile) {
188                 common_element_start('form', array('id' => 'unsubscribe', 'method' => 'POST',
189                                                                                    'action' => common_local_url('unsubscribe')));
190                 common_element('input', array('id' => 'unsubscribeto',
191                                                                           'name' => 'unsubscribeto',
192                                                                           'type' => 'hidden',
193                                                                           'value' => $profile->nickname));
194                 common_element('input', array('type' => 'submit',
195                                                                           'class' => 'button',
196                                                                           'value' => _t('Unsubscribe')));
197                 common_element_end('form');
198         }
199
200         function show_subscriptions($profile) {
201                 global $config;
202
203                 # XXX: add a limit
204                 $subs = DB_DataObject::factory('subscription');
205                 $subs->subscriber = $profile->id;
206
207                 # We ask for an extra one to know if we need to do another page
208
209                 $subs->limit(0, SUBSCRIPTIONS);
210
211                 $subs_count = $subs->find();
212
213                 common_element_start('div', array('id' => 'subscriptions'));
214
215                 common_element('h2', NULL, _t('Subscriptions'));
216
217                 if ($subs_count > 0) {
218                         
219                         common_element_start('ul', array('id' => 'subscriptions_avatars'));
220                         
221                         while ($subs->fetch()) {
222
223                                 $other = Profile::staticGet($subs->subscribed);
224
225                                 common_element_start('li');
226                                 common_element_start('a', array('title' => ($other->fullname) ?
227                                                                                                 $other->fullname :
228                                                                                                 $other->nickname,
229                                                                                                 'href' => $other->profileurl,
230                                                                                                 'class' => 'subscription'));
231                                 $avatar = $other->getAvatar(AVATAR_MINI_SIZE);
232                                 common_element('img', array('src' => (($avatar) ? $avatar->url :  common_default_avatar(AVATAR_MINI_SIZE)),
233                                                                                         'width' => AVATAR_MINI_SIZE,
234                                                                                         'height' => AVATAR_MINI_SIZE,
235                                                                                         'class' => 'avatar mini',
236                                                                                         'alt' =>  ($other->fullname) ?
237                                                                                         $other->fullname :
238                                                                                         $other->nickname));
239                                 common_element_end('a');
240                                 common_element_end('li');
241                         }
242                         common_element_end('ul');
243                 }
244
245                 common_element_start('p', array('id' => 'subscriptions_viewall'));
246                 
247                 common_element('a', array('href' => common_local_url('subscriptions',
248                                                                                                                          array('nickname' => $profile->nickname)),
249                                                                   'class' => 'moresubscriptions'),
250                                            _t('All subscriptions'));
251
252                 common_element_end('p');
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                 if ($cnt > 0) {
302                         common_element_start('ul', array('id' => 'notices'));
303                         
304                         for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
305                                 if ($notice->fetch()) {
306                                         $this->show_notice($notice);
307                                 } else {
308                                         // shouldn't happen!
309                                         break;
310                                 }
311                         }
312                         
313                         common_element_end('ul');
314                 }
315                 common_pagination($page>1, $cnt>NOTICES_PER_PAGE, $page,
316                                                   'showstream', array('nickname' => $profile->nickname));
317         }
318
319         function show_last_notice($profile) {
320
321                 common_element('h2', NULL, _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('p', array('class' => 'notice_current'));
331                         common_raw(common_render_content($notice->content, $notice));
332                         common_element_end('p');
333                 }
334         }
335
336         function show_notice($notice) {
337                 $profile = $notice->getProfile();
338                 # XXX: RDFa
339                 common_element_start('li', array('class' => 'notice_single',
340                                                                                  'id' => 'notice-' . $notice->id));
341                 $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
342                 # FIXME: URL, image, video, audio
343                 common_element_start('p');
344                 common_raw(common_render_content($notice->content, $notice));
345                 common_element_end('p');
346                 common_element_start('p', array('class' => 'time'));
347                 common_element('a', array('class' => 'notice',
348                                                                   'href' => $noticeurl),
349                                            common_date_string($notice->created));
350                 common_element_end('p');
351                 common_element_end('li');
352         }
353 }