]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelistitem.php
Merge branch 'master' into 1.0.x
[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::staticGet('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                 $this->showFaveForm();
148                 $this->showReplyLink();
149                 $this->showRepeatForm();
150                 $this->showDeleteLink();
151                 $this->out->elementEnd('div');
152             }
153             Event::handle('EndShowNoticeOptions', array($this));
154         }
155     }
156
157     /**
158      * start a single notice.
159      *
160      * @return void
161      */
162     function showStart()
163     {
164         if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
165             $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
166             $class = 'hentry notice';
167             if ($this->notice->scope != 0 && $this->notice->scope != 1) {
168                 $class .= ' limited-scope';
169             }
170             $this->out->elementStart('li', array('class' => $class,
171                                                  'id' => 'notice-' . $id));
172             Event::handle('EndOpenNoticeListItemElement', array($this));
173         }
174     }
175
176     /**
177      * show the "favorite" form
178      *
179      * @return void
180      */
181     function showFaveForm()
182     {
183         if (Event::handle('StartShowFaveForm', array($this))) {
184             $user = common_current_user();
185             if ($user) {
186                 if ($user->hasFave($this->notice)) {
187                     $disfavor = new DisfavorForm($this->out, $this->notice);
188                     $disfavor->show();
189                 } else {
190                     $favor = new FavorForm($this->out, $this->notice);
191                     $favor->show();
192                 }
193             }
194             Event::handle('EndShowFaveForm', array($this));
195         }
196     }
197
198     /**
199      * show the author of a notice
200      *
201      * By default, this shows the avatar and (linked) nickname of the author.
202      *
203      * @return void
204      */
205
206     function showAuthor()
207     {
208         $this->out->elementStart('div', 'author');
209
210         $this->out->elementStart('span', 'vcard author');
211
212         $attrs = array('href' => $this->profile->profileurl,
213                        'class' => 'url',
214                        'title' => $this->profile->nickname);
215
216         $this->out->elementStart('a', $attrs);
217         $this->showAvatar();
218         $this->out->text(' ');
219         $this->out->element('span',array('class' => 'fn'),
220                             $this->profile->getBestName());
221         $this->out->elementEnd('a');
222
223         $this->out->elementEnd('span');
224
225         $this->showAddressees();
226
227         $this->out->elementEnd('div');
228     }
229
230     function showAddressees()
231     {
232         $ga = $this->getGroupAddressees();
233         $pa = $this->getProfileAddressees();
234
235         $a = array_merge($ga, $pa);
236
237         if (!empty($a)) {
238             $this->out->elementStart('span', 'addressees');
239             $first = true;
240             foreach ($a 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 getGroupAddressees()
257     {
258         $ga = array();
259
260         $groups = $this->getGroups();
261
262         foreach ($groups as $group) {
263             $ga[] = array('href' => $group->homeUrl(),
264                           'title' => $group->nickname,
265                           'class' => 'addressee group',
266                           'text' => $group->getBestName());
267         }
268
269         return $ga;
270     }
271
272     function getGroups()
273     {
274         return $this->notice->getGroups();
275     }
276
277     function getProfileAddressees()
278     {
279         $pa = array();
280
281         $replies = $this->getReplyProfiles();
282
283         foreach ($replies as $reply) {
284             $pa[] = array('href' => $reply->profileurl,
285                           'title' => $reply->nickname,
286                           'class' => 'addressee account',
287                           'text' => $reply->getBestName());
288         }
289
290         return $pa;
291     }
292
293     function getReplyProfiles()
294     {
295         return $this->notice->getReplyProfiles();
296     }
297
298     /**
299      * show the avatar of the notice's author
300      *
301      * This will use the default avatar if no avatar is assigned for the author.
302      * It makes a link to the author's profile.
303      *
304      * @return void
305      */
306     function showAvatar()
307     {
308         $avatar_size = $this->avatarSize();
309
310         $avatar = $this->profile->getAvatar($avatar_size);
311
312         $this->out->element('img', array('src' => ($avatar) ?
313                                          $avatar->displayUrl() :
314                                          Avatar::defaultImage($avatar_size),
315                                          'class' => 'avatar photo',
316                                          'width' => $avatar_size,
317                                          'height' => $avatar_size,
318                                          'alt' =>
319                                          ($this->profile->fullname) ?
320                                          $this->profile->fullname :
321                                          $this->profile->nickname));
322     }
323
324     function avatarSize()
325     {
326         return AVATAR_STREAM_SIZE;
327     }
328
329     /**
330      * show the nickname of the author
331      *
332      * Links to the author's profile page
333      *
334      * @return void
335      */
336     function showNickname()
337     {
338         $this->out->raw('<span class="nickname fn">' .
339                         htmlspecialchars($this->profile->nickname) .
340                         '</span>');
341     }
342
343     /**
344      * show the content of the notice
345      *
346      * Shows the content of the notice. This is pre-rendered for efficiency
347      * at save time. Some very old notices might not be pre-rendered, so
348      * they're rendered on the spot.
349      *
350      * @return void
351      */
352     function showContent()
353     {
354         // FIXME: URL, image, video, audio
355         $this->out->elementStart('p', array('class' => 'entry-content'));
356         if ($this->notice->rendered) {
357             $this->out->raw($this->notice->rendered);
358         } else {
359             // XXX: may be some uncooked notices in the DB,
360             // we cook them right now. This should probably disappear in future
361             // versions (>> 0.4.x)
362             $this->out->raw(common_render_content($this->notice->content, $this->notice));
363         }
364         $this->out->elementEnd('p');
365     }
366
367     function showNoticeAttachments() {
368         if (common_config('attachments', 'show_thumbs')) {
369             $al = new InlineAttachmentList($this->notice, $this->out);
370             $al->show();
371         }
372     }
373
374     /**
375      * show the link to the main page for the notice
376      *
377      * Displays a link to the page for a notice, with "relative" time. Tries to
378      * get remote notice URLs correct, but doesn't always succeed.
379      *
380      * @return void
381      */
382     function showNoticeLink()
383     {
384         $noticeurl = $this->notice->bestUrl();
385
386         // above should always return an URL
387
388         assert(!empty($noticeurl));
389
390         $this->out->elementStart('a', array('rel' => 'bookmark',
391                                             'class' => 'timestamp',
392                                             'href' => $noticeurl));
393         $dt = common_date_iso8601($this->notice->created);
394         $this->out->element('abbr', array('class' => 'published',
395                                           'title' => $dt),
396                             common_date_string($this->notice->created));
397         $this->out->elementEnd('a');
398     }
399
400     /**
401      * show the notice location
402      *
403      * shows the notice location in the correct language.
404      *
405      * If an URL is available, makes a link. Otherwise, just a span.
406      *
407      * @return void
408      */
409     function showNoticeLocation()
410     {
411         $id = $this->notice->id;
412
413         $location = $this->notice->getLocation();
414
415         if (empty($location)) {
416             return;
417         }
418
419         $name = $location->getName();
420
421         $lat = $this->notice->lat;
422         $lon = $this->notice->lon;
423         $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
424
425         if (empty($name)) {
426             $latdms = $this->decimalDegreesToDMS(abs($lat));
427             $londms = $this->decimalDegreesToDMS(abs($lon));
428             // TRANS: Used in coordinates as abbreviation of north.
429             $north = _('N');
430             // TRANS: Used in coordinates as abbreviation of south.
431             $south = _('S');
432             // TRANS: Used in coordinates as abbreviation of east.
433             $east = _('E');
434             // TRANS: Used in coordinates as abbreviation of west.
435             $west = _('W');
436             $name = sprintf(
437                 // TRANS: Coordinates message.
438                 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
439                 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
440                 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
441                 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
442                 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
443                 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
444                 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
445         }
446
447         $url  = $location->getUrl();
448
449         $this->out->text(' ');
450         $this->out->elementStart('span', array('class' => 'location'));
451         // TRANS: Followed by geo location.
452         $this->out->text(_('at'));
453         $this->out->text(' ');
454         if (empty($url)) {
455             $this->out->element('abbr', array('class' => 'geo',
456                                               'title' => $latlon),
457                                 $name);
458         } else {
459             $xstr = new XMLStringer(false);
460             $xstr->elementStart('a', array('href' => $url,
461                                            'rel' => 'external'));
462             $xstr->element('abbr', array('class' => 'geo',
463                                          'title' => $latlon),
464                            $name);
465             $xstr->elementEnd('a');
466             $this->out->raw($xstr->getString());
467         }
468         $this->out->elementEnd('span');
469     }
470
471     /**
472      * @param number $dec decimal degrees
473      * @return array split into 'deg', 'min', and 'sec'
474      */
475     function decimalDegreesToDMS($dec)
476     {
477         $deg = intval($dec);
478         $tempma = abs($dec) - abs($deg);
479
480         $tempma = $tempma * 3600;
481         $min = floor($tempma / 60);
482         $sec = $tempma - ($min*60);
483
484         return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
485     }
486
487     /**
488      * Show the source of the notice
489      *
490      * Either the name (and link) of the API client that posted the notice,
491      * or one of other other channels.
492      *
493      * @return void
494      */
495     function showNoticeSource()
496     {
497         $ns = $this->notice->getSource();
498
499         if ($ns) {
500             // TRANS: A possible notice source (web interface).
501             $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
502             $this->out->text(' ');
503             $this->out->elementStart('span', 'source');
504             // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
505             // TRANS: Followed by notice source.
506             $this->out->text(_('from'));
507             $this->out->text(' ');
508
509             $name  = $source_name;
510             $url   = $ns->url;
511             $title = null;
512
513             if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
514                 $name = $source_name;
515                 $url  = $ns->url;
516             }
517             Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
518
519             // if $ns->name and $ns->url are populated we have
520             // configured a source attr somewhere
521             if (!empty($name) && !empty($url)) {
522                 $this->out->elementStart('span', 'device');
523
524                 $attrs = array(
525                     'href' => $url,
526                     'rel' => 'external'
527                 );
528
529                 if (!empty($title)) {
530                     $attrs['title'] = $title;
531                 }
532
533                 $this->out->element('a', $attrs, $name);
534                 $this->out->elementEnd('span');
535             } else {
536                 $this->out->element('span', 'device', $name);
537             }
538
539             $this->out->elementEnd('span');
540         }
541     }
542
543     /**
544      * show link to notice this notice is a reply to
545      *
546      * If this notice is a reply, show a link to the notice it is replying to. The
547      * heavy lifting for figuring out replies happens at save time.
548      *
549      * @return void
550      */
551     function showContext()
552     {
553         if ($this->notice->hasConversation()) {
554             $conv = Conversation::staticGet(
555                 'id',
556                 $this->notice->conversation
557             );
558             $convurl = $conv->uri;
559             if (!empty($convurl)) {
560                 $this->out->text(' ');
561                 $this->out->element(
562                     'a',
563                     array(
564                     'href' => $convurl.'#notice-'.$this->notice->id,
565                     'class' => 'response'),
566                     // TRANS: Addition in notice list item if notice is part of a conversation.
567                     _('in context')
568                 );
569             } else {
570                 $msg = sprintf(
571                     "Couldn't find Conversation ID %d to make 'in context'"
572                     . "link for Notice ID %d",
573                     $this->notice->conversation,
574                     $this->notice->id
575                 );
576                 common_log(LOG_WARNING, $msg);
577             }
578         }
579     }
580
581     /**
582      * show a link to the author of repeat
583      *
584      * @return void
585      */
586     function showRepeat()
587     {
588         if (!empty($this->repeat)) {
589
590             $repeater = Profile::staticGet('id', $this->repeat->profile_id);
591
592             $attrs = array('href' => $repeater->profileurl,
593                            'class' => 'url');
594
595             if (!empty($repeater->fullname)) {
596                 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
597             }
598
599             $this->out->elementStart('span', 'repeat vcard');
600
601             // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
602             $this->out->raw(_('Repeated by'));
603
604             $this->out->elementStart('a', $attrs);
605             $this->out->element('span', 'fn nickname', $repeater->nickname);
606             $this->out->elementEnd('a');
607
608             $this->out->elementEnd('span');
609         }
610     }
611
612     /**
613      * show a link to reply to the current notice
614      *
615      * Should either do the reply in the current notice form (if available), or
616      * link out to the notice-posting form. A little flakey, doesn't always work.
617      *
618      * @return void
619      */
620     function showReplyLink()
621     {
622         if (common_logged_in()) {
623             $this->out->text(' ');
624             $reply_url = common_local_url('newnotice',
625                                           array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
626             $this->out->elementStart('a', array('href' => $reply_url,
627                                                 'class' => 'notice_reply',
628                                                 // TRANS: Link title in notice list item to reply to a notice.
629                                                 'title' => _('Reply to this notice')));
630             // TRANS: Link text in notice list item to reply to a notice.
631             $this->out->text(_('Reply'));
632             $this->out->text(' ');
633             $this->out->element('span', 'notice_id', $this->notice->id);
634             $this->out->elementEnd('a');
635         }
636     }
637
638     /**
639      * if the user is the author, let them delete the notice
640      *
641      * @return void
642      */
643     function showDeleteLink()
644     {
645         $user = common_current_user();
646
647         $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
648
649         if (!empty($user) &&
650             ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
651             $this->out->text(' ');
652             $deleteurl = common_local_url('deletenotice',
653                                           array('notice' => $todel->id));
654             $this->out->element('a', array('href' => $deleteurl,
655                                            'class' => 'notice_delete',
656                                            // TRANS: Link title in notice list item to delete a notice.
657                                            'title' => _('Delete this notice')),
658                                            // TRANS: Link text in notice list item to delete a notice.
659                                            _('Delete'));
660         }
661     }
662
663     /**
664      * show the form to repeat a notice
665      *
666      * @return void
667      */
668     function showRepeatForm()
669     {
670         if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
671             $this->notice->scope == Notice::SITE_SCOPE) {
672             $user = common_current_user();
673             if (!empty($user) &&
674                 $user->id != $this->notice->profile_id) {
675                 $this->out->text(' ');
676                 $profile = $user->getProfile();
677                 if ($profile->hasRepeated($this->notice->id)) {
678                     $this->out->element('span', array('class' => 'repeated',
679                                                       // TRANS: Title for repeat form status in notice list when a notice has been repeated.
680                                                       'title' => _('Notice repeated.')),
681                                         // TRANS: Repeat form status in notice list when a notice has been repeated.
682                                         _('Repeated'));
683                 } else {
684                     $rf = new RepeatForm($this->out, $this->notice);
685                     $rf->show();
686                 }
687             }
688         }
689     }
690
691     /**
692      * finish the notice
693      *
694      * Close the last elements in the notice list item
695      *
696      * @return void
697      */
698     function showEnd()
699     {
700         if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
701             $this->out->elementEnd('li');
702             Event::handle('EndCloseNoticeListItemElement', array($this));
703         }
704     }
705 }