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