]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/threadednoticelist.php
18d13505c8ddcde23ae79d57aa02a558c06f02de
[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('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
31
32 /**
33  * widget for displaying a list of notices
34  *
35  * There are a number of actions that display a list of notices, in
36  * reverse chronological order. This widget abstracts out most of the
37  * code for UI for notice lists. It's overridden to hide some
38  * data for e.g. the profile page.
39  *
40  * @category UI
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  * @see      Notice
46  * @see      NoticeListItem
47  * @see      ProfileNoticeList
48  */
49 class ThreadedNoticeList extends NoticeList
50 {
51     protected $userProfile;
52
53     function __construct(Notice $notice, Action $out=null, $profile=-1)
54     {
55         parent::__construct($notice, $out);
56         if (is_int($profile) && $profile == -1) {
57             $profile = Profile::current();
58         }
59         $this->userProfile = $profile;
60     }
61
62     /**
63      * show the list of notices
64      *
65      * "Uses up" the stream by looping through it. So, probably can't
66      * be called twice on the same list.
67      *
68      * @return int count of notices listed.
69      */
70     function show()
71     {
72         $this->out->elementStart('div', array('id' =>'notices_primary'));
73         // TRANS: Header for Notices section.
74         $this->out->element('h2', null, _m('HEADER','Notices'));
75         $this->out->elementStart('ol', array('class' => 'notices threaded-notices xoxo'));
76
77                 $notices = $this->notice->fetchAll();
78                 $total = count($notices);
79                 $notices = array_slice($notices, 0, NOTICES_PER_PAGE);
80                 
81         $allnotices = self::_allNotices($notices);
82         self::prefill($allnotices);
83         
84         $conversations = array();
85         
86         foreach ($notices as $notice) {
87
88             // Collapse repeats into their originals...
89             
90             if ($notice->repeat_of) {
91                 $orig = Notice::getKV('id', $notice->repeat_of);
92                 if ($orig instanceof Notice) {
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 instanceof Notice) {
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 $total;
123     }
124
125     function _allNotices($notices)
126     {
127         $convId = array();
128         foreach ($notices as $notice) {
129             $convId[] = $notice->conversation;
130         }
131         $convId = array_unique($convId);
132         $allMap = Notice::listGet('conversation', $convId);
133         $allArray = array();
134         foreach ($allMap as $convId => $convNotices) {
135             $allArray = array_merge($allArray, $convNotices);
136         }
137         return $allArray;
138     }
139
140     /**
141      * returns a new list item for the current notice
142      *
143      * Recipe (factory?) method; overridden by sub-classes to give
144      * a different list item class.
145      *
146      * @param Notice $notice the current notice
147      *
148      * @return NoticeListItem a list item for displaying the notice
149      */
150     function newListItem(Notice $notice)
151     {
152         return new ThreadedNoticeListItem($notice, $this->out, $this->userProfile);
153     }
154 }
155
156 /**
157  * widget for displaying a single notice
158  *
159  * This widget has the core smarts for showing a single notice: what to display,
160  * where, and under which circumstances. Its key method is show(); this is a recipe
161  * that calls all the other show*() methods to build up a single notice. The
162  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
163  * author info (since that's implicit by the data in the page).
164  *
165  * @category UI
166  * @package  StatusNet
167  * @author   Evan Prodromou <evan@status.net>
168  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
169  * @link     http://status.net/
170  * @see      NoticeList
171  * @see      ProfileNoticeListItem
172  */
173 class ThreadedNoticeListItem extends NoticeListItem
174 {
175     protected $userProfile = null;
176
177     function __construct(Notice $notice, Action $out=null, $profile=null)
178     {
179         parent::__construct($notice, $out);
180         $this->userProfile = $profile;
181     }
182
183     function initialItems()
184     {
185         return 3;
186     }
187
188     /**
189      * finish the notice
190      *
191      * Close the last elements in the notice list item
192      *
193      * @return void
194      */
195     function showEnd()
196     {
197         $max = $this->initialItems();
198         if (!$this->repeat) {
199             $stream = new ConversationNoticeStream($this->notice->conversation, $this->userProfile);
200             $notice = $stream->getNotices(0, $max + 2);
201             $notices = array();
202             $cnt = 0;
203             $moreCutoff = null;
204             while ($notice->fetch()) {
205                 if (Event::handle('StartAddNoticeReply', array($this, $this->notice, $notice))) {
206                     // Don't list repeats as separate notices in a conversation
207                     if (!empty($notice->repeat_of)) {
208                         continue;
209                     }
210
211                     if ($notice->id == $this->notice->id) {
212                         // Skip!
213                         continue;
214                     }
215                     $cnt++;
216                     if ($cnt > $max) {
217                         // boo-yah
218                         $moreCutoff = clone($notice);
219                         break;
220                     }
221                     $notices[] = clone($notice); // *grumble* inefficient as hell
222                     Event::handle('EndAddNoticeReply', array($this, $this->notice, $notice));
223                 }
224             }
225
226             if (Event::handle('StartShowThreadedNoticeTail', array($this, $this->notice, &$notices))) {
227                 $threadActive = count($notices) > 0; // has this thread had any activity?
228
229                 $this->out->elementStart('ul', 'notices threaded-replies xoxo');
230
231                 if (Event::handle('StartShowThreadedNoticeTailItems', array($this, $this->notice, &$threadActive))) {
232
233                     // Show the repeats-button for this notice. If there are repeats,
234                     // the show() function will return true, definitely setting our
235                     // $threadActive flag, which will be used later to show a reply box.
236                     $item = new ThreadedNoticeListRepeatsItem($this->notice, $this->out);
237                     $threadActive = $item->show() || $threadActive;
238
239                     Event::handle('EndShowThreadedNoticeTailItems', array($this, $this->notice, &$threadActive));
240                 }
241
242                 if (count($notices)>0) {
243                     if ($moreCutoff) {
244                         $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out, count($notices));
245                         $item->show();
246                     }
247                     foreach (array_reverse($notices) as $notice) {
248                         if (Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice))) {
249                             $item = new ThreadedNoticeListSubItem($notice, $this->notice, $this->out);
250                             $item->show();
251                             Event::handle('EndShowThreadedNoticeSub', array($this, $this->notice, $notice));
252                         }
253                     }
254                 }
255
256                 if ($threadActive && Profile::current() instanceof Profile) {
257                     // @fixme do a proper can-post check that's consistent
258                     // with the JS side
259                     $item = new ThreadedNoticeListReplyItem($this->notice, $this->out);
260                     $item->show();
261                 }
262                 $this->out->elementEnd('ul');
263                 Event::handle('EndShowThreadedNoticeTail', array($this, $this->notice, $notices));
264             }
265         }
266
267         parent::showEnd();
268     }
269 }
270
271 // @todo FIXME: needs documentation.
272 class ThreadedNoticeListSubItem extends NoticeListItem
273 {
274     protected $root = null;
275
276     function __construct(Notice $notice, $root, $out)
277     {
278         $this->root = $root;
279         parent::__construct($notice, $out);
280     }
281
282     function avatarSize()
283     {
284         return AVATAR_STREAM_SIZE; // @fixme would like something in between
285     }
286
287     function showNoticeLocation()
288     {
289         //
290     }
291
292     function showNoticeSource()
293     {
294         //
295     }
296
297     function getReplyProfiles()
298     {
299         $all = parent::getReplyProfiles();
300
301         $profiles = array();
302
303         $rootAuthor = $this->root->getProfile();
304
305         foreach ($all as $profile) {
306             if ($profile->id != $rootAuthor->id) {
307                 $profiles[] = $profile;
308             }
309         }
310
311         return $profiles;
312     }
313
314     function showEnd()
315     {
316         $threadActive = null;   // unused here for now, but maybe in the future?
317         if (Event::handle('StartShowThreadedNoticeTailItems', array($this, $this->notice, &$threadActive))) {
318             $item = new ThreadedNoticeListInlineRepeatsItem($this->notice, $this->out);
319             $hasRepeats = $item->show();
320             Event::handle('EndShowThreadedNoticeTailItems', array($this, $this->notice, &$threadActive));
321         }
322         parent::showEnd();
323     }
324 }
325
326 /**
327  * Placeholder for loading more replies...
328  */
329 class ThreadedNoticeListMoreItem extends NoticeListItem
330 {
331     protected $cnt;
332
333     function __construct(Notice $notice, Action $out, $cnt)
334     {
335         parent::__construct($notice, $out);
336         $this->cnt = $cnt;
337     }
338
339     /**
340      * recipe function for displaying a single notice.
341      *
342      * This uses all the other methods to correctly display a notice. Override
343      * it or one of the others to fine-tune the output.
344      *
345      * @return void
346      */
347     function show()
348     {
349         $this->showStart();
350         $this->showMiniForm();
351         $this->showEnd();
352     }
353
354     /**
355      * start a single notice.
356      *
357      * @return void
358      */
359     function showStart()
360     {
361         $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
362     }
363
364     function showEnd()
365     {
366         $this->out->elementEnd('li');
367     }
368
369     function showMiniForm()
370     {
371         $id = $this->notice->conversation;
372         $url = common_local_url('conversation', array('id' => $id));
373
374         $n = Conversation::noticeCount($id) - 1;
375
376         // TRANS: Link to show replies for a notice.
377         // TRANS: %d is the number of replies to a notice and used for plural.
378         $msg = sprintf(_m('Show reply', 'Show all %d replies', $n), $n);
379
380         $this->out->element('a', array('href' => $url), $msg);
381     }
382 }
383
384 /**
385  * Placeholder for reply form...
386  * Same as get added at runtime via SN.U.NoticeInlineReplyPlaceholder
387  */
388 class ThreadedNoticeListReplyItem extends NoticeListItem
389 {
390     /**
391      * recipe function for displaying a single notice.
392      *
393      * This uses all the other methods to correctly display a notice. Override
394      * it or one of the others to fine-tune the output.
395      *
396      * @return void
397      */
398     function show()
399     {
400         $this->showStart();
401         $this->showMiniForm();
402         $this->showEnd();
403     }
404
405     /**
406      * start a single notice.
407      *
408      * @return void
409      */
410     function showStart()
411     {
412         $this->out->elementStart('li', array('class' => 'notice-reply-placeholder'));
413     }
414
415     function showMiniForm()
416     {
417         $this->out->element('input', array('class' => 'placeholder',
418                                            // TRANS: Field label for reply mini form.
419                                            'value' => _('Write a reply...')));
420     }
421 }
422
423 /**
424  * Placeholder for showing repeats...
425  */
426 class ThreadedNoticeListRepeatsItem extends NoticeListActorsItem
427 {
428     function getProfiles()
429     {
430         $repeats = $this->notice->getRepeats();
431
432         $profiles = array();
433
434         foreach ($repeats as $rep) {
435             $profiles[] = $rep->profile_id;
436         }
437
438         return $profiles;
439     }
440
441     function magicList($items)
442     {
443         if (count($items) > 4) {
444             return parent::magicList(array_slice($items, 0, 3));
445         } else {
446             return parent::magicList($items);
447         }
448     }
449
450     function getListMessage($count, $you)
451     {
452         if ($count == 1 && $you) {
453             // darn first person being different from third person!
454             // TRANS: List message for notice repeated by logged in user.
455             return _m('REPEATLIST', 'You repeated this.');
456         } else if ($count > 4) {
457             // TRANS: List message for when more than 4 people repeat something.
458             // TRANS: %%s is a list of users liking a notice, %d is the number over 4 that like the notice.
459             // TRANS: Plural is decided on the total number of users liking the notice (count of %%s + %d).
460             return sprintf(_m('%%s and %d other repeated this.',
461                               '%%s and %d others repeated this.',
462                               $count - 3),
463                            $count - 3);
464         } else {
465             // TRANS: List message for repeated notices.
466             // TRANS: %%s is a list of users who have repeated a notice.
467             // TRANS: Plural is based on the number of of users that have repeated a notice.
468             return sprintf(_m('%%s repeated this.',
469                               '%%s repeated this.',
470                               $count),
471                            $count);
472         }
473     }
474
475     function showStart()
476     {
477         $this->out->elementStart('li', array('class' => 'notice-data notice-repeats'));
478     }
479
480     function showEnd()
481     {
482         $this->out->elementEnd('li');
483     }
484 }
485
486 // @todo FIXME: needs documentation.
487 class ThreadedNoticeListInlineRepeatsItem extends ThreadedNoticeListRepeatsItem
488 {
489     function showStart()
490     {
491         $this->out->elementStart('div', array('class' => 'notice-repeats'));
492     }
493
494     function showEnd()
495     {
496         $this->out->elementEnd('div');
497     }
498 }