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