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=null)
57 parent::__construct($notice, $out);
58 $this->userProfile = $profile;
62 * show the list of notices
64 * "Uses up" the stream by looping through it. So, probably can't
65 * be called twice on the same list.
67 * @return int count of notices listed.
71 $this->out->elementStart('div', array('id' =>'notices_primary'));
72 // TRANS: Header for Notices section.
73 $this->out->element('h2', null, _m('HEADER','Notices'));
74 $this->out->elementStart('ol', array('class' => 'notices threaded-notices xoxo'));
77 $conversations = array();
78 while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
81 if ($cnt > NOTICES_PER_PAGE) {
85 // Collapse repeats into their originals...
86 $notice = $this->notice;
87 if ($notice->repeat_of) {
88 $orig = Notice::staticGet('id', $notice->repeat_of);
93 $convo = $notice->conversation;
94 if (!empty($conversations[$convo])) {
95 // Seen this convo already -- skip!
98 $conversations[$convo] = true;
100 // Get the convo's root notice
101 $root = $notice->conversationRoot();
107 $item = $this->newListItem($notice);
109 } catch (Exception $e) {
110 // we log exceptions and continue
111 common_log(LOG_ERR, $e->getMessage());
116 $this->out->elementEnd('ol');
117 $this->out->elementEnd('div');
123 * returns a new list item for the current notice
125 * Recipe (factory?) method; overridden by sub-classes to give
126 * a different list item class.
128 * @param Notice $notice the current notice
130 * @return NoticeListItem a list item for displaying the notice
132 function newListItem($notice)
134 return new ThreadedNoticeListItem($notice, $this->out, $this->userProfile);
139 * widget for displaying a single notice
141 * This widget has the core smarts for showing a single notice: what to display,
142 * where, and under which circumstances. Its key method is show(); this is a recipe
143 * that calls all the other show*() methods to build up a single notice. The
144 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
145 * author info (since that's implicit by the data in the page).
149 * @author Evan Prodromou <evan@status.net>
150 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
151 * @link http://status.net/
153 * @see ProfileNoticeListItem
155 class ThreadedNoticeListItem extends NoticeListItem
157 protected $userProfile = null;
159 function __construct($notice, $out=null, $profile=null)
161 parent::__construct($notice, $out);
162 $this->userProfile = $profile;
165 function initialItems()
170 function showContext()
178 * Close the last elements in the notice list item
184 $max = $this->initialItems();
185 if (!$this->repeat) {
186 $stream = new ConversationNoticeStream($this->notice->conversation, $this->userProfile);
187 $notice = $stream->getNotices(0, $max + 2);
191 while ($notice->fetch()) {
192 if ($notice->id == $this->notice->id) {
199 $moreCutoff = clone($notice);
202 $notices[] = clone($notice); // *grumble* inefficient as hell
205 if (Event::handle('StartShowThreadedNoticeTail', array($this, $this->notice, &$notices))) {
206 $this->out->elementStart('ul', 'notices threaded-replies xoxo');
208 $item = new ThreadedNoticeListFavesItem($this->notice, $this->out);
209 $hasFaves = $item->show();
211 $item = new ThreadedNoticeListRepeatsItem($this->notice, $this->out);
212 $hasRepeats = $item->show();
216 $item = new ThreadedNoticeListMoreItem($moreCutoff, $this->out);
219 foreach (array_reverse($notices) as $notice) {
220 if (Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice))) {
221 $item = new ThreadedNoticeListSubItem($notice, $this->out);
223 Event::handle('StartShowThreadedNoticeSub', array($this, $this->notice, $notice));
228 if ($notices || $hasFaves || $hasRepeats) {
229 // @fixme do a proper can-post check that's consistent
231 if (common_current_user()) {
232 $item = new ThreadedNoticeListReplyItem($this->notice, $this->out);
236 $this->out->elementEnd('ul');
237 Event::handle('EndShowThreadedNoticeTail', array($this, $this->notice, $notices));
245 // @todo FIXME: needs documentation.
246 class ThreadedNoticeListSubItem extends NoticeListItem
248 function avatarSize()
250 return AVATAR_STREAM_SIZE; // @fixme would like something in between
253 function showNoticeLocation()
258 function showNoticeSource()
263 function showContext()
270 $item = new ThreadedNoticeListInlineFavesItem($this->notice, $this->out);
271 $hasFaves = $item->show();
277 * Placeholder for loading more replies...
279 class ThreadedNoticeListMoreItem extends NoticeListItem
282 * recipe function for displaying a single notice.
284 * This uses all the other methods to correctly display a notice. Override
285 * it or one of the others to fine-tune the output.
292 $this->showMiniForm();
297 * start a single notice.
303 $this->out->elementStart('li', array('class' => 'notice-reply-comments'));
306 function showMiniForm()
308 $id = $this->notice->conversation;
309 $url = common_local_url('conversationreplies', array('id' => $id));
311 $n = Conversation::noticeCount($id) - 1;
313 // TRANS: Link to show replies for a notice.
314 // TRANS: %d is the number of replies to a notice and used for plural.
315 $msg = sprintf(_m('Show reply', 'Show all %d replies', $n), $n);
317 $this->out->element('a', array('href' => $url), $msg);
322 * Placeholder for reply form...
323 * Same as get added at runtime via SN.U.NoticeInlineReplyPlaceholder
325 class ThreadedNoticeListReplyItem extends NoticeListItem
328 * recipe function for displaying a single notice.
330 * This uses all the other methods to correctly display a notice. Override
331 * it or one of the others to fine-tune the output.
338 $this->showMiniForm();
343 * start a single notice.
349 $this->out->elementStart('li', array('class' => 'notice-reply-placeholder'));
352 function showMiniForm()
354 $this->out->element('input', array('class' => 'placeholder',
355 // TRANS: Field label for reply mini form.
356 'value' => _('Write a reply...')));
361 * Placeholder for showing faves...
363 abstract class NoticeListActorsItem extends NoticeListItem
366 * @return array of profile IDs
368 abstract function getProfiles();
370 abstract function getListMessage($count, $you);
376 $cur = common_current_user();
377 foreach ($this->getProfiles() as $id) {
378 if ($cur && $cur->id == $id) {
380 // TRANS: Reference to the logged in user in favourite list.
381 array_unshift($links, _m('FAVELIST', 'You'));
383 $profile = Profile::staticGet('id', $id);
385 $links[] = sprintf('<a href="%s" title="%s">%s</a>',
386 htmlspecialchars($profile->profileurl),
387 htmlspecialchars($profile->getBestName()),
388 htmlspecialchars($profile->nickname));
394 $count = count($links);
395 $msg = $this->getListMessage($count, $you);
396 $out = sprintf($msg, $this->magicList($links));
399 $this->out->raw($out);
407 function magicList($items)
409 if (count($items) == 0) {
411 } else if (count($items) == 1) {
414 $first = array_slice($items, 0, -1);
415 $last = array_slice($items, -1, 1);
416 // TRANS: Separator in list of user names like "You, Bob, Mary".
417 $separator = _(', ');
418 // TRANS: For building a list such as "You, bob, mary and 5 others have favored this notice".
419 // TRANS: %1$s is a list of users, separated by a separator (default: ", "), %2$s is the last user in the list.
420 return sprintf(_m('FAVELIST', '%1$s and %2$s'), implode($separator, $first), implode($separator, $last));
426 * Placeholder for showing faves...
428 class ThreadedNoticeListFavesItem extends NoticeListActorsItem
430 function getProfiles()
432 $fave = Fave::byNotice($this->notice->id);
434 while ($fave->fetch()) {
435 $profiles[] = $fave->user_id;
440 function getListMessage($count, $you)
442 if ($count == 1 && $you) {
443 // darn first person being different from third person!
444 // TRANS: List message for notice favoured by logged in user.
445 return _m('FAVELIST', 'You have favored this notice.');
447 // TRANS: List message for favoured notices.
448 // TRANS: %d is the number of users that have favoured a notice.
449 return sprintf(_m('One person has favored this notice.',
450 '%d people have favored this notice.',
458 $this->out->elementStart('li', array('class' => 'notice-data notice-faves'));
463 $this->out->elementEnd('li');
468 // @todo FIXME: needs documentation.
469 class ThreadedNoticeListInlineFavesItem extends ThreadedNoticeListFavesItem
473 $this->out->elementStart('div', array('class' => 'entry-content notice-faves'));
478 $this->out->elementEnd('div');
483 * Placeholder for showing faves...
485 class ThreadedNoticeListRepeatsItem extends NoticeListActorsItem
487 function getProfiles()
489 $rep = $this->notice->repeatStream();
492 while ($rep->fetch()) {
493 $profiles[] = $rep->profile_id;
498 function getListMessage($count, $you)
500 if ($count == 1 && $you) {
501 // darn first person being different from third person!
502 // TRANS: List message for notice repeated by logged in user.
503 return _m('REPEATLIST', 'You have repeated this notice.');
505 // TRANS: List message for repeated notices.
506 // TRANS: %d is the number of users that have repeated a notice.
507 return sprintf(_m('One person has repeated this notice.',
508 '%d people have repeated this notice.',
516 $this->out->elementStart('li', array('class' => 'notice-data notice-repeats'));
521 $this->out->elementEnd('li');