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'));
79 $notices = $this->notice->fetchAll();
80 $total = count($notices);
81 $notices = array_slice($notices, 0, NOTICES_PER_PAGE);
83 $allnotices = self::_allNotices($notices);
84 self::prefill($allnotices);
86 $conversations = array();
88 foreach ($notices as $notice) {
90 // Collapse repeats into their originals...
92 if ($notice->repeat_of) {
93 $orig = Notice::getKV('id', $notice->repeat_of);
98 $convo = $notice->conversation;
99 if (!empty($conversations[$convo])) {
100 // Seen this convo already -- skip!
103 $conversations[$convo] = true;
105 // Get the convo's root notice
106 $root = $notice->conversationRoot($this->userProfile);
112 $item = $this->newListItem($notice);
114 } catch (Exception $e) {
115 // we log exceptions and continue
116 common_log(LOG_ERR, $e->getMessage());
121 $this->out->elementEnd('ol');
122 $this->out->elementEnd('div');
127 function _allNotices($notices)
130 foreach ($notices as $notice) {
131 $convId[] = $notice->conversation;
133 $convId = array_unique($convId);
134 $allMap = Notice::listGet('conversation', $convId);
136 foreach ($allMap as $convId => $convNotices) {
137 $allArray = array_merge($allArray, $convNotices);
143 * returns a new list item for the current notice
145 * Recipe (factory?) method; overridden by sub-classes to give
146 * a different list item class.
148 * @param Notice $notice the current notice
150 * @return NoticeListItem a list item for displaying the notice
152 function newListItem($notice)
154 return new ThreadedNoticeListItem($notice, $this->out, $this->userProfile);
159 * widget for displaying a single notice
161 * This widget has the core smarts for showing a single notice: what to display,
162 * where, and under which circumstances. Its key method is show(); this is a recipe
163 * that calls all the other show*() methods to build up a single notice. The
164 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
165 * author info (since that's implicit by the data in the page).
169 * @author Evan Prodromou <evan@status.net>
170 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
171 * @link http://status.net/
173 * @see ProfileNoticeListItem
175 class ThreadedNoticeListItem extends NoticeListItem
177 protected $userProfile = null;
179 function __construct($notice, $out=null, $profile=null)
181 parent::__construct($notice, $out);
182 $this->userProfile = $profile;
185 function initialItems()
190 function showContext()
198 * Close the last elements in the notice list item
204 $max = $this->initialItems();
205 if (!$this->repeat) {
206 $stream = new ConversationNoticeStream($this->notice->conversation, $this->userProfile);
207 $notice = $stream->getNotices(0, $max + 2);
211 while ($notice->fetch()) {
212 if (Event::handle('StartAddNoticeReply', array($this, $this->notice, $notice))) {
213 if ($notice->id == $this->notice->id) {
220 $moreCutoff = clone($notice);
223 $notices[] = clone($notice); // *grumble* inefficient as hell
224 Event::handle('EndAddNoticeReply', array($this, $this->notice, $notice));
228 if (Event::handle('StartShowThreadedNoticeTail', array($this, $this->notice, &$notices))) {
229 $this->out->elementStart('ul', 'notices threaded-replies xoxo');
231 $item = new ThreadedNoticeListFavesItem($this->notice, $this->out);
232 $hasFaves = $item->show();
234 $item = new ThreadedNoticeListRepeatsItem($this->notice, $this->out);
235 $hasRepeats = $item->show();
240 $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out, count($notices));
243 foreach (array_reverse($notices) as $notice) {
244 if (Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice))) {
245 $item = new ThreadedNoticeListSubItem($notice, $this->notice, $this->out);
247 Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice));
252 if ($notices || $hasFaves || $hasRepeats) {
253 // @fixme do a proper can-post check that's consistent
255 if (common_current_user()) {
256 $item = new ThreadedNoticeListReplyItem($this->notice, $this->out);
260 $this->out->elementEnd('ul');
261 Event::handle('EndShowThreadedNoticeTail', array($this, $this->notice, $notices));
269 // @todo FIXME: needs documentation.
270 class ThreadedNoticeListSubItem extends NoticeListItem
272 protected $root = null;
274 function __construct($notice, $root, $out)
277 parent::__construct($notice, $out);
280 function avatarSize()
282 return AVATAR_STREAM_SIZE; // @fixme would like something in between
285 function showNoticeLocation()
290 function showNoticeSource()
295 function showContext()
300 function getReplyProfiles()
302 $all = parent::getReplyProfiles();
306 $rootAuthor = $this->root->getProfile();
308 foreach ($all as $profile) {
309 if ($profile->id != $rootAuthor->id) {
310 $profiles[] = $profile;
319 $item = new ThreadedNoticeListInlineFavesItem($this->notice, $this->out);
320 $hasFaves = $item->show();
326 * Placeholder for loading more replies...
328 class ThreadedNoticeListMoreItem extends NoticeListItem
332 function __construct($notice, $out, $cnt)
334 parent::__construct($notice, $out);
339 * recipe function for displaying a single notice.
341 * This uses all the other methods to correctly display a notice. Override
342 * it or one of the others to fine-tune the output.
349 $this->showMiniForm();
354 * start a single notice.
360 $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
363 function showMiniForm()
365 $id = $this->notice->conversation;
366 $url = common_local_url('conversationreplies', array('id' => $id));
368 $n = Conversation::noticeCount($id) - 1;
370 // TRANS: Link to show replies for a notice.
371 // TRANS: %d is the number of replies to a notice and used for plural.
372 $msg = sprintf(_m('Show reply', 'Show all %d replies', $n), $n);
374 $this->out->element('a', array('href' => $url), $msg);
379 * Placeholder for reply form...
380 * Same as get added at runtime via SN.U.NoticeInlineReplyPlaceholder
382 class ThreadedNoticeListReplyItem extends NoticeListItem
385 * recipe function for displaying a single notice.
387 * This uses all the other methods to correctly display a notice. Override
388 * it or one of the others to fine-tune the output.
395 $this->showMiniForm();
400 * start a single notice.
406 $this->out->elementStart('li', array('class' => 'notice-reply-placeholder'));
409 function showMiniForm()
411 $this->out->element('input', array('class' => 'placeholder',
412 // TRANS: Field label for reply mini form.
413 'value' => _('Write a reply...')));
418 * Placeholder for showing faves...
420 abstract class NoticeListActorsItem extends NoticeListItem
423 * @return array of profile IDs
425 abstract function getProfiles();
427 abstract function getListMessage($count, $you);
433 $cur = common_current_user();
434 foreach ($this->getProfiles() as $id) {
435 if ($cur && $cur->id == $id) {
437 // TRANS: Reference to the logged in user in favourite list.
438 array_unshift($links, _m('FAVELIST', 'You'));
440 $profile = Profile::getKV('id', $id);
442 $links[] = sprintf('<a href="%s">%s</a>',
443 htmlspecialchars($profile->profileurl),
444 htmlspecialchars($profile->getBestName()));
450 $count = count($links);
451 $msg = $this->getListMessage($count, $you);
452 $out = sprintf($msg, $this->magicList($links));
455 $this->out->raw($out);
463 function magicList($items)
465 if (count($items) == 0) {
467 } else if (count($items) == 1) {
470 $first = array_slice($items, 0, -1);
471 $last = array_slice($items, -1, 1);
472 // TRANS: Separator in list of user names like "Jim, Bob, Mary".
473 $separator = _(', ');
474 // TRANS: For building a list such as "Jim, Bob, Mary and 5 others like this".
475 // TRANS: %1$s is a list of users, separated by a separator (default: ", "), %2$s is the last user in the list.
476 return sprintf(_m('FAVELIST', '%1$s and %2$s'), implode($separator, $first), implode($separator, $last));
482 * Placeholder for showing faves...
484 class ThreadedNoticeListFavesItem extends NoticeListActorsItem
486 function getProfiles()
488 $faves = $this->notice->getFaves();
490 foreach ($faves as $fave) {
491 $profiles[] = $fave->user_id;
496 function magicList($items)
498 if (count($items) > 4) {
499 return parent::magicList(array_slice($items, 0, 3));
501 return parent::magicList($items);
505 function getListMessage($count, $you)
507 if ($count == 1 && $you) {
508 // darn first person being different from third person!
509 // TRANS: List message for notice favoured by logged in user.
510 return _m('FAVELIST', 'You like this.');
511 } else if ($count > 4) {
512 // TRANS: List message for when more than 4 people like something.
513 // TRANS: %%s is a list of users liking a notice, %d is the number over 4 that like the notice.
514 // TRANS: Plural is decided on the total number of users liking the notice (count of %%s + %d).
515 return sprintf(_m('%%s and %d others like this.',
516 '%%s and %d others like this.',
520 // TRANS: List message for favoured notices.
521 // TRANS: %%s is a list of users liking a notice.
522 // TRANS: Plural is based on the number of of users that have favoured a notice.
523 return sprintf(_m('%%s likes this.',
532 $this->out->elementStart('li', array('class' => 'notice-data notice-faves'));
537 $this->out->elementEnd('li');
541 // @todo FIXME: needs documentation.
542 class ThreadedNoticeListInlineFavesItem extends ThreadedNoticeListFavesItem
546 $this->out->elementStart('div', array('class' => 'entry-content notice-faves'));
551 $this->out->elementEnd('div');
556 * Placeholder for showing faves...
558 class ThreadedNoticeListRepeatsItem extends NoticeListActorsItem
560 function getProfiles()
562 $repeats = $this->notice->getRepeats();
566 foreach ($repeats as $rep) {
567 $profiles[] = $rep->profile_id;
573 function magicList($items)
575 if (count($items) > 4) {
576 return parent::magicList(array_slice($items, 0, 3));
578 return parent::magicList($items);
582 function getListMessage($count, $you)
584 if ($count == 1 && $you) {
585 // darn first person being different from third person!
586 // TRANS: List message for notice repeated by logged in user.
587 return _m('REPEATLIST', 'You repeated this.');
588 } else if ($count > 4) {
589 // TRANS: List message for when more than 4 people repeat something.
590 // TRANS: %%s is a list of users liking a notice, %d is the number over 4 that like the notice.
591 // TRANS: Plural is decided on the total number of users liking the notice (count of %%s + %d).
592 return sprintf(_m('%%s and %d other repeated this.',
593 '%%s and %d others repeated this.',
597 // TRANS: List message for favoured notices.
598 // TRANS: %%s is a list of users liking a notice.
599 // TRANS: Plural is based on the number of of users that have favoured a notice.
600 return sprintf(_m('%%s repeated this.',
601 '%%s repeated this.',
609 $this->out->elementStart('li', array('class' => 'notice-data notice-repeats'));
614 $this->out->elementEnd('li');