]> 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         $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 ($notice->id == $this->notice->id) {
196                     // Skip!
197                     continue;
198                 }
199                 $cnt++;
200                 if ($cnt > $max) {
201                     // boo-yah
202                     $moreCutoff = clone($notice);
203                     break;
204                 }
205                 $notices[] = clone($notice); // *grumble* inefficient as hell
206             }
207
208             if (Event::handle('StartShowThreadedNoticeTail', array($this, $this->notice, &$notices))) {
209                 $this->out->elementStart('ul', 'notices threaded-replies xoxo');
210
211                 $item = new ThreadedNoticeListFavesItem($this->notice, $this->out);
212                 $hasFaves = $item->show();
213
214                 $item = new ThreadedNoticeListRepeatsItem($this->notice, $this->out);
215                 $hasRepeats = $item->show();
216
217                 if ($notices) {
218                     if ($moreCutoff) {
219                         $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out);
220                         $item->show();
221                     }
222                     foreach (array_reverse($notices) as $notice) {
223                         if (Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice))) {
224                             $item = new ThreadedNoticeListSubItem($notice, $this->notice, $this->out);
225                             $item->show();
226                             Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice));
227                         }
228                     }
229                 }
230
231                 if ($notices || $hasFaves || $hasRepeats) {
232                     // @fixme do a proper can-post check that's consistent
233                     // with the JS side
234                     if (common_current_user()) {
235                         $item = new ThreadedNoticeListReplyItem($this->notice, $this->out);
236                         $item->show();
237                     }
238                 }
239                 $this->out->elementEnd('ul');
240                 Event::handle('EndShowThreadedNoticeTail', array($this, $this->notice, $notices));
241             }
242         }
243
244         parent::showEnd();
245     }
246 }
247
248 // @todo FIXME: needs documentation.
249 class ThreadedNoticeListSubItem extends NoticeListItem
250 {
251     protected $root = null;
252
253     function __construct($notice, $root, $out)
254     {
255         $this->root = $root;
256         parent::__construct($notice, $out);
257     }
258
259     function avatarSize()
260     {
261         return AVATAR_STREAM_SIZE; // @fixme would like something in between
262     }
263
264     function showNoticeLocation()
265     {
266         //
267     }
268
269     function showNoticeSource()
270     {
271         //
272     }
273
274     function showContext()
275     {
276         //
277     }
278
279     function getReplyProfiles()
280     {
281         $all = parent::getReplyProfiles();
282
283         $profiles = array();
284
285         $rootAuthor = $this->root->getProfile();
286
287         foreach ($all as $profile) {
288             if ($profile->id != $rootAuthor->id) {
289                 $profiles[] = $profile;
290             }
291         }
292
293         return $profiles;
294     }
295
296     function showEnd()
297     {
298         $item = new ThreadedNoticeListInlineFavesItem($this->notice, $this->out);
299         $hasFaves = $item->show();
300         parent::showEnd();
301     }
302 }
303
304 /**
305  * Placeholder for loading more replies...
306  */
307 class ThreadedNoticeListMoreItem extends NoticeListItem
308 {
309     /**
310      * recipe function for displaying a single notice.
311      *
312      * This uses all the other methods to correctly display a notice. Override
313      * it or one of the others to fine-tune the output.
314      *
315      * @return void
316      */
317     function show()
318     {
319         $this->showStart();
320         $this->showMiniForm();
321         $this->showEnd();
322     }
323
324     /**
325      * start a single notice.
326      *
327      * @return void
328      */
329     function showStart()
330     {
331         $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
332     }
333
334     function showMiniForm()
335     {
336         $id = $this->notice->conversation;
337         $url = common_local_url('conversationreplies', array('id' => $id));
338
339         $n = Conversation::noticeCount($id) - 1;
340
341         // TRANS: Link to show replies for a notice.
342         // TRANS: %d is the number of replies to a notice and used for plural.
343         $msg = sprintf(_m('Show reply', 'Show all %d replies', $n), $n);
344
345         $this->out->element('a', array('href' => $url), $msg);
346     }
347 }
348
349 /**
350  * Placeholder for reply form...
351  * Same as get added at runtime via SN.U.NoticeInlineReplyPlaceholder
352  */
353 class ThreadedNoticeListReplyItem extends NoticeListItem
354 {
355     /**
356      * recipe function for displaying a single notice.
357      *
358      * This uses all the other methods to correctly display a notice. Override
359      * it or one of the others to fine-tune the output.
360      *
361      * @return void
362      */
363     function show()
364     {
365         $this->showStart();
366         $this->showMiniForm();
367         $this->showEnd();
368     }
369
370     /**
371      * start a single notice.
372      *
373      * @return void
374      */
375     function showStart()
376     {
377         $this->out->elementStart('li', array('class' => 'notice-reply-placeholder'));
378     }
379
380     function showMiniForm()
381     {
382         $this->out->element('input', array('class' => 'placeholder',
383                                            // TRANS: Field label for reply mini form.
384                                            'value' => _('Write a reply...')));
385     }
386 }
387
388 /**
389  * Placeholder for showing faves...
390  */
391 abstract class NoticeListActorsItem extends NoticeListItem
392 {
393     /**
394      * @return array of profile IDs
395      */
396     abstract function getProfiles();
397
398     abstract function getListMessage($count, $you);
399
400     function show()
401     {
402         $links = array();
403         $you = false;
404         $cur = common_current_user();
405         foreach ($this->getProfiles() as $id) {
406             if ($cur && $cur->id == $id) {
407                 $you = true;
408                 // TRANS: Reference to the logged in user in favourite list.
409                 array_unshift($links, _m('FAVELIST', 'You'));
410             } else {
411                 $profile = Profile::staticGet('id', $id);
412                 if ($profile) {
413                     $links[] = sprintf('<a href="%s" title="%s">%s</a>',
414                                        htmlspecialchars($profile->profileurl),
415                                        htmlspecialchars($profile->getBestName()),
416                                        htmlspecialchars($profile->nickname));
417                 }
418             }
419         }
420
421         if ($links) {
422             $count = count($links);
423             $msg = $this->getListMessage($count, $you);
424             $out = sprintf($msg, $this->magicList($links));
425
426             $this->showStart();
427             $this->out->raw($out);
428             $this->showEnd();
429             return $count;
430         } else {
431             return 0;
432         }
433     }
434
435     function magicList($items)
436     {
437         if (count($items) == 0) {
438             return '';
439         } else if (count($items) == 1) {
440             return $items[0];
441         } else {
442             $first = array_slice($items, 0, -1);
443             $last = array_slice($items, -1, 1);
444             // TRANS: Separator in list of user names like "You, Bob, Mary".
445             $separator = _(', ');
446             // TRANS: For building a list such as "You, bob, mary and 5 others have favored this notice".
447             // TRANS: %1$s is a list of users, separated by a separator (default: ", "), %2$s is the last user in the list.
448             return sprintf(_m('FAVELIST', '%1$s and %2$s'), implode($separator, $first), implode($separator, $last));
449         }
450     }
451 }
452
453 /**
454  * Placeholder for showing faves...
455  */
456 class ThreadedNoticeListFavesItem extends NoticeListActorsItem
457 {
458     function getProfiles()
459     {
460         $fave = Fave::byNotice($this->notice->id);
461         $profiles = array();
462         while ($fave->fetch()) {
463             $profiles[] = $fave->user_id;
464         }
465         return $profiles;
466     }
467
468     function getListMessage($count, $you)
469     {
470         if ($count == 1 && $you) {
471             // darn first person being different from third person!
472             // TRANS: List message for notice favoured by logged in user.
473             return _m('FAVELIST', 'You have favored this notice.');
474         } else {
475             // TRANS: List message for favoured notices.
476             // TRANS: %d is the number of users that have favoured a notice.
477             return sprintf(_m('One person has favored this notice.',
478                               '%d people have favored this notice.',
479                               $count),
480                            $count);
481         }
482     }
483
484     function showStart()
485     {
486         $this->out->elementStart('li', array('class' => 'notice-data notice-faves'));
487     }
488
489     function showEnd()
490     {
491         $this->out->elementEnd('li');
492     }
493
494 }
495
496 // @todo FIXME: needs documentation.
497 class ThreadedNoticeListInlineFavesItem extends ThreadedNoticeListFavesItem
498 {
499     function showStart()
500     {
501         $this->out->elementStart('div', array('class' => 'entry-content notice-faves'));
502     }
503
504     function showEnd()
505     {
506         $this->out->elementEnd('div');
507     }
508 }
509
510 /**
511  * Placeholder for showing faves...
512  */
513 class ThreadedNoticeListRepeatsItem extends NoticeListActorsItem
514 {
515     function getProfiles()
516     {
517         $rep = $this->notice->repeatStream();
518
519         $profiles = array();
520         while ($rep->fetch()) {
521             $profiles[] = $rep->profile_id;
522         }
523         return $profiles;
524     }
525
526     function getListMessage($count, $you)
527     {
528         if ($count == 1 && $you) {
529             // darn first person being different from third person!
530             // TRANS: List message for notice repeated by logged in user.
531             return _m('REPEATLIST', 'You have repeated this notice.');
532         } else {
533             // TRANS: List message for repeated notices.
534             // TRANS: %d is the number of users that have repeated a notice.
535             return sprintf(_m('One person has repeated this notice.',
536                               '%d people have repeated this notice.',
537                               $count),
538                            $count);
539         }
540     }
541
542     function showStart()
543     {
544         $this->out->elementStart('li', array('class' => 'notice-data notice-repeats'));
545     }
546
547     function showEnd()
548     {
549         $this->out->elementEnd('li');
550     }
551 }