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