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