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, $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 $this->showContext();
131 Event::handle('EndShowNoticeInfo', array($this));
134 $this->out->elementEnd('div');
137 function showNoticeOptions()
139 if (Event::handle('StartShowNoticeOptions', array($this))) {
140 $user = common_current_user();
142 $this->out->elementStart('div', 'notice-options');
143 if (Event::handle('StartShowNoticeOptionItems', array($this))) {
144 $this->showFaveForm();
145 $this->showReplyLink();
146 $this->showRepeatForm();
147 $this->showDeleteLink();
148 Event::handle('EndShowNoticeOptionItems', array($this));
150 $this->out->elementEnd('div');
152 Event::handle('EndShowNoticeOptions', array($this));
157 * start a single notice.
163 if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
164 $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
165 $class = 'hentry notice';
166 if ($this->notice->scope != 0 && $this->notice->scope != 1) {
167 $class .= ' limited-scope';
169 if (!empty($this->notice->source)) {
170 $class .= ' notice-source-'.$this->notice->source;
172 $this->out->elementStart('li', array('class' => $class,
173 'id' => 'notice-' . $id));
174 Event::handle('EndOpenNoticeListItemElement', array($this));
179 * show the "favorite" form
183 function showFaveForm()
185 if (Event::handle('StartShowFaveForm', array($this))) {
186 $user = common_current_user();
188 if ($user->hasFave($this->notice)) {
189 $disfavor = new DisfavorForm($this->out, $this->notice);
192 $favor = new FavorForm($this->out, $this->notice);
196 Event::handle('EndShowFaveForm', array($this));
201 * show the author of a notice
203 * By default, this shows the avatar and (linked) nickname of the author.
208 function showAuthor()
210 $this->out->elementStart('div', 'author');
212 $this->out->elementStart('span', 'vcard author');
214 $attrs = array('href' => $this->profile->profileurl,
216 'title' => $this->profile->nickname);
218 $this->out->elementStart('a', $attrs);
220 $this->out->text(' ');
221 $this->out->element('span',array('class' => 'fn'), $this->profile->getStreamName());
222 $this->out->elementEnd('a');
224 $this->out->elementEnd('span');
226 $this->showAddressees();
228 $this->out->elementEnd('div');
231 function showAddressees()
233 $pa = $this->getProfileAddressees();
236 $this->out->elementStart('span', 'addressees');
238 foreach ($pa as $addr) {
240 // TRANS: Separator in profile addressees list.
241 $this->out->text(_m('SEPARATOR',', '));
243 // Start of profile addressees list.
246 $text = $addr['text'];
247 unset($addr['text']);
248 $this->out->element('a', $addr, $text);
250 $this->out->elementEnd('span', 'addressees');
254 function getProfileAddressees()
258 $attentions = $this->getReplyProfiles();
260 foreach ($attentions as $attn) {
261 $class = $attn->isGroup() ? 'group' : 'account';
262 $pa[] = array('href' => $attn->profileurl,
263 'title' => $attn->nickname,
264 'class' => "addressee {$class}",
265 'text' => $attn->getStreamName());
271 function getReplyProfiles()
273 return $this->notice->getReplyProfiles();
277 * show the avatar of the notice's author
279 * This will use the default avatar if no avatar is assigned for the author.
280 * It makes a link to the author's profile.
284 function showAvatar()
286 $avatar_size = $this->avatarSize();
288 $avatarUrl = $this->profile->avatarUrl($avatar_size);
290 $this->out->element('img', array('src' => $avatarUrl,
291 'class' => 'avatar photo',
292 'width' => $avatar_size,
293 'height' => $avatar_size,
295 ($this->profile->fullname) ?
296 $this->profile->fullname :
297 $this->profile->nickname));
300 function avatarSize()
302 return AVATAR_STREAM_SIZE;
306 * show the nickname of the author
308 * Links to the author's profile page
312 function showNickname()
314 $this->out->raw('<span class="nickname fn">' .
315 htmlspecialchars($this->profile->nickname) .
320 * show the content of the notice
322 * Shows the content of the notice. This is pre-rendered for efficiency
323 * at save time. Some very old notices might not be pre-rendered, so
324 * they're rendered on the spot.
328 function showContent()
330 // FIXME: URL, image, video, audio
331 $this->out->elementStart('p', array('class' => 'entry-content'));
332 if ($this->notice->rendered) {
333 $this->out->raw($this->notice->rendered);
335 // XXX: may be some uncooked notices in the DB,
336 // we cook them right now. This should probably disappear in future
337 // versions (>> 0.4.x)
338 $this->out->raw(common_render_content($this->notice->content, $this->notice));
340 $this->out->elementEnd('p');
343 function showNoticeAttachments() {
344 if (common_config('attachments', 'show_thumbs')) {
345 $al = new InlineAttachmentList($this->notice, $this->out);
351 * show the link to the main page for the notice
353 * Displays a local link to the rendered notice, with "relative" time.
357 function showNoticeLink()
359 $this->out->elementStart('a', array('rel' => 'bookmark',
360 'class' => 'timestamp',
361 'href' => $this->notice->getLocalUrl()));
362 $this->out->element('time', array('class' => 'dt-published',
363 'datetime' => common_date_iso8601($this->notice->created),
364 // TRANS: Timestamp title (tooltip text) for NoticeListItem
365 'title' => common_exact_date($this->notice->created)),
366 common_date_string($this->notice->created));
367 $this->out->elementEnd('a');
371 * show the notice location
373 * shows the notice location in the correct language.
375 * If an URL is available, makes a link. Otherwise, just a span.
379 function showNoticeLocation()
381 $id = $this->notice->id;
383 $location = $this->notice->getLocation();
385 if (empty($location)) {
389 $name = $location->getName();
391 $lat = $this->notice->lat;
392 $lon = $this->notice->lon;
393 $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
396 $latdms = $this->decimalDegreesToDMS(abs($lat));
397 $londms = $this->decimalDegreesToDMS(abs($lon));
398 // TRANS: Used in coordinates as abbreviation of north.
400 // TRANS: Used in coordinates as abbreviation of south.
402 // TRANS: Used in coordinates as abbreviation of east.
404 // TRANS: Used in coordinates as abbreviation of west.
407 // TRANS: Coordinates message.
408 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
409 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
410 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
411 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
412 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
413 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
414 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
417 $url = $location->getUrl();
419 $this->out->text(' ');
420 $this->out->elementStart('span', array('class' => 'location'));
421 // TRANS: Followed by geo location.
422 $this->out->text(_('at'));
423 $this->out->text(' ');
425 $this->out->element('abbr', array('class' => 'geo',
429 $xstr = new XMLStringer(false);
430 $xstr->elementStart('a', array('href' => $url,
431 'rel' => 'external'));
432 $xstr->element('abbr', array('class' => 'geo',
435 $xstr->elementEnd('a');
436 $this->out->raw($xstr->getString());
438 $this->out->elementEnd('span');
442 * @param number $dec decimal degrees
443 * @return array split into 'deg', 'min', and 'sec'
445 function decimalDegreesToDMS($dec)
448 $tempma = abs($dec) - abs($deg);
450 $tempma = $tempma * 3600;
451 $min = floor($tempma / 60);
452 $sec = $tempma - ($min*60);
454 return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
458 * Show the source of the notice
460 * Either the name (and link) of the API client that posted the notice,
461 * or one of other other channels.
465 function showNoticeSource()
467 $ns = $this->notice->getSource();
470 // TRANS: A possible notice source (web interface).
471 $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
472 $this->out->text(' ');
473 $this->out->elementStart('span', 'source');
474 // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
475 // TRANS: Followed by notice source.
476 $this->out->text(_('from'));
477 $this->out->text(' ');
479 $name = $source_name;
483 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
484 $name = $source_name;
487 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
489 // if $ns->name and $ns->url are populated we have
490 // configured a source attr somewhere
491 if (!empty($name) && !empty($url)) {
492 $this->out->elementStart('span', 'device');
499 if (!empty($title)) {
500 $attrs['title'] = $title;
503 $this->out->element('a', $attrs, $name);
504 $this->out->elementEnd('span');
506 $this->out->element('span', 'device', $name);
509 $this->out->elementEnd('span');
514 * show link to notice this notice is a reply to
516 * If this notice is a reply, show a link to the notice it is replying to. The
517 * heavy lifting for figuring out replies happens at save time.
521 function showContext()
523 if ($this->notice->hasConversation()) {
524 $conv = Conversation::getKV(
526 $this->notice->conversation
528 $convurl = $conv->uri;
529 if (!empty($convurl)) {
530 $this->out->text(' ');
534 'href' => $convurl.'#notice-'.$this->notice->id,
535 'class' => 'response'),
536 // TRANS: Addition in notice list item if notice is part of a conversation.
541 "Couldn't find Conversation ID %d to make 'in context'"
542 . "link for Notice ID %d",
543 $this->notice->conversation,
546 common_log(LOG_WARNING, $msg);
552 * show a link to the author of repeat
556 function showRepeat()
558 if (!empty($this->repeat)) {
560 $repeater = Profile::getKV('id', $this->repeat->profile_id);
562 $attrs = array('href' => $repeater->profileurl,
565 if (!empty($repeater->fullname)) {
566 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
569 $this->out->elementStart('span', 'repeat vcard');
571 // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
572 $this->out->raw(_('Repeated by'));
573 $this->out->raw(' ');
575 $this->out->elementStart('a', $attrs);
576 $this->out->element('span', 'fn nickname', $repeater->nickname);
577 $this->out->elementEnd('a');
579 $this->out->elementEnd('span');
584 * show a link to reply to the current notice
586 * Should either do the reply in the current notice form (if available), or
587 * link out to the notice-posting form. A little flakey, doesn't always work.
591 function showReplyLink()
593 if (common_logged_in()) {
594 $this->out->text(' ');
595 $reply_url = common_local_url('newnotice',
596 array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
597 $this->out->elementStart('a', array('href' => $reply_url,
598 'class' => 'notice_reply',
599 // TRANS: Link title in notice list item to reply to a notice.
600 'title' => _('Reply to this notice.')));
601 // TRANS: Link text in notice list item to reply to a notice.
602 $this->out->text(_('Reply'));
603 $this->out->text(' ');
604 $this->out->element('span', 'notice_id', $this->notice->id);
605 $this->out->elementEnd('a');
610 * if the user is the author, let them delete the notice
614 function showDeleteLink()
616 $user = common_current_user();
618 $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
621 ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
622 $this->out->text(' ');
623 $deleteurl = common_local_url('deletenotice',
624 array('notice' => $todel->id));
625 $this->out->element('a', array('href' => $deleteurl,
626 'class' => 'notice_delete',
627 // TRANS: Link title in notice list item to delete a notice.
628 'title' => _('Delete this notice from the timeline.')),
629 // TRANS: Link text in notice list item to delete a notice.
635 * show the form to repeat a notice
639 function showRepeatForm()
641 if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
642 $this->notice->scope == Notice::SITE_SCOPE) {
643 $user = common_current_user();
645 $user->id != $this->notice->profile_id) {
646 $this->out->text(' ');
647 $profile = $user->getProfile();
648 if ($profile->hasRepeated($this->notice)) {
649 $this->out->element('span', array('class' => 'repeated',
650 // TRANS: Title for repeat form status in notice list when a notice has been repeated.
651 'title' => _('Notice repeated.')),
652 // TRANS: Repeat form status in notice list when a notice has been repeated.
655 $rf = new RepeatForm($this->out, $this->notice);
665 * Close the last elements in the notice list item
671 if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
672 $this->out->elementEnd('li');
673 Event::handle('EndCloseNoticeListItemElement', array($this));
678 * Get the notice in question
680 * For hooks, etc., this may be useful
682 * @return Notice The notice we're showing
687 return $this->notice;