3 * StatusNet, the distributed open-source microblogging tool
5 * widget for displaying a list of notices
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.
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.
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/>.
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/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 * widget for displaying a list of notices
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.
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/
49 * @see ProfileNoticeList
52 class ThreadedNoticeList extends NoticeList
55 * show the list of notices
57 * "Uses up" the stream by looping through it. So, probably can't
58 * be called twice on the same list.
60 * @return int count of notices listed.
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'));
70 $conversations = array();
71 while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
74 if ($cnt > NOTICES_PER_PAGE) {
78 // Collapse repeats into their originals...
79 $notice = $this->notice;
80 if ($notice->repeat_of) {
81 $orig = Notice::staticGet('id', $notice->repeat_of);
86 $convo = $notice->conversation;
87 if (!empty($conversations[$convo])) {
88 // Seen this convo already -- skip!
91 $conversations[$convo] = true;
93 // Get the convo's root notice
94 // @fixme stream goes in wrong direction, this needs sane caching
95 //$notice = Notice::conversationStream($convo, 0, 1);
98 $root->conversation = $notice->conversation;
99 $root->orderBy('CREATED');
104 $item = $this->newListItem($root);
106 } catch (Exception $e) {
107 // we log exceptions and continue
108 common_log(LOG_ERR, $e->getMessage());
113 $this->out->elementEnd('ol');
114 $this->out->elementEnd('div');
120 * returns a new list item for the current notice
122 * Recipe (factory?) method; overridden by sub-classes to give
123 * a different list item class.
125 * @param Notice $notice the current notice
127 * @return NoticeListItem a list item for displaying the notice
130 function newListItem($notice)
132 return new ThreadedNoticeListItem($notice, $this->out);
137 * widget for displaying a single notice
139 * This widget has the core smarts for showing a single notice: what to display,
140 * where, and under which circumstances. Its key method is show(); this is a recipe
141 * that calls all the other show*() methods to build up a single notice. The
142 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
143 * author info (since that's implicit by the data in the page).
147 * @author Evan Prodromou <evan@status.net>
148 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
149 * @link http://status.net/
151 * @see ProfileNoticeListItem
154 class ThreadedNoticeListItem extends NoticeListItem
156 const INITIAL_ITEMS = 3;
158 function showContext()
166 * Close the last elements in the notice list item
173 if (!$this->repeat) {
174 $notice = Notice::conversationStream($this->notice->conversation, 0, self::INITIAL_ITEMS + 2);
178 while ($notice->fetch()) {
179 if ($notice->id == $this->notice->id) {
184 if ($cnt > self::INITIAL_ITEMS) {
186 $moreCutoff = clone($notice);
189 $notices[] = clone($notice); // *grumble* inefficient as hell
192 $this->out->elementStart('ul', 'notices threaded-replies xoxo');
194 $item = new ThreadedNoticeListFavesItem($this->notice, $this->out);
195 $hasFaves = $item->show();
197 $item = new ThreadedNoticeListRepeatsItem($this->notice, $this->out);
198 $hasRepeats = $item->show();
202 $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out);
205 foreach (array_reverse($notices) as $notice) {
206 $item = new ThreadedNoticeListSubItem($notice, $this->out);
210 if ($notices || $hasFaves || $hasRepeats) {
211 // @fixme do a proper can-post check that's consistent
213 if (common_current_user()) {
214 $item = new ThreadedNoticeListReplyItem($this->notice, $this->out);
218 $this->out->elementEnd('ul');
225 class ThreadedNoticeListSubItem extends NoticeListItem
228 function avatarSize()
230 return AVATAR_STREAM_SIZE; // @fixme would like something in between
233 function showNoticeLocation()
238 function showNoticeSource()
243 function showContext()
250 $item = new ThreadedNoticeListInlineFavesItem($this->notice, $this->out);
251 $hasFaves = $item->show();
257 * Placeholder for loading more replies...
259 class ThreadedNoticeListMoreItem extends NoticeListItem
263 * recipe function for displaying a single notice.
265 * This uses all the other methods to correctly display a notice. Override
266 * it or one of the others to fine-tune the output.
274 $this->showMiniForm();
279 * start a single notice.
286 $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
289 function showMiniForm()
291 $id = $this->notice->conversation;
292 $url = common_local_url('conversation', array('id' => $id)) . '#notice-' . $this->notice->id;
294 $notice = new Notice();
295 $notice->conversation = $id;
296 $n = $notice->count() - 1;
297 $msg = sprintf(_m('Show %d reply', 'Show all %d replies', $n), $n);
299 $this->out->element('a', array('href' => $url), $msg);
305 * Placeholder for reply form...
306 * Same as get added at runtime via SN.U.NoticeInlineReplyPlaceholder
308 class ThreadedNoticeListReplyItem extends NoticeListItem
312 * recipe function for displaying a single notice.
314 * This uses all the other methods to correctly display a notice. Override
315 * it or one of the others to fine-tune the output.
323 $this->showMiniForm();
328 * start a single notice.
335 $this->out->elementStart('li', array('class' => 'notice-reply-placeholder'));
338 function showMiniForm()
340 $this->out->element('input', array('class' => 'placeholder',
341 'value' => _('Write a reply...')));
346 * Placeholder for showing faves...
348 abstract class NoticeListActorsItem extends NoticeListItem
351 * @return array of profile IDs
353 abstract function getProfiles();
355 abstract function getListMessage($count, $you);
361 $cur = common_current_user();
362 foreach ($this->getProfiles() as $id) {
363 if ($cur && $cur->id == $id) {
365 array_unshift($links, _m('FAVELIST', 'You'));
367 $profile = Profile::staticGet('id', $id);
369 $links[] = sprintf('<a href="%s" title="%s">%s</a>',
370 htmlspecialchars($profile->profileurl),
371 htmlspecialchars($profile->getBestName()),
372 htmlspecialchars($profile->nickname));
378 $count = count($links);
379 $msg = $this->getListMessage($count, $you);
380 $out = sprintf($msg, $this->magicList($links));
383 $this->out->raw($out);
391 function magicList($items)
393 if (count($items) == 0) {
395 } else if (count($items) == 1) {
398 $first = array_slice($items, 0, -1);
399 $last = array_slice($items, -1, 1);
400 // TRANS For building a list such as "You, bob, mary and 5 others have favored this notice".
401 return sprintf(_m('FAVELIST', '%1$s and %2$s'), implode(', ', $first), implode(', ', $last));
407 * Placeholder for showing faves...
409 class ThreadedNoticeListFavesItem extends NoticeListActorsItem
411 function getProfiles()
413 // @fixme caching & scalability!
415 $fave->notice_id = $this->notice->id;
419 while ($fave->fetch()) {
420 $profiles[] = $fave->user_id;
425 function getListMessage($count, $you)
427 if ($count == 1 && $you) {
428 // darn first person being different from third person!
429 return _m('FAVELIST', 'You have favored this notice.');
431 // if 'you' is the first item,
432 return _m('FAVELIST', '%1$s has favored this notice.', '%1$s have favored this notice.', $count);
438 $this->out->elementStart('li', array('class' => 'notice-data notice-faves'));
443 $this->out->elementEnd('li');
448 class ThreadedNoticeListInlineFavesItem extends ThreadedNoticeListFavesItem
452 $this->out->elementStart('div', array('class' => 'entry-content notice-faves'));
457 $this->out->elementEnd('div');
462 * Placeholder for showing faves...
464 class ThreadedNoticeListRepeatsItem extends NoticeListActorsItem
466 function getProfiles()
468 // @fixme caching & scalability!
470 $rep->repeat_of = $this->notice->id;
474 while ($rep->fetch()) {
475 $profiles[] = $rep->profile_id;
480 function getListMessage($count, $you)
482 if ($count == 1 && $you) {
483 // darn first person being different from third person!
484 return _m('REPEATLIST', 'You have repeated this notice.');
486 // if 'you' is the first item,
487 return _m('REPEATLIST', '%1$s has repeated this notice.', '%1$s have repeated this notice.', $count);
493 $this->out->elementStart('li', array('class' => 'notice-data notice-repeats'));
498 $this->out->elementEnd('li');