]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelist.php
Fix syntax errors that snuck in while documenting translations
[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         if ($this->notice->source) {
492             $this->out->text(' ');
493             $this->out->elementStart('span', 'source');
494             $this->out->text(_('from'));
495             $source_name = _($this->notice->source);
496             $this->out->text(' ');
497             switch ($this->notice->source) {
498              case 'web':
499              case 'xmpp':
500              case 'mail':
501              case 'omb':
502              case 'system':
503              case 'api':
504                 $this->out->element('span', 'device', $source_name);
505                 break;
506              default:
507
508                 $name = $source_name;
509                 $url  = null;
510
511                 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
512                     $ns = Notice_source::staticGet($this->notice->source);
513
514                     if ($ns) {
515                         $name = $ns->name;
516                         $url  = $ns->url;
517                     } else {
518                         $app = Oauth_application::staticGet('name', $this->notice->source);
519                         if ($app) {
520                             $name = $app->name;
521                             $url  = $app->source_url;
522                         }
523                     }
524                 }
525                 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
526
527                 if (!empty($name) && !empty($url)) {
528                     $this->out->elementStart('span', 'device');
529                     $this->out->element('a', array('href' => $url,
530                                                    'rel' => 'external',
531                                                    'title' => $title),
532                                         $name);
533                     $this->out->elementEnd('span');
534                 } else {
535                     $this->out->element('span', 'device', $name);
536                 }
537                 break;
538             }
539             $this->out->elementEnd('span');
540         }
541     }
542
543     /**
544      * show link to notice this notice is a reply to
545      *
546      * If this notice is a reply, show a link to the notice it is replying to. The
547      * heavy lifting for figuring out replies happens at save time.
548      *
549      * @return void
550      */
551
552     function showContext()
553     {
554         if ($this->notice->hasConversation()) {
555             $conv = Conversation::staticGet(
556                 'id',
557                 $this->notice->conversation
558             );
559             $convurl = $conv->uri;
560             if (!empty($convurl)) {
561                 $this->out->text(' ');
562                 $this->out->element(
563                     'a',
564                     array(
565                     'href' => $convurl.'#notice-'.$this->notice->id,
566                     'class' => 'response'),
567                     _('in context')
568                 );
569             } else {
570                 $msg = sprintf(
571                     "Couldn't find Conversation ID %d to make 'in context'"
572                     . "link for Notice ID %d",
573                     $this->notice->conversation,
574                     $this->notice->id
575                 );
576                 common_log(LOG_WARNING, $msg);
577             }
578         }
579     }
580
581     /**
582      * show a link to the author of repeat
583      *
584      * @return void
585      */
586
587     function showRepeat()
588     {
589         if (!empty($this->repeat)) {
590
591             $repeater = Profile::staticGet('id', $this->repeat->profile_id);
592
593             $attrs = array('href' => $repeater->profileurl,
594                            'class' => 'url');
595
596             if (!empty($repeater->fullname)) {
597                 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
598             }
599
600             $this->out->elementStart('span', 'repeat vcard');
601
602             $this->out->raw(_('Repeated by'));
603
604             $this->out->elementStart('a', $attrs);
605             $this->out->element('span', 'fn nickname', $repeater->nickname);
606             $this->out->elementEnd('a');
607
608             $this->out->elementEnd('span');
609         }
610     }
611
612     /**
613      * show a link to reply to the current notice
614      *
615      * Should either do the reply in the current notice form (if available), or
616      * link out to the notice-posting form. A little flakey, doesn't always work.
617      *
618      * @return void
619      */
620
621     function showReplyLink()
622     {
623         if (common_logged_in()) {
624             $this->out->text(' ');
625             $reply_url = common_local_url('newnotice',
626                                           array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
627             $this->out->elementStart('a', array('href' => $reply_url,
628                                                 'class' => 'notice_reply',
629                                                 'title' => _('Reply to this notice')));
630             $this->out->text(_('Reply'));
631             $this->out->text(' ');
632             $this->out->element('span', 'notice_id', $this->notice->id);
633             $this->out->elementEnd('a');
634         }
635     }
636
637     /**
638      * if the user is the author, let them delete the notice
639      *
640      * @return void
641      */
642
643     function showDeleteLink()
644     {
645         $user = common_current_user();
646
647         $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
648
649         if (!empty($user) &&
650             ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
651             $this->out->text(' ');
652             $deleteurl = common_local_url('deletenotice',
653                                           array('notice' => $todel->id));
654             $this->out->element('a', array('href' => $deleteurl,
655                                            'class' => 'notice_delete',
656                                            'title' => _('Delete this notice')), _('Delete'));
657         }
658     }
659
660     /**
661      * show the form to repeat a notice
662      *
663      * @return void
664      */
665
666     function showRepeatForm()
667     {
668         $user = common_current_user();
669         if ($user && $user->id != $this->notice->profile_id) {
670             $this->out->text(' ');
671             $profile = $user->getProfile();
672             if ($profile->hasRepeated($this->notice->id)) {
673                 $this->out->element('span', array('class' => 'repeated',
674                                                   'title' => _('Notice repeated')),
675                                             _('Repeated'));
676             } else {
677                 $rf = new RepeatForm($this->out, $this->notice);
678                 $rf->show();
679             }
680         }
681     }
682
683     /**
684      * finish the notice
685      *
686      * Close the last elements in the notice list item
687      *
688      * @return void
689      */
690
691     function showEnd()
692     {
693         $this->out->elementEnd('li');
694     }
695 }