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