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