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