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('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
33 * widget for displaying a list of notices
35 * There are a number of actions that display a list of notices, in
36 * reverse chronological order. This widget abstracts out most of the
37 * code for UI for notice lists. It's overridden to hide some
38 * data for e.g. the profile page.
42 * @author Evan Prodromou <evan@status.net>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://status.net/
47 * @see ProfileNoticeList
49 class ThreadedNoticeList extends NoticeList
51 protected $userProfile;
53 function __construct($notice, $out=null, $profile=-1)
55 parent::__construct($notice, $out);
56 if (is_int($profile) && $profile == -1) {
57 $profile = Profile::current();
59 $this->userProfile = $profile;
63 * show the list of notices
65 * "Uses up" the stream by looping through it. So, probably can't
66 * be called twice on the same list.
68 * @return int count of notices listed.
72 $this->out->elementStart('div', array('id' =>'notices_primary'));
73 // TRANS: Header for Notices section.
74 $this->out->element('h2', null, _m('HEADER','Notices'));
75 $this->out->elementStart('ol', array('class' => 'notices threaded-notices xoxo'));
77 $notices = $this->notice->fetchAll();
78 $total = count($notices);
79 $notices = array_slice($notices, 0, NOTICES_PER_PAGE);
81 $allnotices = self::_allNotices($notices);
82 self::prefill($allnotices);
84 $conversations = array();
86 foreach ($notices as $notice) {
88 // Collapse repeats into their originals...
90 if ($notice->repeat_of) {
91 $orig = Notice::getKV('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');
125 function _allNotices($notices)
128 foreach ($notices as $notice) {
129 $convId[] = $notice->conversation;
131 $convId = array_unique($convId);
132 $allMap = Notice::listGet('conversation', $convId);
134 foreach ($allMap as $convId => $convNotices) {
135 $allArray = array_merge($allArray, $convNotices);
141 * returns a new list item for the current notice
143 * Recipe (factory?) method; overridden by sub-classes to give
144 * a different list item class.
146 * @param Notice $notice the current notice
148 * @return NoticeListItem a list item for displaying the notice
150 function newListItem($notice)
152 return new ThreadedNoticeListItem($notice, $this->out, $this->userProfile);
157 * widget for displaying a single notice
159 * This widget has the core smarts for showing a single notice: what to display,
160 * where, and under which circumstances. Its key method is show(); this is a recipe
161 * that calls all the other show*() methods to build up a single notice. The
162 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
163 * author info (since that's implicit by the data in the page).
167 * @author Evan Prodromou <evan@status.net>
168 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
169 * @link http://status.net/
171 * @see ProfileNoticeListItem
173 class ThreadedNoticeListItem extends NoticeListItem
175 protected $userProfile = null;
177 function __construct($notice, $out=null, $profile=null)
179 parent::__construct($notice, $out);
180 $this->userProfile = $profile;
183 function initialItems()
188 function showContext()
196 * Close the last elements in the notice list item
202 $max = $this->initialItems();
203 if (!$this->repeat) {
204 $stream = new ConversationNoticeStream($this->notice->conversation, $this->userProfile);
205 $notice = $stream->getNotices(0, $max + 2);
209 while ($notice->fetch()) {
210 if (Event::handle('StartAddNoticeReply', array($this, $this->notice, $notice))) {
211 if ($notice->id == $this->notice->id) {
218 $moreCutoff = clone($notice);
221 $notices[] = clone($notice); // *grumble* inefficient as hell
222 Event::handle('EndAddNoticeReply', array($this, $this->notice, $notice));
226 if (Event::handle('StartShowThreadedNoticeTail', array($this, $this->notice, &$notices))) {
227 $this->out->elementStart('ul', 'notices threaded-replies xoxo');
229 $item = new ThreadedNoticeListFavesItem($this->notice, $this->out);
230 $hasFaves = $item->show();
232 $item = new ThreadedNoticeListRepeatsItem($this->notice, $this->out);
233 $hasRepeats = $item->show();
238 $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out, count($notices));
241 foreach (array_reverse($notices) as $notice) {
242 if (Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice))) {
243 $item = new ThreadedNoticeListSubItem($notice, $this->notice, $this->out);
245 Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice));
250 if ($notices || $hasFaves || $hasRepeats) {
251 // @fixme do a proper can-post check that's consistent
253 if (common_current_user()) {
254 $item = new ThreadedNoticeListReplyItem($this->notice, $this->out);
258 $this->out->elementEnd('ul');
259 Event::handle('EndShowThreadedNoticeTail', array($this, $this->notice, $notices));
267 // @todo FIXME: needs documentation.
268 class ThreadedNoticeListSubItem extends NoticeListItem
270 protected $root = null;
272 function __construct($notice, $root, $out)
275 parent::__construct($notice, $out);
278 function avatarSize()
280 return AVATAR_STREAM_SIZE; // @fixme would like something in between
283 function showNoticeLocation()
288 function showNoticeSource()
293 function showContext()
298 function getReplyProfiles()
300 $all = parent::getReplyProfiles();
304 $rootAuthor = $this->root->getProfile();
306 foreach ($all as $profile) {
307 if ($profile->id != $rootAuthor->id) {
308 $profiles[] = $profile;
317 $item = new ThreadedNoticeListInlineFavesItem($this->notice, $this->out);
318 $hasFaves = $item->show();
324 * Placeholder for loading more replies...
326 class ThreadedNoticeListMoreItem extends NoticeListItem
330 function __construct($notice, $out, $cnt)
332 parent::__construct($notice, $out);
337 * recipe function for displaying a single notice.
339 * This uses all the other methods to correctly display a notice. Override
340 * it or one of the others to fine-tune the output.
347 $this->showMiniForm();
352 * start a single notice.
358 $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
361 function showMiniForm()
363 $id = $this->notice->conversation;
364 $url = common_local_url('conversationreplies', array('id' => $id));
366 $n = Conversation::noticeCount($id) - 1;
368 // TRANS: Link to show replies for a notice.
369 // TRANS: %d is the number of replies to a notice and used for plural.
370 $msg = sprintf(_m('Show reply', 'Show all %d replies', $n), $n);
372 $this->out->element('a', array('href' => $url), $msg);
377 * Placeholder for reply form...
378 * Same as get added at runtime via SN.U.NoticeInlineReplyPlaceholder
380 class ThreadedNoticeListReplyItem extends NoticeListItem
383 * recipe function for displaying a single notice.
385 * This uses all the other methods to correctly display a notice. Override
386 * it or one of the others to fine-tune the output.
393 $this->showMiniForm();
398 * start a single notice.
404 $this->out->elementStart('li', array('class' => 'notice-reply-placeholder'));
407 function showMiniForm()
409 $this->out->element('input', array('class' => 'placeholder',
410 // TRANS: Field label for reply mini form.
411 'value' => _('Write a reply...')));
416 * Placeholder for showing faves...
418 abstract class NoticeListActorsItem extends NoticeListItem
421 * @return array of profile IDs
423 abstract function getProfiles();
425 abstract function getListMessage($count, $you);
431 $cur = common_current_user();
432 foreach ($this->getProfiles() as $id) {
433 if ($cur && $cur->id == $id) {
435 // TRANS: Reference to the logged in user in favourite list.
436 array_unshift($links, _m('FAVELIST', 'You'));
438 $profile = Profile::getKV('id', $id);
440 $links[] = sprintf('<a href="%s">%s</a>',
441 htmlspecialchars($profile->profileurl),
442 htmlspecialchars($profile->getBestName()));
448 $count = count($links);
449 $msg = $this->getListMessage($count, $you);
450 $out = sprintf($msg, $this->magicList($links));
453 $this->out->raw($out);
461 function magicList($items)
463 if (count($items) == 0) {
465 } else if (count($items) == 1) {
468 $first = array_slice($items, 0, -1);
469 $last = array_slice($items, -1, 1);
470 // TRANS: Separator in list of user names like "Jim, Bob, Mary".
471 $separator = _(', ');
472 // TRANS: For building a list such as "Jim, Bob, Mary and 5 others like this".
473 // TRANS: %1$s is a list of users, separated by a separator (default: ", "), %2$s is the last user in the list.
474 return sprintf(_m('FAVELIST', '%1$s and %2$s'), implode($separator, $first), implode($separator, $last));
480 * Placeholder for showing faves...
482 class ThreadedNoticeListFavesItem extends NoticeListActorsItem
484 function getProfiles()
486 $faves = $this->notice->getFaves();
488 foreach ($faves as $fave) {
489 $profiles[] = $fave->user_id;
494 function magicList($items)
496 if (count($items) > 4) {
497 return parent::magicList(array_slice($items, 0, 3));
499 return parent::magicList($items);
503 function getListMessage($count, $you)
505 if ($count == 1 && $you) {
506 // darn first person being different from third person!
507 // TRANS: List message for notice favoured by logged in user.
508 return _m('FAVELIST', 'You like this.');
509 } else if ($count > 4) {
510 // TRANS: List message for when more than 4 people like something.
511 // TRANS: %%s is a list of users liking a notice, %d is the number over 4 that like the notice.
512 // TRANS: Plural is decided on the total number of users liking the notice (count of %%s + %d).
513 return sprintf(_m('%%s and %d others like this.',
514 '%%s and %d others like this.',
518 // TRANS: List message for favoured notices.
519 // TRANS: %%s is a list of users liking a notice.
520 // TRANS: Plural is based on the number of of users that have favoured a notice.
521 return sprintf(_m('%%s likes this.',
530 $this->out->elementStart('li', array('class' => 'notice-data notice-faves'));
535 $this->out->elementEnd('li');
539 // @todo FIXME: needs documentation.
540 class ThreadedNoticeListInlineFavesItem extends ThreadedNoticeListFavesItem
544 $this->out->elementStart('div', array('class' => 'entry-content notice-faves'));
549 $this->out->elementEnd('div');
554 * Placeholder for showing faves...
556 class ThreadedNoticeListRepeatsItem extends NoticeListActorsItem
558 function getProfiles()
560 $repeats = $this->notice->getRepeats();
564 foreach ($repeats as $rep) {
565 $profiles[] = $rep->profile_id;
571 function magicList($items)
573 if (count($items) > 4) {
574 return parent::magicList(array_slice($items, 0, 3));
576 return parent::magicList($items);
580 function getListMessage($count, $you)
582 if ($count == 1 && $you) {
583 // darn first person being different from third person!
584 // TRANS: List message for notice repeated by logged in user.
585 return _m('REPEATLIST', 'You repeated this.');
586 } else if ($count > 4) {
587 // TRANS: List message for when more than 4 people repeat something.
588 // TRANS: %%s is a list of users liking a notice, %d is the number over 4 that like the notice.
589 // TRANS: Plural is decided on the total number of users liking the notice (count of %%s + %d).
590 return sprintf(_m('%%s and %d other repeated this.',
591 '%%s and %d others repeated this.',
595 // TRANS: List message for favoured notices.
596 // TRANS: %%s is a list of users liking a notice.
597 // TRANS: Plural is based on the number of of users that have favoured a notice.
598 return sprintf(_m('%%s repeated this.',
599 '%%s repeated this.',
607 $this->out->elementStart('li', array('class' => 'notice-data notice-repeats'));
612 $this->out->elementEnd('li');