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