]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelistitem.php
No nickname in profile listing
[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     /**
62      * constructor
63      *
64      * Also initializes the profile attribute.
65      *
66      * @param Notice $notice The notice we'll display
67      */
68     function __construct(Notice $notice, HTMLOutputter $out=null)
69     {
70         parent::__construct($out);
71         if (!empty($notice->repeat_of)) {
72             $original = Notice::getKV('id', $notice->repeat_of);
73             if (!$original instanceof Notice) { // could have been deleted
74                 $this->notice = $notice;
75             } else {
76                 $this->notice = $original;
77                 $this->repeat = $notice;
78             }
79         } else {
80             $this->notice  = $notice;
81         }
82         $this->profile = $this->notice->getProfile();
83     }
84
85     /**
86      * recipe function for displaying a single notice.
87      *
88      * This uses all the other methods to correctly display a notice. Override
89      * it or one of the others to fine-tune the output.
90      *
91      * @return void
92      */
93     function show()
94     {
95         if (empty($this->notice)) {
96             common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
97             return;
98         } else if (empty($this->profile)) {
99             common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
100             return;
101         }
102
103         $this->showStart();
104         if (Event::handle('StartShowNoticeItem', array($this))) {
105             $this->showNotice();
106             $this->showNoticeAttachments();
107             $this->showNoticeInfo();
108             $this->showNoticeOptions();
109             Event::handle('EndShowNoticeItem', array($this));
110         }
111         $this->showEnd();
112     }
113
114     function showNotice()
115     {
116         $this->showNoticeTitle();
117         $this->showAuthor();
118         $this->showAddressees();
119         $this->showContent();
120     }
121
122     function showNoticeTitle()
123     {
124         if (Event::handle('StartShowNoticeTitle', array($this))) {
125             $this->element('a', array('href' => $this->notice->getUrl(),
126                                       'class' => 'p-name metadata'),
127                            $this->notice->getTitle());
128             Event::handle('EndShowNoticeTitle', array($this));
129         }
130     }
131
132     function showNoticeInfo()
133     {
134         $this->out->elementStart('div', 'entry-metadata');
135         if (Event::handle('StartShowNoticeInfo', array($this))) {
136             $this->showNoticeLink();
137             $this->showNoticeSource();
138             $this->showNoticeLocation();
139             if ($this->notice->hasConversation()) {
140                 $this->showContext();
141             }
142             $this->showRepeat();
143             Event::handle('EndShowNoticeInfo', array($this));
144         }
145
146         $this->out->elementEnd('div');
147     }
148
149     function showNoticeOptions()
150     {
151         if (Event::handle('StartShowNoticeOptions', array($this))) {
152             $user = common_current_user();
153             if ($user) {
154                 $this->out->elementStart('div', 'notice-options');
155                 if (Event::handle('StartShowNoticeOptionItems', array($this))) {
156                     $this->showFaveForm();
157                     $this->showReplyLink();
158                     $this->showRepeatForm();
159                     $this->showDeleteLink();
160                     Event::handle('EndShowNoticeOptionItems', array($this));
161                 }
162                 $this->out->elementEnd('div');
163             }
164             Event::handle('EndShowNoticeOptions', array($this));
165         }
166     }
167
168     /**
169      * start a single notice.
170      *
171      * @return void
172      */
173     function showStart()
174     {
175         if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
176             $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
177             $class = 'h-entry notice';
178             if ($this->notice->scope != 0 && $this->notice->scope != 1) {
179                 $class .= ' limited-scope';
180             }
181             if (!empty($this->notice->source)) {
182                 $class .= ' notice-source-'.$this->notice->source;
183             }
184             $this->out->elementStart('li', array('class' => $class,
185                                                  'id' => 'notice-' . $id));
186             Event::handle('EndOpenNoticeListItemElement', array($this));
187         }
188     }
189
190     /**
191      * show the "favorite" form
192      *
193      * @return void
194      */
195     function showFaveForm()
196     {
197         if (Event::handle('StartShowFaveForm', array($this))) {
198             $user = common_current_user();
199             if ($user) {
200                 if ($user->hasFave($this->notice)) {
201                     $disfavor = new DisfavorForm($this->out, $this->notice);
202                     $disfavor->show();
203                 } else {
204                     $favor = new FavorForm($this->out, $this->notice);
205                     $favor->show();
206                 }
207             }
208             Event::handle('EndShowFaveForm', array($this));
209         }
210     }
211
212     /**
213      * show the author of a notice
214      *
215      * By default, this shows the avatar and (linked) nickname of the author.
216      *
217      * @return void
218      */
219
220     function showAuthor()
221     {
222         $attrs = array('href' => $this->profile->profileurl,
223                        'class' => 'h-card p-author',
224                        'title' => $this->profile->getNickname());
225
226         $this->out->elementStart('a', $attrs);
227         $this->showAvatar($this->profile);
228         $this->out->text($this->profile->getStreamName());
229         $this->out->elementEnd('a');
230     }
231
232     function showAddressees()
233     {
234         $pa = $this->getProfileAddressees();
235
236         if (!empty($pa)) {
237             $this->out->elementStart('ul', 'addressees');
238             $first = true;
239             foreach ($pa as $addr) {
240                 $this->out->elementStart('li', 'h-card');
241                 $text = $addr['text'];
242                 unset($addr['text']);
243                 $this->out->element('a', $addr, $text);
244                 $this->out->elementEnd('li');
245             }
246             $this->out->elementEnd('ul', 'addressees');
247         }
248     }
249
250     function getProfileAddressees()
251     {
252         $pa = array();
253
254         $attentions = $this->getReplyProfiles();
255
256         foreach ($attentions as $attn) {
257             $class = $attn->isGroup() ? 'group' : 'account';
258             $pa[] = array('href' => $attn->profileurl,
259                           'title' => $attn->getNickname(),
260                           'class' => "addressee {$class}",
261                           'text' => $attn->getStreamName());
262         }
263
264         return $pa;
265     }
266
267     function getReplyProfiles()
268     {
269         return $this->notice->getReplyProfiles();
270     }
271
272     /**
273      * show the nickname of the author
274      *
275      * Links to the author's profile page
276      *
277      * @return void
278      */
279     function showNickname()
280     {
281         $this->out->raw('<span class="p-name">' .
282                         htmlspecialchars($this->profile->getNickname()) .
283                         '</span>');
284     }
285
286     /**
287      * show the content of the notice
288      *
289      * Shows the content of the notice. This is pre-rendered for efficiency
290      * at save time. Some very old notices might not be pre-rendered, so
291      * they're rendered on the spot.
292      *
293      * @return void
294      */
295     function showContent()
296     {
297         // FIXME: URL, image, video, audio
298         $this->out->elementStart('div', array('class' => 'e-content'));
299         if ($this->notice->rendered) {
300             $this->out->raw($this->notice->rendered);
301         } else {
302             // XXX: may be some uncooked notices in the DB,
303             // we cook them right now. This should probably disappear in future
304             // versions (>> 0.4.x)
305             $this->out->raw(common_render_content($this->notice->content, $this->notice));
306         }
307         $this->out->elementEnd('div');
308     }
309
310     function showNoticeAttachments() {
311         if (common_config('attachments', 'show_thumbs')) {
312             $al = new InlineAttachmentList($this->notice, $this->out);
313             $al->show();
314         }
315     }
316
317     /**
318      * show the link to the main page for the notice
319      *
320      * Displays a local link to the rendered notice, with "relative" time.
321      *
322      * @return void
323      */
324     function showNoticeLink()
325     {
326         $this->out->elementStart('a', array('rel' => 'bookmark',
327                                             'class' => 'u-url timestamp',
328                                             'href' => $this->notice->getLocalUrl()));
329         $this->out->element('time', array('class' => 'dt-published',
330                                           'datetime' => common_date_iso8601($this->notice->created),
331                                           // TRANS: Timestamp title (tooltip text) for NoticeListItem
332                                           'title' => common_exact_date($this->notice->created)),
333                             common_date_string($this->notice->created));
334         $this->out->elementEnd('a');
335     }
336
337     /**
338      * show the notice location
339      *
340      * shows the notice location in the correct language.
341      *
342      * If an URL is available, makes a link. Otherwise, just a span.
343      *
344      * @return void
345      */
346     function showNoticeLocation()
347     {
348         $id = $this->notice->id;
349
350         $location = $this->notice->getLocation();
351
352         if (empty($location)) {
353             return;
354         }
355
356         $name = $location->getName();
357
358         $lat = $this->notice->lat;
359         $lon = $this->notice->lon;
360         $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
361
362         if (empty($name)) {
363             $latdms = $this->decimalDegreesToDMS(abs($lat));
364             $londms = $this->decimalDegreesToDMS(abs($lon));
365             // TRANS: Used in coordinates as abbreviation of north.
366             $north = _('N');
367             // TRANS: Used in coordinates as abbreviation of south.
368             $south = _('S');
369             // TRANS: Used in coordinates as abbreviation of east.
370             $east = _('E');
371             // TRANS: Used in coordinates as abbreviation of west.
372             $west = _('W');
373             $name = sprintf(
374                 // TRANS: Coordinates message.
375                 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
376                 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
377                 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
378                 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
379                 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
380                 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
381                 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
382         }
383
384         $url  = $location->getUrl();
385
386         $this->out->text(' ');
387         $this->out->elementStart('span', array('class' => 'location'));
388         // TRANS: Followed by geo location.
389         $this->out->text(_('at'));
390         $this->out->text(' ');
391         if (empty($url)) {
392             $this->out->element('abbr', array('class' => 'geo',
393                                               'title' => $latlon),
394                                 $name);
395         } else {
396             $xstr = new XMLStringer(false);
397             $xstr->elementStart('a', array('href' => $url,
398                                            'rel' => 'external'));
399             $xstr->element('abbr', array('class' => 'geo',
400                                          'title' => $latlon),
401                            $name);
402             $xstr->elementEnd('a');
403             $this->out->raw($xstr->getString());
404         }
405         $this->out->elementEnd('span');
406     }
407
408     /**
409      * @param number $dec decimal degrees
410      * @return array split into 'deg', 'min', and 'sec'
411      */
412     function decimalDegreesToDMS($dec)
413     {
414         $deg = intval($dec);
415         $tempma = abs($dec) - abs($deg);
416
417         $tempma = $tempma * 3600;
418         $min = floor($tempma / 60);
419         $sec = $tempma - ($min*60);
420
421         return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
422     }
423
424     /**
425      * Show the source of the notice
426      *
427      * Either the name (and link) of the API client that posted the notice,
428      * or one of other other channels.
429      *
430      * @return void
431      */
432     function showNoticeSource()
433     {
434         $ns = $this->notice->getSource();
435
436         if ($ns) {
437             // TRANS: A possible notice source (web interface).
438             $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
439             $this->out->text(' ');
440             $this->out->elementStart('span', 'source');
441             // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
442             // TRANS: Followed by notice source.
443             $this->out->text(_('from'));
444             $this->out->text(' ');
445
446             $name  = $source_name;
447             $url   = $ns->url;
448             $title = null;
449
450             if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
451                 $name = $source_name;
452                 $url  = $ns->url;
453             }
454             Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
455
456             // if $ns->name and $ns->url are populated we have
457             // configured a source attr somewhere
458             if (!empty($name) && !empty($url)) {
459                 $this->out->elementStart('span', 'device');
460
461                 $attrs = array(
462                     'href' => $url,
463                     'rel' => 'external'
464                 );
465
466                 if (!empty($title)) {
467                     $attrs['title'] = $title;
468                 }
469
470                 $this->out->element('a', $attrs, $name);
471                 $this->out->elementEnd('span');
472             } else {
473                 $this->out->element('span', 'device', $name);
474             }
475
476             $this->out->elementEnd('span');
477         }
478     }
479
480     /**
481      * show link to notice this notice is a reply to
482      *
483      * If this notice is a reply, show a link to the notice it is replying to. The
484      * heavy lifting for figuring out replies happens at save time.
485      *
486      * @return void
487      */
488     function showContext()
489     {
490         $this->out->element('a',
491                             array('href' => $this->notice->getConversationUrl(),
492                                   'class' => 'conversation'),
493                             // TRANS: Addition in notice list item if notice is part of a conversation.
494                             _('in context'));
495     }
496
497     /**
498      * show a link to the author of repeat
499      *
500      * @return void
501      */
502     function showRepeat()
503     {
504         if (!empty($this->repeat)) {
505
506             $repeater = Profile::getKV('id', $this->repeat->profile_id);
507
508             $attrs = array('href' => $repeater->profileurl,
509                            'class' => 'h-card p-author',
510                            'title' => $repeater->getFancyName());
511
512             $this->out->elementStart('span', 'repeat h-entry');
513
514             // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
515             $this->out->raw(_('Repeated by').' ');
516
517             $this->out->element('a', $attrs, $repeater->getNickname());
518
519             $this->out->elementEnd('span');
520         }
521     }
522
523     /**
524      * show a link to reply to the current notice
525      *
526      * Should either do the reply in the current notice form (if available), or
527      * link out to the notice-posting form. A little flakey, doesn't always work.
528      *
529      * @return void
530      */
531     function showReplyLink()
532     {
533         if (common_logged_in()) {
534             $this->out->text(' ');
535             $reply_url = common_local_url('newnotice',
536                                           array('replyto' => $this->profile->getNickname(), 'inreplyto' => $this->notice->id));
537             $this->out->elementStart('a', array('href' => $reply_url,
538                                                 'class' => 'notice_reply',
539                                                 // TRANS: Link title in notice list item to reply to a notice.
540                                                 'title' => _('Reply to this notice.')));
541             // TRANS: Link text in notice list item to reply to a notice.
542             $this->out->text(_('Reply'));
543             $this->out->text(' ');
544             $this->out->element('span', 'notice_id', $this->notice->id);
545             $this->out->elementEnd('a');
546         }
547     }
548
549     /**
550      * if the user is the author, let them delete the notice
551      *
552      * @return void
553      */
554     function showDeleteLink()
555     {
556         $user = common_current_user();
557
558         $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
559
560         if (!empty($user) &&
561             ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
562             $this->out->text(' ');
563             $deleteurl = common_local_url('deletenotice',
564                                           array('notice' => $todel->id));
565             $this->out->element('a', array('href' => $deleteurl,
566                                            'class' => 'notice_delete',
567                                            // TRANS: Link title in notice list item to delete a notice.
568                                            'title' => _('Delete this notice from the timeline.')),
569                                            // TRANS: Link text in notice list item to delete a notice.
570                                            _('Delete'));
571         }
572     }
573
574     /**
575      * show the form to repeat a notice
576      *
577      * @return void
578      */
579     function showRepeatForm()
580     {
581         if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
582             $this->notice->scope == Notice::SITE_SCOPE) {
583             $user = common_current_user();
584             if (!empty($user) &&
585                 $user->id != $this->notice->profile_id) {
586                 $this->out->text(' ');
587                 $profile = $user->getProfile();
588                 if ($profile->hasRepeated($this->notice)) {
589                     $this->out->element('span', array('class' => 'repeated',
590                                                       // TRANS: Title for repeat form status in notice list when a notice has been repeated.
591                                                       'title' => _('Notice repeated.')),
592                                         // TRANS: Repeat form status in notice list when a notice has been repeated.
593                                         _('Repeated'));
594                 } else {
595                     $rf = new RepeatForm($this->out, $this->notice);
596                     $rf->show();
597                 }
598             }
599         }
600     }
601
602     /**
603      * finish the notice
604      *
605      * Close the last elements in the notice list item
606      *
607      * @return void
608      */
609     function showEnd()
610     {
611         if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
612             $this->out->elementEnd('li');
613             Event::handle('EndCloseNoticeListItemElement', array($this));
614         }
615     }
616
617     /**
618      * Get the notice in question
619      *
620      * For hooks, etc., this may be useful
621      *
622      * @return Notice The notice we're showing
623      */
624
625     function getNotice()
626     {
627         return $this->notice;
628     }
629 }