]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/threadednoticelist.php
Merge remote-tracking branch 'gitorious/1.0.x' into 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                     NoticeList::prefill($notices, AVATAR_MINI_SIZE);
243                     foreach (array_reverse($notices) as $notice) {
244                         if (Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice))) {
245                             $item = new ThreadedNoticeListSubItem($notice, $this->notice, $this->out);
246                             $item->show();
247                             Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice));
248                         }
249                     }
250                 }
251
252                 if ($notices || $hasFaves || $hasRepeats) {
253                     // @fixme do a proper can-post check that's consistent
254                     // with the JS side
255                     if (common_current_user()) {
256                         $item = new ThreadedNoticeListReplyItem($this->notice, $this->out);
257                         $item->show();
258                     }
259                 }
260                 $this->out->elementEnd('ul');
261                 Event::handle('EndShowThreadedNoticeTail', array($this, $this->notice, $notices));
262             }
263         }
264
265         parent::showEnd();
266     }
267 }
268
269 // @todo FIXME: needs documentation.
270 class ThreadedNoticeListSubItem extends NoticeListItem
271 {
272     protected $root = null;
273
274     function __construct($notice, $root, $out)
275     {
276         $this->root = $root;
277         parent::__construct($notice, $out);
278     }
279
280     function avatarSize()
281     {
282         return AVATAR_STREAM_SIZE; // @fixme would like something in between
283     }
284
285     function showNoticeLocation()
286     {
287         //
288     }
289
290     function showNoticeSource()
291     {
292         //
293     }
294
295     function showContext()
296     {
297         //
298     }
299
300     function getReplyProfiles()
301     {
302         $all = parent::getReplyProfiles();
303
304         $profiles = array();
305
306         $rootAuthor = $this->root->getProfile();
307
308         foreach ($all as $profile) {
309             if ($profile->id != $rootAuthor->id) {
310                 $profiles[] = $profile;
311             }
312         }
313
314         return $profiles;
315     }
316
317     function showEnd()
318     {
319         $item = new ThreadedNoticeListInlineFavesItem($this->notice, $this->out);
320         $hasFaves = $item->show();
321         parent::showEnd();
322     }
323 }
324
325 /**
326  * Placeholder for loading more replies...
327  */
328 class ThreadedNoticeListMoreItem extends NoticeListItem
329 {
330     protected $cnt;
331
332     function __construct($notice, $out, $cnt)
333     {
334         parent::__construct($notice, $out);
335         $this->cnt = $cnt;
336     }
337
338     /**
339      * recipe function for displaying a single notice.
340      *
341      * This uses all the other methods to correctly display a notice. Override
342      * it or one of the others to fine-tune the output.
343      *
344      * @return void
345      */
346     function show()
347     {
348         $this->showStart();
349         $this->showMiniForm();
350         $this->showEnd();
351     }
352
353     /**
354      * start a single notice.
355      *
356      * @return void
357      */
358     function showStart()
359     {
360         $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
361     }
362
363     function showMiniForm()
364     {
365         $id = $this->notice->conversation;
366         $url = common_local_url('conversationreplies', array('id' => $id));
367
368         $n = Conversation::noticeCount($id) - 1;
369
370         // TRANS: Link to show replies for a notice.
371         // TRANS: %d is the number of replies to a notice and used for plural.
372         $msg = sprintf(_m('Show reply', 'Show all %d replies', $n), $n);
373
374         $this->out->element('a', array('href' => $url), $msg);
375     }
376 }
377
378 /**
379  * Placeholder for reply form...
380  * Same as get added at runtime via SN.U.NoticeInlineReplyPlaceholder
381  */
382 class ThreadedNoticeListReplyItem extends NoticeListItem
383 {
384     /**
385      * recipe function for displaying a single notice.
386      *
387      * This uses all the other methods to correctly display a notice. Override
388      * it or one of the others to fine-tune the output.
389      *
390      * @return void
391      */
392     function show()
393     {
394         $this->showStart();
395         $this->showMiniForm();
396         $this->showEnd();
397     }
398
399     /**
400      * start a single notice.
401      *
402      * @return void
403      */
404     function showStart()
405     {
406         $this->out->elementStart('li', array('class' => 'notice-reply-placeholder'));
407     }
408
409     function showMiniForm()
410     {
411         $this->out->element('input', array('class' => 'placeholder',
412                                            // TRANS: Field label for reply mini form.
413                                            'value' => _('Write a reply...')));
414     }
415 }
416
417 /**
418  * Placeholder for showing faves...
419  */
420 abstract class NoticeListActorsItem extends NoticeListItem
421 {
422     /**
423      * @return array of profile IDs
424      */
425     abstract function getProfiles();
426
427     abstract function getListMessage($count, $you);
428
429     function show()
430     {
431         $links = array();
432         $you = false;
433         $cur = common_current_user();
434         foreach ($this->getProfiles() as $id) {
435             if ($cur && $cur->id == $id) {
436                 $you = true;
437                 // TRANS: Reference to the logged in user in favourite list.
438                 array_unshift($links, _m('FAVELIST', 'You'));
439             } else {
440                 $profile = Profile::staticGet('id', $id);
441                 if ($profile) {
442                     $links[] = sprintf('<a href="%s">%s</a>',
443                                        htmlspecialchars($profile->profileurl),
444                                        htmlspecialchars($profile->getBestName()));
445                 }
446             }
447         }
448
449         if ($links) {
450             $count = count($links);
451             $msg = $this->getListMessage($count, $you);
452             $out = sprintf($msg, $this->magicList($links));
453
454             $this->showStart();
455             $this->out->raw($out);
456             $this->showEnd();
457             return $count;
458         } else {
459             return 0;
460         }
461     }
462
463     function magicList($items)
464     {
465         if (count($items) == 0) {
466             return '';
467         } else if (count($items) == 1) {
468             return $items[0];
469         } else {
470             $first = array_slice($items, 0, -1);
471             $last = array_slice($items, -1, 1);
472             // TRANS: Separator in list of user names like "Jim, Bob, Mary".
473             $separator = _(', ');
474             // TRANS: For building a list such as "Jim, Bob, Mary and 5 others like this".
475             // TRANS: %1$s is a list of users, separated by a separator (default: ", "), %2$s is the last user in the list.
476             return sprintf(_m('FAVELIST', '%1$s and %2$s'), implode($separator, $first), implode($separator, $last));
477         }
478     }
479 }
480
481 /**
482  * Placeholder for showing faves...
483  */
484 class ThreadedNoticeListFavesItem extends NoticeListActorsItem
485 {
486     function getProfiles()
487     {
488         $faves = $this->notice->getFaves();
489         $profiles = array();
490         foreach ($faves as $fave) {
491             $profiles[] = $fave->user_id;
492         }
493         return $profiles;
494     }
495
496     function magicList($items)
497     {
498         if (count($items) > 4) {
499             return parent::magicList(array_slice($items, 0, 3));
500         } else {
501             return parent::magicList($items);
502         }
503     }
504
505     function getListMessage($count, $you)
506     {
507         if ($count == 1 && $you) {
508             // darn first person being different from third person!
509             // TRANS: List message for notice favoured by logged in user.
510             return _m('FAVELIST', 'You like this.');
511         } else if ($count > 4) {
512             // TRANS: List message for when more than 4 people like something.
513             // TRANS: %%s is a list of users liking a notice, %d is the number over 4 that like the notice.
514             // TRANS: Plural is decided on the total number of users liking the notice (count of %%s + %d).
515             return sprintf(_m('%%s and %d others like this.',
516                               '%%s and %d others like this.',
517                               $count),
518                            $count - 3);
519         } else {
520             // TRANS: List message for favoured notices.
521             // TRANS: %%s is a list of users liking a notice.
522             // TRANS: Plural is based on the number of of users that have favoured a notice.
523             return sprintf(_m('%%s likes this.',
524                               '%%s like this.',
525                               $count),
526                            $count);
527         }
528     }
529
530     function showStart()
531     {
532         $this->out->elementStart('li', array('class' => 'notice-data notice-faves'));
533     }
534
535     function showEnd()
536     {
537         $this->out->elementEnd('li');
538     }
539 }
540
541 // @todo FIXME: needs documentation.
542 class ThreadedNoticeListInlineFavesItem extends ThreadedNoticeListFavesItem
543 {
544     function showStart()
545     {
546         $this->out->elementStart('div', array('class' => 'entry-content notice-faves'));
547     }
548
549     function showEnd()
550     {
551         $this->out->elementEnd('div');
552     }
553 }
554
555 /**
556  * Placeholder for showing faves...
557  */
558 class ThreadedNoticeListRepeatsItem extends NoticeListActorsItem
559 {
560     function getProfiles()
561     {
562         $rep = $this->notice->repeatStream();
563
564         $profiles = array();
565         while ($rep->fetch()) {
566             $profiles[] = $rep->profile_id;
567         }
568         return $profiles;
569     }
570
571     function getListMessage($count, $you)
572     {
573         if ($count == 1 && $you) {
574             // darn first person being different from third person!
575             // TRANS: List message for notice repeated by logged in user.
576             return _m('REPEATLIST', 'You have repeated this notice.');
577         } else {
578             // TRANS: List message for repeated notices.
579             // TRANS: %d is the number of users that have repeated a notice.
580             return sprintf(_m('One person has repeated this notice.',
581                               '%d people have repeated this notice.',
582                               $count),
583                            $count);
584         }
585     }
586
587     function showStart()
588     {
589         $this->out->elementStart('li', array('class' => 'notice-data notice-repeats'));
590     }
591
592     function showEnd()
593     {
594         $this->out->elementEnd('li');
595     }
596 }