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