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 self::prefill(self::_allNotices($notices));
85 $conversations = array();
87 foreach ($notices as $notice) {
89 // Collapse repeats into their originals...
91 if ($notice->repeat_of) {
92 $orig = Notice::staticGet('id', $notice->repeat_of);
97 $convo = $notice->conversation;
98 if (!empty($conversations[$convo])) {
99 // Seen this convo already -- skip!
102 $conversations[$convo] = true;
104 // Get the convo's root notice
105 $root = $notice->conversationRoot($this->userProfile);
111 $item = $this->newListItem($notice);
113 } catch (Exception $e) {
114 // we log exceptions and continue
115 common_log(LOG_ERR, $e->getMessage());
120 $this->out->elementEnd('ol');
121 $this->out->elementEnd('div');
126 function _allNotices($notices)
129 foreach ($notices as $notice) {
130 $convId[] = $notice->conversation;
132 $convId = array_unique($convId);
133 $allMap = Memcached_DataObject::listGet('Notice', 'conversation', $convId);
135 foreach ($allMap as $convId => $convNotices) {
136 $allArray = array_merge($allArray, $convNotices);
142 * returns a new list item for the current notice
144 * Recipe (factory?) method; overridden by sub-classes to give
145 * a different list item class.
147 * @param Notice $notice the current notice
149 * @return NoticeListItem a list item for displaying the notice
151 function newListItem($notice)
153 return new ThreadedNoticeListItem($notice, $this->out, $this->userProfile);
158 * widget for displaying a single notice
160 * This widget has the core smarts for showing a single notice: what to display,
161 * where, and under which circumstances. Its key method is show(); this is a recipe
162 * that calls all the other show*() methods to build up a single notice. The
163 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
164 * author info (since that's implicit by the data in the page).
168 * @author Evan Prodromou <evan@status.net>
169 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
170 * @link http://status.net/
172 * @see ProfileNoticeListItem
174 class ThreadedNoticeListItem extends NoticeListItem
176 protected $userProfile = null;
178 function __construct($notice, $out=null, $profile=null)
180 parent::__construct($notice, $out);
181 $this->userProfile = $profile;
184 function initialItems()
189 function showContext()
197 * Close the last elements in the notice list item
203 $max = $this->initialItems();
204 if (!$this->repeat) {
205 $stream = new ConversationNoticeStream($this->notice->conversation, $this->userProfile);
206 $notice = $stream->getNotices(0, $max + 2);
210 while ($notice->fetch()) {
211 if (Event::handle('StartAddNoticeReply', array($this, $this->notice, $notice))) {
212 if ($notice->id == $this->notice->id) {
219 $moreCutoff = clone($notice);
222 $notices[] = clone($notice); // *grumble* inefficient as hell
223 Event::handle('EndAddNoticeReply', array($this, $this->notice, $notice));
227 if (Event::handle('StartShowThreadedNoticeTail', array($this, $this->notice, &$notices))) {
228 $this->out->elementStart('ul', 'notices threaded-replies xoxo');
230 $item = new ThreadedNoticeListFavesItem($this->notice, $this->out);
231 $hasFaves = $item->show();
233 $item = new ThreadedNoticeListRepeatsItem($this->notice, $this->out);
234 $hasRepeats = $item->show();
239 $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out, count($notices));
242 foreach (array_reverse($notices) as $notice) {
243 if (Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice))) {
244 $item = new ThreadedNoticeListSubItem($notice, $this->notice, $this->out);
246 Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice));
251 if ($notices || $hasFaves || $hasRepeats) {
252 // @fixme do a proper can-post check that's consistent
254 if (common_current_user()) {
255 $item = new ThreadedNoticeListReplyItem($this->notice, $this->out);
259 $this->out->elementEnd('ul');
260 Event::handle('EndShowThreadedNoticeTail', array($this, $this->notice, $notices));
268 // @todo FIXME: needs documentation.
269 class ThreadedNoticeListSubItem extends NoticeListItem
271 protected $root = null;
273 function __construct($notice, $root, $out)
276 parent::__construct($notice, $out);
279 function avatarSize()
281 return AVATAR_STREAM_SIZE; // @fixme would like something in between
284 function showNoticeLocation()
289 function showNoticeSource()
294 function showContext()
299 function getReplyProfiles()
301 $all = parent::getReplyProfiles();
305 $rootAuthor = $this->root->getProfile();
307 foreach ($all as $profile) {
308 if ($profile->id != $rootAuthor->id) {
309 $profiles[] = $profile;
318 $item = new ThreadedNoticeListInlineFavesItem($this->notice, $this->out);
319 $hasFaves = $item->show();
325 * Placeholder for loading more replies...
327 class ThreadedNoticeListMoreItem extends NoticeListItem
331 function __construct($notice, $out, $cnt)
333 parent::__construct($notice, $out);
338 * recipe function for displaying a single notice.
340 * This uses all the other methods to correctly display a notice. Override
341 * it or one of the others to fine-tune the output.
348 $this->showMiniForm();
353 * start a single notice.
359 $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
362 function showMiniForm()
364 $id = $this->notice->conversation;
365 $url = common_local_url('conversationreplies', array('id' => $id));
367 $n = Conversation::noticeCount($id) - 1;
369 // TRANS: Link to show replies for a notice.
370 // TRANS: %d is the number of replies to a notice and used for plural.
371 $msg = sprintf(_m('Show reply', 'Show all %d replies', $n), $n);
373 $this->out->element('a', array('href' => $url), $msg);
378 * Placeholder for reply form...
379 * Same as get added at runtime via SN.U.NoticeInlineReplyPlaceholder
381 class ThreadedNoticeListReplyItem extends NoticeListItem
384 * recipe function for displaying a single notice.
386 * This uses all the other methods to correctly display a notice. Override
387 * it or one of the others to fine-tune the output.
394 $this->showMiniForm();
399 * start a single notice.
405 $this->out->elementStart('li', array('class' => 'notice-reply-placeholder'));
408 function showMiniForm()
410 $this->out->element('input', array('class' => 'placeholder',
411 // TRANS: Field label for reply mini form.
412 'value' => _('Write a reply...')));
417 * Placeholder for showing faves...
419 abstract class NoticeListActorsItem extends NoticeListItem
422 * @return array of profile IDs
424 abstract function getProfiles();
426 abstract function getListMessage($count, $you);
432 $cur = common_current_user();
433 foreach ($this->getProfiles() as $id) {
434 if ($cur && $cur->id == $id) {
436 // TRANS: Reference to the logged in user in favourite list.
437 array_unshift($links, _m('FAVELIST', 'You'));
439 $profile = Profile::staticGet('id', $id);
441 $links[] = sprintf('<a href="%s">%s</a>',
442 htmlspecialchars($profile->profileurl),
443 htmlspecialchars($profile->getBestName()));
449 $count = count($links);
450 $msg = $this->getListMessage($count, $you);
451 $out = sprintf($msg, $this->magicList($links));
454 $this->out->raw($out);
462 function magicList($items)
464 if (count($items) == 0) {
466 } else if (count($items) == 1) {
469 $first = array_slice($items, 0, -1);
470 $last = array_slice($items, -1, 1);
471 // TRANS: Separator in list of user names like "Jim, Bob, Mary".
472 $separator = _(', ');
473 // TRANS: For building a list such as "Jim, Bob, Mary and 5 others like this".
474 // TRANS: %1$s is a list of users, separated by a separator (default: ", "), %2$s is the last user in the list.
475 return sprintf(_m('FAVELIST', '%1$s and %2$s'), implode($separator, $first), implode($separator, $last));
481 * Placeholder for showing faves...
483 class ThreadedNoticeListFavesItem extends NoticeListActorsItem
485 function getProfiles()
487 $faves = $this->notice->getFaves();
489 foreach ($faves as $fave) {
490 $profiles[] = $fave->user_id;
495 function magicList($items)
497 if (count($items) > 4) {
498 return parent::magicList(array_slice($items, 0, 3));
500 return parent::magicList($items);
504 function getListMessage($count, $you)
506 if ($count == 1 && $you) {
507 // darn first person being different from third person!
508 // TRANS: List message for notice favoured by logged in user.
509 return _m('FAVELIST', 'You like this.');
510 } else if ($count > 4) {
511 // TRANS: List message for when more than 4 people like something.
512 // TRANS: %%s is a list of users liking a notice, %d is the number over 4 that like the notice.
513 // TRANS: Plural is decided on the total number of users liking the notice (count of %%s + %d).
514 return sprintf(_m('%%s and %d others like this.',
515 '%%s and %d others like this.',
519 // TRANS: List message for favoured notices.
520 // TRANS: %%s is a list of users liking a notice.
521 // TRANS: Plural is based on the number of of users that have favoured a notice.
522 return sprintf(_m('%%s likes this.',
531 $this->out->elementStart('li', array('class' => 'notice-data notice-faves'));
536 $this->out->elementEnd('li');
540 // @todo FIXME: needs documentation.
541 class ThreadedNoticeListInlineFavesItem extends ThreadedNoticeListFavesItem
545 $this->out->elementStart('div', array('class' => 'entry-content notice-faves'));
550 $this->out->elementEnd('div');
555 * Placeholder for showing faves...
557 class ThreadedNoticeListRepeatsItem extends NoticeListActorsItem
559 function getProfiles()
561 $repeats = $this->notice->getRepeats();
565 foreach ($repeats as $rep) {
566 $profiles[] = $rep->profile_id;
572 function getListMessage($count, $you)
574 if ($count == 1 && $you) {
575 // darn first person being different from third person!
576 // TRANS: List message for notice repeated by logged in user.
577 return _m('REPEATLIST', 'You have repeated this notice.');
579 // TRANS: List message for repeated notices.
580 // TRANS: %d is the number of users that have repeated a notice.
581 return sprintf(_m('One person has repeated this notice.',
582 '%d people have repeated this notice.',
590 $this->out->elementStart('li', array('class' => 'notice-data notice-repeats'));
595 $this->out->elementEnd('li');