]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelistitem.php
Stronger typing for NoticeListItem and so
[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, HTMLOutputter $out=null)
69     {
70         parent::__construct($out);
71         if (!empty($notice->repeat_of)) {
72             $original = Notice::getKV('id', $notice->repeat_of);
73             if (empty($original)) { // 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         $this->out->elementStart('div', 'entry-title');
117         $this->showAuthor();
118         $this->showContent();
119         $this->out->elementEnd('div');
120     }
121
122     function showNoticeInfo()
123     {
124         $this->out->elementStart('div', 'entry-content');
125         if (Event::handle('StartShowNoticeInfo', array($this))) {
126             $this->showNoticeLink();
127             $this->showNoticeSource();
128             $this->showNoticeLocation();
129             if ($this->notice->hasConversation()) {
130                 $this->showContext();
131             }
132             $this->showRepeat();
133             Event::handle('EndShowNoticeInfo', array($this));
134         }
135
136         $this->out->elementEnd('div');
137     }
138
139     function showNoticeOptions()
140     {
141         if (Event::handle('StartShowNoticeOptions', array($this))) {
142             $user = common_current_user();
143             if ($user) {
144                 $this->out->elementStart('div', 'notice-options');
145                 if (Event::handle('StartShowNoticeOptionItems', array($this))) {
146                     $this->showFaveForm();
147                     $this->showReplyLink();
148                     $this->showRepeatForm();
149                     $this->showDeleteLink();
150                     Event::handle('EndShowNoticeOptionItems', array($this));
151                 }
152                 $this->out->elementEnd('div');
153             }
154             Event::handle('EndShowNoticeOptions', array($this));
155         }
156     }
157
158     /**
159      * start a single notice.
160      *
161      * @return void
162      */
163     function showStart()
164     {
165         if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
166             $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
167             $class = 'hentry notice';
168             if ($this->notice->scope != 0 && $this->notice->scope != 1) {
169                 $class .= ' limited-scope';
170             }
171             if (!empty($this->notice->source)) {
172                 $class .= ' notice-source-'.$this->notice->source;
173             }
174             $this->out->elementStart('li', array('class' => $class,
175                                                  'id' => 'notice-' . $id));
176             Event::handle('EndOpenNoticeListItemElement', array($this));
177         }
178     }
179
180     /**
181      * show the "favorite" form
182      *
183      * @return void
184      */
185     function showFaveForm()
186     {
187         if (Event::handle('StartShowFaveForm', array($this))) {
188             $user = common_current_user();
189             if ($user) {
190                 if ($user->hasFave($this->notice)) {
191                     $disfavor = new DisfavorForm($this->out, $this->notice);
192                     $disfavor->show();
193                 } else {
194                     $favor = new FavorForm($this->out, $this->notice);
195                     $favor->show();
196                 }
197             }
198             Event::handle('EndShowFaveForm', array($this));
199         }
200     }
201
202     /**
203      * show the author of a notice
204      *
205      * By default, this shows the avatar and (linked) nickname of the author.
206      *
207      * @return void
208      */
209
210     function showAuthor()
211     {
212         $this->out->elementStart('div', 'author');
213
214         $this->out->elementStart('span', 'vcard author');
215
216         $attrs = array('href' => $this->profile->profileurl,
217                        'class' => 'url',
218                        'title' => $this->profile->nickname);
219
220         $this->out->elementStart('a', $attrs);
221         $this->showAvatar();
222         $this->out->text(' ');
223         $this->out->element('span',array('class' => 'fn'), $this->profile->getStreamName());
224         $this->out->elementEnd('a');
225
226         $this->out->elementEnd('span');
227
228         $this->showAddressees();
229
230         $this->out->elementEnd('div');
231     }
232
233     function showAddressees()
234     {
235         $pa = $this->getProfileAddressees();
236
237         if (!empty($pa)) {
238             $this->out->elementStart('span', 'addressees');
239             $first = true;
240             foreach ($pa as $addr) {
241                 if (!$first) {
242                     // TRANS: Separator in profile addressees list.
243                     $this->out->text(_m('SEPARATOR',', '));
244                 } else {
245                     // Start of profile addressees list.
246                     $first = false;
247                 }
248                 $text = $addr['text'];
249                 unset($addr['text']);
250                 $this->out->element('a', $addr, $text);
251             }
252             $this->out->elementEnd('span', 'addressees');
253         }
254     }
255
256     function getProfileAddressees()
257     {
258         $pa = array();
259
260         $attentions = $this->getReplyProfiles();
261
262         foreach ($attentions as $attn) {
263             $class = $attn->isGroup() ? 'group' : 'account';
264             $pa[] = array('href' => $attn->profileurl,
265                           'title' => $attn->nickname,
266                           'class' => "addressee {$class}",
267                           'text' => $attn->getStreamName());
268         }
269
270         return $pa;
271     }
272
273     function getReplyProfiles()
274     {
275         return $this->notice->getReplyProfiles();
276     }
277
278     /**
279      * show the avatar of the notice's author
280      *
281      * This will use the default avatar if no avatar is assigned for the author.
282      * It makes a link to the author's profile.
283      *
284      * @return void
285      */
286     function showAvatar()
287     {
288         $avatar_size = $this->avatarSize();
289
290         $avatarUrl = $this->profile->avatarUrl($avatar_size);
291
292         $this->out->element('img', array('src' => $avatarUrl,
293                                          'class' => 'avatar photo',
294                                          'width' => $avatar_size,
295                                          'height' => $avatar_size,
296                                          'alt' =>
297                                          ($this->profile->fullname) ?
298                                          $this->profile->fullname :
299                                          $this->profile->nickname));
300     }
301
302     function avatarSize()
303     {
304         return AVATAR_STREAM_SIZE;
305     }
306
307     /**
308      * show the nickname of the author
309      *
310      * Links to the author's profile page
311      *
312      * @return void
313      */
314     function showNickname()
315     {
316         $this->out->raw('<span class="nickname fn">' .
317                         htmlspecialchars($this->profile->nickname) .
318                         '</span>');
319     }
320
321     /**
322      * show the content of the notice
323      *
324      * Shows the content of the notice. This is pre-rendered for efficiency
325      * at save time. Some very old notices might not be pre-rendered, so
326      * they're rendered on the spot.
327      *
328      * @return void
329      */
330     function showContent()
331     {
332         // FIXME: URL, image, video, audio
333         $this->out->elementStart('p', array('class' => 'entry-content'));
334         if ($this->notice->rendered) {
335             $this->out->raw($this->notice->rendered);
336         } else {
337             // XXX: may be some uncooked notices in the DB,
338             // we cook them right now. This should probably disappear in future
339             // versions (>> 0.4.x)
340             $this->out->raw(common_render_content($this->notice->content, $this->notice));
341         }
342         $this->out->elementEnd('p');
343     }
344
345     function showNoticeAttachments() {
346         if (common_config('attachments', 'show_thumbs')) {
347             $al = new InlineAttachmentList($this->notice, $this->out);
348             $al->show();
349         }
350     }
351
352     /**
353      * show the link to the main page for the notice
354      *
355      * Displays a local link to the rendered notice, with "relative" time.
356      *
357      * @return void
358      */
359     function showNoticeLink()
360     {
361         $this->out->elementStart('a', array('rel' => 'bookmark',
362                                             'class' => 'timestamp',
363                                             'href' => $this->notice->getLocalUrl()));
364         $this->out->element('time', array('class' => 'dt-published',
365                                           'datetime' => common_date_iso8601($this->notice->created),
366                                           // TRANS: Timestamp title (tooltip text) for NoticeListItem
367                                           'title' => common_exact_date($this->notice->created)),
368                             common_date_string($this->notice->created));
369         $this->out->elementEnd('a');
370     }
371
372     /**
373      * show the notice location
374      *
375      * shows the notice location in the correct language.
376      *
377      * If an URL is available, makes a link. Otherwise, just a span.
378      *
379      * @return void
380      */
381     function showNoticeLocation()
382     {
383         $id = $this->notice->id;
384
385         $location = $this->notice->getLocation();
386
387         if (empty($location)) {
388             return;
389         }
390
391         $name = $location->getName();
392
393         $lat = $this->notice->lat;
394         $lon = $this->notice->lon;
395         $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
396
397         if (empty($name)) {
398             $latdms = $this->decimalDegreesToDMS(abs($lat));
399             $londms = $this->decimalDegreesToDMS(abs($lon));
400             // TRANS: Used in coordinates as abbreviation of north.
401             $north = _('N');
402             // TRANS: Used in coordinates as abbreviation of south.
403             $south = _('S');
404             // TRANS: Used in coordinates as abbreviation of east.
405             $east = _('E');
406             // TRANS: Used in coordinates as abbreviation of west.
407             $west = _('W');
408             $name = sprintf(
409                 // TRANS: Coordinates message.
410                 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
411                 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
412                 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
413                 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
414                 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
415                 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
416                 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
417         }
418
419         $url  = $location->getUrl();
420
421         $this->out->text(' ');
422         $this->out->elementStart('span', array('class' => 'location'));
423         // TRANS: Followed by geo location.
424         $this->out->text(_('at'));
425         $this->out->text(' ');
426         if (empty($url)) {
427             $this->out->element('abbr', array('class' => 'geo',
428                                               'title' => $latlon),
429                                 $name);
430         } else {
431             $xstr = new XMLStringer(false);
432             $xstr->elementStart('a', array('href' => $url,
433                                            'rel' => 'external'));
434             $xstr->element('abbr', array('class' => 'geo',
435                                          'title' => $latlon),
436                            $name);
437             $xstr->elementEnd('a');
438             $this->out->raw($xstr->getString());
439         }
440         $this->out->elementEnd('span');
441     }
442
443     /**
444      * @param number $dec decimal degrees
445      * @return array split into 'deg', 'min', and 'sec'
446      */
447     function decimalDegreesToDMS($dec)
448     {
449         $deg = intval($dec);
450         $tempma = abs($dec) - abs($deg);
451
452         $tempma = $tempma * 3600;
453         $min = floor($tempma / 60);
454         $sec = $tempma - ($min*60);
455
456         return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
457     }
458
459     /**
460      * Show the source of the notice
461      *
462      * Either the name (and link) of the API client that posted the notice,
463      * or one of other other channels.
464      *
465      * @return void
466      */
467     function showNoticeSource()
468     {
469         $ns = $this->notice->getSource();
470
471         if ($ns) {
472             // TRANS: A possible notice source (web interface).
473             $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
474             $this->out->text(' ');
475             $this->out->elementStart('span', 'source');
476             // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
477             // TRANS: Followed by notice source.
478             $this->out->text(_('from'));
479             $this->out->text(' ');
480
481             $name  = $source_name;
482             $url   = $ns->url;
483             $title = null;
484
485             if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
486                 $name = $source_name;
487                 $url  = $ns->url;
488             }
489             Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
490
491             // if $ns->name and $ns->url are populated we have
492             // configured a source attr somewhere
493             if (!empty($name) && !empty($url)) {
494                 $this->out->elementStart('span', 'device');
495
496                 $attrs = array(
497                     'href' => $url,
498                     'rel' => 'external'
499                 );
500
501                 if (!empty($title)) {
502                     $attrs['title'] = $title;
503                 }
504
505                 $this->out->element('a', $attrs, $name);
506                 $this->out->elementEnd('span');
507             } else {
508                 $this->out->element('span', 'device', $name);
509             }
510
511             $this->out->elementEnd('span');
512         }
513     }
514
515     /**
516      * show link to notice this notice is a reply to
517      *
518      * If this notice is a reply, show a link to the notice it is replying to. The
519      * heavy lifting for figuring out replies happens at save time.
520      *
521      * @return void
522      */
523     function showContext()
524     {
525         $this->out->element('a',
526                             array('href' => $this->notice->getConversationUrl(),
527                                   'class' => 'conversation'),
528                             // TRANS: Addition in notice list item if notice is part of a conversation.
529                             _('in context'));
530     }
531
532     /**
533      * show a link to the author of repeat
534      *
535      * @return void
536      */
537     function showRepeat()
538     {
539         if (!empty($this->repeat)) {
540
541             $repeater = Profile::getKV('id', $this->repeat->profile_id);
542
543             $attrs = array('href' => $repeater->profileurl,
544                            'class' => 'url');
545
546             if (!empty($repeater->fullname)) {
547                 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
548             }
549
550             $this->out->elementStart('span', 'repeat vcard');
551
552             // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
553             $this->out->raw(_('Repeated by'));
554             $this->out->raw(' ');
555
556             $this->out->elementStart('a', $attrs);
557             $this->out->element('span', 'fn nickname', $repeater->nickname);
558             $this->out->elementEnd('a');
559
560             $this->out->elementEnd('span');
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->nickname, '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',
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      * show the form to repeat a notice
617      *
618      * @return void
619      */
620     function showRepeatForm()
621     {
622         if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
623             $this->notice->scope == Notice::SITE_SCOPE) {
624             $user = common_current_user();
625             if (!empty($user) &&
626                 $user->id != $this->notice->profile_id) {
627                 $this->out->text(' ');
628                 $profile = $user->getProfile();
629                 if ($profile->hasRepeated($this->notice)) {
630                     $this->out->element('span', array('class' => 'repeated',
631                                                       // TRANS: Title for repeat form status in notice list when a notice has been repeated.
632                                                       'title' => _('Notice repeated.')),
633                                         // TRANS: Repeat form status in notice list when a notice has been repeated.
634                                         _('Repeated'));
635                 } else {
636                     $rf = new RepeatForm($this->out, $this->notice);
637                     $rf->show();
638                 }
639             }
640         }
641     }
642
643     /**
644      * finish the notice
645      *
646      * Close the last elements in the notice list item
647      *
648      * @return void
649      */
650     function showEnd()
651     {
652         if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
653             $this->out->elementEnd('li');
654             Event::handle('EndCloseNoticeListItemElement', array($this));
655         }
656     }
657
658     /**
659      * Get the notice in question
660      *
661      * For hooks, etc., this may be useful
662      *
663      * @return Notice The notice we're showing
664      */
665
666     function getNotice()
667     {
668         return $this->notice;
669     }
670 }