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