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