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 ($notice->id == $this->notice->id) {
202 $moreCutoff = clone($notice);
205 $notices[] = clone($notice); // *grumble* inefficient as hell
208 if (Event::handle('StartShowThreadedNoticeTail', array($this, $this->notice, &$notices))) {
209 $this->out->elementStart('ul', 'notices threaded-replies xoxo');
211 $item = new ThreadedNoticeListFavesItem($this->notice, $this->out);
212 $hasFaves = $item->show();
214 $item = new ThreadedNoticeListRepeatsItem($this->notice, $this->out);
215 $hasRepeats = $item->show();
219 $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out);
222 foreach (array_reverse($notices) as $notice) {
223 if (Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice))) {
224 $item = new ThreadedNoticeListSubItem($notice, $this->notice, $this->out);
226 Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice));
231 if ($notices || $hasFaves || $hasRepeats) {
232 // @fixme do a proper can-post check that's consistent
234 if (common_current_user()) {
235 $item = new ThreadedNoticeListReplyItem($this->notice, $this->out);
239 $this->out->elementEnd('ul');
240 Event::handle('EndShowThreadedNoticeTail', array($this, $this->notice, $notices));
248 // @todo FIXME: needs documentation.
249 class ThreadedNoticeListSubItem extends NoticeListItem
251 protected $root = null;
253 function __construct($notice, $root, $out)
256 parent::__construct($notice, $out);
259 function avatarSize()
261 return AVATAR_STREAM_SIZE; // @fixme would like something in between
264 function showNoticeLocation()
269 function showNoticeSource()
274 function showContext()
279 function getReplyProfiles()
281 $all = parent::getReplyProfiles();
285 $rootAuthor = $this->root->getProfile();
287 foreach ($all as $profile) {
288 if ($profile->id != $rootAuthor->id) {
289 $profiles[] = $profile;
298 $item = new ThreadedNoticeListInlineFavesItem($this->notice, $this->out);
299 $hasFaves = $item->show();
305 * Placeholder for loading more replies...
307 class ThreadedNoticeListMoreItem extends NoticeListItem
310 * recipe function for displaying a single notice.
312 * This uses all the other methods to correctly display a notice. Override
313 * it or one of the others to fine-tune the output.
320 $this->showMiniForm();
325 * start a single notice.
331 $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
334 function showMiniForm()
336 $id = $this->notice->conversation;
337 $url = common_local_url('conversationreplies', array('id' => $id));
339 $n = Conversation::noticeCount($id) - 1;
341 // TRANS: Link to show replies for a notice.
342 // TRANS: %d is the number of replies to a notice and used for plural.
343 $msg = sprintf(_m('Show reply', 'Show all %d replies', $n), $n);
345 $this->out->element('a', array('href' => $url), $msg);
350 * Placeholder for reply form...
351 * Same as get added at runtime via SN.U.NoticeInlineReplyPlaceholder
353 class ThreadedNoticeListReplyItem extends NoticeListItem
356 * recipe function for displaying a single notice.
358 * This uses all the other methods to correctly display a notice. Override
359 * it or one of the others to fine-tune the output.
366 $this->showMiniForm();
371 * start a single notice.
377 $this->out->elementStart('li', array('class' => 'notice-reply-placeholder'));
380 function showMiniForm()
382 $this->out->element('input', array('class' => 'placeholder',
383 // TRANS: Field label for reply mini form.
384 'value' => _('Write a reply...')));
389 * Placeholder for showing faves...
391 abstract class NoticeListActorsItem extends NoticeListItem
394 * @return array of profile IDs
396 abstract function getProfiles();
398 abstract function getListMessage($count, $you);
404 $cur = common_current_user();
405 foreach ($this->getProfiles() as $id) {
406 if ($cur && $cur->id == $id) {
408 // TRANS: Reference to the logged in user in favourite list.
409 array_unshift($links, _m('FAVELIST', 'You'));
411 $profile = Profile::staticGet('id', $id);
413 $links[] = sprintf('<a href="%s" title="%s">%s</a>',
414 htmlspecialchars($profile->profileurl),
415 htmlspecialchars($profile->getBestName()),
416 htmlspecialchars($profile->nickname));
422 $count = count($links);
423 $msg = $this->getListMessage($count, $you);
424 $out = sprintf($msg, $this->magicList($links));
427 $this->out->raw($out);
435 function magicList($items)
437 if (count($items) == 0) {
439 } else if (count($items) == 1) {
442 $first = array_slice($items, 0, -1);
443 $last = array_slice($items, -1, 1);
444 // TRANS: Separator in list of user names like "You, Bob, Mary".
445 $separator = _(', ');
446 // TRANS: For building a list such as "You, bob, mary and 5 others have favored this notice".
447 // TRANS: %1$s is a list of users, separated by a separator (default: ", "), %2$s is the last user in the list.
448 return sprintf(_m('FAVELIST', '%1$s and %2$s'), implode($separator, $first), implode($separator, $last));
454 * Placeholder for showing faves...
456 class ThreadedNoticeListFavesItem extends NoticeListActorsItem
458 function getProfiles()
460 $fave = Fave::byNotice($this->notice->id);
462 while ($fave->fetch()) {
463 $profiles[] = $fave->user_id;
468 function getListMessage($count, $you)
470 if ($count == 1 && $you) {
471 // darn first person being different from third person!
472 // TRANS: List message for notice favoured by logged in user.
473 return _m('FAVELIST', 'You have favored this notice.');
475 // TRANS: List message for favoured notices.
476 // TRANS: %d is the number of users that have favoured a notice.
477 return sprintf(_m('One person has favored this notice.',
478 '%d people have favored this notice.',
486 $this->out->elementStart('li', array('class' => 'notice-data notice-faves'));
491 $this->out->elementEnd('li');
496 // @todo FIXME: needs documentation.
497 class ThreadedNoticeListInlineFavesItem extends ThreadedNoticeListFavesItem
501 $this->out->elementStart('div', array('class' => 'entry-content notice-faves'));
506 $this->out->elementEnd('div');
511 * Placeholder for showing faves...
513 class ThreadedNoticeListRepeatsItem extends NoticeListActorsItem
515 function getProfiles()
517 $rep = $this->notice->repeatStream();
520 while ($rep->fetch()) {
521 $profiles[] = $rep->profile_id;
526 function getListMessage($count, $you)
528 if ($count == 1 && $you) {
529 // darn first person being different from third person!
530 // TRANS: List message for notice repeated by logged in user.
531 return _m('REPEATLIST', 'You have repeated this notice.');
533 // TRANS: List message for repeated notices.
534 // TRANS: %d is the number of users that have repeated a notice.
535 return sprintf(_m('One person has repeated this notice.',
536 '%d people have repeated this notice.',
544 $this->out->elementStart('li', array('class' => 'notice-data notice-repeats'));
549 $this->out->elementEnd('li');