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::staticGet('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 $this->showFaveForm();
148 $this->showReplyLink();
149 $this->showRepeatForm();
150 $this->showDeleteLink();
151 $this->out->elementEnd('div');
153 Event::handle('EndShowNoticeOptions', array($this));
158 * start a single notice.
164 if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
165 $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
166 $class = 'hentry notice';
167 if ($this->notice->scope != 0 && $this->notice->scope != 1) {
168 $class .= ' limited-scope';
170 if (!empty($this->notice->source)) {
171 $class .= ' notice-source-'.$this->notice->source;
173 $this->out->elementStart('li', array('class' => $class,
174 'id' => 'notice-' . $id));
175 Event::handle('EndOpenNoticeListItemElement', array($this));
180 * show the "favorite" form
184 function showFaveForm()
186 if (Event::handle('StartShowFaveForm', array($this))) {
187 $user = common_current_user();
189 if ($user->hasFave($this->notice)) {
190 $disfavor = new DisfavorForm($this->out, $this->notice);
193 $favor = new FavorForm($this->out, $this->notice);
197 Event::handle('EndShowFaveForm', array($this));
202 * show the author of a notice
204 * By default, this shows the avatar and (linked) nickname of the author.
209 function showAuthor()
211 $this->out->elementStart('div', 'author');
213 $this->out->elementStart('span', 'vcard author');
215 $attrs = array('href' => $this->profile->profileurl,
217 'title' => $this->profile->nickname);
219 $this->out->elementStart('a', $attrs);
221 $this->out->text(' ');
222 $this->out->element('span',array('class' => 'fn'),
223 $this->profile->getBestName());
224 $this->out->elementEnd('a');
226 $this->out->elementEnd('span');
228 $this->showAddressees();
230 $this->out->elementEnd('div');
233 function showAddressees()
235 $ga = $this->getGroupAddressees();
236 $pa = $this->getProfileAddressees();
238 $a = array_merge($ga, $pa);
241 $this->out->elementStart('span', 'addressees');
243 foreach ($a as $addr) {
245 // TRANS: Separator in profile addressees list.
246 $this->out->text(_m('SEPARATOR',', '));
248 // Start of profile addressees list.
251 $text = $addr['text'];
252 unset($addr['text']);
253 $this->out->element('a', $addr, $text);
255 $this->out->elementEnd('span', 'addressees');
259 function getGroupAddressees()
263 $groups = $this->getGroups();
265 foreach ($groups as $group) {
266 $ga[] = array('href' => $group->homeUrl(),
267 'title' => $group->nickname,
268 'class' => 'addressee group',
269 'text' => $group->getBestName());
277 return $this->notice->getGroups();
280 function getProfileAddressees()
284 $replies = $this->getReplyProfiles();
286 foreach ($replies as $reply) {
287 $pa[] = array('href' => $reply->profileurl,
288 'title' => $reply->nickname,
289 'class' => 'addressee account',
290 'text' => $reply->getBestName());
296 function getReplyProfiles()
298 return $this->notice->getReplyProfiles();
302 * show the avatar of the notice's author
304 * This will use the default avatar if no avatar is assigned for the author.
305 * It makes a link to the author's profile.
309 function showAvatar()
311 $avatar_size = $this->avatarSize();
313 $avatar = $this->profile->getAvatar($avatar_size);
315 $this->out->element('img', array('src' => ($avatar) ?
316 $avatar->displayUrl() :
317 Avatar::defaultImage($avatar_size),
318 'class' => 'avatar photo',
319 'width' => $avatar_size,
320 'height' => $avatar_size,
322 ($this->profile->fullname) ?
323 $this->profile->fullname :
324 $this->profile->nickname));
327 function avatarSize()
329 return AVATAR_STREAM_SIZE;
333 * show the nickname of the author
335 * Links to the author's profile page
339 function showNickname()
341 $this->out->raw('<span class="nickname fn">' .
342 htmlspecialchars($this->profile->nickname) .
347 * show the content of the notice
349 * Shows the content of the notice. This is pre-rendered for efficiency
350 * at save time. Some very old notices might not be pre-rendered, so
351 * they're rendered on the spot.
355 function showContent()
357 // FIXME: URL, image, video, audio
358 $this->out->elementStart('p', array('class' => 'entry-content'));
359 if ($this->notice->rendered) {
360 $this->out->raw($this->notice->rendered);
362 // XXX: may be some uncooked notices in the DB,
363 // we cook them right now. This should probably disappear in future
364 // versions (>> 0.4.x)
365 $this->out->raw(common_render_content($this->notice->content, $this->notice));
367 $this->out->elementEnd('p');
370 function showNoticeAttachments() {
371 if (common_config('attachments', 'show_thumbs')) {
372 $al = new InlineAttachmentList($this->notice, $this->out);
378 * show the link to the main page for the notice
380 * Displays a link to the page for a notice, with "relative" time. Tries to
381 * get remote notice URLs correct, but doesn't always succeed.
385 function showNoticeLink()
387 $noticeurl = $this->notice->bestUrl();
389 // above should always return an URL
391 assert(!empty($noticeurl));
393 $this->out->elementStart('a', array('rel' => 'bookmark',
394 'class' => 'timestamp',
395 'href' => $noticeurl));
396 $dt = common_date_iso8601($this->notice->created);
397 $this->out->element('abbr', array('class' => 'published',
399 common_date_string($this->notice->created));
400 $this->out->elementEnd('a');
404 * show the notice location
406 * shows the notice location in the correct language.
408 * If an URL is available, makes a link. Otherwise, just a span.
412 function showNoticeLocation()
414 $id = $this->notice->id;
416 $location = $this->notice->getLocation();
418 if (empty($location)) {
422 $name = $location->getName();
424 $lat = $this->notice->lat;
425 $lon = $this->notice->lon;
426 $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
429 $latdms = $this->decimalDegreesToDMS(abs($lat));
430 $londms = $this->decimalDegreesToDMS(abs($lon));
431 // TRANS: Used in coordinates as abbreviation of north.
433 // TRANS: Used in coordinates as abbreviation of south.
435 // TRANS: Used in coordinates as abbreviation of east.
437 // TRANS: Used in coordinates as abbreviation of west.
440 // TRANS: Coordinates message.
441 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
442 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
443 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
444 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
445 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
446 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
447 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
450 $url = $location->getUrl();
452 $this->out->text(' ');
453 $this->out->elementStart('span', array('class' => 'location'));
454 // TRANS: Followed by geo location.
455 $this->out->text(_('at'));
456 $this->out->text(' ');
458 $this->out->element('abbr', array('class' => 'geo',
462 $xstr = new XMLStringer(false);
463 $xstr->elementStart('a', array('href' => $url,
464 'rel' => 'external'));
465 $xstr->element('abbr', array('class' => 'geo',
468 $xstr->elementEnd('a');
469 $this->out->raw($xstr->getString());
471 $this->out->elementEnd('span');
475 * @param number $dec decimal degrees
476 * @return array split into 'deg', 'min', and 'sec'
478 function decimalDegreesToDMS($dec)
481 $tempma = abs($dec) - abs($deg);
483 $tempma = $tempma * 3600;
484 $min = floor($tempma / 60);
485 $sec = $tempma - ($min*60);
487 return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
491 * Show the source of the notice
493 * Either the name (and link) of the API client that posted the notice,
494 * or one of other other channels.
498 function showNoticeSource()
500 $ns = $this->notice->getSource();
503 // TRANS: A possible notice source (web interface).
504 $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
505 $this->out->text(' ');
506 $this->out->elementStart('span', 'source');
507 // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
508 // TRANS: Followed by notice source.
509 $this->out->text(_('from'));
510 $this->out->text(' ');
512 $name = $source_name;
516 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
517 $name = $source_name;
520 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
522 // if $ns->name and $ns->url are populated we have
523 // configured a source attr somewhere
524 if (!empty($name) && !empty($url)) {
525 $this->out->elementStart('span', 'device');
532 if (!empty($title)) {
533 $attrs['title'] = $title;
536 $this->out->element('a', $attrs, $name);
537 $this->out->elementEnd('span');
539 $this->out->element('span', 'device', $name);
542 $this->out->elementEnd('span');
547 * show link to notice this notice is a reply to
549 * If this notice is a reply, show a link to the notice it is replying to. The
550 * heavy lifting for figuring out replies happens at save time.
554 function showContext()
556 if ($this->notice->hasConversation()) {
557 $conv = Conversation::staticGet(
559 $this->notice->conversation
561 $convurl = $conv->uri;
562 if (!empty($convurl)) {
563 $this->out->text(' ');
567 'href' => $convurl.'#notice-'.$this->notice->id,
568 'class' => 'response'),
569 // TRANS: Addition in notice list item if notice is part of a conversation.
574 "Couldn't find Conversation ID %d to make 'in context'"
575 . "link for Notice ID %d",
576 $this->notice->conversation,
579 common_log(LOG_WARNING, $msg);
585 * show a link to the author of repeat
589 function showRepeat()
591 if (!empty($this->repeat)) {
593 $repeater = Profile::staticGet('id', $this->repeat->profile_id);
595 $attrs = array('href' => $repeater->profileurl,
598 if (!empty($repeater->fullname)) {
599 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
602 $this->out->elementStart('span', 'repeat vcard');
604 // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
605 $this->out->raw(_('Repeated by'));
607 $this->out->elementStart('a', $attrs);
608 $this->out->element('span', 'fn nickname', $repeater->nickname);
609 $this->out->elementEnd('a');
611 $this->out->elementEnd('span');
616 * show a link to reply to the current notice
618 * Should either do the reply in the current notice form (if available), or
619 * link out to the notice-posting form. A little flakey, doesn't always work.
623 function showReplyLink()
625 if (common_logged_in()) {
626 $this->out->text(' ');
627 $reply_url = common_local_url('newnotice',
628 array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
629 $this->out->elementStart('a', array('href' => $reply_url,
630 'class' => 'notice_reply',
631 // TRANS: Link title in notice list item to reply to a notice.
632 'title' => _('Reply to this notice.')));
633 // TRANS: Link text in notice list item to reply to a notice.
634 $this->out->text(_('Reply'));
635 $this->out->text(' ');
636 $this->out->element('span', 'notice_id', $this->notice->id);
637 $this->out->elementEnd('a');
642 * if the user is the author, let them delete the notice
646 function showDeleteLink()
648 $user = common_current_user();
650 $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
653 ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
654 $this->out->text(' ');
655 $deleteurl = common_local_url('deletenotice',
656 array('notice' => $todel->id));
657 $this->out->element('a', array('href' => $deleteurl,
658 'class' => 'notice_delete',
659 // TRANS: Link title in notice list item to delete a notice.
660 'title' => _('Delete this notice from the timeline.')),
661 // TRANS: Link text in notice list item to delete a notice.
667 * show the form to repeat a notice
671 function showRepeatForm()
673 if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
674 $this->notice->scope == Notice::SITE_SCOPE) {
675 $user = common_current_user();
677 $user->id != $this->notice->profile_id) {
678 $this->out->text(' ');
679 $profile = $user->getProfile();
680 if ($profile->hasRepeated($this->notice->id)) {
681 $this->out->element('span', array('class' => 'repeated',
682 // TRANS: Title for repeat form status in notice list when a notice has been repeated.
683 'title' => _('Notice repeated.')),
684 // TRANS: Repeat form status in notice list when a notice has been repeated.
687 $rf = new RepeatForm($this->out, $this->notice);
697 * Close the last elements in the notice list item
703 if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
704 $this->out->elementEnd('li');
705 Event::handle('EndCloseNoticeListItemElement', array($this));