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('GNUSOCIAL')) { exit(1); }
34 * widget for displaying a single notice
36 * This widget has the core smarts for showing a single notice: what to display,
37 * where, and under which circumstances. Its key method is show(); this is a recipe
38 * that calls all the other show*() methods to build up a single notice. The
39 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
40 * author info (since that's implicit by the data in the 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/
48 * @see ProfileNoticeListItem
50 class NoticeListItem extends Widget
52 /** The notice this item will show. */
55 /** The notice that was repeated. */
58 /** The profile of the author of the notice, extracted once for convenience. */
64 * Also initializes the profile attribute.
66 * @param Notice $notice The notice we'll display
68 function __construct(Notice $notice, HTMLOutputter $out=null)
70 parent::__construct($out);
71 if (!empty($notice->repeat_of)) {
72 $original = Notice::getKV('id', $notice->repeat_of);
73 if (empty($original)) { // could have been deleted
74 $this->notice = $notice;
76 $this->notice = $original;
77 $this->repeat = $notice;
80 $this->notice = $notice;
82 $this->profile = $this->notice->getProfile();
86 * recipe function for displaying a single notice.
88 * This uses all the other methods to correctly display a notice. Override
89 * it or one of the others to fine-tune the output.
95 if (empty($this->notice)) {
96 common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
98 } else if (empty($this->profile)) {
99 common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
104 if (Event::handle('StartShowNoticeItem', array($this))) {
106 $this->showNoticeAttachments();
107 $this->showNoticeInfo();
108 $this->showNoticeOptions();
109 Event::handle('EndShowNoticeItem', array($this));
114 function showNotice()
116 $this->out->elementStart('div', 'entry-title');
118 $this->showContent();
119 $this->out->elementEnd('div');
122 function showNoticeInfo()
124 $this->out->elementStart('div', 'entry-content');
125 if (Event::handle('StartShowNoticeInfo', array($this))) {
126 $this->showNoticeLink();
127 $this->showNoticeSource();
128 $this->showNoticeLocation();
129 if ($this->notice->hasConversation()) {
130 $this->showContext();
133 Event::handle('EndShowNoticeInfo', array($this));
136 $this->out->elementEnd('div');
139 function showNoticeOptions()
141 if (Event::handle('StartShowNoticeOptions', array($this))) {
142 $user = common_current_user();
144 $this->out->elementStart('div', 'notice-options');
145 if (Event::handle('StartShowNoticeOptionItems', array($this))) {
146 $this->showFaveForm();
147 $this->showReplyLink();
148 $this->showRepeatForm();
149 $this->showDeleteLink();
150 Event::handle('EndShowNoticeOptionItems', array($this));
152 $this->out->elementEnd('div');
154 Event::handle('EndShowNoticeOptions', array($this));
159 * start a single notice.
165 if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
166 $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
167 $class = 'hentry notice';
168 if ($this->notice->scope != 0 && $this->notice->scope != 1) {
169 $class .= ' limited-scope';
171 if (!empty($this->notice->source)) {
172 $class .= ' notice-source-'.$this->notice->source;
174 $this->out->elementStart('li', array('class' => $class,
175 'id' => 'notice-' . $id));
176 Event::handle('EndOpenNoticeListItemElement', array($this));
181 * show the "favorite" form
185 function showFaveForm()
187 if (Event::handle('StartShowFaveForm', array($this))) {
188 $user = common_current_user();
190 if ($user->hasFave($this->notice)) {
191 $disfavor = new DisfavorForm($this->out, $this->notice);
194 $favor = new FavorForm($this->out, $this->notice);
198 Event::handle('EndShowFaveForm', array($this));
203 * show the author of a notice
205 * By default, this shows the avatar and (linked) nickname of the author.
210 function showAuthor()
212 $this->out->elementStart('div', 'author');
214 $this->out->elementStart('span', 'vcard author');
216 $attrs = array('href' => $this->profile->profileurl,
218 'title' => $this->profile->nickname);
220 $this->out->elementStart('a', $attrs);
222 $this->out->text(' ');
223 $this->out->element('span',array('class' => 'fn'), $this->profile->getStreamName());
224 $this->out->elementEnd('a');
226 $this->out->elementEnd('span');
228 $this->showAddressees();
230 $this->out->elementEnd('div');
233 function showAddressees()
235 $pa = $this->getProfileAddressees();
238 $this->out->elementStart('span', 'addressees');
240 foreach ($pa as $addr) {
242 // TRANS: Separator in profile addressees list.
243 $this->out->text(_m('SEPARATOR',', '));
245 // Start of profile addressees list.
248 $text = $addr['text'];
249 unset($addr['text']);
250 $this->out->element('a', $addr, $text);
252 $this->out->elementEnd('span', 'addressees');
256 function getProfileAddressees()
260 $attentions = $this->getReplyProfiles();
262 foreach ($attentions as $attn) {
263 $class = $attn->isGroup() ? 'group' : 'account';
264 $pa[] = array('href' => $attn->profileurl,
265 'title' => $attn->nickname,
266 'class' => "addressee {$class}",
267 'text' => $attn->getStreamName());
273 function getReplyProfiles()
275 return $this->notice->getReplyProfiles();
279 * show the avatar of the notice's author
281 * This will use the default avatar if no avatar is assigned for the author.
282 * It makes a link to the author's profile.
286 function showAvatar()
288 $avatar_size = $this->avatarSize();
290 $avatarUrl = $this->profile->avatarUrl($avatar_size);
292 $this->out->element('img', array('src' => $avatarUrl,
293 'class' => 'avatar photo',
294 'width' => $avatar_size,
295 'height' => $avatar_size,
297 ($this->profile->fullname) ?
298 $this->profile->fullname :
299 $this->profile->nickname));
302 function avatarSize()
304 return AVATAR_STREAM_SIZE;
308 * show the nickname of the author
310 * Links to the author's profile page
314 function showNickname()
316 $this->out->raw('<span class="nickname fn">' .
317 htmlspecialchars($this->profile->nickname) .
322 * show the content of the notice
324 * Shows the content of the notice. This is pre-rendered for efficiency
325 * at save time. Some very old notices might not be pre-rendered, so
326 * they're rendered on the spot.
330 function showContent()
332 // FIXME: URL, image, video, audio
333 $this->out->elementStart('p', array('class' => 'entry-content'));
334 if ($this->notice->rendered) {
335 $this->out->raw($this->notice->rendered);
337 // XXX: may be some uncooked notices in the DB,
338 // we cook them right now. This should probably disappear in future
339 // versions (>> 0.4.x)
340 $this->out->raw(common_render_content($this->notice->content, $this->notice));
342 $this->out->elementEnd('p');
345 function showNoticeAttachments() {
346 if (common_config('attachments', 'show_thumbs')) {
347 $al = new InlineAttachmentList($this->notice, $this->out);
353 * show the link to the main page for the notice
355 * Displays a local link to the rendered notice, with "relative" time.
359 function showNoticeLink()
361 $this->out->elementStart('a', array('rel' => 'bookmark',
362 'class' => 'timestamp',
363 'href' => $this->notice->getLocalUrl()));
364 $this->out->element('time', array('class' => 'dt-published',
365 'datetime' => common_date_iso8601($this->notice->created),
366 // TRANS: Timestamp title (tooltip text) for NoticeListItem
367 'title' => common_exact_date($this->notice->created)),
368 common_date_string($this->notice->created));
369 $this->out->elementEnd('a');
373 * show the notice location
375 * shows the notice location in the correct language.
377 * If an URL is available, makes a link. Otherwise, just a span.
381 function showNoticeLocation()
383 $id = $this->notice->id;
385 $location = $this->notice->getLocation();
387 if (empty($location)) {
391 $name = $location->getName();
393 $lat = $this->notice->lat;
394 $lon = $this->notice->lon;
395 $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
398 $latdms = $this->decimalDegreesToDMS(abs($lat));
399 $londms = $this->decimalDegreesToDMS(abs($lon));
400 // TRANS: Used in coordinates as abbreviation of north.
402 // TRANS: Used in coordinates as abbreviation of south.
404 // TRANS: Used in coordinates as abbreviation of east.
406 // TRANS: Used in coordinates as abbreviation of west.
409 // TRANS: Coordinates message.
410 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
411 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
412 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
413 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
414 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
415 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
416 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
419 $url = $location->getUrl();
421 $this->out->text(' ');
422 $this->out->elementStart('span', array('class' => 'location'));
423 // TRANS: Followed by geo location.
424 $this->out->text(_('at'));
425 $this->out->text(' ');
427 $this->out->element('abbr', array('class' => 'geo',
431 $xstr = new XMLStringer(false);
432 $xstr->elementStart('a', array('href' => $url,
433 'rel' => 'external'));
434 $xstr->element('abbr', array('class' => 'geo',
437 $xstr->elementEnd('a');
438 $this->out->raw($xstr->getString());
440 $this->out->elementEnd('span');
444 * @param number $dec decimal degrees
445 * @return array split into 'deg', 'min', and 'sec'
447 function decimalDegreesToDMS($dec)
450 $tempma = abs($dec) - abs($deg);
452 $tempma = $tempma * 3600;
453 $min = floor($tempma / 60);
454 $sec = $tempma - ($min*60);
456 return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
460 * Show the source of the notice
462 * Either the name (and link) of the API client that posted the notice,
463 * or one of other other channels.
467 function showNoticeSource()
469 $ns = $this->notice->getSource();
472 // TRANS: A possible notice source (web interface).
473 $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
474 $this->out->text(' ');
475 $this->out->elementStart('span', 'source');
476 // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
477 // TRANS: Followed by notice source.
478 $this->out->text(_('from'));
479 $this->out->text(' ');
481 $name = $source_name;
485 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
486 $name = $source_name;
489 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
491 // if $ns->name and $ns->url are populated we have
492 // configured a source attr somewhere
493 if (!empty($name) && !empty($url)) {
494 $this->out->elementStart('span', 'device');
501 if (!empty($title)) {
502 $attrs['title'] = $title;
505 $this->out->element('a', $attrs, $name);
506 $this->out->elementEnd('span');
508 $this->out->element('span', 'device', $name);
511 $this->out->elementEnd('span');
516 * show link to notice this notice is a reply to
518 * If this notice is a reply, show a link to the notice it is replying to. The
519 * heavy lifting for figuring out replies happens at save time.
523 function showContext()
525 $this->out->element('a',
526 array('href' => $this->notice->getConversationUrl(),
527 'class' => 'conversation'),
528 // TRANS: Addition in notice list item if notice is part of a conversation.
533 * show a link to the author of repeat
537 function showRepeat()
539 if (!empty($this->repeat)) {
541 $repeater = Profile::getKV('id', $this->repeat->profile_id);
543 $attrs = array('href' => $repeater->profileurl,
546 if (!empty($repeater->fullname)) {
547 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
550 $this->out->elementStart('span', 'repeat vcard');
552 // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
553 $this->out->raw(_('Repeated by'));
554 $this->out->raw(' ');
556 $this->out->elementStart('a', $attrs);
557 $this->out->element('span', 'fn nickname', $repeater->nickname);
558 $this->out->elementEnd('a');
560 $this->out->elementEnd('span');
565 * show a link to reply to the current notice
567 * Should either do the reply in the current notice form (if available), or
568 * link out to the notice-posting form. A little flakey, doesn't always work.
572 function showReplyLink()
574 if (common_logged_in()) {
575 $this->out->text(' ');
576 $reply_url = common_local_url('newnotice',
577 array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
578 $this->out->elementStart('a', array('href' => $reply_url,
579 'class' => 'notice_reply',
580 // TRANS: Link title in notice list item to reply to a notice.
581 'title' => _('Reply to this notice.')));
582 // TRANS: Link text in notice list item to reply to a notice.
583 $this->out->text(_('Reply'));
584 $this->out->text(' ');
585 $this->out->element('span', 'notice_id', $this->notice->id);
586 $this->out->elementEnd('a');
591 * if the user is the author, let them delete the notice
595 function showDeleteLink()
597 $user = common_current_user();
599 $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
602 ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
603 $this->out->text(' ');
604 $deleteurl = common_local_url('deletenotice',
605 array('notice' => $todel->id));
606 $this->out->element('a', array('href' => $deleteurl,
607 'class' => 'notice_delete',
608 // TRANS: Link title in notice list item to delete a notice.
609 'title' => _('Delete this notice from the timeline.')),
610 // TRANS: Link text in notice list item to delete a notice.
616 * show the form to repeat a notice
620 function showRepeatForm()
622 if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
623 $this->notice->scope == Notice::SITE_SCOPE) {
624 $user = common_current_user();
626 $user->id != $this->notice->profile_id) {
627 $this->out->text(' ');
628 $profile = $user->getProfile();
629 if ($profile->hasRepeated($this->notice)) {
630 $this->out->element('span', array('class' => 'repeated',
631 // TRANS: Title for repeat form status in notice list when a notice has been repeated.
632 'title' => _('Notice repeated.')),
633 // TRANS: Repeat form status in notice list when a notice has been repeated.
636 $rf = new RepeatForm($this->out, $this->notice);
646 * Close the last elements in the notice list item
652 if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
653 $this->out->elementEnd('li');
654 Event::handle('EndCloseNoticeListItemElement', array($this));
659 * Get the notice in question
661 * For hooks, etc., this may be useful
663 * @return Notice The notice we're showing
668 return $this->notice;