]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelist.php
Merge branch '0.9.x' into 1.0.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->element('span', array('class' => 'nickname fn'),
344                             $this->profile->nickname);
345     }
346
347     /**
348      * show the content of the notice
349      *
350      * Shows the content of the notice. This is pre-rendered for efficiency
351      * at save time. Some very old notices might not be pre-rendered, so
352      * they're rendered on the spot.
353      *
354      * @return void
355      */
356
357     function showContent()
358     {
359         // FIXME: URL, image, video, audio
360         $this->out->elementStart('p', array('class' => 'entry-content'));
361         if ($this->notice->rendered) {
362             $this->out->raw($this->notice->rendered);
363         } else {
364             // XXX: may be some uncooked notices in the DB,
365             // we cook them right now. This should probably disappear in future
366             // versions (>> 0.4.x)
367             $this->out->raw(common_render_content($this->notice->content, $this->notice));
368         }
369         $this->out->elementEnd('p');
370     }
371
372     /**
373      * show the link to the main page for the notice
374      *
375      * Displays a link to the page for a notice, with "relative" time. Tries to
376      * get remote notice URLs correct, but doesn't always succeed.
377      *
378      * @return void
379      */
380
381     function showNoticeLink()
382     {
383         $noticeurl = $this->notice->bestUrl();
384
385         // above should always return an URL
386
387         assert(!empty($noticeurl));
388
389         $this->out->elementStart('a', array('rel' => 'bookmark',
390                                             'class' => 'timestamp',
391                                             'href' => $noticeurl));
392         $dt = common_date_iso8601($this->notice->created);
393         $this->out->element('abbr', array('class' => 'published',
394                                           'title' => $dt),
395                             common_date_string($this->notice->created));
396         $this->out->elementEnd('a');
397     }
398
399     /**
400      * show the notice location
401      *
402      * shows the notice location in the correct language.
403      *
404      * If an URL is available, makes a link. Otherwise, just a span.
405      *
406      * @return void
407      */
408
409     function showNoticeLocation()
410     {
411         $id = $this->notice->id;
412
413         $location = $this->notice->getLocation();
414
415         if (empty($location)) {
416             return;
417         }
418
419         $name = $location->getName();
420
421         $lat = $this->notice->lat;
422         $lon = $this->notice->lon;
423         $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
424
425         if (empty($name)) {
426             $latdms = $this->decimalDegreesToDMS(abs($lat));
427             $londms = $this->decimalDegreesToDMS(abs($lon));
428             $name = sprintf(
429                 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
430                 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0?_('N'):_('S')),
431                 $londms['deg'],$londms['min'], $londms['sec'],($lon>0?_('E'):_('W')));
432         }
433
434         $url  = $location->getUrl();
435
436         $this->out->text(' ');
437         $this->out->elementStart('span', array('class' => 'location'));
438         $this->out->text(_('at'));
439         $this->out->text(' ');
440         if (empty($url)) {
441             $this->out->element('abbr', array('class' => 'geo',
442                                               'title' => $latlon),
443                                 $name);
444         } else {
445             $this->out->elementStart('a', array('href' => $url));
446             $this->out->element('abbr', array('class' => 'geo',
447                                               'title' => $latlon),
448                                 $name);
449             $this->out->elementEnd('a');
450         }
451         $this->out->elementEnd('span');
452     }
453
454     function decimalDegreesToDMS($dec)
455     {
456
457         $vars = explode(".",$dec);
458         $deg = $vars[0];
459         $tempma = "0.".$vars[1];
460
461         $tempma = $tempma * 3600;
462         $min = floor($tempma / 60);
463         $sec = $tempma - ($min*60);
464
465         return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
466     }
467
468     /**
469      * Show the source of the notice
470      *
471      * Either the name (and link) of the API client that posted the notice,
472      * or one of other other channels.
473      *
474      * @return void
475      */
476
477     function showNoticeSource()
478     {
479         if ($this->notice->source) {
480             $this->out->text(' ');
481             $this->out->elementStart('span', 'source');
482             $this->out->text(_('from'));
483             $source_name = _($this->notice->source);
484             $this->out->text(' ');
485             switch ($this->notice->source) {
486              case 'web':
487              case 'xmpp':
488              case 'mail':
489              case 'omb':
490              case 'system':
491              case 'api':
492                 $this->out->element('span', 'device', $source_name);
493                 break;
494              default:
495
496                 $name = $source_name;
497                 $url  = null;
498
499                 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
500                     $ns = Notice_source::staticGet($this->notice->source);
501
502                     if ($ns) {
503                         $name = $ns->name;
504                         $url  = $ns->url;
505                     } else {
506                         $app = Oauth_application::staticGet('name', $this->notice->source);
507                         if ($app) {
508                             $name = $app->name;
509                             $url  = $app->source_url;
510                         }
511                     }
512                 }
513                 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
514
515                 if (!empty($name) && !empty($url)) {
516                     $this->out->elementStart('span', 'device');
517                     $this->out->element('a', array('href' => $url,
518                                                    'rel' => 'external',
519                                                    'title' => $title),
520                                         $name);
521                     $this->out->elementEnd('span');
522                 } else {
523                     $this->out->element('span', 'device', $name);
524                 }
525                 break;
526             }
527             $this->out->elementEnd('span');
528         }
529     }
530
531     /**
532      * show link to notice this notice is a reply to
533      *
534      * If this notice is a reply, show a link to the notice it is replying to. The
535      * heavy lifting for figuring out replies happens at save time.
536      *
537      * @return void
538      */
539
540     function showContext()
541     {
542         $hasConversation = false;
543         if( !empty($this->notice->conversation)
544             && $this->notice->conversation != $this->notice->id){
545             $hasConversation = true;
546         }else{
547             $conversation = Notice::conversationStream($this->notice->id, 1, 1);
548             if($conversation->N > 0){
549                 $hasConversation = true;
550             }
551         }
552         if ($hasConversation){
553             $this->out->text(' ');
554             $convurl = common_local_url('conversation',
555                                          array('id' => $this->notice->conversation));
556             $this->out->element('a', array('href' => $convurl.'#notice-'.$this->notice->id,
557                                            'class' => 'response'),
558                                 _('in context'));
559         }
560     }
561
562     /**
563      * show a link to the author of repeat
564      *
565      * @return void
566      */
567
568     function showRepeat()
569     {
570         if (!empty($this->repeat)) {
571
572             $repeater = Profile::staticGet('id', $this->repeat->profile_id);
573
574             $attrs = array('href' => $repeater->profileurl,
575                            'class' => 'url');
576
577             if (!empty($repeater->fullname)) {
578                 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
579             }
580
581             $this->out->elementStart('span', 'repeat vcard');
582
583             $this->out->raw(_('Repeated by'));
584
585             $this->out->elementStart('a', $attrs);
586             $this->out->element('span', 'fn nickname', $repeater->nickname);
587             $this->out->elementEnd('a');
588
589             $this->out->elementEnd('span');
590         }
591     }
592
593     /**
594      * show a link to reply to the current notice
595      *
596      * Should either do the reply in the current notice form (if available), or
597      * link out to the notice-posting form. A little flakey, doesn't always work.
598      *
599      * @return void
600      */
601
602     function showReplyLink()
603     {
604         if (common_logged_in()) {
605             $this->out->text(' ');
606             $reply_url = common_local_url('newnotice',
607                                           array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
608             $this->out->elementStart('a', array('href' => $reply_url,
609                                                 'class' => 'notice_reply',
610                                                 'title' => _('Reply to this notice')));
611             $this->out->text(_('Reply'));
612             $this->out->text(' ');
613             $this->out->element('span', 'notice_id', $this->notice->id);
614             $this->out->elementEnd('a');
615         }
616     }
617
618     /**
619      * if the user is the author, let them delete the notice
620      *
621      * @return void
622      */
623
624     function showDeleteLink()
625     {
626         $user = common_current_user();
627
628         $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
629
630         if (!empty($user) &&
631             ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
632             $this->out->text(' ');
633             $deleteurl = common_local_url('deletenotice',
634                                           array('notice' => $todel->id));
635             $this->out->element('a', array('href' => $deleteurl,
636                                            'class' => 'notice_delete',
637                                            'title' => _('Delete this notice')), _('Delete'));
638         }
639     }
640
641     /**
642      * show the form to repeat a notice
643      *
644      * @return void
645      */
646
647     function showRepeatForm()
648     {
649         $user = common_current_user();
650         if ($user && $user->id != $this->notice->profile_id) {
651             $this->out->text(' ');
652             $profile = $user->getProfile();
653             if ($profile->hasRepeated($this->notice->id)) {
654                 $this->out->element('span', array('class' => 'repeated',
655                                                   'title' => _('Notice repeated')),
656                                             _('Repeated'));
657             } else {
658                 $rf = new RepeatForm($this->out, $this->notice);
659                 $rf->show();
660             }
661         }
662     }
663
664     /**
665      * finish the notice
666      *
667      * Close the last elements in the notice list item
668      *
669      * @return void
670      */
671
672     function showEnd()
673     {
674         $this->out->elementEnd('li');
675     }
676 }