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