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