]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelist.php
visual tweaks for convo view
[quix0rs-gnu-social.git] / lib / noticelist.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * widget for displaying a list of notices
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  UI
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/favorform.php';
36 require_once INSTALLDIR.'/lib/disfavorform.php';
37 require_once INSTALLDIR.'/lib/attachmentlist.php';
38
39 /**
40  * widget for displaying a list of notices
41  *
42  * There are a number of actions that display a list of notices, in
43  * reverse chronological order. This widget abstracts out most of the
44  * code for UI for notice lists. It's overridden to hide some
45  * data for e.g. the profile page.
46  *
47  * @category UI
48  * @package  StatusNet
49  * @author   Evan Prodromou <evan@status.net>
50  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
51  * @link     http://status.net/
52  * @see      Notice
53  * @see      NoticeListItem
54  * @see      ProfileNoticeList
55  */
56
57 class NoticeList extends Widget
58 {
59     /** the current stream of notices being displayed. */
60
61     var $notice = null;
62
63     /**
64      * constructor
65      *
66      * @param Notice $notice stream of notices from DB_DataObject
67      */
68
69     function __construct($notice, $out=null)
70     {
71         parent::__construct($out);
72         $this->notice = $notice;
73     }
74
75     /**
76      * show the list of notices
77      *
78      * "Uses up" the stream by looping through it. So, probably can't
79      * be called twice on the same list.
80      *
81      * @return int count of notices listed.
82      */
83
84     function show()
85     {
86         $this->out->elementStart('div', array('id' =>'notices_primary'));
87         $this->out->element('h2', null, _('Notices'));
88         $this->out->elementStart('ol', array('class' => 'notices xoxo'));
89
90         $cnt = 0;
91
92         while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
93             $cnt++;
94
95             if ($cnt > NOTICES_PER_PAGE) {
96                 break;
97             }
98
99             try {
100                 $item = $this->newListItem($this->notice);
101                 $item->show();
102             } catch (Exception $e) {
103                 // we log exceptions and continue
104                 common_log(LOG_ERR, $e->getMessage());
105                 continue;
106             }
107         }
108
109         $this->out->elementEnd('ol');
110         $this->out->elementEnd('div');
111
112         return $cnt;
113     }
114
115     /**
116      * returns a new list item for the current notice
117      *
118      * Recipe (factory?) method; overridden by sub-classes to give
119      * a different list item class.
120      *
121      * @param Notice $notice the current notice
122      *
123      * @return NoticeListItem a list item for displaying the notice
124      */
125
126     function newListItem($notice)
127     {
128         return new NoticeListItem($notice, $this->out);
129     }
130 }
131
132 /**
133  * widget for displaying a single notice
134  *
135  * This widget has the core smarts for showing a single notice: what to display,
136  * where, and under which circumstances. Its key method is show(); this is a recipe
137  * that calls all the other show*() methods to build up a single notice. The
138  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
139  * author info (since that's implicit by the data in the page).
140  *
141  * @category UI
142  * @package  StatusNet
143  * @author   Evan Prodromou <evan@status.net>
144  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
145  * @link     http://status.net/
146  * @see      NoticeList
147  * @see      ProfileNoticeListItem
148  */
149
150 class NoticeListItem extends Widget
151 {
152     /** The notice this item will show. */
153
154     var $notice = null;
155
156     /** The notice that was repeated. */
157
158     var $repeat = null;
159
160     /** The profile of the author of the notice, extracted once for convenience. */
161
162     var $profile = null;
163
164     /**
165      * constructor
166      *
167      * Also initializes the profile attribute.
168      *
169      * @param Notice $notice The notice we'll display
170      */
171
172     function __construct($notice, $out=null)
173     {
174         parent::__construct($out);
175         if (!empty($notice->repeat_of)) {
176             $original = Notice::staticGet('id', $notice->repeat_of);
177             if (empty($original)) { // could have been deleted
178                 $this->notice = $notice;
179             } else {
180                 $this->notice = $original;
181                 $this->repeat = $notice;
182             }
183         } else {
184             $this->notice  = $notice;
185         }
186         $this->profile = $this->notice->getProfile();
187     }
188
189     /**
190      * recipe function for displaying a single notice.
191      *
192      * This uses all the other methods to correctly display a notice. Override
193      * it or one of the others to fine-tune the output.
194      *
195      * @return void
196      */
197
198     function show()
199     {
200         if (empty($this->notice)) {
201             common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
202             return;
203         } else if (empty($this->profile)) {
204             common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
205             return;
206         }
207
208         $this->showStart();
209         if (Event::handle('StartShowNoticeItem', array($this))) {
210             $this->showNotice();
211             $this->showNoticeAttachments();
212             $this->showNoticeInfo();
213             $this->showNoticeOptions();
214             Event::handle('EndShowNoticeItem', array($this));
215         }
216         $this->showEnd();
217     }
218
219     function showNotice()
220     {
221         $this->out->elementStart('div', 'entry-title');
222         $this->showAuthor();
223         $this->showContent();
224         $this->out->elementEnd('div');
225     }
226
227     function showNoticeInfo()
228     {
229         $this->out->elementStart('div', 'entry-content');
230         if (Event::handle('StartShowNoticeInfo', array($this))) {
231             $this->showNoticeLink();
232             $this->showNoticeSource();
233             $this->showNoticeLocation();
234             $this->showContext();
235             $this->showRepeat();
236             Event::handle('EndShowNoticeInfo', array($this));
237         }
238
239         $this->out->elementEnd('div');
240     }
241
242     function showNoticeOptions()
243     {
244         if (Event::handle('StartShowNoticeOptions', array($this))) {
245             $user = common_current_user();
246             if ($user) {
247                 $this->out->elementStart('div', 'notice-options');
248                 $this->showFaveForm();
249                 $this->showReplyLink();
250                 $this->showRepeatForm();
251                 $this->showDeleteLink();
252                 $this->out->elementEnd('div');
253             }
254             Event::handle('EndShowNoticeOptions', array($this));
255         }
256     }
257
258     /**
259      * start a single notice.
260      *
261      * @return void
262      */
263
264     function showStart()
265     {
266         if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
267             $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
268             $this->out->elementStart('li', array('class' => 'hentry notice',
269                                                  'id' => 'notice-' . $id));
270             Event::handle('EndOpenNoticeListItemElement', array($this));
271         }
272     }
273
274     /**
275      * show the "favorite" form
276      *
277      * @return void
278      */
279
280     function showFaveForm()
281     {
282         if (Event::handle('StartShowFaveForm', array($this))) {
283             $user = common_current_user();
284             if ($user) {
285                 if ($user->hasFave($this->notice)) {
286                     $disfavor = new DisfavorForm($this->out, $this->notice);
287                     $disfavor->show();
288                 } else {
289                     $favor = new FavorForm($this->out, $this->notice);
290                     $favor->show();
291                 }
292             }
293             Event::handle('EndShowFaveForm', array($this));
294         }
295     }
296
297     /**
298      * show the author of a notice
299      *
300      * By default, this shows the avatar and (linked) nickname of the author.
301      *
302      * @return void
303      */
304
305     function showAuthor()
306     {
307         $this->out->elementStart('span', 'vcard author');
308         $attrs = array('href' => $this->profile->profileurl,
309                        'class' => 'url');
310         if (!empty($this->profile->fullname)) {
311             $attrs['title'] = $this->profile->getFancyName();
312         }
313         $this->out->elementStart('a', $attrs);
314         $this->showAvatar();
315         $this->out->text(' ');
316         $this->showNickname();
317         $this->out->elementEnd('a');
318         $this->out->elementEnd('span');
319     }
320
321     /**
322      * show the avatar of the notice's author
323      *
324      * This will use the default avatar if no avatar is assigned for the author.
325      * It makes a link to the author's profile.
326      *
327      * @return void
328      */
329
330     function showAvatar()
331     {
332         $avatar_size = $this->avatarSize();
333
334         $avatar = $this->profile->getAvatar($avatar_size);
335
336         $this->out->element('img', array('src' => ($avatar) ?
337                                          $avatar->displayUrl() :
338                                          Avatar::defaultImage($avatar_size),
339                                          'class' => 'avatar photo',
340                                          'width' => $avatar_size,
341                                          'height' => $avatar_size,
342                                          'alt' =>
343                                          ($this->profile->fullname) ?
344                                          $this->profile->fullname :
345                                          $this->profile->nickname));
346     }
347
348     function avatarSize()
349     {
350         return AVATAR_STREAM_SIZE;
351     }
352
353     /**
354      * show the nickname of the author
355      *
356      * Links to the author's profile page
357      *
358      * @return void
359      */
360
361     function showNickname()
362     {
363         $this->out->raw('<span class="nickname fn">' .
364                         htmlspecialchars($this->profile->nickname) .
365                         '</span>');
366     }
367
368     /**
369      * show the content of the notice
370      *
371      * Shows the content of the notice. This is pre-rendered for efficiency
372      * at save time. Some very old notices might not be pre-rendered, so
373      * they're rendered on the spot.
374      *
375      * @return void
376      */
377
378     function showContent()
379     {
380         // FIXME: URL, image, video, audio
381         $this->out->elementStart('p', array('class' => 'entry-content'));
382         if ($this->notice->rendered) {
383             $this->out->raw($this->notice->rendered);
384         } else {
385             // XXX: may be some uncooked notices in the DB,
386             // we cook them right now. This should probably disappear in future
387             // versions (>> 0.4.x)
388             $this->out->raw(common_render_content($this->notice->content, $this->notice));
389         }
390         $this->out->elementEnd('p');
391     }
392
393     function showNoticeAttachments() {
394         if (common_config('attachments', 'show_thumbs')) {
395             $al = new InlineAttachmentList($this->notice, $this->out);
396             $al->show();
397         }
398     }
399
400     /**
401      * show the link to the main page for the notice
402      *
403      * Displays a link to the page for a notice, with "relative" time. Tries to
404      * get remote notice URLs correct, but doesn't always succeed.
405      *
406      * @return void
407      */
408
409     function showNoticeLink()
410     {
411         $noticeurl = $this->notice->bestUrl();
412
413         // above should always return an URL
414
415         assert(!empty($noticeurl));
416
417         $this->out->elementStart('a', array('rel' => 'bookmark',
418                                             'class' => 'timestamp',
419                                             'href' => $noticeurl));
420         $dt = common_date_iso8601($this->notice->created);
421         $this->out->element('abbr', array('class' => 'published',
422                                           'title' => $dt),
423                             common_date_string($this->notice->created));
424         $this->out->elementEnd('a');
425     }
426
427     /**
428      * show the notice location
429      *
430      * shows the notice location in the correct language.
431      *
432      * If an URL is available, makes a link. Otherwise, just a span.
433      *
434      * @return void
435      */
436
437     function showNoticeLocation()
438     {
439         $id = $this->notice->id;
440
441         $location = $this->notice->getLocation();
442
443         if (empty($location)) {
444             return;
445         }
446
447         $name = $location->getName();
448
449         $lat = $this->notice->lat;
450         $lon = $this->notice->lon;
451         $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
452
453         if (empty($name)) {
454             $latdms = $this->decimalDegreesToDMS(abs($lat));
455             $londms = $this->decimalDegreesToDMS(abs($lon));
456             // TRANS: Used in coordinates as abbreviation of north
457             $north = _('N');
458             // TRANS: Used in coordinates as abbreviation of south
459             $south = _('S');
460             // TRANS: Used in coordinates as abbreviation of east
461             $east = _('E');
462             // TRANS: Used in coordinates as abbreviation of west
463             $west = _('W');
464             $name = sprintf(
465                 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
466                 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
467                 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
468         }
469
470         $url  = $location->getUrl();
471
472         $this->out->text(' ');
473         $this->out->elementStart('span', array('class' => 'location'));
474         $this->out->text(_('at'));
475         $this->out->text(' ');
476         if (empty($url)) {
477             $this->out->element('abbr', array('class' => 'geo',
478                                               'title' => $latlon),
479                                 $name);
480         } else {
481             $xstr = new XMLStringer(false);
482             $xstr->elementStart('a', array('href' => $url,
483                                            'rel' => 'external'));
484             $xstr->element('abbr', array('class' => 'geo',
485                                          'title' => $latlon),
486                            $name);
487             $xstr->elementEnd('a');
488             $this->out->raw($xstr->getString());
489         }
490         $this->out->elementEnd('span');
491     }
492
493     /**
494      * @param number $dec decimal degrees
495      * @return array split into 'deg', 'min', and 'sec'
496      */
497     function decimalDegreesToDMS($dec)
498     {
499         $deg = intval($dec);
500         $tempma = abs($dec) - abs($deg);
501
502         $tempma = $tempma * 3600;
503         $min = floor($tempma / 60);
504         $sec = $tempma - ($min*60);
505
506         return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
507     }
508
509     /**
510      * Show the source of the notice
511      *
512      * Either the name (and link) of the API client that posted the notice,
513      * or one of other other channels.
514      *
515      * @return void
516      */
517
518     function showNoticeSource()
519     {
520         $ns = $this->notice->getSource();
521
522         if ($ns) {
523             $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _('web')) : _($ns->name);
524             $this->out->text(' ');
525             $this->out->elementStart('span', 'source');
526             // FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
527             $this->out->text(_('from'));
528             $this->out->text(' ');
529
530             $name  = $source_name;
531             $url   = $ns->url;
532             $title = null;
533
534             if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
535                 $name = $source_name;
536                 $url  = $ns->url;
537             }
538             Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
539
540             // if $ns->name and $ns->url are populated we have
541             // configured a source attr somewhere
542             if (!empty($name) && !empty($url)) {
543
544                 $this->out->elementStart('span', 'device');
545
546                 $attrs = array(
547                     'href' => $url,
548                     'rel' => 'external'
549                 );
550
551                 if (!empty($title)) {
552                     $attrs['title'] = $title;
553                 }
554
555                 $this->out->element('a', $attrs, $name);
556                 $this->out->elementEnd('span');
557             } else {
558                 $this->out->element('span', 'device', $name);
559             }
560
561             $this->out->elementEnd('span');
562         }
563     }
564
565     /**
566      * show link to notice this notice is a reply to
567      *
568      * If this notice is a reply, show a link to the notice it is replying to. The
569      * heavy lifting for figuring out replies happens at save time.
570      *
571      * @return void
572      */
573
574     function showContext()
575     {
576         if ($this->notice->hasConversation()) {
577             $conv = Conversation::staticGet(
578                 'id',
579                 $this->notice->conversation
580             );
581             $convurl = $conv->uri;
582             if (!empty($convurl)) {
583                 $this->out->text(' ');
584                 $this->out->element(
585                     'a',
586                     array(
587                     'href' => $convurl.'#notice-'.$this->notice->id,
588                     'class' => 'response'),
589                     _('in context')
590                 );
591             } else {
592                 $msg = sprintf(
593                     "Couldn't find Conversation ID %d to make 'in context'"
594                     . "link for Notice ID %d",
595                     $this->notice->conversation,
596                     $this->notice->id
597                 );
598                 common_log(LOG_WARNING, $msg);
599             }
600         }
601     }
602
603     /**
604      * show a link to the author of repeat
605      *
606      * @return void
607      */
608
609     function showRepeat()
610     {
611         if (!empty($this->repeat)) {
612
613             $repeater = Profile::staticGet('id', $this->repeat->profile_id);
614
615             $attrs = array('href' => $repeater->profileurl,
616                            'class' => 'url');
617
618             if (!empty($repeater->fullname)) {
619                 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
620             }
621
622             $this->out->elementStart('span', 'repeat vcard');
623
624             $this->out->raw(_('Repeated by'));
625
626             $this->out->elementStart('a', $attrs);
627             $this->out->element('span', 'fn nickname', $repeater->nickname);
628             $this->out->elementEnd('a');
629
630             $this->out->elementEnd('span');
631         }
632     }
633
634     /**
635      * show a link to reply to the current notice
636      *
637      * Should either do the reply in the current notice form (if available), or
638      * link out to the notice-posting form. A little flakey, doesn't always work.
639      *
640      * @return void
641      */
642
643     function showReplyLink()
644     {
645         if (common_logged_in()) {
646             $this->out->text(' ');
647             $reply_url = common_local_url('newnotice',
648                                           array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
649             $this->out->elementStart('a', array('href' => $reply_url,
650                                                 'class' => 'notice_reply',
651                                                 'title' => _('Reply to this notice')));
652             $this->out->text(_('Reply'));
653             $this->out->text(' ');
654             $this->out->element('span', 'notice_id', $this->notice->id);
655             $this->out->elementEnd('a');
656         }
657     }
658
659     /**
660      * if the user is the author, let them delete the notice
661      *
662      * @return void
663      */
664
665     function showDeleteLink()
666     {
667         $user = common_current_user();
668
669         $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
670
671         if (!empty($user) &&
672             ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
673             $this->out->text(' ');
674             $deleteurl = common_local_url('deletenotice',
675                                           array('notice' => $todel->id));
676             $this->out->element('a', array('href' => $deleteurl,
677                                            'class' => 'notice_delete',
678                                            'title' => _('Delete this notice')), _('Delete'));
679         }
680     }
681
682     /**
683      * show the form to repeat a notice
684      *
685      * @return void
686      */
687
688     function showRepeatForm()
689     {
690         $user = common_current_user();
691         if ($user && $user->id != $this->notice->profile_id) {
692             $this->out->text(' ');
693             $profile = $user->getProfile();
694             if ($profile->hasRepeated($this->notice->id)) {
695                 $this->out->element('span', array('class' => 'repeated',
696                                                   'title' => _('Notice repeated')),
697                                             _('Repeated'));
698             } else {
699                 $rf = new RepeatForm($this->out, $this->notice);
700                 $rf->show();
701             }
702         }
703     }
704
705     /**
706      * finish the notice
707      *
708      * Close the last elements in the notice list item
709      *
710      * @return void
711      */
712
713     function showEnd()
714     {
715         if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
716             $this->out->elementEnd('li');
717             Event::handle('EndCloseNoticeListItemElement', array($this));
718         }
719     }
720 }