]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelist.php
Merge branch 'testing' of gitorious.org:statusnet/mainline into 0.9.x
[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             // TRANS: Used in coordinates as abbreviation of north
430             $north = _('N');
431             // TRANS: Used in coordinates as abbreviation of south
432             $south = _('S');
433             // TRANS: Used in coordinates as abbreviation of east
434             $east = _('E');
435             // TRANS: Used in coordinates as abbreviation of west
436             $west = _('W');
437             $name = sprintf(
438                 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
439                 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
440                 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
441         }
442
443         $url  = $location->getUrl();
444
445         $this->out->text(' ');
446         $this->out->elementStart('span', array('class' => 'location'));
447         $this->out->text(_('at'));
448         $this->out->text(' ');
449         if (empty($url)) {
450             $this->out->element('abbr', array('class' => 'geo',
451                                               'title' => $latlon),
452                                 $name);
453         } else {
454             $xstr = new XMLStringer(false);
455             $xstr->elementStart('a', array('href' => $url,
456                                            'rel' => 'external'));
457             $xstr->element('abbr', array('class' => 'geo',
458                                          'title' => $latlon),
459                            $name);
460             $xstr->elementEnd('a');
461             $this->out->raw($xstr->getString());
462         }
463         $this->out->elementEnd('span');
464     }
465
466     function decimalDegreesToDMS($dec)
467     {
468
469         $vars = explode(".",$dec);
470         $deg = $vars[0];
471         $tempma = "0.".$vars[1];
472
473         $tempma = $tempma * 3600;
474         $min = floor($tempma / 60);
475         $sec = $tempma - ($min*60);
476
477         return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
478     }
479
480     /**
481      * Show the source of the notice
482      *
483      * Either the name (and link) of the API client that posted the notice,
484      * or one of other other channels.
485      *
486      * @return void
487      */
488
489     function showNoticeSource()
490     {
491         $ns = $this->notice->getSource();
492
493         if ($ns) {
494             $source_name = _($ns->code);
495             $this->out->text(' ');
496             $this->out->elementStart('span', 'source');
497             $this->out->text(_('from'));
498             $this->out->text(' ');
499
500             $name  = $source_name;
501             $url   = $ns->url;
502             $title = null;
503
504             if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
505                 $name = $source_name;
506                 $url  = $ns->url;
507             }
508             Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
509
510             // if $ns->name and $ns->url are populated we have
511             // configured a source attr somewhere
512             if (!empty($name) && !empty($url)) {
513
514                 $this->out->elementStart('span', 'device');
515
516                 $attrs = array(
517                     'href' => $url,
518                     'rel' => 'external'
519                 );
520
521                 if (!empty($title)) {
522                     $attrs['title'] = $title;
523                 }
524
525                 $this->out->element('a', $attrs, $name);
526                 $this->out->elementEnd('span');
527             } else {
528                 $this->out->element('span', 'device', $name);
529             }
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         if ($this->notice->hasConversation()) {
547             $conv = Conversation::staticGet(
548                 'id',
549                 $this->notice->conversation
550             );
551             $convurl = $conv->uri;
552             if (!empty($convurl)) {
553                 $this->out->text(' ');
554                 $this->out->element(
555                     'a',
556                     array(
557                     'href' => $convurl.'#notice-'.$this->notice->id,
558                     'class' => 'response'),
559                     _('in context')
560                 );
561             } else {
562                 $msg = sprintf(
563                     "Couldn't find Conversation ID %d to make 'in context'"
564                     . "link for Notice ID %d",
565                     $this->notice->conversation,
566                     $this->notice->id
567                 );
568                 common_log(LOG_WARNING, $msg);
569             }
570         }
571     }
572
573     /**
574      * show a link to the author of repeat
575      *
576      * @return void
577      */
578
579     function showRepeat()
580     {
581         if (!empty($this->repeat)) {
582
583             $repeater = Profile::staticGet('id', $this->repeat->profile_id);
584
585             $attrs = array('href' => $repeater->profileurl,
586                            'class' => 'url');
587
588             if (!empty($repeater->fullname)) {
589                 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
590             }
591
592             $this->out->elementStart('span', 'repeat vcard');
593
594             $this->out->raw(_('Repeated by'));
595
596             $this->out->elementStart('a', $attrs);
597             $this->out->element('span', 'fn nickname', $repeater->nickname);
598             $this->out->elementEnd('a');
599
600             $this->out->elementEnd('span');
601         }
602     }
603
604     /**
605      * show a link to reply to the current notice
606      *
607      * Should either do the reply in the current notice form (if available), or
608      * link out to the notice-posting form. A little flakey, doesn't always work.
609      *
610      * @return void
611      */
612
613     function showReplyLink()
614     {
615         if (common_logged_in()) {
616             $this->out->text(' ');
617             $reply_url = common_local_url('newnotice',
618                                           array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
619             $this->out->elementStart('a', array('href' => $reply_url,
620                                                 'class' => 'notice_reply',
621                                                 'title' => _('Reply to this notice')));
622             $this->out->text(_('Reply'));
623             $this->out->text(' ');
624             $this->out->element('span', 'notice_id', $this->notice->id);
625             $this->out->elementEnd('a');
626         }
627     }
628
629     /**
630      * if the user is the author, let them delete the notice
631      *
632      * @return void
633      */
634
635     function showDeleteLink()
636     {
637         $user = common_current_user();
638
639         $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
640
641         if (!empty($user) &&
642             ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
643             $this->out->text(' ');
644             $deleteurl = common_local_url('deletenotice',
645                                           array('notice' => $todel->id));
646             $this->out->element('a', array('href' => $deleteurl,
647                                            'class' => 'notice_delete',
648                                            'title' => _('Delete this notice')), _('Delete'));
649         }
650     }
651
652     /**
653      * show the form to repeat a notice
654      *
655      * @return void
656      */
657
658     function showRepeatForm()
659     {
660         $user = common_current_user();
661         if ($user && $user->id != $this->notice->profile_id) {
662             $this->out->text(' ');
663             $profile = $user->getProfile();
664             if ($profile->hasRepeated($this->notice->id)) {
665                 $this->out->element('span', array('class' => 'repeated',
666                                                   'title' => _('Notice repeated')),
667                                             _('Repeated'));
668             } else {
669                 $rf = new RepeatForm($this->out, $this->notice);
670                 $rf->show();
671             }
672         }
673     }
674
675     /**
676      * finish the notice
677      *
678      * Close the last elements in the notice list item
679      *
680      * @return void
681      */
682
683     function showEnd()
684     {
685         $this->out->elementEnd('li');
686     }
687 }