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 $convo = $this->notice->conversation;
79 if (!empty($conversations[$convo])) {
80 // Seen this convo already -- skip!
83 $conversations[$convo] = true;
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);
89 $notice = new Notice();
90 $notice->conversation = $this->notice->conversation;
91 $notice->orderBy('CREATED');
96 $item = $this->newListItem($notice);
98 } catch (Exception $e) {
99 // we log exceptions and continue
100 common_log(LOG_ERR, $e->getMessage());
105 $this->out->elementEnd('ol');
106 $this->out->elementEnd('div');
112 * returns a new list item for the current notice
114 * Recipe (factory?) method; overridden by sub-classes to give
115 * a different list item class.
117 * @param Notice $notice the current notice
119 * @return NoticeListItem a list item for displaying the notice
122 function newListItem($notice)
124 return new ThreadedNoticeListItem($notice, $this->out);
129 * widget for displaying a single notice
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).
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/
143 * @see ProfileNoticeListItem
146 class ThreadedNoticeListItem extends NoticeListItem
148 const INITIAL_ITEMS = 3;
150 function showContext()
158 * Close the last elements in the notice list item
165 if (!$this->repeat) {
166 $notice = Notice::conversationStream($this->notice->conversation, 0, self::INITIAL_ITEMS + 2);
170 while ($notice->fetch()) {
171 if ($notice->id == $this->notice->id) {
176 if ($cnt > self::INITIAL_ITEMS) {
178 $moreCutoff = clone($notice);
181 $notices[] = clone($notice); // *grumble* inefficient as hell
184 $this->out->elementStart('ul', 'notices threaded-replies xoxo');
185 $item = new ThreadedNoticeListFavesItem($this->notice, $this->out);
186 $hasFaves = $item->show();
189 $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out);
192 foreach (array_reverse($notices) as $notice) {
193 $item = new ThreadedNoticeListSubItem($notice, $this->out);
197 if ($notices || $hasFaves) {
198 // @fixme do a proper can-post check that's consistent
200 if (common_current_user()) {
201 $item = new ThreadedNoticeListReplyItem($this->notice, $this->out);
205 $this->out->elementEnd('ul');
212 class ThreadedNoticeListSubItem extends NoticeListItem
215 function avatarSize()
217 return AVATAR_STREAM_SIZE; // @fixme would like something in between
220 function showNoticeLocation()
225 function showNoticeSource()
230 function showContext()
237 $item = new ThreadedNoticeListInlineFavesItem($this->notice, $this->out);
238 $hasFaves = $item->show();
244 * Placeholder for loading more replies...
246 class ThreadedNoticeListMoreItem extends NoticeListItem
250 * recipe function for displaying a single notice.
252 * This uses all the other methods to correctly display a notice. Override
253 * it or one of the others to fine-tune the output.
261 $this->showMiniForm();
266 * start a single notice.
273 $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
276 function showMiniForm()
278 $id = $this->notice->conversation;
279 $url = common_local_url('conversation', array('id' => $id)) . '#notice-' . $this->notice->id;
281 $notice = new Notice();
282 $notice->conversation = $id;
283 $n = $notice->count() - 1;
284 $msg = sprintf(_m('Show %d reply', 'Show all %d replies', $n), $n);
286 $this->out->element('a', array('href' => $url), $msg);
292 * Placeholder for reply form...
293 * Same as get added at runtime via SN.U.NoticeInlineReplyPlaceholder
295 class ThreadedNoticeListReplyItem extends NoticeListItem
299 * recipe function for displaying a single notice.
301 * This uses all the other methods to correctly display a notice. Override
302 * it or one of the others to fine-tune the output.
310 $this->showMiniForm();
315 * start a single notice.
322 $this->out->elementStart('li', array('class' => 'notice-reply-placeholder'));
325 function showMiniForm()
327 $this->out->element('input', array('class' => 'placeholder',
328 'value' => _('Write a reply...')));
333 * Placeholder for showing faves...
335 class ThreadedNoticeListFavesItem extends NoticeListItem
339 // @fixme caching & scalability!
341 $fave->notice_id = $this->notice->id;
344 $cur = common_current_user();
347 while ($fave->fetch()) {
348 if ($cur && $cur->id == $fave->user_id) {
351 $profiles[] = $fave->user_id;
357 $links[] = _m('FAVELIST', 'You');
359 foreach ($profiles as $id) {
360 $profile = Profile::staticGet('id', $id);
362 $links[] = sprintf('<a href="%s" title="%s">%s</a>',
363 htmlspecialchars($profile->profileurl),
364 htmlspecialchars($profile->getBestName()),
365 htmlspecialchars($profile->nickname));
370 $count = count($links);
371 if ($count == 1 && $you) {
372 // darn first person being different from third person!
373 $msg = _m('FAVELIST', 'You have favored this notice.');
375 // if 'you' is the first item,
376 $msg = _m('FAVELIST', '%1$s has favored this notice.', '%1$s have favored this notice.', $count);
378 $out = sprintf($msg, $this->magicList($links));
381 $this->out->raw($out);
391 $this->out->elementStart('li', array('class' => 'notice-data notice-faves'));
396 $this->out->elementEnd('li');
399 function magicList($items)
401 if (count($items) == 0) {
403 } else if (count($items) == 1) {
406 $first = array_slice($items, 0, -1);
407 $last = array_slice($items, -1, 1);
408 // TRANS For building a list such as "You, bob, mary and 5 others have favored this notice".
409 return sprintf(_m('FAVELIST', '%1$s and %2$s'), implode(', ', $first), implode(', ', $last));
414 class ThreadedNoticeListInlineFavesItem extends ThreadedNoticeListFavesItem
418 $this->out->elementStart('div', array('class' => 'entry-content notice-faves'));
423 $this->out->elementEnd('div');