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