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, Action $out=null)
70 parent::__construct($out);
71 if (!empty($notice->repeat_of)) {
72 $original = Notice::getKV('id', $notice->repeat_of);
73 if (!$original instanceof Notice) { // 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->showNoticeTitle();
118 $this->showAddressees();
119 $this->showContent();
122 function showNoticeTitle()
124 if (Event::handle('StartShowNoticeTitle', array($this))) {
125 $this->element('a', array('href' => $this->notice->getUrl(),
126 'class' => 'p-name metadata'),
127 $this->notice->getTitle());
128 Event::handle('EndShowNoticeTitle', array($this));
132 function showNoticeInfo()
134 $this->out->elementStart('div', 'entry-metadata');
135 if (Event::handle('StartShowNoticeInfo', array($this))) {
136 $this->showNoticeLink();
137 $this->showNoticeSource();
138 $this->showNoticeLocation();
139 if ($this->notice->hasConversation()) {
140 $this->showContext();
143 Event::handle('EndShowNoticeInfo', array($this));
146 $this->out->elementEnd('div');
149 function showNoticeOptions()
151 if (Event::handle('StartShowNoticeOptions', array($this))) {
152 $user = common_current_user();
154 if ($user instanceof User) {
155 $this->out->elementStart('div', 'notice-options');
156 if (Event::handle('StartShowNoticeOptionItems', array($this))) {
157 $this->showReplyLink();
158 $this->showRepeatForm();
159 $this->showDeleteLink();
160 Event::handle('EndShowNoticeOptionItems', array($this));
162 $this->out->elementEnd('div');
165 Event::handle('EndShowNoticeOptions', array($this));
170 * start a single notice.
176 if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
177 $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
178 $class = 'h-entry notice';
179 if ($this->notice->scope != 0 && $this->notice->scope != 1) {
180 $class .= ' limited-scope';
182 if (!empty($this->notice->source)) {
183 $class .= ' notice-source-'.$this->notice->source;
185 $this->out->elementStart('li', array('class' => $class,
186 'id' => 'notice-' . $id));
187 Event::handle('EndOpenNoticeListItemElement', array($this));
192 * show the author of a notice
194 * By default, this shows the avatar and (linked) nickname of the author.
199 function showAuthor()
201 $attrs = array('href' => $this->profile->profileurl,
202 'class' => 'h-card p-author',
203 'title' => $this->profile->getNickname());
205 if (Event::handle('StartShowNoticeItemAuthor', array($this->profile, $this->out, &$attrs))) {
206 $this->out->elementStart('a', $attrs);
207 $this->showAvatar($this->profile);
208 $this->out->text($this->profile->getStreamName());
209 $this->out->elementEnd('a');
210 Event::handle('EndShowNoticeItemAuthor', array($this->profile, $this->out));
214 function showAddressees()
216 $pa = $this->getProfileAddressees();
219 $this->out->elementStart('ul', 'addressees');
221 foreach ($pa as $addr) {
222 $this->out->elementStart('li', 'h-card');
223 $text = $addr['text'];
224 unset($addr['text']);
225 $this->out->element('a', $addr, $text);
226 $this->out->elementEnd('li');
228 $this->out->elementEnd('ul', 'addressees');
232 function getProfileAddressees()
236 $attentions = $this->getReplyProfiles();
238 foreach ($attentions as $attn) {
239 $class = $attn->isGroup() ? 'group' : 'account';
240 $pa[] = array('href' => $attn->profileurl,
241 'title' => $attn->getNickname(),
242 'class' => "addressee {$class}",
243 'text' => $attn->getStreamName());
249 function getReplyProfiles()
251 return $this->notice->getReplyProfiles();
255 * show the nickname of the author
257 * Links to the author's profile page
261 function showNickname()
263 $this->out->raw('<span class="p-name">' .
264 htmlspecialchars($this->profile->getNickname()) .
269 * show the content of the notice
271 * Shows the content of the notice. This is pre-rendered for efficiency
272 * at save time. Some very old notices might not be pre-rendered, so
273 * they're rendered on the spot.
277 function showContent()
279 // FIXME: URL, image, video, audio
280 $this->out->elementStart('div', array('class' => 'e-content'));
281 if (Event::handle('StartShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()))) {
282 if ($this->notice->rendered) {
283 $this->out->raw($this->notice->rendered);
285 // XXX: may be some uncooked notices in the DB,
286 // we cook them right now. This should probably disappear in future
287 // versions (>> 0.4.x)
288 $this->out->raw(common_render_content($this->notice->content, $this->notice));
290 Event::handle('EndShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()));
292 $this->out->elementEnd('div');
295 function showNoticeAttachments() {
296 if (common_config('attachments', 'show_thumbs')) {
297 $al = new InlineAttachmentList($this->notice, $this->out);
303 * show the link to the main page for the notice
305 * Displays a local link to the rendered notice, with "relative" time.
309 function showNoticeLink()
311 $this->out->elementStart('a', array('rel' => 'bookmark',
312 'class' => 'u-url timestamp',
313 'href' => $this->notice->getLocalUrl()));
314 $this->out->element('time', array('class' => 'dt-published',
315 'datetime' => common_date_iso8601($this->notice->created),
316 // TRANS: Timestamp title (tooltip text) for NoticeListItem
317 'title' => common_exact_date($this->notice->created)),
318 common_date_string($this->notice->created));
319 $this->out->elementEnd('a');
323 * show the notice location
325 * shows the notice location in the correct language.
327 * If an URL is available, makes a link. Otherwise, just a span.
331 function showNoticeLocation()
333 $id = $this->notice->id;
335 $location = $this->notice->getLocation();
337 if (empty($location)) {
341 $name = $location->getName();
343 $lat = $this->notice->lat;
344 $lon = $this->notice->lon;
345 $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
348 $latdms = $this->decimalDegreesToDMS(abs($lat));
349 $londms = $this->decimalDegreesToDMS(abs($lon));
350 // TRANS: Used in coordinates as abbreviation of north.
352 // TRANS: Used in coordinates as abbreviation of south.
354 // TRANS: Used in coordinates as abbreviation of east.
356 // TRANS: Used in coordinates as abbreviation of west.
359 // TRANS: Coordinates message.
360 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
361 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
362 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
363 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
364 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
365 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
366 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
369 $url = $location->getUrl();
371 $this->out->text(' ');
372 $this->out->elementStart('span', array('class' => 'location'));
373 // TRANS: Followed by geo location.
374 $this->out->text(_('at'));
375 $this->out->text(' ');
377 $this->out->element('abbr', array('class' => 'geo',
381 $xstr = new XMLStringer(false);
382 $xstr->elementStart('a', array('href' => $url,
383 'rel' => 'external'));
384 $xstr->element('abbr', array('class' => 'geo',
387 $xstr->elementEnd('a');
388 $this->out->raw($xstr->getString());
390 $this->out->elementEnd('span');
394 * @param number $dec decimal degrees
395 * @return array split into 'deg', 'min', and 'sec'
397 function decimalDegreesToDMS($dec)
400 $tempma = abs($dec) - abs($deg);
402 $tempma = $tempma * 3600;
403 $min = floor($tempma / 60);
404 $sec = $tempma - ($min*60);
406 return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
410 * Show the source of the notice
412 * Either the name (and link) of the API client that posted the notice,
413 * or one of other other channels.
417 function showNoticeSource()
419 $ns = $this->notice->getSource();
422 // TRANS: A possible notice source (web interface).
423 $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
424 $this->out->text(' ');
425 $this->out->elementStart('span', 'source');
426 // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
427 // TRANS: Followed by notice source.
428 $this->out->text(_('from'));
429 $this->out->text(' ');
431 $name = $source_name;
435 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
436 $name = $source_name;
439 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
441 // if $ns->name and $ns->url are populated we have
442 // configured a source attr somewhere
443 if (!empty($name) && !empty($url)) {
444 $this->out->elementStart('span', 'device');
451 if (!empty($title)) {
452 $attrs['title'] = $title;
455 $this->out->element('a', $attrs, $name);
456 $this->out->elementEnd('span');
458 $this->out->element('span', 'device', $name);
461 $this->out->elementEnd('span');
466 * show link to notice this notice is a reply to
468 * If this notice is a reply, show a link to the notice it is replying to. The
469 * heavy lifting for figuring out replies happens at save time.
473 function showContext()
475 $this->out->element('a',
476 array('href' => $this->notice->getConversationUrl(),
477 'class' => 'conversation'),
478 // TRANS: Addition in notice list item if notice is part of a conversation.
483 * show a link to the author of repeat
487 function showRepeat()
489 if (!empty($this->repeat)) {
491 $repeater = Profile::getKV('id', $this->repeat->profile_id);
493 $attrs = array('href' => $repeater->profileurl,
494 'class' => 'h-card p-author',
495 'title' => $repeater->getFancyName());
497 $this->out->elementStart('span', 'repeat h-entry');
499 // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
500 $this->out->raw(_('Repeated by').' ');
502 $this->out->element('a', $attrs, $repeater->getNickname());
504 $this->out->elementEnd('span');
509 * show a link to reply to the current notice
511 * Should either do the reply in the current notice form (if available), or
512 * link out to the notice-posting form. A little flakey, doesn't always work.
516 function showReplyLink()
518 if (common_logged_in()) {
519 $this->out->text(' ');
520 $reply_url = common_local_url('newnotice',
521 array('replyto' => $this->profile->getNickname(), 'inreplyto' => $this->notice->id));
522 $this->out->elementStart('a', array('href' => $reply_url,
523 'class' => 'notice_reply',
524 // TRANS: Link title in notice list item to reply to a notice.
525 'title' => _('Reply to this notice.')));
526 // TRANS: Link text in notice list item to reply to a notice.
527 $this->out->text(_('Reply'));
528 $this->out->text(' ');
529 $this->out->element('span', 'notice_id', $this->notice->id);
530 $this->out->elementEnd('a');
535 * if the user is the author, let them delete the notice
539 function showDeleteLink()
541 $user = common_current_user();
543 $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
546 ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
547 $this->out->text(' ');
548 $deleteurl = common_local_url('deletenotice',
549 array('notice' => $todel->id));
550 $this->out->element('a', array('href' => $deleteurl,
551 'class' => 'notice_delete',
552 // TRANS: Link title in notice list item to delete a notice.
553 'title' => _('Delete this notice from the timeline.')),
554 // TRANS: Link text in notice list item to delete a notice.
560 * show the form to repeat a notice
564 function showRepeatForm()
566 if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
567 $this->notice->scope == Notice::SITE_SCOPE) {
568 $user = common_current_user();
570 $user->id != $this->notice->profile_id) {
571 $this->out->text(' ');
572 $profile = $user->getProfile();
573 if ($profile->hasRepeated($this->notice)) {
574 $this->out->element('span', array('class' => 'repeated',
575 // TRANS: Title for repeat form status in notice list when a notice has been repeated.
576 'title' => _('Notice repeated.')),
577 // TRANS: Repeat form status in notice list when a notice has been repeated.
580 $rf = new RepeatForm($this->out, $this->notice);
590 * Close the last elements in the notice list item
596 if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
597 $this->out->elementEnd('li');
598 Event::handle('EndCloseNoticeListItemElement', array($this));
603 * Get the notice in question
605 * For hooks, etc., this may be useful
607 * @return Notice The notice we're showing
612 return $this->notice;