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