]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelistitem.php
Merge branch '1.0.x' into testing
[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             $this->out->elementStart('li', array('class' => 'hentry notice',
174                                                  'id' => 'notice-' . $id));
175             Event::handle('EndOpenNoticeListItemElement', array($this));
176         }
177     }
178
179     /**
180      * show the "favorite" form
181      *
182      * @return void
183      */
184
185     function showFaveForm()
186     {
187         if (Event::handle('StartShowFaveForm', array($this))) {
188             $user = common_current_user();
189             if ($user) {
190                 if ($user->hasFave($this->notice)) {
191                     $disfavor = new DisfavorForm($this->out, $this->notice);
192                     $disfavor->show();
193                 } else {
194                     $favor = new FavorForm($this->out, $this->notice);
195                     $favor->show();
196                 }
197             }
198             Event::handle('EndShowFaveForm', array($this));
199         }
200     }
201
202     /**
203      * show the author of a notice
204      *
205      * By default, this shows the avatar and (linked) nickname of the author.
206      *
207      * @return void
208      */
209
210     function showAuthor()
211     {
212         $this->out->elementStart('span', 'vcard author');
213         $attrs = array('href' => $this->profile->profileurl,
214                        'class' => 'url');
215         if (!empty($this->profile->fullname)) {
216             $attrs['title'] = $this->profile->getFancyName();
217         }
218         $this->out->elementStart('a', $attrs);
219         $this->showAvatar();
220         $this->out->text(' ');
221         $this->showNickname();
222         $this->out->elementEnd('a');
223         $this->out->elementEnd('span');
224     }
225
226     /**
227      * show the avatar of the notice's author
228      *
229      * This will use the default avatar if no avatar is assigned for the author.
230      * It makes a link to the author's profile.
231      *
232      * @return void
233      */
234
235     function showAvatar()
236     {
237         $avatar_size = $this->avatarSize();
238
239         $avatar = $this->profile->getAvatar($avatar_size);
240
241         $this->out->element('img', array('src' => ($avatar) ?
242                                          $avatar->displayUrl() :
243                                          Avatar::defaultImage($avatar_size),
244                                          'class' => 'avatar photo',
245                                          'width' => $avatar_size,
246                                          'height' => $avatar_size,
247                                          'alt' =>
248                                          ($this->profile->fullname) ?
249                                          $this->profile->fullname :
250                                          $this->profile->nickname));
251     }
252
253     function avatarSize()
254     {
255         return AVATAR_STREAM_SIZE;
256     }
257
258     /**
259      * show the nickname of the author
260      *
261      * Links to the author's profile page
262      *
263      * @return void
264      */
265
266     function showNickname()
267     {
268         $this->out->raw('<span class="nickname fn">' .
269                         htmlspecialchars($this->profile->nickname) .
270                         '</span>');
271     }
272
273     /**
274      * show the content of the notice
275      *
276      * Shows the content of the notice. This is pre-rendered for efficiency
277      * at save time. Some very old notices might not be pre-rendered, so
278      * they're rendered on the spot.
279      *
280      * @return void
281      */
282
283     function showContent()
284     {
285         // FIXME: URL, image, video, audio
286         $this->out->elementStart('p', array('class' => 'entry-content'));
287         if ($this->notice->rendered) {
288             $this->out->raw($this->notice->rendered);
289         } else {
290             // XXX: may be some uncooked notices in the DB,
291             // we cook them right now. This should probably disappear in future
292             // versions (>> 0.4.x)
293             $this->out->raw(common_render_content($this->notice->content, $this->notice));
294         }
295         $this->out->elementEnd('p');
296     }
297
298     function showNoticeAttachments() {
299         if (common_config('attachments', 'show_thumbs')) {
300             $al = new InlineAttachmentList($this->notice, $this->out);
301             $al->show();
302         }
303     }
304
305     /**
306      * show the link to the main page for the notice
307      *
308      * Displays a link to the page for a notice, with "relative" time. Tries to
309      * get remote notice URLs correct, but doesn't always succeed.
310      *
311      * @return void
312      */
313
314     function showNoticeLink()
315     {
316         $noticeurl = $this->notice->bestUrl();
317
318         // above should always return an URL
319
320         assert(!empty($noticeurl));
321
322         $this->out->elementStart('a', array('rel' => 'bookmark',
323                                             'class' => 'timestamp',
324                                             'href' => $noticeurl));
325         $dt = common_date_iso8601($this->notice->created);
326         $this->out->element('abbr', array('class' => 'published',
327                                           'title' => $dt),
328                             common_date_string($this->notice->created));
329         $this->out->elementEnd('a');
330     }
331
332     /**
333      * show the notice location
334      *
335      * shows the notice location in the correct language.
336      *
337      * If an URL is available, makes a link. Otherwise, just a span.
338      *
339      * @return void
340      */
341
342     function showNoticeLocation()
343     {
344         $id = $this->notice->id;
345
346         $location = $this->notice->getLocation();
347
348         if (empty($location)) {
349             return;
350         }
351
352         $name = $location->getName();
353
354         $lat = $this->notice->lat;
355         $lon = $this->notice->lon;
356         $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
357
358         if (empty($name)) {
359             $latdms = $this->decimalDegreesToDMS(abs($lat));
360             $londms = $this->decimalDegreesToDMS(abs($lon));
361             // TRANS: Used in coordinates as abbreviation of north
362             $north = _('N');
363             // TRANS: Used in coordinates as abbreviation of south
364             $south = _('S');
365             // TRANS: Used in coordinates as abbreviation of east
366             $east = _('E');
367             // TRANS: Used in coordinates as abbreviation of west
368             $west = _('W');
369             $name = sprintf(
370                 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
371                 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
372                 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
373         }
374
375         $url  = $location->getUrl();
376
377         $this->out->text(' ');
378         $this->out->elementStart('span', array('class' => 'location'));
379         $this->out->text(_('at'));
380         $this->out->text(' ');
381         if (empty($url)) {
382             $this->out->element('abbr', array('class' => 'geo',
383                                               'title' => $latlon),
384                                 $name);
385         } else {
386             $xstr = new XMLStringer(false);
387             $xstr->elementStart('a', array('href' => $url,
388                                            'rel' => 'external'));
389             $xstr->element('abbr', array('class' => 'geo',
390                                          'title' => $latlon),
391                            $name);
392             $xstr->elementEnd('a');
393             $this->out->raw($xstr->getString());
394         }
395         $this->out->elementEnd('span');
396     }
397
398     /**
399      * @param number $dec decimal degrees
400      * @return array split into 'deg', 'min', and 'sec'
401      */
402     function decimalDegreesToDMS($dec)
403     {
404         $deg = intval($dec);
405         $tempma = abs($dec) - abs($deg);
406
407         $tempma = $tempma * 3600;
408         $min = floor($tempma / 60);
409         $sec = $tempma - ($min*60);
410
411         return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
412     }
413
414     /**
415      * Show the source of the notice
416      *
417      * Either the name (and link) of the API client that posted the notice,
418      * or one of other other channels.
419      *
420      * @return void
421      */
422
423     function showNoticeSource()
424     {
425         $ns = $this->notice->getSource();
426
427         if ($ns) {
428             $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _('web')) : _($ns->name);
429             $this->out->text(' ');
430             $this->out->elementStart('span', 'source');
431             // FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
432             $this->out->text(_('from'));
433             $this->out->text(' ');
434
435             $name  = $source_name;
436             $url   = $ns->url;
437             $title = null;
438
439             if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
440                 $name = $source_name;
441                 $url  = $ns->url;
442             }
443             Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
444
445             // if $ns->name and $ns->url are populated we have
446             // configured a source attr somewhere
447             if (!empty($name) && !empty($url)) {
448
449                 $this->out->elementStart('span', 'device');
450
451                 $attrs = array(
452                     'href' => $url,
453                     'rel' => 'external'
454                 );
455
456                 if (!empty($title)) {
457                     $attrs['title'] = $title;
458                 }
459
460                 $this->out->element('a', $attrs, $name);
461                 $this->out->elementEnd('span');
462             } else {
463                 $this->out->element('span', 'device', $name);
464             }
465
466             $this->out->elementEnd('span');
467         }
468     }
469
470     /**
471      * show link to notice this notice is a reply to
472      *
473      * If this notice is a reply, show a link to the notice it is replying to. The
474      * heavy lifting for figuring out replies happens at save time.
475      *
476      * @return void
477      */
478
479     function showContext()
480     {
481         if ($this->notice->hasConversation()) {
482             $conv = Conversation::staticGet(
483                 'id',
484                 $this->notice->conversation
485             );
486             $convurl = $conv->uri;
487             if (!empty($convurl)) {
488                 $this->out->text(' ');
489                 $this->out->element(
490                     'a',
491                     array(
492                     'href' => $convurl.'#notice-'.$this->notice->id,
493                     'class' => 'response'),
494                     _('in context')
495                 );
496             } else {
497                 $msg = sprintf(
498                     "Couldn't find Conversation ID %d to make 'in context'"
499                     . "link for Notice ID %d",
500                     $this->notice->conversation,
501                     $this->notice->id
502                 );
503                 common_log(LOG_WARNING, $msg);
504             }
505         }
506     }
507
508     /**
509      * show a link to the author of repeat
510      *
511      * @return void
512      */
513
514     function showRepeat()
515     {
516         if (!empty($this->repeat)) {
517
518             $repeater = Profile::staticGet('id', $this->repeat->profile_id);
519
520             $attrs = array('href' => $repeater->profileurl,
521                            'class' => 'url');
522
523             if (!empty($repeater->fullname)) {
524                 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
525             }
526
527             $this->out->elementStart('span', 'repeat vcard');
528
529             $this->out->raw(_('Repeated by'));
530
531             $this->out->elementStart('a', $attrs);
532             $this->out->element('span', 'fn nickname', $repeater->nickname);
533             $this->out->elementEnd('a');
534
535             $this->out->elementEnd('span');
536         }
537     }
538
539     /**
540      * show a link to reply to the current notice
541      *
542      * Should either do the reply in the current notice form (if available), or
543      * link out to the notice-posting form. A little flakey, doesn't always work.
544      *
545      * @return void
546      */
547
548     function showReplyLink()
549     {
550         if (common_logged_in()) {
551             $this->out->text(' ');
552             $reply_url = common_local_url('newnotice',
553                                           array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
554             $this->out->elementStart('a', array('href' => $reply_url,
555                                                 'class' => 'notice_reply',
556                                                 'title' => _('Reply to this notice')));
557             $this->out->text(_('Reply'));
558             $this->out->text(' ');
559             $this->out->element('span', 'notice_id', $this->notice->id);
560             $this->out->elementEnd('a');
561         }
562     }
563
564     /**
565      * if the user is the author, let them delete the notice
566      *
567      * @return void
568      */
569
570     function showDeleteLink()
571     {
572         $user = common_current_user();
573
574         $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
575
576         if (!empty($user) &&
577             ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
578             $this->out->text(' ');
579             $deleteurl = common_local_url('deletenotice',
580                                           array('notice' => $todel->id));
581             $this->out->element('a', array('href' => $deleteurl,
582                                            'class' => 'notice_delete',
583                                            'title' => _('Delete this notice')), _('Delete'));
584         }
585     }
586
587     /**
588      * show the form to repeat a notice
589      *
590      * @return void
591      */
592
593     function showRepeatForm()
594     {
595         $user = common_current_user();
596         if ($user && $user->id != $this->notice->profile_id) {
597             $this->out->text(' ');
598             $profile = $user->getProfile();
599             if ($profile->hasRepeated($this->notice->id)) {
600                 $this->out->element('span', array('class' => 'repeated',
601                                                   'title' => _('Notice repeated')),
602                                             _('Repeated'));
603             } else {
604                 $rf = new RepeatForm($this->out, $this->notice);
605                 $rf->show();
606             }
607         }
608     }
609
610     /**
611      * finish the notice
612      *
613      * Close the last elements in the notice list item
614      *
615      * @return void
616      */
617
618     function showEnd()
619     {
620         if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
621             $this->out->elementEnd('li');
622             Event::handle('EndCloseNoticeListItemElement', array($this));
623         }
624     }
625 }