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