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