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