]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelistitem.php
8fa5c5dcd4de2bfcd9ce8a3f160c4c42080135ea
[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('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * widget for displaying a single notice
39  *
40  * This widget has the core smarts for showing a single notice: what to display,
41  * where, and under which circumstances. Its key method is show(); this is a recipe
42  * that calls all the other show*() methods to build up a single notice. The
43  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
44  * author info (since that's implicit by the data in the page).
45  *
46  * @category UI
47  * @package  StatusNet
48  * @author   Evan Prodromou <evan@status.net>
49  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50  * @link     http://status.net/
51  * @see      NoticeList
52  * @see      ProfileNoticeListItem
53  */
54 class NoticeListItem extends Widget
55 {
56     /** The notice this item will show. */
57     var $notice = null;
58
59     /** The notice that was repeated. */
60     var $repeat = null;
61
62     /** The profile of the author of the notice, extracted once for convenience. */
63     var $profile = null;
64
65     /**
66      * constructor
67      *
68      * Also initializes the profile attribute.
69      *
70      * @param Notice $notice The notice we'll display
71      */
72     function __construct($notice, $out=null)
73     {
74         parent::__construct($out);
75         if (!empty($notice->repeat_of)) {
76             $original = Notice::getKV('id', $notice->repeat_of);
77             if (empty($original)) { // could have been deleted
78                 $this->notice = $notice;
79             } else {
80                 $this->notice = $original;
81                 $this->repeat = $notice;
82             }
83         } else {
84             $this->notice  = $notice;
85         }
86         $this->profile = $this->notice->getProfile();
87     }
88
89     /**
90      * recipe function for displaying a single notice.
91      *
92      * This uses all the other methods to correctly display a notice. Override
93      * it or one of the others to fine-tune the output.
94      *
95      * @return void
96      */
97     function show()
98     {
99         if (empty($this->notice)) {
100             common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
101             return;
102         } else if (empty($this->profile)) {
103             common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
104             return;
105         }
106
107         $this->showStart();
108         if (Event::handle('StartShowNoticeItem', array($this))) {
109             $this->showNotice();
110             $this->showNoticeAttachments();
111             $this->showNoticeInfo();
112             $this->showNoticeOptions();
113             Event::handle('EndShowNoticeItem', array($this));
114         }
115         $this->showEnd();
116     }
117
118     function showNotice()
119     {
120         $this->out->elementStart('div', 'entry-title');
121         $this->showAuthor();
122         $this->showContent();
123         $this->out->elementEnd('div');
124     }
125
126     function showNoticeInfo()
127     {
128         $this->out->elementStart('div', 'entry-content');
129         if (Event::handle('StartShowNoticeInfo', array($this))) {
130             $this->showNoticeLink();
131             $this->showNoticeSource();
132             $this->showNoticeLocation();
133             $this->showContext();
134             $this->showRepeat();
135             Event::handle('EndShowNoticeInfo', array($this));
136         }
137
138         $this->out->elementEnd('div');
139     }
140
141     function showNoticeOptions()
142     {
143         if (Event::handle('StartShowNoticeOptions', array($this))) {
144             $user = common_current_user();
145             if ($user) {
146                 $this->out->elementStart('div', 'notice-options');
147                 if (Event::handle('StartShowNoticeOptionItems', array($this))) {
148                     $this->showFaveForm();
149                     $this->showReplyLink();
150                     $this->showRepeatForm();
151                     $this->showDeleteLink();
152                     Event::handle('EndShowNoticeOptionItems', array($this));
153                 }
154                 $this->out->elementEnd('div');
155             }
156             Event::handle('EndShowNoticeOptions', array($this));
157         }
158     }
159
160     /**
161      * start a single notice.
162      *
163      * @return void
164      */
165     function showStart()
166     {
167         if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
168             $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
169             $class = 'hentry notice';
170             if ($this->notice->scope != 0 && $this->notice->scope != 1) {
171                 $class .= ' limited-scope';
172             }
173             if (!empty($this->notice->source)) {
174                 $class .= ' notice-source-'.$this->notice->source;
175             }
176             $this->out->elementStart('li', array('class' => $class,
177                                                  'id' => 'notice-' . $id));
178             Event::handle('EndOpenNoticeListItemElement', array($this));
179         }
180     }
181
182     /**
183      * show the "favorite" form
184      *
185      * @return void
186      */
187     function showFaveForm()
188     {
189         if (Event::handle('StartShowFaveForm', array($this))) {
190             $user = common_current_user();
191             if ($user) {
192                 if ($user->hasFave($this->notice)) {
193                     $disfavor = new DisfavorForm($this->out, $this->notice);
194                     $disfavor->show();
195                 } else {
196                     $favor = new FavorForm($this->out, $this->notice);
197                     $favor->show();
198                 }
199             }
200             Event::handle('EndShowFaveForm', array($this));
201         }
202     }
203
204     /**
205      * show the author of a notice
206      *
207      * By default, this shows the avatar and (linked) nickname of the author.
208      *
209      * @return void
210      */
211
212     function showAuthor()
213     {
214         $this->out->elementStart('div', 'author');
215
216         $this->out->elementStart('span', 'vcard author');
217
218         $attrs = array('href' => $this->profile->profileurl,
219                        'class' => 'url',
220                        'title' => $this->profile->nickname);
221
222         $this->out->elementStart('a', $attrs);
223         $this->showAvatar();
224         $this->out->text(' ');
225         $user = common_current_user();
226         if (!empty($user) && $user->streamNicknames()) {
227             $this->out->element('span',array('class' => 'fn'),
228                                 $this->profile->nickname);
229         } else {
230             $this->out->element('span',array('class' => 'fn'),
231                                 $this->profile->getBestName());
232         }
233         $this->out->elementEnd('a');
234
235         $this->out->elementEnd('span');
236
237         $this->showAddressees();
238
239         $this->out->elementEnd('div');
240     }
241
242     function showAddressees()
243     {
244         $ga = $this->getGroupAddressees();
245         $pa = $this->getProfileAddressees();
246
247         $a = array_merge($ga, $pa);
248
249         if (!empty($a)) {
250             $this->out->elementStart('span', 'addressees');
251             $first = true;
252             foreach ($a as $addr) {
253                 if (!$first) {
254                     // TRANS: Separator in profile addressees list.
255                     $this->out->text(_m('SEPARATOR',', '));
256                 } else {
257                     // Start of profile addressees list.
258                     $first = false;
259                 }
260                 $text = $addr['text'];
261                 unset($addr['text']);
262                 $this->out->element('a', $addr, $text);
263             }
264             $this->out->elementEnd('span', 'addressees');
265         }
266     }
267
268     function getGroupAddressees()
269     {
270         $ga = array();
271
272         $groups = $this->getGroups();
273
274         $user = common_current_user();
275
276         $streamNicknames = !empty($user) && $user->streamNicknames();
277
278         foreach ($groups as $group) {
279             $ga[] = array('href' => $group->homeUrl(),
280                           'title' => $group->nickname,
281                           'class' => 'addressee group',
282                           'text' => ($streamNicknames) ? $group->nickname : $group->getBestName());
283         }
284
285         return $ga;
286     }
287
288     function getGroups()
289     {
290         return $this->notice->getGroups();
291     }
292
293     function getProfileAddressees()
294     {
295         $pa = array();
296
297         $replies = $this->getReplyProfiles();
298
299         $user = common_current_user();
300
301         $streamNicknames = !empty($user) && $user->streamNicknames();
302
303         foreach ($replies as $reply) {
304             $pa[] = array('href' => $reply->profileurl,
305                           'title' => $reply->nickname,
306                           'class' => 'addressee account',
307                           'text' => ($streamNicknames) ? $reply->nickname : $reply->getBestName());
308         }
309
310         return $pa;
311     }
312
313     function getReplyProfiles()
314     {
315         return $this->notice->getReplyProfiles();
316     }
317
318     /**
319      * show the avatar of the notice's author
320      *
321      * This will use the default avatar if no avatar is assigned for the author.
322      * It makes a link to the author's profile.
323      *
324      * @return void
325      */
326     function showAvatar()
327     {
328         $avatar_size = $this->avatarSize();
329
330         $avatarUrl = $this->profile->avatarUrl($avatar_size);
331
332         $this->out->element('img', array('src' => $avatarUrl,
333                                          'class' => 'avatar photo',
334                                          'width' => $avatar_size,
335                                          'height' => $avatar_size,
336                                          'alt' =>
337                                          ($this->profile->fullname) ?
338                                          $this->profile->fullname :
339                                          $this->profile->nickname));
340     }
341
342     function avatarSize()
343     {
344         return AVATAR_STREAM_SIZE;
345     }
346
347     /**
348      * show the nickname of the author
349      *
350      * Links to the author's profile page
351      *
352      * @return void
353      */
354     function showNickname()
355     {
356         $this->out->raw('<span class="nickname fn">' .
357                         htmlspecialchars($this->profile->nickname) .
358                         '</span>');
359     }
360
361     /**
362      * show the content of the notice
363      *
364      * Shows the content of the notice. This is pre-rendered for efficiency
365      * at save time. Some very old notices might not be pre-rendered, so
366      * they're rendered on the spot.
367      *
368      * @return void
369      */
370     function showContent()
371     {
372         // FIXME: URL, image, video, audio
373         $this->out->elementStart('p', array('class' => 'entry-content'));
374         if ($this->notice->rendered) {
375             $this->out->raw($this->notice->rendered);
376         } else {
377             // XXX: may be some uncooked notices in the DB,
378             // we cook them right now. This should probably disappear in future
379             // versions (>> 0.4.x)
380             $this->out->raw(common_render_content($this->notice->content, $this->notice));
381         }
382         $this->out->elementEnd('p');
383     }
384
385     function showNoticeAttachments() {
386         if (common_config('attachments', 'show_thumbs')) {
387             $al = new InlineAttachmentList($this->notice, $this->out);
388             $al->show();
389         }
390     }
391
392     /**
393      * show the link to the main page for the notice
394      *
395      * Displays a link to the page for a notice, with "relative" time. Tries to
396      * get remote notice URLs correct, but doesn't always succeed.
397      *
398      * @return void
399      */
400     function showNoticeLink()
401     {
402         $noticeurl = $this->notice->bestUrl();
403
404         // above should always return an URL
405
406         assert(!empty($noticeurl));
407
408         $this->out->elementStart('a', array('rel' => 'bookmark',
409                                             'class' => 'timestamp',
410                                             'href' => $noticeurl));
411         $dt = common_date_iso8601($this->notice->created);
412         $this->out->element('abbr', array('class' => 'published',
413                                           'title' => $dt),
414                             common_date_string($this->notice->created));
415         $this->out->elementEnd('a');
416     }
417
418     /**
419      * show the notice location
420      *
421      * shows the notice location in the correct language.
422      *
423      * If an URL is available, makes a link. Otherwise, just a span.
424      *
425      * @return void
426      */
427     function showNoticeLocation()
428     {
429         $id = $this->notice->id;
430
431         $location = $this->notice->getLocation();
432
433         if (empty($location)) {
434             return;
435         }
436
437         $name = $location->getName();
438
439         $lat = $this->notice->lat;
440         $lon = $this->notice->lon;
441         $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
442
443         if (empty($name)) {
444             $latdms = $this->decimalDegreesToDMS(abs($lat));
445             $londms = $this->decimalDegreesToDMS(abs($lon));
446             // TRANS: Used in coordinates as abbreviation of north.
447             $north = _('N');
448             // TRANS: Used in coordinates as abbreviation of south.
449             $south = _('S');
450             // TRANS: Used in coordinates as abbreviation of east.
451             $east = _('E');
452             // TRANS: Used in coordinates as abbreviation of west.
453             $west = _('W');
454             $name = sprintf(
455                 // TRANS: Coordinates message.
456                 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
457                 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
458                 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
459                 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
460                 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
461                 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
462                 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
463         }
464
465         $url  = $location->getUrl();
466
467         $this->out->text(' ');
468         $this->out->elementStart('span', array('class' => 'location'));
469         // TRANS: Followed by geo location.
470         $this->out->text(_('at'));
471         $this->out->text(' ');
472         if (empty($url)) {
473             $this->out->element('abbr', array('class' => 'geo',
474                                               'title' => $latlon),
475                                 $name);
476         } else {
477             $xstr = new XMLStringer(false);
478             $xstr->elementStart('a', array('href' => $url,
479                                            'rel' => 'external'));
480             $xstr->element('abbr', array('class' => 'geo',
481                                          'title' => $latlon),
482                            $name);
483             $xstr->elementEnd('a');
484             $this->out->raw($xstr->getString());
485         }
486         $this->out->elementEnd('span');
487     }
488
489     /**
490      * @param number $dec decimal degrees
491      * @return array split into 'deg', 'min', and 'sec'
492      */
493     function decimalDegreesToDMS($dec)
494     {
495         $deg = intval($dec);
496         $tempma = abs($dec) - abs($deg);
497
498         $tempma = $tempma * 3600;
499         $min = floor($tempma / 60);
500         $sec = $tempma - ($min*60);
501
502         return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
503     }
504
505     /**
506      * Show the source of the notice
507      *
508      * Either the name (and link) of the API client that posted the notice,
509      * or one of other other channels.
510      *
511      * @return void
512      */
513     function showNoticeSource()
514     {
515         $ns = $this->notice->getSource();
516
517         if ($ns) {
518             // TRANS: A possible notice source (web interface).
519             $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
520             $this->out->text(' ');
521             $this->out->elementStart('span', 'source');
522             // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
523             // TRANS: Followed by notice source.
524             $this->out->text(_('from'));
525             $this->out->text(' ');
526
527             $name  = $source_name;
528             $url   = $ns->url;
529             $title = null;
530
531             if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
532                 $name = $source_name;
533                 $url  = $ns->url;
534             }
535             Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
536
537             // if $ns->name and $ns->url are populated we have
538             // configured a source attr somewhere
539             if (!empty($name) && !empty($url)) {
540                 $this->out->elementStart('span', 'device');
541
542                 $attrs = array(
543                     'href' => $url,
544                     'rel' => 'external'
545                 );
546
547                 if (!empty($title)) {
548                     $attrs['title'] = $title;
549                 }
550
551                 $this->out->element('a', $attrs, $name);
552                 $this->out->elementEnd('span');
553             } else {
554                 $this->out->element('span', 'device', $name);
555             }
556
557             $this->out->elementEnd('span');
558         }
559     }
560
561     /**
562      * show link to notice this notice is a reply to
563      *
564      * If this notice is a reply, show a link to the notice it is replying to. The
565      * heavy lifting for figuring out replies happens at save time.
566      *
567      * @return void
568      */
569     function showContext()
570     {
571         if ($this->notice->hasConversation()) {
572             $conv = Conversation::getKV(
573                 'id',
574                 $this->notice->conversation
575             );
576             $convurl = $conv->uri;
577             if (!empty($convurl)) {
578                 $this->out->text(' ');
579                 $this->out->element(
580                     'a',
581                     array(
582                     'href' => $convurl.'#notice-'.$this->notice->id,
583                     'class' => 'response'),
584                     // TRANS: Addition in notice list item if notice is part of a conversation.
585                     _('in context')
586                 );
587             } else {
588                 $msg = sprintf(
589                     "Couldn't find Conversation ID %d to make 'in context'"
590                     . "link for Notice ID %d",
591                     $this->notice->conversation,
592                     $this->notice->id
593                 );
594                 common_log(LOG_WARNING, $msg);
595             }
596         }
597     }
598
599     /**
600      * show a link to the author of repeat
601      *
602      * @return void
603      */
604     function showRepeat()
605     {
606         if (!empty($this->repeat)) {
607
608             $repeater = Profile::getKV('id', $this->repeat->profile_id);
609
610             $attrs = array('href' => $repeater->profileurl,
611                            'class' => 'url');
612
613             if (!empty($repeater->fullname)) {
614                 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
615             }
616
617             $this->out->elementStart('span', 'repeat vcard');
618
619             // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
620             $this->out->raw(_('Repeated by'));
621             $this->out->raw(' ');
622
623             $this->out->elementStart('a', $attrs);
624             $this->out->element('span', 'fn nickname', $repeater->nickname);
625             $this->out->elementEnd('a');
626
627             $this->out->elementEnd('span');
628         }
629     }
630
631     /**
632      * show a link to reply to the current notice
633      *
634      * Should either do the reply in the current notice form (if available), or
635      * link out to the notice-posting form. A little flakey, doesn't always work.
636      *
637      * @return void
638      */
639     function showReplyLink()
640     {
641         if (common_logged_in()) {
642             $this->out->text(' ');
643             $reply_url = common_local_url('newnotice',
644                                           array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
645             $this->out->elementStart('a', array('href' => $reply_url,
646                                                 'class' => 'notice_reply',
647                                                 // TRANS: Link title in notice list item to reply to a notice.
648                                                 'title' => _('Reply to this notice.')));
649             // TRANS: Link text in notice list item to reply to a notice.
650             $this->out->text(_('Reply'));
651             $this->out->text(' ');
652             $this->out->element('span', 'notice_id', $this->notice->id);
653             $this->out->elementEnd('a');
654         }
655     }
656
657     /**
658      * if the user is the author, let them delete the notice
659      *
660      * @return void
661      */
662     function showDeleteLink()
663     {
664         $user = common_current_user();
665
666         $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
667
668         if (!empty($user) &&
669             ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
670             $this->out->text(' ');
671             $deleteurl = common_local_url('deletenotice',
672                                           array('notice' => $todel->id));
673             $this->out->element('a', array('href' => $deleteurl,
674                                            'class' => 'notice_delete',
675                                            // TRANS: Link title in notice list item to delete a notice.
676                                            'title' => _('Delete this notice from the timeline.')),
677                                            // TRANS: Link text in notice list item to delete a notice.
678                                            _('Delete'));
679         }
680     }
681
682     /**
683      * show the form to repeat a notice
684      *
685      * @return void
686      */
687     function showRepeatForm()
688     {
689         if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
690             $this->notice->scope == Notice::SITE_SCOPE) {
691             $user = common_current_user();
692             if (!empty($user) &&
693                 $user->id != $this->notice->profile_id) {
694                 $this->out->text(' ');
695                 $profile = $user->getProfile();
696                 if ($profile->hasRepeated($this->notice->id)) {
697                     $this->out->element('span', array('class' => 'repeated',
698                                                       // TRANS: Title for repeat form status in notice list when a notice has been repeated.
699                                                       'title' => _('Notice repeated.')),
700                                         // TRANS: Repeat form status in notice list when a notice has been repeated.
701                                         _('Repeated'));
702                 } else {
703                     $rf = new RepeatForm($this->out, $this->notice);
704                     $rf->show();
705                 }
706             }
707         }
708     }
709
710     /**
711      * finish the notice
712      *
713      * Close the last elements in the notice list item
714      *
715      * @return void
716      */
717     function showEnd()
718     {
719         if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
720             $this->out->elementEnd('li');
721             Event::handle('EndCloseNoticeListItemElement', array($this));
722         }
723     }
724
725     /**
726      * Get the notice in question
727      *
728      * For hooks, etc., this may be useful
729      *
730      * @return Notice The notice we're showing
731      */
732
733     function getNotice()
734     {
735         return $this->notice;
736     }
737 }