]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelistitem.php
Merge branch 'master' of gitorious.org:social/mainline into social-master
[quix0rs-gnu-social.git] / lib / noticelistitem.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * An item in a notice list
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Widget
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * widget for displaying a single notice
35  *
36  * This widget has the core smarts for showing a single notice: what to display,
37  * where, and under which circumstances. Its key method is show(); this is a recipe
38  * that calls all the other show*() methods to build up a single notice. The
39  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
40  * author info (since that's implicit by the data in the page).
41  *
42  * @category UI
43  * @package  StatusNet
44  * @author   Evan Prodromou <evan@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://status.net/
47  * @see      NoticeList
48  * @see      ProfileNoticeListItem
49  */
50 class NoticeListItem extends Widget
51 {
52     /** The notice this item will show. */
53     var $notice = null;
54
55     /** The notice that was repeated. */
56     var $repeat = null;
57
58     /** The profile of the author of the notice, extracted once for convenience. */
59     var $profile = null;
60
61     protected $addressees = true;
62     protected $attachments = true;
63     protected $id_prefix = null;
64     protected $options = true;
65     protected $maxchars = 0;   // if <= 0 it means use full posts
66
67     /**
68      * constructor
69      *
70      * Also initializes the profile attribute.
71      *
72      * @param Notice $notice The notice we'll display
73      */
74     function __construct(Notice $notice, Action $out=null, array $prefs=array())
75     {
76         parent::__construct($out);
77         if (!empty($notice->repeat_of)) {
78             $original = Notice::getKV('id', $notice->repeat_of);
79             if (!$original instanceof Notice) { // could have been deleted
80                 $this->notice = $notice;
81             } else {
82                 $this->notice = $original;
83                 $this->repeat = $notice;
84             }
85         } else {
86             $this->notice  = $notice;
87         }
88
89         $this->profile = $this->notice->getProfile();
90         
91         // integer preferences
92         foreach(array('maxchars') as $key) {
93             if (array_key_exists($key, $prefs)) {
94                 $this->$key = (int)$prefs[$key];
95             }
96         }
97         // boolean preferences
98         foreach(array('addressees', 'attachments', 'options') as $key) {
99             if (array_key_exists($key, $prefs)) {
100                 $this->$key = (bool)$prefs[$key];
101             }
102         }
103         // string preferences
104         foreach(array('id_prefix') as $key) {
105             if (array_key_exists($key, $prefs)) {
106                 $this->$key = $prefs[$key];
107             }
108         }
109     }
110
111     /**
112      * recipe function for displaying a single notice.
113      *
114      * This uses all the other methods to correctly display a notice. Override
115      * it or one of the others to fine-tune the output.
116      *
117      * @return void
118      */
119     function show()
120     {
121         if (empty($this->notice)) {
122             common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
123             return;
124         } else if (empty($this->profile)) {
125             common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
126             return;
127         }
128
129         $this->showStart();
130         if (Event::handle('StartShowNoticeItem', array($this))) {
131             $this->showNotice();
132             Event::handle('EndShowNoticeItem', array($this));
133         }
134         $this->showEnd();
135     }
136
137     function showNotice()
138     {
139         if (Event::handle('StartShowNoticeItemNotice', array($this))) {
140             $this->showNoticeHeaders();
141             $this->showContent();
142             $this->showNoticeFooter();
143             Event::handle('EndShowNoticeItemNotice', array($this));
144         }
145     }
146
147     function showNoticeHeaders()
148     {
149         $this->elementStart('section', array('class'=>'notice-headers'));
150         $this->showNoticeTitle();
151         $this->showAuthor();
152         if ($this->addressees) { $this->showAddressees(); }
153         $this->elementEnd('section');
154     }
155
156     function showNoticeFooter()
157     {
158         $this->elementStart('footer');
159         $this->showNoticeInfo();
160         if ($this->attachments) { $this->showNoticeAttachments(); }
161         if ($this->options) { $this->showNoticeOptions(); }
162         $this->elementEnd('footer');
163     }
164
165     function showNoticeTitle()
166     {
167         if (Event::handle('StartShowNoticeTitle', array($this))) {
168             $this->element('a', array('href' => $this->notice->getUrl(),
169                                       'class' => 'notice-title'),
170                            $this->notice->getTitle());
171             Event::handle('EndShowNoticeTitle', array($this));
172         }
173     }
174
175     function showNoticeInfo()
176     {
177         if (Event::handle('StartShowNoticeInfo', array($this))) {
178             $this->showNoticeLink();
179             $this->showNoticeSource();
180             $this->showNoticeLocation();
181             $this->showPermalink();
182             $this->showRepeat();
183             Event::handle('EndShowNoticeInfo', array($this));
184         }
185     }
186
187     function showNoticeOptions()
188     {
189         if (Event::handle('StartShowNoticeOptions', array($this))) {
190             $user = common_current_user();
191
192             if ($user instanceof User) {
193                 $this->out->elementStart('div', 'notice-options');
194                 if (Event::handle('StartShowNoticeOptionItems', array($this))) {
195                     $this->showReplyLink();
196                     $this->showRepeatForm();
197                     $this->showDeleteLink();
198                     Event::handle('EndShowNoticeOptionItems', array($this));
199                 }
200                 $this->out->elementEnd('div');
201             }
202
203             Event::handle('EndShowNoticeOptions', array($this));
204         }
205     }
206
207     /**
208      * start a single notice.
209      *
210      * @return void
211      */
212     function showStart()
213     {
214         if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
215             $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
216             $class = 'h-entry notice';
217             if ($this->notice->scope != 0 && $this->notice->scope != 1) {
218                 $class .= ' limited-scope';
219             }
220             if (!empty($this->notice->source)) {
221                 $class .= ' notice-source-'.$this->notice->source;
222             }
223             $id_prefix = (strlen($this->id_prefix) ? $this->id_prefix . '-' : '');
224             $this->out->elementStart('li', array('class' => $class,
225                                                  'id' => "${id_prefix}notice-${id}"));
226             Event::handle('EndOpenNoticeListItemElement', array($this));
227         }
228     }
229
230     /**
231      * show the author of a notice
232      *
233      * By default, this shows the avatar and (linked) nickname of the author.
234      *
235      * @return void
236      */
237
238     function showAuthor()
239     {
240         $attrs = array('href' => $this->profile->profileurl,
241                        'class' => 'h-card p-author',
242                        'title' => $this->profile->getNickname());
243
244         if (Event::handle('StartShowNoticeItemAuthor', array($this->profile, $this->out, &$attrs))) {
245             $this->out->elementStart('a', $attrs);
246             $this->showAvatar($this->profile);
247             $this->out->text($this->profile->getStreamName());
248             $this->out->elementEnd('a');
249             Event::handle('EndShowNoticeItemAuthor', array($this->profile, $this->out));
250         }
251     }
252
253     function showAddressees()
254     {
255         $pa = $this->getProfileAddressees();
256
257         if (!empty($pa)) {
258             $this->out->elementStart('ul', 'addressees');
259             $first = true;
260             foreach ($pa as $addr) {
261                 $this->out->elementStart('li', 'h-card');
262                 $text = $addr['text'];
263                 unset($addr['text']);
264                 $this->out->element('a', $addr, $text);
265                 $this->out->elementEnd('li');
266             }
267             $this->out->elementEnd('ul', 'addressees');
268         }
269     }
270
271     function getProfileAddressees()
272     {
273         $pa = array();
274
275         $attentions = $this->getReplyProfiles();
276
277         foreach ($attentions as $attn) {
278             $class = $attn->isGroup() ? 'group' : 'account';
279             $pa[] = array('href' => $attn->profileurl,
280                           'title' => $attn->getNickname(),
281                           'class' => "addressee {$class}",
282                           'text' => $attn->getStreamName());
283         }
284
285         return $pa;
286     }
287
288     function getReplyProfiles()
289     {
290         return $this->notice->getReplyProfiles();
291     }
292
293     /**
294      * show the nickname of the author
295      *
296      * Links to the author's profile page
297      *
298      * @return void
299      */
300     function showNickname()
301     {
302         $this->out->raw('<span class="p-name">' .
303                         htmlspecialchars($this->profile->getNickname()) .
304                         '</span>');
305     }
306
307     /**
308      * show the content of the notice
309      *
310      * Shows the content of the notice. This is pre-rendered for efficiency
311      * at save time. Some very old notices might not be pre-rendered, so
312      * they're rendered on the spot.
313      *
314      * @return void
315      */
316     function showContent()
317     {
318         // FIXME: URL, image, video, audio
319         $this->out->elementStart('article', array('class' => 'e-content'));
320         if (Event::handle('StartShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()))) {
321             if ($this->maxchars > 0 && mb_strlen($this->notice->content) > $this->maxchars) {
322                 $this->out->text(mb_substr($this->notice->content, 0, $this->maxchars) . '[…]');
323             } elseif ($this->notice->rendered) {
324                 $this->out->raw($this->notice->rendered);
325             } else {
326                 // XXX: may be some uncooked notices in the DB,
327                 // we cook them right now. This should probably disappear in future
328                 // versions (>> 0.4.x)
329                 $this->out->raw(common_render_content($this->notice->content, $this->notice));
330             }
331             Event::handle('EndShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()));
332         }
333         $this->out->elementEnd('article');
334     }
335
336     function showNoticeAttachments() {
337         if (common_config('attachments', 'show_thumbs')) {
338             $al = new InlineAttachmentList($this->notice, $this->out);
339             $al->show();
340         }
341     }
342
343     /**
344      * show the link to the main page for the notice
345      *
346      * Displays a local link to the rendered notice, with "relative" time.
347      *
348      * @return void
349      */
350     function showNoticeLink()
351     {
352         $this->out->elementStart('a', array('rel' => 'bookmark',
353                                             'class' => 'timestamp',
354                                             'href' => Conversation::getUrlFromNotice($this->notice)));
355         $this->out->element('time', array('class' => 'dt-published',
356                                           'datetime' => common_date_iso8601($this->notice->created),
357                                           // TRANS: Timestamp title (tooltip text) for NoticeListItem
358                                           'title' => common_exact_date($this->notice->created)),
359                             common_date_string($this->notice->created));
360         $this->out->elementEnd('a');
361     }
362
363     /**
364      * show the notice location
365      *
366      * shows the notice location in the correct language.
367      *
368      * If an URL is available, makes a link. Otherwise, just a span.
369      *
370      * @return void
371      */
372     function showNoticeLocation()
373     {
374         $id = $this->notice->id;
375
376         $location = $this->notice->getLocation();
377
378         if (empty($location)) {
379             return;
380         }
381
382         $name = $location->getName();
383
384         $lat = $this->notice->lat;
385         $lon = $this->notice->lon;
386         $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
387
388         if (empty($name)) {
389             $latdms = $this->decimalDegreesToDMS(abs($lat));
390             $londms = $this->decimalDegreesToDMS(abs($lon));
391             // TRANS: Used in coordinates as abbreviation of north.
392             $north = _('N');
393             // TRANS: Used in coordinates as abbreviation of south.
394             $south = _('S');
395             // TRANS: Used in coordinates as abbreviation of east.
396             $east = _('E');
397             // TRANS: Used in coordinates as abbreviation of west.
398             $west = _('W');
399             $name = sprintf(
400                 // TRANS: Coordinates message.
401                 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
402                 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
403                 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
404                 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
405                 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
406                 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
407                 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
408         }
409
410         $url  = $location->getUrl();
411
412         $this->out->text(' ');
413         $this->out->elementStart('span', array('class' => 'location'));
414         // TRANS: Followed by geo location.
415         $this->out->text(_('at'));
416         $this->out->text(' ');
417         if (empty($url)) {
418             $this->out->element('abbr', array('class' => 'geo',
419                                               'title' => $latlon),
420                                 $name);
421         } else {
422             $xstr = new XMLStringer(false);
423             $xstr->elementStart('a', array('href' => $url,
424                                            'rel' => 'external'));
425             $xstr->element('abbr', array('class' => 'geo',
426                                          'title' => $latlon),
427                            $name);
428             $xstr->elementEnd('a');
429             $this->out->raw($xstr->getString());
430         }
431         $this->out->elementEnd('span');
432     }
433
434     /**
435      * @param number $dec decimal degrees
436      * @return array split into 'deg', 'min', and 'sec'
437      */
438     function decimalDegreesToDMS($dec)
439     {
440         $deg = intval($dec);
441         $tempma = abs($dec) - abs($deg);
442
443         $tempma = $tempma * 3600;
444         $min = floor($tempma / 60);
445         $sec = $tempma - ($min*60);
446
447         return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
448     }
449
450     /**
451      * Show the source of the notice
452      *
453      * Either the name (and link) of the API client that posted the notice,
454      * or one of other other channels.
455      *
456      * @return void
457      */
458     function showNoticeSource()
459     {
460         $ns = $this->notice->getSource();
461
462         if (!$ns instanceof Notice_source) {
463             return false;
464         }
465
466         // TRANS: A possible notice source (web interface).
467         $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
468         $this->out->text(' ');
469         $this->out->elementStart('span', 'source');
470         // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
471         // TRANS: Followed by notice source.
472         $this->out->text(_('from'));
473         $this->out->text(' ');
474
475         $name  = $source_name;
476         $url   = $ns->url;
477         $title = null;
478
479         if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
480             $name = $source_name;
481             $url  = $ns->url;
482         }
483         Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
484
485         // if $ns->name and $ns->url are populated we have
486         // configured a source attr somewhere
487         if (!empty($name) && !empty($url)) {
488             $this->out->elementStart('span', 'device');
489
490             $attrs = array(
491                 'href' => $url,
492                 'rel' => 'external'
493             );
494
495             if (!empty($title)) {
496                 $attrs['title'] = $title;
497             }
498
499             $this->out->element('a', $attrs, $name);
500             $this->out->elementEnd('span');
501         } else {
502             $this->out->element('span', 'device', $name);
503         }
504
505         $this->out->elementEnd('span');
506     }
507
508     /**
509      * show link to single-notice view for this notice item
510      *
511      * A permalink that goes to this specific object and nothing else
512      *
513      * @return void
514      */
515     function showPermalink()
516     {
517         $class = 'permalink u-url';
518         if (!$this->notice->isLocal()) {
519             $class .= ' external';
520         }
521         try {
522             $this->out->element('a',
523                         array('href' => $this->notice->getUrl(),
524                               'class' => $class),
525                         // TRANS: Addition in notice list item for single-notice view.
526                         _('permalink'));
527         } catch (InvalidUrlException $e) {
528             // no permalink available
529         }
530     }
531
532     /**
533      * show a link to the author of repeat
534      *
535      * @return void
536      */
537     function showRepeat()
538     {
539         if (!empty($this->repeat)) {
540
541             $repeater = Profile::getKV('id', $this->repeat->profile_id);
542
543             $attrs = array('href' => $repeater->profileurl,
544                            'class' => 'h-card p-author',
545                            'title' => $repeater->getFancyName());
546
547             $this->out->elementStart('span', 'repeat h-entry');
548
549             // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
550             $this->out->raw(_('Repeated by').' ');
551
552             $this->out->element('a', $attrs, $repeater->getNickname());
553
554             $this->out->elementEnd('span');
555         }
556     }
557
558     /**
559      * show a link to reply to the current notice
560      *
561      * Should either do the reply in the current notice form (if available), or
562      * link out to the notice-posting form. A little flakey, doesn't always work.
563      *
564      * @return void
565      */
566     function showReplyLink()
567     {
568         if (common_logged_in()) {
569             $this->out->text(' ');
570             $reply_url = common_local_url('newnotice',
571                                           array('replyto' => $this->profile->getNickname(), 'inreplyto' => $this->notice->id));
572             $this->out->elementStart('a', array('href' => $reply_url,
573                                                 'class' => 'notice_reply',
574                                                 // TRANS: Link title in notice list item to reply to a notice.
575                                                 'title' => _('Reply to this notice.')));
576             // TRANS: Link text in notice list item to reply to a notice.
577             $this->out->text(_('Reply'));
578             $this->out->text(' ');
579             $this->out->element('span', 'notice_id', $this->notice->id);
580             $this->out->elementEnd('a');
581         }
582     }
583
584     /**
585      * if the user is the author, let them delete the notice
586      *
587      * @return void
588      */
589     function showDeleteLink()
590     {
591         $user = common_current_user();
592
593         $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
594
595         if (!empty($user) &&
596             ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
597             $this->out->text(' ');
598             $deleteurl = common_local_url('deletenotice',
599                                           array('notice' => $todel->id));
600             $this->out->element('a', array('href' => $deleteurl,
601                                            'class' => 'notice_delete',
602                                            // TRANS: Link title in notice list item to delete a notice.
603                                            'title' => _('Delete this notice from the timeline.')),
604                                            // TRANS: Link text in notice list item to delete a notice.
605                                            _('Delete'));
606         }
607     }
608
609     /**
610      * show the form to repeat a notice
611      *
612      * @return void
613      */
614     function showRepeatForm()
615     {
616         if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
617             $this->notice->scope == Notice::SITE_SCOPE) {
618             $user = common_current_user();
619             if (!empty($user) &&
620                 $user->id != $this->notice->profile_id) {
621                 $this->out->text(' ');
622                 $profile = $user->getProfile();
623                 if ($profile->hasRepeated($this->notice)) {
624                     $this->out->element('span', array('class' => 'repeated',
625                                                       // TRANS: Title for repeat form status in notice list when a notice has been repeated.
626                                                       'title' => _('Notice repeated.')),
627                                         // TRANS: Repeat form status in notice list when a notice has been repeated.
628                                         _('Repeated'));
629                 } else {
630                     $rf = new RepeatForm($this->out, $this->notice);
631                     $rf->show();
632                 }
633             }
634         }
635     }
636
637     /**
638      * finish the notice
639      *
640      * Close the last elements in the notice list item
641      *
642      * @return void
643      */
644     function showEnd()
645     {
646         if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
647             $this->out->elementEnd('li');
648             Event::handle('EndCloseNoticeListItemElement', array($this));
649         }
650     }
651
652     /**
653      * Get the notice in question
654      *
655      * For hooks, etc., this may be useful
656      *
657      * @return Notice The notice we're showing
658      */
659
660     function getNotice()
661     {
662         return $this->notice;
663     }
664 }