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