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. */
61 protected $addressees = true;
62 protected $attachments = true;
63 protected $id_prefix = null;
64 protected $options = true;
65 protected $maxchars = 0; // if <= 0 it means use full posts
66 protected $item_tag = 'li';
72 * Also initializes the profile attribute.
74 * @param Notice $notice The notice we'll display
76 function __construct(Notice $notice, Action $out=null, array $prefs=array())
78 parent::__construct($out);
79 if (!empty($notice->repeat_of)) {
80 $original = Notice::getKV('id', $notice->repeat_of);
81 if (!$original instanceof Notice) { // could have been deleted
82 $this->notice = $notice;
84 $this->notice = $original;
85 $this->repeat = $notice;
88 $this->notice = $notice;
91 $this->profile = $this->notice->getProfile();
93 // integer preferences
94 foreach(array('maxchars') as $key) {
95 if (array_key_exists($key, $prefs)) {
96 $this->$key = (int)$prefs[$key];
99 // boolean preferences
100 foreach(array('addressees', 'attachments', 'options') as $key) {
101 if (array_key_exists($key, $prefs)) {
102 $this->$key = (bool)$prefs[$key];
105 // string preferences
106 foreach(array('id_prefix', 'item_tag') as $key) {
107 if (array_key_exists($key, $prefs)) {
108 $this->$key = $prefs[$key];
114 * recipe function for displaying a single notice.
116 * This uses all the other methods to correctly display a notice. Override
117 * it or one of the others to fine-tune the output.
123 if (empty($this->notice)) {
124 common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
126 } else if (empty($this->profile)) {
127 common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
132 if (Event::handle('StartShowNoticeItem', array($this))) {
134 Event::handle('EndShowNoticeItem', array($this));
139 function showNotice()
141 if (Event::handle('StartShowNoticeItemNotice', array($this))) {
142 $this->showNoticeHeaders();
143 $this->showContent();
144 $this->showNoticeFooter();
145 Event::handle('EndShowNoticeItemNotice', array($this));
149 function showNoticeHeaders()
151 $this->elementStart('section', array('class'=>'notice-headers'));
152 $this->showNoticeTitle();
155 if (!empty($this->notice->reply_to) || count($this->getProfileAddressees()) > 0) {
156 $this->elementStart('div', array('class' => 'parents'));
159 } catch (NoParentNoticeException $e) {
161 } catch (InvalidUrlException $e) {
162 // parent had an invalid URL so we can't show it
164 if ($this->addressees) { $this->showAddressees(); }
165 $this->elementEnd('div');
167 $this->elementEnd('section');
170 function showNoticeFooter()
172 $this->elementStart('footer');
173 $this->showNoticeInfo();
174 if ($this->options) { $this->showNoticeOptions(); }
175 if ($this->attachments) { $this->showNoticeAttachments(); }
176 $this->elementEnd('footer');
179 function showNoticeTitle()
181 if (Event::handle('StartShowNoticeTitle', array($this))) {
182 $nameClass = $this->notice->getTitle(false) ? 'p-name ' : '';
183 $this->element('a', array('href' => $this->notice->getUri(),
184 'class' => $nameClass . 'u-uid'),
185 $this->notice->getTitle());
186 Event::handle('EndShowNoticeTitle', array($this));
190 function showNoticeInfo()
192 if (Event::handle('StartShowNoticeInfo', array($this))) {
193 $this->showContextLink();
194 $this->showNoticeLink();
195 $this->showNoticeSource();
196 $this->showNoticeLocation();
197 $this->showPermalink();
198 Event::handle('EndShowNoticeInfo', array($this));
202 function showNoticeOptions()
204 if (Event::handle('StartShowNoticeOptions', array($this))) {
205 $user = common_current_user();
207 $this->out->elementStart('div', 'notice-options');
208 if (Event::handle('StartShowNoticeOptionItems', array($this))) {
209 $this->showReplyLink();
210 $this->showDeleteLink();
211 Event::handle('EndShowNoticeOptionItems', array($this));
213 $this->out->elementEnd('div');
215 Event::handle('EndShowNoticeOptions', array($this));
220 * start a single notice.
226 if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
227 $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
228 $class = 'h-entry notice';
229 if ($this->notice->scope != 0 && $this->notice->scope != 1) {
230 $class .= ' limited-scope';
232 if (!empty($this->notice->source)) {
233 $class .= ' notice-source-'.$this->notice->source;
235 $id_prefix = (strlen($this->id_prefix) ? $this->id_prefix . '-' : '');
236 $this->out->elementStart($this->item_tag, array('class' => $class,
237 'id' => "${id_prefix}notice-${id}"));
238 Event::handle('EndOpenNoticeListItemElement', array($this));
243 * show the author of a notice
245 * By default, this shows the avatar and (linked) nickname of the author.
250 function showAuthor()
252 $attrs = array('href' => $this->profile->profileurl,
254 'title' => $this->profile->getNickname());
255 if(empty($this->repeat)) { $attrs['class'] .= ' p-author'; }
257 if (Event::handle('StartShowNoticeItemAuthor', array($this->profile, $this->out, &$attrs))) {
258 $this->out->elementStart('a', $attrs);
259 $this->showAvatar($this->profile);
260 $this->out->text($this->profile->getStreamName());
261 $this->out->elementEnd('a');
262 Event::handle('EndShowNoticeItemAuthor', array($this->profile, $this->out));
266 function showParent()
271 'href' => $this->notice->getParent()->getUrl(),
272 'class' => 'u-in-reply-to',
273 'rel' => 'in-reply-to'
279 function showAddressees()
281 $pa = $this->getProfileAddressees();
284 $this->out->elementStart('ul', 'addressees');
286 foreach ($pa as $addr) {
287 $this->out->elementStart('li', 'h-card');
288 $text = $addr['text'];
289 unset($addr['text']);
290 $this->out->element('a', $addr, $text);
291 $this->out->elementEnd('li');
293 $this->out->elementEnd('ul', 'addressees');
297 function getProfileAddressees()
299 if($this->pa) { return $this->pa; }
302 $attentions = $this->getAttentionProfiles();
304 foreach ($attentions as $attn) {
305 $class = $attn->isGroup() ? 'group' : 'account';
306 $profileurl = $attn->getUri();
307 if (common_valid_http_url($profileurl)) {
310 $profileurl = $attn->getUrl();
312 $this->pa[] = array('href' => $profileurl,
313 'title' => $attn->getNickname(),
314 'class' => "addressee {$class} p-name u-url",
315 'text' => $attn->getStreamName());
321 function getAttentionProfiles()
323 return $this->notice->getAttentionProfiles();
327 * show the nickname of the author
329 * Links to the author's profile page
333 function showNickname()
335 $this->out->raw('<span class="p-name">' .
336 htmlspecialchars($this->profile->getNickname()) .
341 * show the content of the notice
343 * Shows the content of the notice. This is pre-rendered for efficiency
344 * at save time. Some very old notices might not be pre-rendered, so
345 * they're rendered on the spot.
349 function showContent()
351 // FIXME: URL, image, video, audio
352 $nameClass = $this->notice->getTitle(false) ? '' : 'p-name ';
353 $this->out->elementStart('article', array('class' => $nameClass . 'e-content'));
354 if (Event::handle('StartShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()))) {
355 if ($this->maxchars > 0 && mb_strlen($this->notice->content) > $this->maxchars) {
356 $this->out->text(mb_substr($this->notice->content, 0, $this->maxchars) . '[…]');
358 $this->out->raw($this->notice->getRendered());
360 Event::handle('EndShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()));
362 $this->out->elementEnd('article');
365 function showNoticeAttachments() {
366 if (common_config('attachments', 'show_thumbs')) {
367 $al = new InlineAttachmentList($this->notice, $this->out);
373 * show the link to the main page for the notice
375 * Displays a local link to the rendered notice, with "relative" time.
379 function showNoticeLink()
381 $this->out->element('time', array('class' => 'dt-published',
382 'datetime' => common_date_iso8601($this->notice->created),
383 'title' => common_exact_date($this->notice->created)),
384 common_date_string($this->notice->created));
388 * show the notice location
390 * shows the notice location in the correct language.
392 * If an URL is available, makes a link. Otherwise, just a span.
396 function showNoticeLocation()
399 $location = Notice_location::locFromStored($this->notice);
400 } catch (NoResultException $e) {
402 } catch (ServerException $e) {
406 $name = $location->getName();
408 $lat = $location->lat;
409 $lon = $location->lon;
410 $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
413 $latdms = $this->decimalDegreesToDMS(abs($lat));
414 $londms = $this->decimalDegreesToDMS(abs($lon));
415 // TRANS: Used in coordinates as abbreviation of north.
417 // TRANS: Used in coordinates as abbreviation of south.
419 // TRANS: Used in coordinates as abbreviation of east.
421 // TRANS: Used in coordinates as abbreviation of west.
424 // TRANS: Coordinates message.
425 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
426 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
427 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
428 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
429 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
430 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
431 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
434 $url = $location->getUrl();
436 $this->out->text(' ');
437 $this->out->elementStart('span', array('class' => 'location'));
438 // TRANS: Followed by geo location.
439 $this->out->text(_('at'));
440 $this->out->text(' ');
442 $this->out->element('abbr', array('class' => 'geo',
446 $xstr = new XMLStringer(false);
447 $xstr->elementStart('a', array('href' => $url,
448 'rel' => 'external'));
449 $xstr->element('abbr', array('class' => 'geo',
452 $xstr->elementEnd('a');
453 $this->out->raw($xstr->getString());
455 $this->out->elementEnd('span');
459 * @param number $dec decimal degrees
460 * @return array split into 'deg', 'min', and 'sec'
462 function decimalDegreesToDMS($dec)
465 $tempma = abs($dec) - abs($deg);
467 $tempma = $tempma * 3600;
468 $min = floor($tempma / 60);
469 $sec = $tempma - ($min*60);
471 return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
475 * Show the source of the notice
477 * Either the name (and link) of the API client that posted the notice,
478 * or one of other other channels.
482 function showNoticeSource()
484 $ns = $this->notice->getSource();
486 if (!$ns instanceof Notice_source) {
490 // TRANS: A possible notice source (web interface).
491 $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
492 $this->out->text(' ');
493 $this->out->elementStart('span', 'source');
494 // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
495 // TRANS: Followed by notice source.
496 $this->out->text(_('from'));
497 $this->out->text(' ');
499 $name = $source_name;
503 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
504 $name = $source_name;
507 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
509 // if $ns->name and $ns->url are populated we have
510 // configured a source attr somewhere
511 if (!empty($name) && !empty($url)) {
512 $this->out->elementStart('span', 'device');
519 if (!empty($title)) {
520 $attrs['title'] = $title;
523 $this->out->element('a', $attrs, $name);
524 $this->out->elementEnd('span');
526 $this->out->element('span', 'device', $name);
529 $this->out->elementEnd('span');
533 * show link to single-notice view for this notice item
535 * A permalink that goes to this specific object and nothing else
539 function showPermalink()
541 $class = 'permalink u-url';
542 if (!$this->notice->isLocal()) {
543 $class .= ' external';
548 $this->out->element('a',
549 array('href' => $this->repeat->getUrl(),
552 $class = str_replace('u-url', 'u-repost-of', $class);
554 } catch (InvalidUrlException $e) {
555 // no permalink available
559 $this->out->element('a',
560 array('href' => $this->notice->getUrl(true),
562 // TRANS: Addition in notice list item for single-notice view.
564 } catch (InvalidUrlException $e) {
565 // no permalink available
570 * Show link to conversation view.
572 function showContextLink()
574 $this->out->element('a', array('rel' => 'bookmark',
575 'class' => 'timestamp',
576 'href' => Conversation::getUrlFromNotice($this->notice)),
577 // TRANS: A link to the conversation view of a notice, on the local server.
578 _('In conversation'));
582 * show a link to reply to the current notice
584 * Should either do the reply in the current notice form (if available), or
585 * link out to the notice-posting form. A little flakey, doesn't always work.
589 function showReplyLink()
591 if (common_logged_in()) {
592 $this->out->text(' ');
593 $reply_url = common_local_url('newnotice',
594 array('replyto' => $this->profile->getNickname(), 'inreplyto' => $this->notice->id));
595 $this->out->elementStart('a', array('href' => $reply_url,
596 'class' => 'notice_reply',
597 // TRANS: Link title in notice list item to reply to a notice.
598 'title' => _('Reply to this notice.')));
599 // TRANS: Link text in notice list item to reply to a notice.
600 $this->out->text(_('Reply'));
601 $this->out->text(' ');
602 $this->out->element('span', 'notice_id', $this->notice->id);
603 $this->out->elementEnd('a');
608 * if the user is the author, let them delete the notice
612 function showDeleteLink()
614 $user = common_current_user();
616 $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
619 ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
620 $this->out->text(' ');
621 $deleteurl = common_local_url('deletenotice',
622 array('notice' => $todel->id));
623 $this->out->element('a', array('href' => $deleteurl,
624 'class' => 'notice_delete popup',
625 // TRANS: Link title in notice list item to delete a notice.
626 'title' => _('Delete this notice from the timeline.')),
627 // TRANS: Link text in notice list item to delete a notice.
635 * Close the last elements in the notice list item
641 if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
642 $this->out->elementEnd('li');
643 Event::handle('EndCloseNoticeListItemElement', array($this));
648 * Get the notice in question
650 * For hooks, etc., this may be useful
652 * @return Notice The notice we're showing
657 return $this->notice;