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