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