3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * An item in a notice list
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * widget for displaying a single notice
40 * This widget has the core smarts for showing a single notice: what to display,
41 * where, and under which circumstances. Its key method is show(); this is a recipe
42 * that calls all the other show*() methods to build up a single notice. The
43 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
44 * author info (since that's implicit by the data in the page).
48 * @author Evan Prodromou <evan@status.net>
49 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50 * @link http://status.net/
52 * @see ProfileNoticeListItem
54 class NoticeListItem extends Widget
56 /** The notice this item will show. */
59 /** The notice that was repeated. */
62 /** The profile of the author of the notice, extracted once for convenience. */
68 * Also initializes the profile attribute.
70 * @param Notice $notice The notice we'll display
72 function __construct($notice, $out=null)
74 parent::__construct($out);
75 if (!empty($notice->repeat_of)) {
76 $original = Notice::getKV('id', $notice->repeat_of);
77 if (empty($original)) { // could have been deleted
78 $this->notice = $notice;
80 $this->notice = $original;
81 $this->repeat = $notice;
84 $this->notice = $notice;
86 $this->profile = $this->notice->getProfile();
90 * recipe function for displaying a single notice.
92 * This uses all the other methods to correctly display a notice. Override
93 * it or one of the others to fine-tune the output.
99 if (empty($this->notice)) {
100 common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
102 } else if (empty($this->profile)) {
103 common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
108 if (Event::handle('StartShowNoticeItem', array($this))) {
110 $this->showNoticeAttachments();
111 $this->showNoticeInfo();
112 $this->showNoticeOptions();
113 Event::handle('EndShowNoticeItem', array($this));
118 function showNotice()
120 $this->out->elementStart('div', 'entry-title');
122 $this->showContent();
123 $this->out->elementEnd('div');
126 function showNoticeInfo()
128 $this->out->elementStart('div', 'entry-content');
129 if (Event::handle('StartShowNoticeInfo', array($this))) {
130 $this->showNoticeLink();
131 $this->showNoticeSource();
132 $this->showNoticeLocation();
133 $this->showContext();
135 Event::handle('EndShowNoticeInfo', array($this));
138 $this->out->elementEnd('div');
141 function showNoticeOptions()
143 if (Event::handle('StartShowNoticeOptions', array($this))) {
144 $user = common_current_user();
146 $this->out->elementStart('div', 'notice-options');
147 if (Event::handle('StartShowNoticeOptionItems', array($this))) {
148 $this->showFaveForm();
149 $this->showReplyLink();
150 $this->showRepeatForm();
151 $this->showDeleteLink();
152 Event::handle('EndShowNoticeOptionItems', array($this));
154 $this->out->elementEnd('div');
156 Event::handle('EndShowNoticeOptions', array($this));
161 * start a single notice.
167 if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
168 $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
169 $class = 'hentry notice';
170 if ($this->notice->scope != 0 && $this->notice->scope != 1) {
171 $class .= ' limited-scope';
173 if (!empty($this->notice->source)) {
174 $class .= ' notice-source-'.$this->notice->source;
176 $this->out->elementStart('li', array('class' => $class,
177 'id' => 'notice-' . $id));
178 Event::handle('EndOpenNoticeListItemElement', array($this));
183 * show the "favorite" form
187 function showFaveForm()
189 if (Event::handle('StartShowFaveForm', array($this))) {
190 $user = common_current_user();
192 if ($user->hasFave($this->notice)) {
193 $disfavor = new DisfavorForm($this->out, $this->notice);
196 $favor = new FavorForm($this->out, $this->notice);
200 Event::handle('EndShowFaveForm', array($this));
205 * show the author of a notice
207 * By default, this shows the avatar and (linked) nickname of the author.
212 function showAuthor()
214 $this->out->elementStart('div', 'author');
216 $this->out->elementStart('span', 'vcard author');
218 $attrs = array('href' => $this->profile->profileurl,
220 'title' => $this->profile->nickname);
222 $this->out->elementStart('a', $attrs);
224 $this->out->text(' ');
225 $user = common_current_user();
226 if (!empty($user) && $user->streamNicknames()) {
227 $this->out->element('span',array('class' => 'fn'),
228 $this->profile->nickname);
230 $this->out->element('span',array('class' => 'fn'),
231 $this->profile->getBestName());
233 $this->out->elementEnd('a');
235 $this->out->elementEnd('span');
237 $this->showAddressees();
239 $this->out->elementEnd('div');
242 function showAddressees()
244 $pa = $this->getProfileAddressees();
247 $this->out->elementStart('span', 'addressees');
249 foreach ($pa as $addr) {
251 // TRANS: Separator in profile addressees list.
252 $this->out->text(_m('SEPARATOR',', '));
254 // Start of profile addressees list.
257 $text = $addr['text'];
258 unset($addr['text']);
259 $this->out->element('a', $addr, $text);
261 $this->out->elementEnd('span', 'addressees');
265 function getProfileAddressees()
269 $attentions = $this->getReplyProfiles();
271 $user = common_current_user();
273 $streamNicknames = !empty($user) && $user->streamNicknames();
275 foreach ($attentions as $attn) {
276 $class = $attn->isGroup() ? 'group' : 'account';
277 $pa[] = array('href' => $attn->profileurl,
278 'title' => $attn->nickname,
279 'class' => "addressee {$class}",
280 'text' => ($streamNicknames) ? $attn->nickname : $attn->getBestName());
286 function getReplyProfiles()
288 return $this->notice->getReplyProfiles();
292 * show the avatar of the notice's author
294 * This will use the default avatar if no avatar is assigned for the author.
295 * It makes a link to the author's profile.
299 function showAvatar()
301 $avatar_size = $this->avatarSize();
303 $avatarUrl = $this->profile->avatarUrl($avatar_size);
305 $this->out->element('img', array('src' => $avatarUrl,
306 'class' => 'avatar photo',
307 'width' => $avatar_size,
308 'height' => $avatar_size,
310 ($this->profile->fullname) ?
311 $this->profile->fullname :
312 $this->profile->nickname));
315 function avatarSize()
317 return AVATAR_STREAM_SIZE;
321 * show the nickname of the author
323 * Links to the author's profile page
327 function showNickname()
329 $this->out->raw('<span class="nickname fn">' .
330 htmlspecialchars($this->profile->nickname) .
335 * show the content of the notice
337 * Shows the content of the notice. This is pre-rendered for efficiency
338 * at save time. Some very old notices might not be pre-rendered, so
339 * they're rendered on the spot.
343 function showContent()
345 // FIXME: URL, image, video, audio
346 $this->out->elementStart('p', array('class' => 'entry-content'));
347 if ($this->notice->rendered) {
348 $this->out->raw($this->notice->rendered);
350 // XXX: may be some uncooked notices in the DB,
351 // we cook them right now. This should probably disappear in future
352 // versions (>> 0.4.x)
353 $this->out->raw(common_render_content($this->notice->content, $this->notice));
355 $this->out->elementEnd('p');
358 function showNoticeAttachments() {
359 if (common_config('attachments', 'show_thumbs')) {
360 $al = new InlineAttachmentList($this->notice, $this->out);
366 * show the link to the main page for the notice
368 * Displays a link to the page for a notice, with "relative" time. Tries to
369 * get remote notice URLs correct, but doesn't always succeed.
373 function showNoticeLink()
375 $noticeurl = $this->notice->bestUrl();
377 // above should always return an URL
379 assert(!empty($noticeurl));
381 $this->out->elementStart('a', array('rel' => 'bookmark',
382 'class' => 'timestamp',
383 'href' => $noticeurl));
384 $dt = common_date_iso8601($this->notice->created);
385 $this->out->element('abbr', array('class' => 'published',
387 common_date_string($this->notice->created));
388 $this->out->elementEnd('a');
392 * show the notice location
394 * shows the notice location in the correct language.
396 * If an URL is available, makes a link. Otherwise, just a span.
400 function showNoticeLocation()
402 $id = $this->notice->id;
404 $location = $this->notice->getLocation();
406 if (empty($location)) {
410 $name = $location->getName();
412 $lat = $this->notice->lat;
413 $lon = $this->notice->lon;
414 $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
417 $latdms = $this->decimalDegreesToDMS(abs($lat));
418 $londms = $this->decimalDegreesToDMS(abs($lon));
419 // TRANS: Used in coordinates as abbreviation of north.
421 // TRANS: Used in coordinates as abbreviation of south.
423 // TRANS: Used in coordinates as abbreviation of east.
425 // TRANS: Used in coordinates as abbreviation of west.
428 // TRANS: Coordinates message.
429 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
430 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
431 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
432 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
433 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
434 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
435 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
438 $url = $location->getUrl();
440 $this->out->text(' ');
441 $this->out->elementStart('span', array('class' => 'location'));
442 // TRANS: Followed by geo location.
443 $this->out->text(_('at'));
444 $this->out->text(' ');
446 $this->out->element('abbr', array('class' => 'geo',
450 $xstr = new XMLStringer(false);
451 $xstr->elementStart('a', array('href' => $url,
452 'rel' => 'external'));
453 $xstr->element('abbr', array('class' => 'geo',
456 $xstr->elementEnd('a');
457 $this->out->raw($xstr->getString());
459 $this->out->elementEnd('span');
463 * @param number $dec decimal degrees
464 * @return array split into 'deg', 'min', and 'sec'
466 function decimalDegreesToDMS($dec)
469 $tempma = abs($dec) - abs($deg);
471 $tempma = $tempma * 3600;
472 $min = floor($tempma / 60);
473 $sec = $tempma - ($min*60);
475 return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
479 * Show the source of the notice
481 * Either the name (and link) of the API client that posted the notice,
482 * or one of other other channels.
486 function showNoticeSource()
488 $ns = $this->notice->getSource();
491 // TRANS: A possible notice source (web interface).
492 $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
493 $this->out->text(' ');
494 $this->out->elementStart('span', 'source');
495 // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
496 // TRANS: Followed by notice source.
497 $this->out->text(_('from'));
498 $this->out->text(' ');
500 $name = $source_name;
504 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
505 $name = $source_name;
508 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
510 // if $ns->name and $ns->url are populated we have
511 // configured a source attr somewhere
512 if (!empty($name) && !empty($url)) {
513 $this->out->elementStart('span', 'device');
520 if (!empty($title)) {
521 $attrs['title'] = $title;
524 $this->out->element('a', $attrs, $name);
525 $this->out->elementEnd('span');
527 $this->out->element('span', 'device', $name);
530 $this->out->elementEnd('span');
535 * show link to notice this notice is a reply to
537 * If this notice is a reply, show a link to the notice it is replying to. The
538 * heavy lifting for figuring out replies happens at save time.
542 function showContext()
544 if ($this->notice->hasConversation()) {
545 $conv = Conversation::getKV(
547 $this->notice->conversation
549 $convurl = $conv->uri;
550 if (!empty($convurl)) {
551 $this->out->text(' ');
555 'href' => $convurl.'#notice-'.$this->notice->id,
556 'class' => 'response'),
557 // TRANS: Addition in notice list item if notice is part of a conversation.
562 "Couldn't find Conversation ID %d to make 'in context'"
563 . "link for Notice ID %d",
564 $this->notice->conversation,
567 common_log(LOG_WARNING, $msg);
573 * show a link to the author of repeat
577 function showRepeat()
579 if (!empty($this->repeat)) {
581 $repeater = Profile::getKV('id', $this->repeat->profile_id);
583 $attrs = array('href' => $repeater->profileurl,
586 if (!empty($repeater->fullname)) {
587 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
590 $this->out->elementStart('span', 'repeat vcard');
592 // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
593 $this->out->raw(_('Repeated by'));
594 $this->out->raw(' ');
596 $this->out->elementStart('a', $attrs);
597 $this->out->element('span', 'fn nickname', $repeater->nickname);
598 $this->out->elementEnd('a');
600 $this->out->elementEnd('span');
605 * show a link to reply to the current notice
607 * Should either do the reply in the current notice form (if available), or
608 * link out to the notice-posting form. A little flakey, doesn't always work.
612 function showReplyLink()
614 if (common_logged_in()) {
615 $this->out->text(' ');
616 $reply_url = common_local_url('newnotice',
617 array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
618 $this->out->elementStart('a', array('href' => $reply_url,
619 'class' => 'notice_reply',
620 // TRANS: Link title in notice list item to reply to a notice.
621 'title' => _('Reply to this notice.')));
622 // TRANS: Link text in notice list item to reply to a notice.
623 $this->out->text(_('Reply'));
624 $this->out->text(' ');
625 $this->out->element('span', 'notice_id', $this->notice->id);
626 $this->out->elementEnd('a');
631 * if the user is the author, let them delete the notice
635 function showDeleteLink()
637 $user = common_current_user();
639 $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
642 ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
643 $this->out->text(' ');
644 $deleteurl = common_local_url('deletenotice',
645 array('notice' => $todel->id));
646 $this->out->element('a', array('href' => $deleteurl,
647 'class' => 'notice_delete',
648 // TRANS: Link title in notice list item to delete a notice.
649 'title' => _('Delete this notice from the timeline.')),
650 // TRANS: Link text in notice list item to delete a notice.
656 * show the form to repeat a notice
660 function showRepeatForm()
662 if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
663 $this->notice->scope == Notice::SITE_SCOPE) {
664 $user = common_current_user();
666 $user->id != $this->notice->profile_id) {
667 $this->out->text(' ');
668 $profile = $user->getProfile();
669 if ($profile->hasRepeated($this->notice)) {
670 $this->out->element('span', array('class' => 'repeated',
671 // TRANS: Title for repeat form status in notice list when a notice has been repeated.
672 'title' => _('Notice repeated.')),
673 // TRANS: Repeat form status in notice list when a notice has been repeated.
676 $rf = new RepeatForm($this->out, $this->notice);
686 * Close the last elements in the notice list item
692 if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
693 $this->out->elementEnd('li');
694 Event::handle('EndCloseNoticeListItemElement', array($this));
699 * Get the notice in question
701 * For hooks, etc., this may be useful
703 * @return Notice The notice we're showing
708 return $this->notice;