]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/threadednoticelist.php
Initial move towards microformats2
[quix0rs-gnu-social.git] / lib / threadednoticelist.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    Brion Vibber <brion@status.net>
25  * @copyright 2011 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
31
32 /**
33  * widget for displaying a list of notices
34  *
35  * There are a number of actions that display a list of notices, in
36  * reverse chronological order. This widget abstracts out most of the
37  * code for UI for notice lists. It's overridden to hide some
38  * data for e.g. the profile page.
39  *
40  * @category UI
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  * @see      Notice
46  * @see      NoticeListItem
47  * @see      ProfileNoticeList
48  */
49 class ThreadedNoticeList extends NoticeList
50 {
51     protected $userProfile;
52
53     function __construct(Notice $notice, HTMLOutputter $out=null, $profile=-1)
54     {
55         parent::__construct($notice, $out);
56         if (is_int($profile) && $profile == -1) {
57             $profile = Profile::current();
58         }
59         $this->userProfile = $profile;
60     }
61
62     /**
63      * show the list of notices
64      *
65      * "Uses up" the stream by looping through it. So, probably can't
66      * be called twice on the same list.
67      *
68      * @return int count of notices listed.
69      */
70     function show()
71     {
72         $this->out->elementStart('div', array('id' =>'notices_primary'));
73         // TRANS: Header for Notices section.
74         $this->out->element('h2', null, _m('HEADER','Notices'));
75         $this->out->elementStart('ol', array('class' => 'notices threaded-notices xoxo'));
76
77                 $notices = $this->notice->fetchAll();
78                 $total = count($notices);
79                 $notices = array_slice($notices, 0, NOTICES_PER_PAGE);
80                 
81         $allnotices = self::_allNotices($notices);
82         self::prefill($allnotices);
83         
84         $conversations = array();
85         
86         foreach ($notices as $notice) {
87
88             // Collapse repeats into their originals...
89             
90             if ($notice->repeat_of) {
91                 $orig = Notice::getKV('id', $notice->repeat_of);
92                 if ($orig instanceof Notice) {
93                     $notice = $orig;
94                 }
95             }
96             $convo = $notice->conversation;
97             if (!empty($conversations[$convo])) {
98                 // Seen this convo already -- skip!
99                 continue;
100             }
101             $conversations[$convo] = true;
102
103             // Get the convo's root notice
104             $root = $notice->conversationRoot($this->userProfile);
105             if ($root instanceof Notice) {
106                 $notice = $root;
107             }
108
109             try {
110                 $item = $this->newListItem($notice);
111                 $item->show();
112             } catch (Exception $e) {
113                 // we log exceptions and continue
114                 common_log(LOG_ERR, $e->getMessage());
115                 continue;
116             }
117         }
118
119         $this->out->elementEnd('ol');
120         $this->out->elementEnd('div');
121
122         return $total;
123     }
124
125     function _allNotices($notices)
126     {
127         $convId = array();
128         foreach ($notices as $notice) {
129             $convId[] = $notice->conversation;
130         }
131         $convId = array_unique($convId);
132         $allMap = Notice::listGet('conversation', $convId);
133         $allArray = array();
134         foreach ($allMap as $convId => $convNotices) {
135             $allArray = array_merge($allArray, $convNotices);
136         }
137         return $allArray;
138     }
139
140     /**
141      * returns a new list item for the current notice
142      *
143      * Recipe (factory?) method; overridden by sub-classes to give
144      * a different list item class.
145      *
146      * @param Notice $notice the current notice
147      *
148      * @return NoticeListItem a list item for displaying the notice
149      */
150     function newListItem(Notice $notice)
151     {
152         return new ThreadedNoticeListItem($notice, $this->out, $this->userProfile);
153     }
154 }
155
156 /**
157  * widget for displaying a single notice
158  *
159  * This widget has the core smarts for showing a single notice: what to display,
160  * where, and under which circumstances. Its key method is show(); this is a recipe
161  * that calls all the other show*() methods to build up a single notice. The
162  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
163  * author info (since that's implicit by the data in the page).
164  *
165  * @category UI
166  * @package  StatusNet
167  * @author   Evan Prodromou <evan@status.net>
168  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
169  * @link     http://status.net/
170  * @see      NoticeList
171  * @see      ProfileNoticeListItem
172  */
173 class ThreadedNoticeListItem extends NoticeListItem
174 {
175     protected $userProfile = null;
176
177     function __construct(Notice $notice, HTMLOutputter $out=null, $profile=null)
178     {
179         parent::__construct($notice, $out);
180         $this->userProfile = $profile;
181     }
182
183     function initialItems()
184     {
185         return 3;
186     }
187
188     function showContext()
189     {
190         // Silence!
191     }
192
193     /**
194      * finish the notice
195      *
196      * Close the last elements in the notice list item
197      *
198      * @return void
199      */
200     function showEnd()
201     {
202         $max = $this->initialItems();
203         if (!$this->repeat) {
204             $stream = new ConversationNoticeStream($this->notice->conversation, $this->userProfile);
205             $notice = $stream->getNotices(0, $max + 2);
206             $notices = array();
207             $cnt = 0;
208             $moreCutoff = null;
209             while ($notice->fetch()) {
210                 if (Event::handle('StartAddNoticeReply', array($this, $this->notice, $notice))) {
211                     // Don't list repeats as separate notices in a conversation
212                     if (!empty($notice->repeat_of)) {
213                         continue;
214                     }
215
216                     if ($notice->id == $this->notice->id) {
217                         // Skip!
218                         continue;
219                     }
220                     $cnt++;
221                     if ($cnt > $max) {
222                         // boo-yah
223                         $moreCutoff = clone($notice);
224                         break;
225                     }
226                     $notices[] = clone($notice); // *grumble* inefficient as hell
227                     Event::handle('EndAddNoticeReply', array($this, $this->notice, $notice));
228                 }
229             }
230
231             if (Event::handle('StartShowThreadedNoticeTail', array($this, $this->notice, &$notices))) {
232                 $this->out->elementStart('ul', 'notices threaded-replies xoxo');
233
234                 $item = new ThreadedNoticeListFavesItem($this->notice, $this->out);
235                 $hasFaves = $item->show();
236
237                 $item = new ThreadedNoticeListRepeatsItem($this->notice, $this->out);
238                 $hasRepeats = $item->show();
239
240                 if ($notices) {
241
242                     if ($moreCutoff) {
243                         $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out, count($notices));
244                         $item->show();
245                     }
246                     foreach (array_reverse($notices) as $notice) {
247                         if (Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice))) {
248                             $item = new ThreadedNoticeListSubItem($notice, $this->notice, $this->out);
249                             $item->show();
250                             Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice));
251                         }
252                     }
253                 }
254
255                 if ($notices || $hasFaves || $hasRepeats) {
256                     // @fixme do a proper can-post check that's consistent
257                     // with the JS side
258                     if (common_current_user()) {
259                         $item = new ThreadedNoticeListReplyItem($this->notice, $this->out);
260                         $item->show();
261                     }
262                 }
263                 $this->out->elementEnd('ul');
264                 Event::handle('EndShowThreadedNoticeTail', array($this, $this->notice, $notices));
265             }
266         }
267
268         parent::showEnd();
269     }
270 }
271
272 // @todo FIXME: needs documentation.
273 class ThreadedNoticeListSubItem extends NoticeListItem
274 {
275     protected $root = null;
276
277     function __construct(Notice $notice, $root, $out)
278     {
279         $this->root = $root;
280         parent::__construct($notice, $out);
281     }
282
283     function avatarSize()
284     {
285         return AVATAR_STREAM_SIZE; // @fixme would like something in between
286     }
287
288     function showNoticeLocation()
289     {
290         //
291     }
292
293     function showNoticeSource()
294     {
295         //
296     }
297
298     function showContext()
299     {
300         //
301     }
302
303     function getReplyProfiles()
304     {
305         $all = parent::getReplyProfiles();
306
307         $profiles = array();
308
309         $rootAuthor = $this->root->getProfile();
310
311         foreach ($all as $profile) {
312             if ($profile->id != $rootAuthor->id) {
313                 $profiles[] = $profile;
314             }
315         }
316
317         return $profiles;
318     }
319
320     function showEnd()
321     {
322         $item = new ThreadedNoticeListInlineFavesItem($this->notice, $this->out);
323         $hasFaves = $item->show();
324         $item = new ThreadedNoticeListInlineRepeatsItem($this->notice, $this->out);
325         $hasRepeats = $item->show();
326         parent::showEnd();
327     }
328 }
329
330 /**
331  * Placeholder for loading more replies...
332  */
333 class ThreadedNoticeListMoreItem extends NoticeListItem
334 {
335     protected $cnt;
336
337     function __construct(Notice $notice, HTMLOutputter $out, $cnt)
338     {
339         parent::__construct($notice, $out);
340         $this->cnt = $cnt;
341     }
342
343     /**
344      * recipe function for displaying a single notice.
345      *
346      * This uses all the other methods to correctly display a notice. Override
347      * it or one of the others to fine-tune the output.
348      *
349      * @return void
350      */
351     function show()
352     {
353         $this->showStart();
354         $this->showMiniForm();
355         $this->showEnd();
356     }
357
358     /**
359      * start a single notice.
360      *
361      * @return void
362      */
363     function showStart()
364     {
365         $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
366     }
367
368     function showMiniForm()
369     {
370         $id = $this->notice->conversation;
371         $url = common_local_url('conversation', array('id' => $id));
372
373         $n = Conversation::noticeCount($id) - 1;
374
375         // TRANS: Link to show replies for a notice.
376         // TRANS: %d is the number of replies to a notice and used for plural.
377         $msg = sprintf(_m('Show reply', 'Show all %d replies', $n), $n);
378
379         $this->out->element('a', array('href' => $url), $msg);
380     }
381 }
382
383 /**
384  * Placeholder for reply form...
385  * Same as get added at runtime via SN.U.NoticeInlineReplyPlaceholder
386  */
387 class ThreadedNoticeListReplyItem extends NoticeListItem
388 {
389     /**
390      * recipe function for displaying a single notice.
391      *
392      * This uses all the other methods to correctly display a notice. Override
393      * it or one of the others to fine-tune the output.
394      *
395      * @return void
396      */
397     function show()
398     {
399         $this->showStart();
400         $this->showMiniForm();
401         $this->showEnd();
402     }
403
404     /**
405      * start a single notice.
406      *
407      * @return void
408      */
409     function showStart()
410     {
411         $this->out->elementStart('li', array('class' => 'notice-reply-placeholder'));
412     }
413
414     function showMiniForm()
415     {
416         $this->out->element('input', array('class' => 'placeholder',
417                                            // TRANS: Field label for reply mini form.
418                                            'value' => _('Write a reply...')));
419     }
420 }
421
422 /**
423  * Placeholder for showing faves...
424  */
425 abstract class NoticeListActorsItem extends NoticeListItem
426 {
427     /**
428      * @return array of profile IDs
429      */
430     abstract function getProfiles();
431
432     abstract function getListMessage($count, $you);
433
434     function show()
435     {
436         $links = array();
437         $you = false;
438         $cur = common_current_user();
439         foreach ($this->getProfiles() as $id) {
440             if ($cur && $cur->id == $id) {
441                 $you = true;
442                 // TRANS: Reference to the logged in user in favourite list.
443                 array_unshift($links, _m('FAVELIST', 'You'));
444             } else {
445                 $profile = Profile::getKV('id', $id);
446                 if ($profile) {
447                     $links[] = sprintf('<a href="%s">%s</a>',
448                                        htmlspecialchars($profile->profileurl),
449                                        htmlspecialchars($profile->getBestName()));
450                 }
451             }
452         }
453
454         if ($links) {
455             $count = count($links);
456             $msg = $this->getListMessage($count, $you);
457             $out = sprintf($msg, $this->magicList($links));
458
459             $this->showStart();
460             $this->out->raw($out);
461             $this->showEnd();
462             return $count;
463         } else {
464             return 0;
465         }
466     }
467
468     function magicList($items)
469     {
470         if (count($items) == 0) {
471             return '';
472         } else if (count($items) == 1) {
473             return $items[0];
474         } else {
475             $first = array_slice($items, 0, -1);
476             $last = array_slice($items, -1, 1);
477             // TRANS: Separator in list of user names like "Jim, Bob, Mary".
478             $separator = _(', ');
479             // TRANS: For building a list such as "Jim, Bob, Mary and 5 others like this".
480             // TRANS: %1$s is a list of users, separated by a separator (default: ", "), %2$s is the last user in the list.
481             return sprintf(_m('FAVELIST', '%1$s and %2$s'), implode($separator, $first), implode($separator, $last));
482         }
483     }
484 }
485
486 /**
487  * Placeholder for showing faves...
488  */
489 class ThreadedNoticeListFavesItem extends NoticeListActorsItem
490 {
491     function getProfiles()
492     {
493         $faves = $this->notice->getFaves();
494         $profiles = array();
495         foreach ($faves as $fave) {
496             $profiles[] = $fave->user_id;
497         }
498         return $profiles;
499     }
500
501     function magicList($items)
502     {
503         if (count($items) > 4) {
504             return parent::magicList(array_slice($items, 0, 3));
505         } else {
506             return parent::magicList($items);
507         }
508     }
509
510     function getListMessage($count, $you)
511     {
512         if ($count == 1 && $you) {
513             // darn first person being different from third person!
514             // TRANS: List message for notice favoured by logged in user.
515             return _m('FAVELIST', 'You like this.');
516         } else if ($count > 4) {
517             // TRANS: List message for when more than 4 people like something.
518             // TRANS: %%s is a list of users liking a notice, %d is the number over 4 that like the notice.
519             // TRANS: Plural is decided on the total number of users liking the notice (count of %%s + %d).
520             return sprintf(_m('%%s and %d others like this.',
521                               '%%s and %d others like this.',
522                               $count),
523                            $count - 3);
524         } else {
525             // TRANS: List message for favoured notices.
526             // TRANS: %%s is a list of users liking a notice.
527             // TRANS: Plural is based on the number of of users that have favoured a notice.
528             return sprintf(_m('%%s likes this.',
529                               '%%s like this.',
530                               $count),
531                            $count);
532         }
533     }
534
535     function showStart()
536     {
537         $this->out->elementStart('li', array('class' => 'notice-data notice-faves'));
538     }
539
540     function showEnd()
541     {
542         $this->out->elementEnd('li');
543     }
544 }
545
546 // @todo FIXME: needs documentation.
547 class ThreadedNoticeListInlineFavesItem extends ThreadedNoticeListFavesItem
548 {
549     function showStart()
550     {
551         $this->out->elementStart('div', array('class' => 'e-content notice-faves'));
552     }
553
554     function showEnd()
555     {
556         $this->out->elementEnd('div');
557     }
558 }
559
560 /**
561  * Placeholder for showing repeats...
562  */
563 class ThreadedNoticeListRepeatsItem extends NoticeListActorsItem
564 {
565     function getProfiles()
566     {
567         $repeats = $this->notice->getRepeats();
568
569         $profiles = array();
570
571         foreach ($repeats as $rep) {
572             $profiles[] = $rep->profile_id;
573         }
574
575         return $profiles;
576     }
577
578     function magicList($items)
579     {
580         if (count($items) > 4) {
581             return parent::magicList(array_slice($items, 0, 3));
582         } else {
583             return parent::magicList($items);
584         }
585     }
586
587     function getListMessage($count, $you)
588     {
589         if ($count == 1 && $you) {
590             // darn first person being different from third person!
591             // TRANS: List message for notice repeated by logged in user.
592             return _m('REPEATLIST', 'You repeated this.');
593         } else if ($count > 4) {
594             // TRANS: List message for when more than 4 people repeat something.
595             // TRANS: %%s is a list of users liking a notice, %d is the number over 4 that like the notice.
596             // TRANS: Plural is decided on the total number of users liking the notice (count of %%s + %d).
597             return sprintf(_m('%%s and %d other repeated this.',
598                               '%%s and %d others repeated this.',
599                               $count - 3),
600                            $count - 3);
601         } else {
602             // TRANS: List message for favoured notices.
603             // TRANS: %%s is a list of users liking a notice.
604             // TRANS: Plural is based on the number of of users that have favoured a notice.
605             return sprintf(_m('%%s repeated this.',
606                               '%%s repeated this.',
607                               $count),
608                            $count);
609         }
610     }
611
612     function showStart()
613     {
614         $this->out->elementStart('li', array('class' => 'notice-data notice-repeats'));
615     }
616
617     function showEnd()
618     {
619         $this->out->elementEnd('li');
620     }
621 }
622
623 // @todo FIXME: needs documentation.
624 class ThreadedNoticeListInlineRepeatsItem extends ThreadedNoticeListRepeatsItem
625 {
626     function showStart()
627     {
628         $this->out->elementStart('div', array('class' => 'e-content notice-repeats'));
629     }
630
631     function showEnd()
632     {
633         $this->out->elementEnd('div');
634     }
635 }