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