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