]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelistitem.php
HTML5 + CSS adaptions, may cause visual breaks
[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 $notice, Action $out=null)
69     {
70         parent::__construct($out);
71         if (!empty($notice->repeat_of)) {
72             $original = Notice::getKV('id', $notice->repeat_of);
73             if (!$original instanceof Notice) { // 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             Event::handle('EndShowNoticeItem', array($this));
107         }
108         $this->showEnd();
109     }
110
111     function showNotice()
112     {
113         if (Event::handle('StartShowNoticeItemNotice', array($this))) {
114             $this->showNoticeHeaders();
115             $this->showContent();
116             $this->showNoticeFooter();
117             Event::handle('EndShowNoticeItemNotice', array($this));
118         }
119     }
120
121     function showNoticeHeaders()
122     {
123         $this->elementStart('section', array('class'=>'notice-headers'));
124         $this->showNoticeTitle();
125         $this->showAuthor();
126         $this->showAddressees();
127         $this->elementEnd('section');
128     }
129
130     function showNoticeFooter()
131     {
132         $this->elementStart('footer');
133         $this->showNoticeInfo();
134         $this->showNoticeAttachments();
135         $this->showNoticeOptions();
136         $this->elementEnd('footer');
137     }
138
139     function showNoticeTitle()
140     {
141         if (Event::handle('StartShowNoticeTitle', array($this))) {
142             $this->element('a', array('href' => $this->notice->getUrl(),
143                                       'class' => 'notice-title'),
144                            $this->notice->getTitle());
145             Event::handle('EndShowNoticeTitle', array($this));
146         }
147     }
148
149     function showNoticeInfo()
150     {
151         if (Event::handle('StartShowNoticeInfo', array($this))) {
152             $this->showNoticeLink();
153             $this->showNoticeSource();
154             $this->showNoticeLocation();
155             $this->showPermalink();
156             $this->showRepeat();
157             Event::handle('EndShowNoticeInfo', array($this));
158         }
159     }
160
161     function showNoticeOptions()
162     {
163         if (Event::handle('StartShowNoticeOptions', array($this))) {
164             $user = common_current_user();
165             if ($user) {
166                 $this->out->elementStart('div', 'notice-options');
167                 if (Event::handle('StartShowNoticeOptionItems', array($this))) {
168                     $this->showReplyLink();
169                     $this->showRepeatForm();
170                     $this->showDeleteLink();
171                     Event::handle('EndShowNoticeOptionItems', array($this));
172                 }
173                 $this->out->elementEnd('div');
174             }
175             Event::handle('EndShowNoticeOptions', array($this));
176         }
177     }
178
179     /**
180      * start a single notice.
181      *
182      * @return void
183      */
184     function showStart()
185     {
186         if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
187             $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
188             $class = 'h-entry notice';
189             if ($this->notice->scope != 0 && $this->notice->scope != 1) {
190                 $class .= ' limited-scope';
191             }
192             if (!empty($this->notice->source)) {
193                 $class .= ' notice-source-'.$this->notice->source;
194             }
195             $this->out->elementStart('li', array('class' => $class,
196                                                  'id' => 'notice-' . $id));
197             Event::handle('EndOpenNoticeListItemElement', array($this));
198         }
199     }
200
201     /**
202      * show the author of a notice
203      *
204      * By default, this shows the avatar and (linked) nickname of the author.
205      *
206      * @return void
207      */
208
209     function showAuthor()
210     {
211         $attrs = array('href' => $this->profile->profileurl,
212                        'class' => 'h-card p-author',
213                        'title' => $this->profile->getNickname());
214
215         if (Event::handle('StartShowNoticeItemAuthor', array($this->profile, $this->out, &$attrs))) {
216             $this->out->elementStart('a', $attrs);
217             $this->showAvatar($this->profile);
218             $this->out->text($this->profile->getStreamName());
219             $this->out->elementEnd('a');
220             Event::handle('EndShowNoticeItemAuthor', array($this->profile, $this->out));
221         }
222     }
223
224     function showAddressees()
225     {
226         $pa = $this->getProfileAddressees();
227
228         if (!empty($pa)) {
229             $this->out->elementStart('ul', 'addressees');
230             $first = true;
231             foreach ($pa as $addr) {
232                 $this->out->elementStart('li', 'h-card');
233                 $text = $addr['text'];
234                 unset($addr['text']);
235                 $this->out->element('a', $addr, $text);
236                 $this->out->elementEnd('li');
237             }
238             $this->out->elementEnd('ul', 'addressees');
239         }
240     }
241
242     function getProfileAddressees()
243     {
244         $pa = array();
245
246         $attentions = $this->getReplyProfiles();
247
248         foreach ($attentions as $attn) {
249             $class = $attn->isGroup() ? 'group' : 'account';
250             $pa[] = array('href' => $attn->profileurl,
251                           'title' => $attn->getNickname(),
252                           'class' => "addressee {$class}",
253                           'text' => $attn->getStreamName());
254         }
255
256         return $pa;
257     }
258
259     function getReplyProfiles()
260     {
261         return $this->notice->getReplyProfiles();
262     }
263
264     /**
265      * show the nickname of the author
266      *
267      * Links to the author's profile page
268      *
269      * @return void
270      */
271     function showNickname()
272     {
273         $this->out->raw('<span class="p-name">' .
274                         htmlspecialchars($this->profile->getNickname()) .
275                         '</span>');
276     }
277
278     /**
279      * show the content of the notice
280      *
281      * Shows the content of the notice. This is pre-rendered for efficiency
282      * at save time. Some very old notices might not be pre-rendered, so
283      * they're rendered on the spot.
284      *
285      * @return void
286      */
287     function showContent()
288     {
289         // FIXME: URL, image, video, audio
290         $this->out->elementStart('article', array('class' => 'e-content'));
291         if (Event::handle('StartShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()))) {
292             if ($this->notice->rendered) {
293                 $this->out->raw($this->notice->rendered);
294             } else {
295                 // XXX: may be some uncooked notices in the DB,
296                 // we cook them right now. This should probably disappear in future
297                 // versions (>> 0.4.x)
298                 $this->out->raw(common_render_content($this->notice->content, $this->notice));
299             }
300             Event::handle('EndShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()));
301         }
302         $this->out->elementEnd('article');
303     }
304
305     function showNoticeAttachments() {
306         if (common_config('attachments', 'show_thumbs')) {
307             $al = new InlineAttachmentList($this->notice, $this->out);
308             $al->show();
309         }
310     }
311
312     /**
313      * show the link to the main page for the notice
314      *
315      * Displays a local link to the rendered notice, with "relative" time.
316      *
317      * @return void
318      */
319     function showNoticeLink()
320     {
321         $this->out->elementStart('a', array('rel' => 'bookmark',
322                                             'class' => 'timestamp',
323                                             'href' => Conversation::getUrlFromNotice($this->notice)));
324         $this->out->element('time', array('class' => 'dt-published',
325                                           'datetime' => common_date_iso8601($this->notice->created),
326                                           // TRANS: Timestamp title (tooltip text) for NoticeListItem
327                                           'title' => common_exact_date($this->notice->created)),
328                             common_date_string($this->notice->created));
329         $this->out->elementEnd('a');
330     }
331
332     /**
333      * show the notice location
334      *
335      * shows the notice location in the correct language.
336      *
337      * If an URL is available, makes a link. Otherwise, just a span.
338      *
339      * @return void
340      */
341     function showNoticeLocation()
342     {
343         $id = $this->notice->id;
344
345         $location = $this->notice->getLocation();
346
347         if (empty($location)) {
348             return;
349         }
350
351         $name = $location->getName();
352
353         $lat = $this->notice->lat;
354         $lon = $this->notice->lon;
355         $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
356
357         if (empty($name)) {
358             $latdms = $this->decimalDegreesToDMS(abs($lat));
359             $londms = $this->decimalDegreesToDMS(abs($lon));
360             // TRANS: Used in coordinates as abbreviation of north.
361             $north = _('N');
362             // TRANS: Used in coordinates as abbreviation of south.
363             $south = _('S');
364             // TRANS: Used in coordinates as abbreviation of east.
365             $east = _('E');
366             // TRANS: Used in coordinates as abbreviation of west.
367             $west = _('W');
368             $name = sprintf(
369                 // TRANS: Coordinates message.
370                 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
371                 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
372                 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
373                 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
374                 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
375                 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
376                 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
377         }
378
379         $url  = $location->getUrl();
380
381         $this->out->text(' ');
382         $this->out->elementStart('span', array('class' => 'location'));
383         // TRANS: Followed by geo location.
384         $this->out->text(_('at'));
385         $this->out->text(' ');
386         if (empty($url)) {
387             $this->out->element('abbr', array('class' => 'geo',
388                                               'title' => $latlon),
389                                 $name);
390         } else {
391             $xstr = new XMLStringer(false);
392             $xstr->elementStart('a', array('href' => $url,
393                                            'rel' => 'external'));
394             $xstr->element('abbr', array('class' => 'geo',
395                                          'title' => $latlon),
396                            $name);
397             $xstr->elementEnd('a');
398             $this->out->raw($xstr->getString());
399         }
400         $this->out->elementEnd('span');
401     }
402
403     /**
404      * @param number $dec decimal degrees
405      * @return array split into 'deg', 'min', and 'sec'
406      */
407     function decimalDegreesToDMS($dec)
408     {
409         $deg = intval($dec);
410         $tempma = abs($dec) - abs($deg);
411
412         $tempma = $tempma * 3600;
413         $min = floor($tempma / 60);
414         $sec = $tempma - ($min*60);
415
416         return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
417     }
418
419     /**
420      * Show the source of the notice
421      *
422      * Either the name (and link) of the API client that posted the notice,
423      * or one of other other channels.
424      *
425      * @return void
426      */
427     function showNoticeSource()
428     {
429         $ns = $this->notice->getSource();
430
431         if (!$ns instanceof Notice_source) {
432             return false;
433         }
434
435         // TRANS: A possible notice source (web interface).
436         $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
437         $this->out->text(' ');
438         $this->out->elementStart('span', 'source');
439         // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
440         // TRANS: Followed by notice source.
441         $this->out->text(_('from'));
442         $this->out->text(' ');
443
444         $name  = $source_name;
445         $url   = $ns->url;
446         $title = null;
447
448         if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
449             $name = $source_name;
450             $url  = $ns->url;
451         }
452         Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
453
454         // if $ns->name and $ns->url are populated we have
455         // configured a source attr somewhere
456         if (!empty($name) && !empty($url)) {
457             $this->out->elementStart('span', 'device');
458
459             $attrs = array(
460                 'href' => $url,
461                 'rel' => 'external'
462             );
463
464             if (!empty($title)) {
465                 $attrs['title'] = $title;
466             }
467
468             $this->out->element('a', $attrs, $name);
469             $this->out->elementEnd('span');
470         } else {
471             $this->out->element('span', 'device', $name);
472         }
473
474         $this->out->elementEnd('span');
475     }
476
477     /**
478      * show link to single-notice view for this notice item
479      *
480      * A permalink that goes to this specific object and nothing else
481      *
482      * @return void
483      */
484     function showPermalink()
485     {
486         $class = 'permalink u-url';
487         if (!$this->notice->isLocal()) {
488             $class .= ' external';
489         }
490         try {
491             $this->out->element('a',
492                         array('href' => $this->notice->getUrl(),
493                               'class' => $class),
494                         // TRANS: Addition in notice list item for single-notice view.
495                         _('permalink'));
496         } catch (InvalidUrlException $e) {
497             // no permalink available
498         }
499     }
500
501     /**
502      * show a link to the author of repeat
503      *
504      * @return void
505      */
506     function showRepeat()
507     {
508         if (!empty($this->repeat)) {
509
510             $repeater = Profile::getKV('id', $this->repeat->profile_id);
511
512             $attrs = array('href' => $repeater->profileurl,
513                            'class' => 'h-card p-author',
514                            'title' => $repeater->getFancyName());
515
516             $this->out->elementStart('span', 'repeat h-entry');
517
518             // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
519             $this->out->raw(_('Repeated by').' ');
520
521             $this->out->element('a', $attrs, $repeater->getNickname());
522
523             $this->out->elementEnd('span');
524         }
525     }
526
527     /**
528      * show a link to reply to the current notice
529      *
530      * Should either do the reply in the current notice form (if available), or
531      * link out to the notice-posting form. A little flakey, doesn't always work.
532      *
533      * @return void
534      */
535     function showReplyLink()
536     {
537         if (common_logged_in()) {
538             $this->out->text(' ');
539             $reply_url = common_local_url('newnotice',
540                                           array('replyto' => $this->profile->getNickname(), 'inreplyto' => $this->notice->id));
541             $this->out->elementStart('a', array('href' => $reply_url,
542                                                 'class' => 'notice_reply',
543                                                 // TRANS: Link title in notice list item to reply to a notice.
544                                                 'title' => _('Reply to this notice.')));
545             // TRANS: Link text in notice list item to reply to a notice.
546             $this->out->text(_('Reply'));
547             $this->out->text(' ');
548             $this->out->element('span', 'notice_id', $this->notice->id);
549             $this->out->elementEnd('a');
550         }
551     }
552
553     /**
554      * if the user is the author, let them delete the notice
555      *
556      * @return void
557      */
558     function showDeleteLink()
559     {
560         $user = common_current_user();
561
562         $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
563
564         if (!empty($user) &&
565             ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
566             $this->out->text(' ');
567             $deleteurl = common_local_url('deletenotice',
568                                           array('notice' => $todel->id));
569             $this->out->element('a', array('href' => $deleteurl,
570                                            'class' => 'notice_delete',
571                                            // TRANS: Link title in notice list item to delete a notice.
572                                            'title' => _('Delete this notice from the timeline.')),
573                                            // TRANS: Link text in notice list item to delete a notice.
574                                            _('Delete'));
575         }
576     }
577
578     /**
579      * show the form to repeat a notice
580      *
581      * @return void
582      */
583     function showRepeatForm()
584     {
585         if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
586             $this->notice->scope == Notice::SITE_SCOPE) {
587             $user = common_current_user();
588             if (!empty($user) &&
589                 $user->id != $this->notice->profile_id) {
590                 $this->out->text(' ');
591                 $profile = $user->getProfile();
592                 if ($profile->hasRepeated($this->notice)) {
593                     $this->out->element('span', array('class' => 'repeated',
594                                                       // TRANS: Title for repeat form status in notice list when a notice has been repeated.
595                                                       'title' => _('Notice repeated.')),
596                                         // TRANS: Repeat form status in notice list when a notice has been repeated.
597                                         _('Repeated'));
598                 } else {
599                     $rf = new RepeatForm($this->out, $this->notice);
600                     $rf->show();
601                 }
602             }
603         }
604     }
605
606     /**
607      * finish the notice
608      *
609      * Close the last elements in the notice list item
610      *
611      * @return void
612      */
613     function showEnd()
614     {
615         if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
616             $this->out->elementEnd('li');
617             Event::handle('EndCloseNoticeListItemElement', array($this));
618         }
619     }
620
621     /**
622      * Get the notice in question
623      *
624      * For hooks, etc., this may be useful
625      *
626      * @return Notice The notice we're showing
627      */
628
629     function getNotice()
630     {
631         return $this->notice;
632     }
633 }