]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/threadednoticelist.php
work in progress
[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
52 class ThreadedNoticeList extends NoticeList
53 {
54     /**
55      * show the list of notices
56      *
57      * "Uses up" the stream by looping through it. So, probably can't
58      * be called twice on the same list.
59      *
60      * @return int count of notices listed.
61      */
62
63     function show()
64     {
65         $this->out->elementStart('div', array('id' =>'notices_primary'));
66         $this->out->element('h2', null, _('Notices'));
67         $this->out->elementStart('ol', array('class' => 'notices threaded-notices xoxo'));
68
69         $cnt = 0;
70         $conversations = array();
71         while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
72             $cnt++;
73
74             if ($cnt > NOTICES_PER_PAGE) {
75                 break;
76             }
77
78             $convo = $this->notice->conversation;
79             if (!empty($conversations[$convo])) {
80                 // Seen this convo already -- skip!
81                 continue;
82             }
83             $conversations[$convo] = true;
84
85             // Get the convo's root notice
86             // @fixme stream goes in wrong direction, this needs sane caching
87             //$notice = Notice::conversationStream($convo, 0, 1);
88             //$notice->fetch();
89             $notice = new Notice();
90             $notice->conversation = $this->notice->conversation;
91             $notice->orderBy('CREATED');
92             $notice->limit(1);
93             $notice->find(true);
94
95             try {
96                 $item = $this->newListItem($notice);
97                 $item->show();
98             } catch (Exception $e) {
99                 // we log exceptions and continue
100                 common_log(LOG_ERR, $e->getMessage());
101                 continue;
102             }
103         }
104
105         $this->out->elementEnd('ol');
106         $this->out->elementEnd('div');
107
108         return $cnt;
109     }
110
111     /**
112      * returns a new list item for the current notice
113      *
114      * Recipe (factory?) method; overridden by sub-classes to give
115      * a different list item class.
116      *
117      * @param Notice $notice the current notice
118      *
119      * @return NoticeListItem a list item for displaying the notice
120      */
121
122     function newListItem($notice)
123     {
124         return new ThreadedNoticeListItem($notice, $this->out);
125     }
126 }
127
128 /**
129  * widget for displaying a single notice
130  *
131  * This widget has the core smarts for showing a single notice: what to display,
132  * where, and under which circumstances. Its key method is show(); this is a recipe
133  * that calls all the other show*() methods to build up a single notice. The
134  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
135  * author info (since that's implicit by the data in the page).
136  *
137  * @category UI
138  * @package  StatusNet
139  * @author   Evan Prodromou <evan@status.net>
140  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
141  * @link     http://status.net/
142  * @see      NoticeList
143  * @see      ProfileNoticeListItem
144  */
145
146 class ThreadedNoticeListItem extends NoticeListItem
147 {
148     const INITIAL_ITEMS = 3;
149
150     function showContext()
151     {
152         // Silence!
153     }
154
155     /**
156      * finish the notice
157      *
158      * Close the last elements in the notice list item
159      *
160      * @return void
161      */
162
163     function showEnd()
164     {
165         if (!$this->repeat) {
166             $notice = Notice::conversationStream($this->notice->conversation, 0, self::INITIAL_ITEMS + 2);
167             $notices = array();
168             $cnt = 0;
169             $moreCutoff = null;
170             while ($notice->fetch()) {
171                 if ($notice->id == $this->notice->id) {
172                     // Skip!
173                     continue;
174                 }
175                 $cnt++;
176                 if ($cnt > self::INITIAL_ITEMS) {
177                     // boo-yah
178                     $moreCutoff = clone($notice);
179                     break;
180                 }
181                 $notices[] = clone($notice); // *grumble* inefficient as hell
182             }
183
184             if ($notices) {
185                 $this->out->elementStart('ul', 'notices threaded-replies xoxo');
186                 $item = new ThreadedNoticeListFavesItem($this->notice, $this->out);
187                 $hasFaves = $item->show();
188                 if ($moreCutoff) {
189                     $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out);
190                     $item->show();
191                 }
192                 foreach (array_reverse($notices) as $notice) {
193                     $item = new ThreadedNoticeListSubItem($notice, $this->out);
194                     $item->show();
195                 }
196                 // @fixme do a proper can-post check that's consistent
197                 // with the JS side
198                 if (common_current_user()) {
199                     $item = new ThreadedNoticeListReplyItem($this->notice, $this->out);
200                     $item->show();
201                 }
202                 $this->out->elementEnd('ul');
203             }
204         }
205
206         parent::showEnd();
207     }
208 }
209
210 class ThreadedNoticeListSubItem extends NoticeListItem
211 {
212
213     function avatarSize()
214     {
215         return AVATAR_STREAM_SIZE; // @fixme would like something in between
216     }
217
218     function showNoticeLocation()
219     {
220         //
221     }
222
223     function showNoticeSource()
224     {
225         //
226     }
227
228     function showContext()
229     {
230         //
231     }
232 }
233
234 /**
235  * Placeholder for loading more replies...
236  */
237 class ThreadedNoticeListMoreItem extends NoticeListItem
238 {
239
240     /**
241      * recipe function for displaying a single notice.
242      *
243      * This uses all the other methods to correctly display a notice. Override
244      * it or one of the others to fine-tune the output.
245      *
246      * @return void
247      */
248
249     function show()
250     {
251         $this->showStart();
252         $this->showMiniForm();
253         $this->showEnd();
254     }
255
256     /**
257      * start a single notice.
258      *
259      * @return void
260      */
261
262     function showStart()
263     {
264         $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
265     }
266
267     function showMiniForm()
268     {
269         $id = $this->notice->conversation;
270         $url = common_local_url('conversation', array('id' => $id)) . '#notice-' . $this->notice->id;
271
272         $notice = new Notice();
273         $notice->conversation = $id;
274         $n = $notice->count() - 1;
275         $msg = sprintf(_m('Show %d reply', 'Show all %d replies', $n), $n);
276
277         $this->out->element('a', array('href' => $url), $msg);
278     }
279 }
280
281
282 /**
283  * Placeholder for reply form...
284  * Same as get added at runtime via SN.U.NoticeInlineReplyPlaceholder
285  */
286 class ThreadedNoticeListReplyItem extends NoticeListItem
287 {
288
289     /**
290      * recipe function for displaying a single notice.
291      *
292      * This uses all the other methods to correctly display a notice. Override
293      * it or one of the others to fine-tune the output.
294      *
295      * @return void
296      */
297
298     function show()
299     {
300         $this->showStart();
301         $this->showMiniForm();
302         $this->showEnd();
303     }
304
305     /**
306      * start a single notice.
307      *
308      * @return void
309      */
310
311     function showStart()
312     {
313         $this->out->elementStart('li', array('class' => 'notice-reply-placeholder'));
314     }
315
316     function showMiniForm()
317     {
318         $this->out->element('input', array('class' => 'placeholder',
319                                            'value' => _('Write a reply...')));
320     }
321 }
322
323 /**
324  * Placeholder for showing faves...
325  */
326 class ThreadedNoticeListFavesItem extends NoticeListItem
327 {
328     function show()
329     {
330         // @fixme caching & scalability!
331         $fave = new Fave();
332         $fave->notice_id = $this->notice->id;
333         $fave->find();
334
335         $cur = common_current_user();
336         $profiles = array();
337         $you = false;
338         while ($fave->fetch()) {
339             if ($cur && $cur->id == $fave->user_id) {
340                 $you = true;
341             } else {
342                 $profiles[] = $fave->user_id;
343             }
344         }
345
346         $links = array();
347         if ($you) {
348             $links[] = _m('FAVELIST', 'You');
349         }
350         foreach ($profiles as $id) {
351             $profile = Profile::staticGet('id', $id);
352             if ($profile) {
353                 $links[] = sprintf('<a href="%s" title="%s">%s</a>',
354                                    htmlspecialchars($profile->profileurl),
355                                    htmlspecialchars($profile->getBestName()),
356                                    htmlspecialchars($profile->nickname));
357             }
358         }
359
360         if ($links) {
361             $count = count($links);
362             if ($count == 1 && $you) {
363                 // darn first person being different from third person!
364                 $msg = _m('FAVELIST', 'You have favored this notice');
365             } else {
366                 // if 'you' is the first item...
367                 $msg = _m('FAVELIST', '%1$s has favored this notice', '%1$s have favored this notice', $count);
368             }
369             $out = sprintf($msg, implode(', ', $links));
370
371             $this->out->elementStart('li', array('class' => 'notice-faves'));
372             $this->out->raw($out);
373             $this->out->elementEnd('li');
374             return $count;
375         } else {
376             return 0;
377         }
378     }
379 }