]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showstream.php
uiredesign + phpdocs
[quix0rs-gnu-social.git] / actions / showstream.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * User profile page
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Personal
23  * @package   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2008-2009 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/personalgroupnav.php';
35 require_once INSTALLDIR.'/lib/noticelist.php';
36 require_once INSTALLDIR.'/lib/feedlist.php';
37
38 /**
39  * User profile page
40  *
41  * When I created this page, "show stream" seemed like the best name for it.
42  * Now, it seems like a really bad name.
43  *
44  * It shows a stream of the user's posts, plus lots of profile info, links
45  * to subscriptions and stuff, etc.
46  *
47  * @category Personal
48  * @package  Laconica
49  * @author   Evan Prodromou <evan@controlyourself.ca>
50  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
51  * @link     http://laconi.ca/
52  */
53
54 class ShowstreamAction extends Action
55 {
56     var $user = null;
57     var $page = null;
58     var $profile = null;
59
60     function title()
61     {
62         if ($this->page == 1) {
63             return $this->user->nickname;
64         } else {
65             return sprintf(_("%s, page %d"),
66                            $this->user->nickname,
67                            $this->page);
68         }
69     }
70
71     function prepare($args)
72     {
73         parent::prepare($args);
74
75         $nickname_arg = $this->arg('nickname');
76         $nickname = common_canonical_nickname($nickname_arg);
77
78         // Permanent redirect on non-canonical nickname
79
80         if ($nickname_arg != $nickname) {
81             $args = array('nickname' => $nickname);
82             if ($this->arg('page') && $this->arg('page') != 1) {
83                 $args['page'] = $this->arg['page'];
84             }
85             common_redirect(common_local_url('showstream', $args), 301);
86             return false;
87         }
88
89         $this->user = User::staticGet('nickname', $nickname);
90
91         if (!$this->user) {
92             $this->clientError(_('No such user.'), 404);
93             return false;
94         }
95
96         $this->profile = $this->user->getProfile();
97
98         if (!$this->profile) {
99             $this->serverError(_('User has no profile.'));
100             return false;
101         }
102
103         $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
104
105         return true;
106     }
107
108     function handle($args)
109     {
110
111         // Looks like we're good; start output
112
113         // For YADIS discovery, we also have a <meta> tag
114
115         header('X-XRDS-Location: '. common_local_url('xrds', array('nickname' =>
116                                                                    $this->user->nickname)));
117
118         $this->showPage();
119     }
120
121     function showContent()
122     {
123         $this->showProfile();
124         $this->showNotices();
125     }
126
127     function showLocalNav()
128     {
129         $nav = new PersonalGroupNav($this);
130         $nav->show();
131     }
132
133     function showPageTitle()
134     {
135          $this->element('h1', NULL, $this->profile->nickname._("'s profile"));
136     }
137
138     function showPageNoticeBlock()
139     {
140         return;
141     }
142
143     function showExportData()
144     {
145         $fl = new FeedList($this);
146         $fl->show(array(0=>array('href'=>common_local_url('userrss',
147                                                           array('nickname' => $this->user->nickname)),
148                                  'type' => 'rss',
149                                  'version' => 'RSS 1.0',
150                                  'item' => 'notices'),
151                         1=>array('href'=>common_local_url('usertimeline',
152                                                           array('nickname' => $this->user->nickname)),
153                                  'type' => 'atom',
154                                  'version' => 'Atom 1.0',
155                                  'item' => 'usertimeline'),
156                         2=>array('href'=>common_local_url('foaf',
157                                                           array('nickname' => $this->user->nickname)),
158                                  'type' => 'rdf',
159                                  'version' => 'FOAF',
160                                  'item' => 'foaf')));
161     }
162
163     function showFeeds()
164     {
165         // Feeds
166         $this->element('link', array('rel' => 'alternate',
167                                      'href' => common_local_url('api',
168                                                                 array('apiaction' => 'statuses',
169                                                                       'method' => 'user_timeline.rss',
170                                                                       'argument' => $this->user->nickname)),
171                                      'type' => 'application/rss+xml',
172                                      'title' => sprintf(_('Notice feed for %s'), $this->user->nickname)));
173         $this->element('link', array('rel' => 'alternate feed',
174                                      'href' => common_local_url('api',
175                                                                 array('apiaction' => 'statuses',
176                                                                       'method' => 'user_timeline.atom',
177                                                                       'argument' => $this->user->nickname)),
178                                      'type' => 'application/atom+xml',
179                                      'title' => sprintf(_('Notice feed for %s'), $this->user->nickname)));
180         $this->element('link', array('rel' => 'alternate',
181                                      'href' => common_local_url('userrss', array('nickname' =>
182                                                                                $this->user->nickname)),
183                                      'type' => 'application/rdf+xml',
184                                      'title' => sprintf(_('Notice feed for %s'), $this->user->nickname)));
185     }
186
187     function extraHead()
188     {
189         // FOAF
190         $this->element('link', array('rel' => 'meta',
191                                      'href' => common_local_url('foaf', array('nickname' =>
192                                                                               $this->user->nickname)),
193                                      'type' => 'application/rdf+xml',
194                                      'title' => 'FOAF'));
195         // for remote subscriptions etc.
196         $this->element('meta', array('http-equiv' => 'X-XRDS-Location',
197                                      'content' => common_local_url('xrds', array('nickname' =>
198                                                                                $this->user->nickname))));
199
200         if ($this->profile->bio) {
201             $this->element('meta', array('name' => 'description',
202                                          'content' => $this->profile->bio));
203         }
204
205         if ($this->user->emailmicroid && $this->user->email && $this->profile->profileurl) {
206             $id = new Microid('mailto:'.$this->user->email,
207                               $this->selfUrl());
208             $this->element('meta', array('name' => 'microid',
209                                          'content' => $id->toString()));
210         }
211         if ($this->user->jabbermicroid && $this->user->jabber && $this->profile->profileurl) {
212             $id = new Microid('xmpp:'.$this->user->jabber,
213                               $this->selfUrl());
214             $this->element('meta', array('name' => 'microid',
215                                          'content' => $id->toString()));
216         }
217
218         // See https://wiki.mozilla.org/Microsummaries
219
220         $this->element('link', array('rel' => 'microsummary',
221                                      'href' => common_local_url('microsummary',
222                                                                 array('nickname' => $this->profile->nickname))));
223     }
224
225     function showProfile()
226     {
227         $this->elementStart('div', array('id' => 'user_profile', 'class' => 'vcard author'));
228         $this->element('h2', null, _('User profile'));
229
230         $avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
231         $this->elementStart('dl', 'user_depiction');
232         $this->element('dt', null, _('Photo'));
233         $this->elementStart('dd');
234         $this->element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_PROFILE_SIZE),
235                                     'class' => 'photo avatar',
236                                     'width' => AVATAR_PROFILE_SIZE,
237                                     'height' => AVATAR_PROFILE_SIZE,
238                                     'alt' => $this->profile->nickname));
239         $this->elementEnd('dd');
240         $this->elementEnd('dl');
241
242         $this->elementStart('dl', 'user_nickname');
243         $this->element('dt', null, _('Nickname'));
244         $this->elementStart('dd');
245             $this->element('a', array('href' => $this->profile->profileurl,
246                                       'rel' => 'me', 'class' => 'nickname url uid'),
247                            $this->profile->nickname);
248         $this->elementEnd('dd');
249         $this->elementEnd('dl');
250
251         if ($this->profile->fullname) {
252             $this->elementStart('dl', 'user_fn');
253             $this->element('dt', null, _('Full name'));
254             $this->elementStart('dd');
255             $this->element('span', 'fn', $this->profile->fullname);
256             $this->elementEnd('dd');
257             $this->elementEnd('dl');
258         }
259
260         if ($this->profile->location) {
261             $this->elementStart('dl', 'user_location');
262             $this->element('dt', null, _('Location'));
263             $this->element('dd', 'location', $this->profile->location);
264             $this->elementEnd('dl');
265         }
266
267         if ($this->profile->homepage) {
268             $this->elementStart('dl', 'user_url');
269             $this->element('dt', null, _('URL'));
270             $this->elementStart('dd');
271             $this->element('a', array('href' => $this->profile->homepage,
272                                       'rel' => 'me', 'class' => 'url'),
273                            $this->profile->homepage);
274             $this->elementEnd('dd');
275             $this->elementEnd('dl');
276         }
277
278         if ($this->profile->bio) {
279             $this->elementStart('dl', 'user_note');
280             $this->element('dt', null, _('Note'));
281             $this->element('dd', 'note', $this->profile->bio);
282             $this->elementEnd('dl');
283         }
284
285         $tags = Profile_tag::getTags($this->profile->id, $this->profile->id);
286         if (count($tags) > 0) {
287             $this->elementStart('dl', 'user_tags');
288             $this->element('dt', null, _('Tags'));
289             $this->elementStart('dd', 'tags');
290             $this->elementStart('ul', 'tags xoxo');
291             foreach ($tags as $tag) {
292                 $this->elementStart('li');
293                 $this->element('span', 'mark_hash', '#');
294                 $this->element('a', array('rel' => 'tag',
295                                           'href' => common_local_url('peopletag',
296                                                                      array('tag' => $tag))),
297                                $tag);
298                 $this->elementEnd('li');
299             }
300             $this->elementEnd('ul');
301             $this->elementEnd('dd');
302             $this->elementEnd('dl');
303         }
304         $this->elementEnd('div');
305
306
307         $this->elementStart('div', array('id' => 'user_actions'));
308         $this->element('h2', null, _('User actions'));
309         $this->elementStart('ul');
310         $this->elementStart('li', array('id' => 'user_subscribe'));
311         $cur = common_current_user();
312         if ($cur) {
313             if ($cur->id != $this->profile->id) {
314                 if ($cur->isSubscribed($this->profile)) {
315                     $sf = new SubscribeForm($this, $this->profile);
316                     $sf->show();
317                 } else {
318                     $usf = new UnsubscribeForm($this, $this->profile);
319                     $usf->show();
320                 }
321             }
322         } else {
323             $this->showRemoteSubscribeLink();
324         }
325         $this->elementEnd('li');
326
327         common_profile_new_message_nudge($cur, $this->user, $this->profile);
328
329         if ($cur && $cur->id != $this->profile->id) {
330             $blocked = $cur->hasBlocked($this->profile);
331             $this->elementStart('li', array('id' => 'user_block'));
332             if ($blocked) {
333                 $bf = new BlockForm($this, $this->profile);
334                 $bf->show();
335             } else {
336                 $ubf = new UnblockForm($this, $this->profile);
337                 $ubf->show();
338             }
339             $this->elementEnd('li');
340         }
341         $this->elementEnd('ul');
342         $this->elementEnd('div');
343     }
344
345     function showRemoteSubscribeLink()
346     {
347         $url = common_local_url('remotesubscribe',
348                                 array('nickname' => $this->profile->nickname));
349         $this->element('a', array('href' => $url,
350                                   'id' => 'remotesubscribe'),
351                        _('Subscribe'));
352     }
353
354     function showNotices()
355     {
356         $notice = $this->user->getNotices(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
357
358         $pnl = new ProfileNoticeList($notice, $this);
359         $cnt = $pnl->show();
360
361         $this->pagination($this->page>1, $cnt>NOTICES_PER_PAGE, $this->page,
362                           'showstream', array('nickname' => $this->user->nickname));
363     }
364
365     function showSections()
366     {
367         $this->showStatistics();
368         $this->showSubscriptions();
369     }
370
371     function showSubscriptions()
372     {
373         $subs = new Subscription();
374         $subs->subscriber = $this->profile->id;
375         $subs->whereAdd('subscribed != ' . $this->profile->id);
376
377         $subs->orderBy('created DESC');
378
379         // We ask for an extra one to know if we need to do another page
380
381         $subs->limit(0, SUBSCRIPTIONS + 1);
382
383         $subs_count = $subs->find();
384
385         $this->elementStart('div', array('id' => 'user_subscriptions',
386                                          'class' => 'section'));
387
388         $this->element('h2', null, _('Subscriptions'));
389
390         if ($subs_count > 0) {
391
392             $this->elementStart('ul', 'users');
393
394             for ($i = 0; $i < min($subs_count, SUBSCRIPTIONS); $i++) {
395
396                 if (!$subs->fetch()) {
397                     common_debug('Weirdly, broke out of subscriptions loop early', __FILE__);
398                     break;
399                 }
400
401                 $other = Profile::staticGet($subs->subscribed);
402
403                 if (!$other) {
404                     common_log_db_error($subs, 'SELECT', __FILE__);
405                     continue;
406                 }
407
408                 $this->elementStart('li', 'vcard');
409                 $this->elementStart('a', array('title' => ($other->fullname) ?
410                                                 $other->fullname :
411                                                 $other->nickname,
412                                                 'href' => $other->profileurl,
413                                                 'rel' => 'contact',
414                                                  'class' => 'url'));
415                 $avatar = $other->getAvatar(AVATAR_MINI_SIZE);
416                 $this->element('img', array('src' => (($avatar) ? common_avatar_display_url($avatar) :  common_default_avatar(AVATAR_MINI_SIZE)),
417                                             'width' => AVATAR_MINI_SIZE,
418                                             'height' => AVATAR_MINI_SIZE,
419                                             'class' => 'avatar photo',
420                                             'alt' =>  ($other->fullname) ?
421                                             $other->fullname :
422                                             $other->nickname));
423                 $this->element('span', 'fn nickname', $other->nickname);
424                 $this->elementEnd('a');
425                 $this->elementEnd('li');
426             }
427
428             $this->elementEnd('ul');
429         }
430
431         if ($subs_count > SUBSCRIPTIONS) {
432             $this->elementStart('p');
433
434             $this->element('a', array('href' => common_local_url('subscriptions',
435                                                                  array('nickname' => $this->profile->nickname)),
436                                       'class' => 'mores'),
437                            _('All subscriptions'));
438             $this->elementEnd('p');
439         }
440
441         $this->elementEnd('div');
442     }
443
444     function showStatistics()
445     {
446         // XXX: WORM cache this
447         $subs = new Subscription();
448         $subs->subscriber = $this->profile->id;
449         $subs_count = (int) $subs->count() - 1;
450
451         $subbed = new Subscription();
452         $subbed->subscribed = $this->profile->id;
453         $subbed_count = (int) $subbed->count() - 1;
454
455         $notices = new Notice();
456         $notices->profile_id = $this->profile->id;
457         $notice_count = (int) $notices->count();
458
459         $this->elementStart('div', array('id' => 'user_statistics',
460                                          'class' => 'section'));
461
462         $this->element('h2', null, _('Statistics'));
463
464         // Other stats...?
465         $this->elementStart('dl', 'user_member-since');
466         $this->element('dt', null, _('Member since'));
467         $this->element('dd', null, date('j M Y',
468                                                  strtotime($this->profile->created)));
469         $this->elementEnd('dl');
470
471         $this->elementStart('dl', 'user_subscriptions');
472         $this->elementStart('dt');
473         $this->element('a', array('href' => common_local_url('subscriptions',
474                                                              array('nickname' => $this->profile->nickname))),
475                        _('Subscriptions'));
476         $this->elementEnd('dt');
477         $this->element('dd', null, (is_int($subs_count)) ? $subs_count : '0');
478         $this->elementEnd('dl');
479
480         $this->elementStart('dl', 'user_subscribers');
481         $this->elementStart('dt');
482         $this->element('a', array('href' => common_local_url('subscribers',
483                                                              array('nickname' => $this->profile->nickname))),
484                        _('Subscribers'));
485         $this->elementEnd('dt');
486         $this->element('dd', 'subscribers', (is_int($subbed_count)) ? $subbed_count : '0');
487         $this->elementEnd('dl');
488
489         $this->elementStart('dl', 'user_notices');
490         $this->element('dt', null, _('Notices'));
491         $this->element('dd', null, (is_int($notice_count)) ? $notice_count : '0');
492         $this->elementEnd('dl');
493
494         $this->elementEnd('div');
495     }
496
497 }
498
499 // We don't show the author for a profile, since we already know who it is!
500
501 class ProfileNoticeList extends NoticeList
502 {
503     function newListItem($notice)
504     {
505         return new ProfileNoticeListItem($notice, $this->out);
506     }
507 }
508
509 class ProfileNoticeListItem extends NoticeListItem
510 {
511     function showAuthor()
512     {
513         return;
514     }
515 }