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
51 class ThreadedNoticeList extends NoticeList
53 protected $userProfile;
55 function __construct($notice, $out=null, $profile=-1)
57 parent::__construct($notice, $out);
58 if (is_int($profile) && $profile == -1) {
59 $profile = Profile::current();
61 $this->userProfile = $profile;
65 * show the list of notices
67 * "Uses up" the stream by looping through it. So, probably can't
68 * be called twice on the same list.
70 * @return int count of notices listed.
74 $this->out->elementStart('div', array('id' =>'notices_primary'));
75 // TRANS: Header for Notices section.
76 $this->out->element('h2', null, _m('HEADER','Notices'));
77 $this->out->elementStart('ol', array('class' => 'notices threaded-notices xoxo'));
80 $conversations = array();
81 while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
84 if ($cnt > NOTICES_PER_PAGE) {
88 // Collapse repeats into their originals...
89 $notice = $this->notice;
90 if ($notice->repeat_of) {
91 $orig = Notice::staticGet('id', $notice->repeat_of);
96 $convo = $notice->conversation;
97 if (!empty($conversations[$convo])) {
98 // Seen this convo already -- skip!
101 $conversations[$convo] = true;
103 // Get the convo's root notice
104 $root = $notice->conversationRoot($this->userProfile);
110 $item = $this->newListItem($notice);
112 } catch (Exception $e) {
113 // we log exceptions and continue
114 common_log(LOG_ERR, $e->getMessage());
119 $this->out->elementEnd('ol');
120 $this->out->elementEnd('div');
126 * returns a new list item for the current notice
128 * Recipe (factory?) method; overridden by sub-classes to give
129 * a different list item class.
131 * @param Notice $notice the current notice
133 * @return NoticeListItem a list item for displaying the notice
135 function newListItem($notice)
137 return new ThreadedNoticeListItem($notice, $this->out, $this->userProfile);
142 * widget for displaying a single notice
144 * This widget has the core smarts for showing a single notice: what to display,
145 * where, and under which circumstances. Its key method is show(); this is a recipe
146 * that calls all the other show*() methods to build up a single notice. The
147 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
148 * author info (since that's implicit by the data in the page).
152 * @author Evan Prodromou <evan@status.net>
153 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
154 * @link http://status.net/
156 * @see ProfileNoticeListItem
158 class ThreadedNoticeListItem extends NoticeListItem
160 protected $userProfile = null;
162 function __construct($notice, $out=null, $profile=null)
164 parent::__construct($notice, $out);
165 $this->userProfile = $profile;
168 function initialItems()
173 function showContext()
181 * Close the last elements in the notice list item
187 $max = $this->initialItems();
188 if (!$this->repeat) {
189 $stream = new ConversationNoticeStream($this->notice->conversation, $this->userProfile);
190 $notice = $stream->getNotices(0, $max + 2);
194 while ($notice->fetch()) {
195 if (Event::handle('StartAddNoticeReply', array($this, $this->notice, $notice))) {
196 if ($notice->id == $this->notice->id) {
203 $moreCutoff = clone($notice);
206 $notices[] = clone($notice); // *grumble* inefficient as hell
207 Event::handle('EndAddNoticeReply', array($this, $this->notice, $notice));
211 if (Event::handle('StartShowThreadedNoticeTail', array($this, $this->notice, &$notices))) {
212 $this->out->elementStart('ul', 'notices threaded-replies xoxo');
214 $item = new ThreadedNoticeListFavesItem($this->notice, $this->out);
215 $hasFaves = $item->show();
217 $item = new ThreadedNoticeListRepeatsItem($this->notice, $this->out);
218 $hasRepeats = $item->show();
223 $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out, count($notices));
226 foreach (array_reverse($notices) as $notice) {
227 if (Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice))) {
228 $item = new ThreadedNoticeListSubItem($notice, $this->notice, $this->out);
230 Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice));
235 if ($notices || $hasFaves || $hasRepeats) {
236 // @fixme do a proper can-post check that's consistent
238 if (common_current_user()) {
239 $item = new ThreadedNoticeListReplyItem($this->notice, $this->out);
243 $this->out->elementEnd('ul');
244 Event::handle('EndShowThreadedNoticeTail', array($this, $this->notice, $notices));
252 // @todo FIXME: needs documentation.
253 class ThreadedNoticeListSubItem extends NoticeListItem
255 protected $root = null;
257 function __construct($notice, $root, $out)
260 parent::__construct($notice, $out);
263 function avatarSize()
265 return AVATAR_STREAM_SIZE; // @fixme would like something in between
268 function showNoticeLocation()
273 function showNoticeSource()
278 function showContext()
283 function getReplyProfiles()
285 $all = parent::getReplyProfiles();
289 $rootAuthor = $this->root->getProfile();
291 foreach ($all as $profile) {
292 if ($profile->id != $rootAuthor->id) {
293 $profiles[] = $profile;
302 $item = new ThreadedNoticeListInlineFavesItem($this->notice, $this->out);
303 $hasFaves = $item->show();
309 * Placeholder for loading more replies...
311 class ThreadedNoticeListMoreItem extends NoticeListItem
315 function __construct($notice, $out, $cnt)
317 parent::__construct($notice, $out);
322 * recipe function for displaying a single notice.
324 * This uses all the other methods to correctly display a notice. Override
325 * it or one of the others to fine-tune the output.
332 $this->showMiniForm();
337 * start a single notice.
343 $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
346 function showMiniForm()
348 $id = $this->notice->conversation;
349 $url = common_local_url('conversationreplies', array('id' => $id));
351 $n = Conversation::noticeCount($id) - 1;
353 // TRANS: Link to show replies for a notice.
354 // TRANS: %d is the number of replies to a notice and used for plural.
355 $msg = sprintf(_m('Show reply', 'Show all %d replies', $n), $n);
357 $this->out->element('a', array('href' => $url), $msg);
362 * Placeholder for reply form...
363 * Same as get added at runtime via SN.U.NoticeInlineReplyPlaceholder
365 class ThreadedNoticeListReplyItem extends NoticeListItem
368 * recipe function for displaying a single notice.
370 * This uses all the other methods to correctly display a notice. Override
371 * it or one of the others to fine-tune the output.
378 $this->showMiniForm();
383 * start a single notice.
389 $this->out->elementStart('li', array('class' => 'notice-reply-placeholder'));
392 function showMiniForm()
394 $this->out->element('input', array('class' => 'placeholder',
395 // TRANS: Field label for reply mini form.
396 'value' => _('Write a reply...')));
401 * Placeholder for showing faves...
403 abstract class NoticeListActorsItem extends NoticeListItem
406 * @return array of profile IDs
408 abstract function getProfiles();
410 abstract function getListMessage($count, $you);
416 $cur = common_current_user();
417 foreach ($this->getProfiles() as $id) {
418 if ($cur && $cur->id == $id) {
420 // TRANS: Reference to the logged in user in favourite list.
421 array_unshift($links, _m('FAVELIST', 'You'));
423 $profile = Profile::staticGet('id', $id);
425 $links[] = sprintf('<a href="%s">%s</a>',
426 htmlspecialchars($profile->profileurl),
427 htmlspecialchars($profile->getBestName()));
433 $count = count($links);
434 $msg = $this->getListMessage($count, $you);
435 $out = sprintf($msg, $this->magicList($links));
438 $this->out->raw($out);
446 function magicList($items)
448 if (count($items) == 0) {
450 } else if (count($items) == 1) {
453 $first = array_slice($items, 0, -1);
454 $last = array_slice($items, -1, 1);
455 // TRANS: Separator in list of user names like "Jim, Bob, Mary".
456 $separator = _(', ');
457 // TRANS: For building a list such as "Jim, Bob, Mary and 5 others like this".
458 // TRANS: %1$s is a list of users, separated by a separator (default: ", "), %2$s is the last user in the list.
459 return sprintf(_m('FAVELIST', '%1$s and %2$s'), implode($separator, $first), implode($separator, $last));
465 * Placeholder for showing faves...
467 class ThreadedNoticeListFavesItem extends NoticeListActorsItem
469 function getProfiles()
471 $fave = Fave::byNotice($this->notice->id);
473 while ($fave->fetch()) {
474 $profiles[] = $fave->user_id;
479 function magicList($items)
481 if (count($items) > 4) {
482 return parent::magicList(array_slice($items, 0, 3));
484 return parent::magicList($items);
488 function getListMessage($count, $you)
490 if ($count == 1 && $you) {
491 // darn first person being different from third person!
492 // TRANS: List message for notice favoured by logged in user.
493 return _m('FAVELIST', 'You like this.');
494 } else if ($count > 4) {
495 // TRANS: List message for when more than 4 people like something.
496 // TRANS: %%s is a list of users liking a notice, %d is the number over 4 that like the notice.
497 // TRANS: Plural is decided on the total number of users liking the notice (count of %%s + %d).
498 return sprintf(_m('%%s and %d others like this.',
499 '%%s and %d others like this.',
503 // TRANS: List message for favoured notices.
504 // TRANS: %%s is a list of users liking a notice.
505 // TRANS: Plural is based on the number of of users that have favoured a notice.
506 return sprintf(_m('%%s likes this.',
515 $this->out->elementStart('li', array('class' => 'notice-data notice-faves'));
520 $this->out->elementEnd('li');
524 // @todo FIXME: needs documentation.
525 class ThreadedNoticeListInlineFavesItem extends ThreadedNoticeListFavesItem
529 $this->out->elementStart('div', array('class' => 'entry-content notice-faves'));
534 $this->out->elementEnd('div');
539 * Placeholder for showing faves...
541 class ThreadedNoticeListRepeatsItem extends NoticeListActorsItem
543 function getProfiles()
545 $rep = $this->notice->repeatStream();
548 while ($rep->fetch()) {
549 $profiles[] = $rep->profile_id;
554 function getListMessage($count, $you)
556 if ($count == 1 && $you) {
557 // darn first person being different from third person!
558 // TRANS: List message for notice repeated by logged in user.
559 return _m('REPEATLIST', 'You have repeated this notice.');
561 // TRANS: List message for repeated notices.
562 // TRANS: %d is the number of users that have repeated a notice.
563 return sprintf(_m('One person has repeated this notice.',
564 '%d people have repeated this notice.',
572 $this->out->elementStart('li', array('class' => 'notice-data notice-repeats'));
577 $this->out->elementEnd('li');