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