]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelistitem.php
ModPlus and some layouting issues regarding fixes
[quix0rs-gnu-social.git] / lib / noticelistitem.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * An item in a notice list
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Widget
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('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         $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->showReplyLink();
157                     $this->showRepeatForm();
158                     $this->showDeleteLink();
159                     Event::handle('EndShowNoticeOptionItems', array($this));
160                 }
161                 $this->out->elementEnd('div');
162             }
163             Event::handle('EndShowNoticeOptions', array($this));
164         }
165     }
166
167     /**
168      * start a single notice.
169      *
170      * @return void
171      */
172     function showStart()
173     {
174         if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
175             $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
176             $class = 'h-entry notice';
177             if ($this->notice->scope != 0 && $this->notice->scope != 1) {
178                 $class .= ' limited-scope';
179             }
180             if (!empty($this->notice->source)) {
181                 $class .= ' notice-source-'.$this->notice->source;
182             }
183             $this->out->elementStart('li', array('class' => $class,
184                                                  'id' => 'notice-' . $id));
185             Event::handle('EndOpenNoticeListItemElement', array($this));
186         }
187     }
188
189     /**
190      * show the author of a notice
191      *
192      * By default, this shows the avatar and (linked) nickname of the author.
193      *
194      * @return void
195      */
196
197     function showAuthor()
198     {
199         $attrs = array('href' => $this->profile->profileurl,
200                        'class' => 'h-card p-author',
201                        'title' => $this->profile->getNickname());
202
203         if (Event::handle('StartShowNoticeItemAuthor', array($this->profile, $this->out, &$attrs))) {
204             $this->out->elementStart('a', $attrs);
205             $this->showAvatar($this->profile);
206             $this->out->text($this->profile->getStreamName());
207             $this->out->elementEnd('a');
208             Event::handle('EndShowNoticeItemAuthor', array($this->profile, $this->out));
209         }
210     }
211
212     function showAddressees()
213     {
214         $pa = $this->getProfileAddressees();
215
216         if (!empty($pa)) {
217             $this->out->elementStart('ul', 'addressees');
218             $first = true;
219             foreach ($pa as $addr) {
220                 $this->out->elementStart('li', 'h-card');
221                 $text = $addr['text'];
222                 unset($addr['text']);
223                 $this->out->element('a', $addr, $text);
224                 $this->out->elementEnd('li');
225             }
226             $this->out->elementEnd('ul', 'addressees');
227         }
228     }
229
230     function getProfileAddressees()
231     {
232         $pa = array();
233
234         $attentions = $this->getReplyProfiles();
235
236         foreach ($attentions as $attn) {
237             $class = $attn->isGroup() ? 'group' : 'account';
238             $pa[] = array('href' => $attn->profileurl,
239                           'title' => $attn->getNickname(),
240                           'class' => "addressee {$class}",
241                           'text' => $attn->getStreamName());
242         }
243
244         return $pa;
245     }
246
247     function getReplyProfiles()
248     {
249         return $this->notice->getReplyProfiles();
250     }
251
252     /**
253      * show the nickname of the author
254      *
255      * Links to the author's profile page
256      *
257      * @return void
258      */
259     function showNickname()
260     {
261         $this->out->raw('<span class="p-name">' .
262                         htmlspecialchars($this->profile->getNickname()) .
263                         '</span>');
264     }
265
266     /**
267      * show the content of the notice
268      *
269      * Shows the content of the notice. This is pre-rendered for efficiency
270      * at save time. Some very old notices might not be pre-rendered, so
271      * they're rendered on the spot.
272      *
273      * @return void
274      */
275     function showContent()
276     {
277         // FIXME: URL, image, video, audio
278         $this->out->elementStart('div', array('class' => 'e-content'));
279         if (Event::handle('StartShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()))) {
280             if ($this->notice->rendered) {
281                 $this->out->raw($this->notice->rendered);
282             } else {
283                 // XXX: may be some uncooked notices in the DB,
284                 // we cook them right now. This should probably disappear in future
285                 // versions (>> 0.4.x)
286                 $this->out->raw(common_render_content($this->notice->content, $this->notice));
287             }
288             Event::handle('EndShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()));
289         }
290         $this->out->elementEnd('div');
291     }
292
293     function showNoticeAttachments() {
294         if (common_config('attachments', 'show_thumbs')) {
295             $al = new InlineAttachmentList($this->notice, $this->out);
296             $al->show();
297         }
298     }
299
300     /**
301      * show the link to the main page for the notice
302      *
303      * Displays a local link to the rendered notice, with "relative" time.
304      *
305      * @return void
306      */
307     function showNoticeLink()
308     {
309         $this->out->elementStart('a', array('rel' => 'bookmark',
310                                             'class' => 'u-url timestamp',
311                                             'href' => $this->notice->getLocalUrl()));
312         $this->out->element('time', array('class' => 'dt-published',
313                                           'datetime' => common_date_iso8601($this->notice->created),
314                                           // TRANS: Timestamp title (tooltip text) for NoticeListItem
315                                           'title' => common_exact_date($this->notice->created)),
316                             common_date_string($this->notice->created));
317         $this->out->elementEnd('a');
318     }
319
320     /**
321      * show the notice location
322      *
323      * shows the notice location in the correct language.
324      *
325      * If an URL is available, makes a link. Otherwise, just a span.
326      *
327      * @return void
328      */
329     function showNoticeLocation()
330     {
331         $id = $this->notice->id;
332
333         $location = $this->notice->getLocation();
334
335         if (empty($location)) {
336             return;
337         }
338
339         $name = $location->getName();
340
341         $lat = $this->notice->lat;
342         $lon = $this->notice->lon;
343         $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
344
345         if (empty($name)) {
346             $latdms = $this->decimalDegreesToDMS(abs($lat));
347             $londms = $this->decimalDegreesToDMS(abs($lon));
348             // TRANS: Used in coordinates as abbreviation of north.
349             $north = _('N');
350             // TRANS: Used in coordinates as abbreviation of south.
351             $south = _('S');
352             // TRANS: Used in coordinates as abbreviation of east.
353             $east = _('E');
354             // TRANS: Used in coordinates as abbreviation of west.
355             $west = _('W');
356             $name = sprintf(
357                 // TRANS: Coordinates message.
358                 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
359                 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
360                 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
361                 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
362                 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
363                 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
364                 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
365         }
366
367         $url  = $location->getUrl();
368
369         $this->out->text(' ');
370         $this->out->elementStart('span', array('class' => 'location'));
371         // TRANS: Followed by geo location.
372         $this->out->text(_('at'));
373         $this->out->text(' ');
374         if (empty($url)) {
375             $this->out->element('abbr', array('class' => 'geo',
376                                               'title' => $latlon),
377                                 $name);
378         } else {
379             $xstr = new XMLStringer(false);
380             $xstr->elementStart('a', array('href' => $url,
381                                            'rel' => 'external'));
382             $xstr->element('abbr', array('class' => 'geo',
383                                          'title' => $latlon),
384                            $name);
385             $xstr->elementEnd('a');
386             $this->out->raw($xstr->getString());
387         }
388         $this->out->elementEnd('span');
389     }
390
391     /**
392      * @param number $dec decimal degrees
393      * @return array split into 'deg', 'min', and 'sec'
394      */
395     function decimalDegreesToDMS($dec)
396     {
397         $deg = intval($dec);
398         $tempma = abs($dec) - abs($deg);
399
400         $tempma = $tempma * 3600;
401         $min = floor($tempma / 60);
402         $sec = $tempma - ($min*60);
403
404         return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
405     }
406
407     /**
408      * Show the source of the notice
409      *
410      * Either the name (and link) of the API client that posted the notice,
411      * or one of other other channels.
412      *
413      * @return void
414      */
415     function showNoticeSource()
416     {
417         $ns = $this->notice->getSource();
418
419         if ($ns) {
420             // TRANS: A possible notice source (web interface).
421             $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
422             $this->out->text(' ');
423             $this->out->elementStart('span', 'source');
424             // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
425             // TRANS: Followed by notice source.
426             $this->out->text(_('from'));
427             $this->out->text(' ');
428
429             $name  = $source_name;
430             $url   = $ns->url;
431             $title = null;
432
433             if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
434                 $name = $source_name;
435                 $url  = $ns->url;
436             }
437             Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
438
439             // if $ns->name and $ns->url are populated we have
440             // configured a source attr somewhere
441             if (!empty($name) && !empty($url)) {
442                 $this->out->elementStart('span', 'device');
443
444                 $attrs = array(
445                     'href' => $url,
446                     'rel' => 'external'
447                 );
448
449                 if (!empty($title)) {
450                     $attrs['title'] = $title;
451                 }
452
453                 $this->out->element('a', $attrs, $name);
454                 $this->out->elementEnd('span');
455             } else {
456                 $this->out->element('span', 'device', $name);
457             }
458
459             $this->out->elementEnd('span');
460         }
461     }
462
463     /**
464      * show link to notice this notice is a reply to
465      *
466      * If this notice is a reply, show a link to the notice it is replying to. The
467      * heavy lifting for figuring out replies happens at save time.
468      *
469      * @return void
470      */
471     function showContext()
472     {
473         $this->out->element('a',
474                             array('href' => $this->notice->getConversationUrl(),
475                                   'class' => 'conversation'),
476                             // TRANS: Addition in notice list item if notice is part of a conversation.
477                             _('in context'));
478     }
479
480     /**
481      * show a link to the author of repeat
482      *
483      * @return void
484      */
485     function showRepeat()
486     {
487         if (!empty($this->repeat)) {
488
489             $repeater = Profile::getKV('id', $this->repeat->profile_id);
490
491             $attrs = array('href' => $repeater->profileurl,
492                            'class' => 'h-card p-author',
493                            'title' => $repeater->getFancyName());
494
495             $this->out->elementStart('span', 'repeat h-entry');
496
497             // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
498             $this->out->raw(_('Repeated by').' ');
499
500             $this->out->element('a', $attrs, $repeater->getNickname());
501
502             $this->out->elementEnd('span');
503         }
504     }
505
506     /**
507      * show a link to reply to the current notice
508      *
509      * Should either do the reply in the current notice form (if available), or
510      * link out to the notice-posting form. A little flakey, doesn't always work.
511      *
512      * @return void
513      */
514     function showReplyLink()
515     {
516         if (common_logged_in()) {
517             $this->out->text(' ');
518             $reply_url = common_local_url('newnotice',
519                                           array('replyto' => $this->profile->getNickname(), 'inreplyto' => $this->notice->id));
520             $this->out->elementStart('a', array('href' => $reply_url,
521                                                 'class' => 'notice_reply',
522                                                 // TRANS: Link title in notice list item to reply to a notice.
523                                                 'title' => _('Reply to this notice.')));
524             // TRANS: Link text in notice list item to reply to a notice.
525             $this->out->text(_('Reply'));
526             $this->out->text(' ');
527             $this->out->element('span', 'notice_id', $this->notice->id);
528             $this->out->elementEnd('a');
529         }
530     }
531
532     /**
533      * if the user is the author, let them delete the notice
534      *
535      * @return void
536      */
537     function showDeleteLink()
538     {
539         $user = common_current_user();
540
541         $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
542
543         if (!empty($user) &&
544             ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
545             $this->out->text(' ');
546             $deleteurl = common_local_url('deletenotice',
547                                           array('notice' => $todel->id));
548             $this->out->element('a', array('href' => $deleteurl,
549                                            'class' => 'notice_delete',
550                                            // TRANS: Link title in notice list item to delete a notice.
551                                            'title' => _('Delete this notice from the timeline.')),
552                                            // TRANS: Link text in notice list item to delete a notice.
553                                            _('Delete'));
554         }
555     }
556
557     /**
558      * show the form to repeat a notice
559      *
560      * @return void
561      */
562     function showRepeatForm()
563     {
564         if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
565             $this->notice->scope == Notice::SITE_SCOPE) {
566             $user = common_current_user();
567             if (!empty($user) &&
568                 $user->id != $this->notice->profile_id) {
569                 $this->out->text(' ');
570                 $profile = $user->getProfile();
571                 if ($profile->hasRepeated($this->notice)) {
572                     $this->out->element('span', array('class' => 'repeated',
573                                                       // TRANS: Title for repeat form status in notice list when a notice has been repeated.
574                                                       'title' => _('Notice repeated.')),
575                                         // TRANS: Repeat form status in notice list when a notice has been repeated.
576                                         _('Repeated'));
577                 } else {
578                     $rf = new RepeatForm($this->out, $this->notice);
579                     $rf->show();
580                 }
581             }
582         }
583     }
584
585     /**
586      * finish the notice
587      *
588      * Close the last elements in the notice list item
589      *
590      * @return void
591      */
592     function showEnd()
593     {
594         if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
595             $this->out->elementEnd('li');
596             Event::handle('EndCloseNoticeListItemElement', array($this));
597         }
598     }
599
600     /**
601      * Get the notice in question
602      *
603      * For hooks, etc., this may be useful
604      *
605      * @return Notice The notice we're showing
606      */
607
608     function getNotice()
609     {
610         return $this->notice;
611     }
612 }