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