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 $root = $notice->conversationRoot();
100 $item = $this->newListItem($notice);
102 } catch (Exception $e) {
103 // we log exceptions and continue
104 common_log(LOG_ERR, $e->getMessage());
109 $this->out->elementEnd('ol');
110 $this->out->elementEnd('div');
116 * returns a new list item for the current notice
118 * Recipe (factory?) method; overridden by sub-classes to give
119 * a different list item class.
121 * @param Notice $notice the current notice
123 * @return NoticeListItem a list item for displaying the notice
126 function newListItem($notice)
128 return new ThreadedNoticeListItem($notice, $this->out);
133 * widget for displaying a single notice
135 * This widget has the core smarts for showing a single notice: what to display,
136 * where, and under which circumstances. Its key method is show(); this is a recipe
137 * that calls all the other show*() methods to build up a single notice. The
138 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
139 * author info (since that's implicit by the data in the page).
143 * @author Evan Prodromou <evan@status.net>
144 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
145 * @link http://status.net/
147 * @see ProfileNoticeListItem
150 class ThreadedNoticeListItem extends NoticeListItem
152 function initialItems()
157 function showContext()
165 * Close the last elements in the notice list item
172 $max = $this->initialItems();
173 if (!$this->repeat) {
174 $notice = Notice::conversationStream($this->notice->conversation, 0, $max + 2);
178 while ($notice->fetch()) {
179 if ($notice->id == $this->notice->id) {
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('conversationreplies', array('id' => $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 $fave = Fave::byNotice($this->notice->id);
415 while ($fave->fetch()) {
416 $profiles[] = $fave->user_id;
421 function getListMessage($count, $you)
423 if ($count == 1 && $you) {
424 // darn first person being different from third person!
425 return _m('FAVELIST', 'You have favored this notice.');
427 // if 'you' is the first item,
428 return _m('FAVELIST', '%1$s has favored this notice.', '%1$s have favored this notice.', $count);
434 $this->out->elementStart('li', array('class' => 'notice-data notice-faves'));
439 $this->out->elementEnd('li');
444 class ThreadedNoticeListInlineFavesItem extends ThreadedNoticeListFavesItem
448 $this->out->elementStart('div', array('class' => 'entry-content notice-faves'));
453 $this->out->elementEnd('div');
458 * Placeholder for showing faves...
460 class ThreadedNoticeListRepeatsItem extends NoticeListActorsItem
462 function getProfiles()
464 $rep = $this->notice->repeatStream();
467 while ($rep->fetch()) {
468 $profiles[] = $rep->profile_id;
473 function getListMessage($count, $you)
475 if ($count == 1 && $you) {
476 // darn first person being different from third person!
477 return _m('REPEATLIST', 'You have repeated this notice.');
479 // if 'you' is the first item,
480 return _m('REPEATLIST', '%1$s has repeated this notice.', '%1$s have repeated this notice.', $count);
486 $this->out->elementStart('li', array('class' => 'notice-data notice-repeats'));
491 $this->out->elementEnd('li');