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
70 * Also initializes the profile attribute.
72 * @param Notice $notice The notice we'll display
74 function __construct(Notice $notice, Action $out=null, array $prefs=array())
76 parent::__construct($out);
77 if (!empty($notice->repeat_of)) {
78 $original = Notice::getKV('id', $notice->repeat_of);
79 if (!$original instanceof Notice) { // could have been deleted
80 $this->notice = $notice;
82 $this->notice = $original;
83 $this->repeat = $notice;
86 $this->notice = $notice;
89 $this->profile = $this->notice->getProfile();
91 // integer preferences
92 foreach(array('maxchars') as $key) {
93 if (array_key_exists($key, $prefs)) {
94 $this->$key = (int)$prefs[$key];
97 // boolean preferences
98 foreach(array('addressees', 'attachments', 'options') as $key) {
99 if (array_key_exists($key, $prefs)) {
100 $this->$key = (bool)$prefs[$key];
103 // string preferences
104 foreach(array('id_prefix') as $key) {
105 if (array_key_exists($key, $prefs)) {
106 $this->$key = $prefs[$key];
112 * recipe function for displaying a single notice.
114 * This uses all the other methods to correctly display a notice. Override
115 * it or one of the others to fine-tune the output.
121 if (empty($this->notice)) {
122 common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
124 } else if (empty($this->profile)) {
125 common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
130 if (Event::handle('StartShowNoticeItem', array($this))) {
132 Event::handle('EndShowNoticeItem', array($this));
137 function showNotice()
139 if (Event::handle('StartShowNoticeItemNotice', array($this))) {
140 $this->showNoticeHeaders();
141 $this->showContent();
142 $this->showNoticeFooter();
143 Event::handle('EndShowNoticeItemNotice', array($this));
147 function showNoticeHeaders()
149 $this->elementStart('section', array('class'=>'notice-headers'));
150 $this->showNoticeTitle();
152 if ($this->addressees) { $this->showAddressees(); }
153 $this->elementEnd('section');
156 function showNoticeFooter()
158 $this->elementStart('footer');
159 $this->showNoticeInfo();
160 if ($this->attachments) { $this->showNoticeAttachments(); }
161 if ($this->options) { $this->showNoticeOptions(); }
162 $this->elementEnd('footer');
165 function showNoticeTitle()
167 if (Event::handle('StartShowNoticeTitle', array($this))) {
168 $this->element('a', array('href' => $this->notice->getUrl(),
169 'class' => 'notice-title'),
170 $this->notice->getTitle());
171 Event::handle('EndShowNoticeTitle', array($this));
175 function showNoticeInfo()
177 if (Event::handle('StartShowNoticeInfo', array($this))) {
178 $this->showNoticeLink();
179 $this->showNoticeSource();
180 $this->showNoticeLocation();
181 $this->showPermalink();
183 Event::handle('EndShowNoticeInfo', array($this));
187 function showNoticeOptions()
189 if (Event::handle('StartShowNoticeOptions', array($this))) {
190 $user = common_current_user();
192 $this->out->elementStart('div', 'notice-options');
193 if (Event::handle('StartShowNoticeOptionItems', array($this))) {
194 $this->showReplyLink();
195 $this->showRepeatForm();
196 $this->showDeleteLink();
197 Event::handle('EndShowNoticeOptionItems', array($this));
199 $this->out->elementEnd('div');
201 Event::handle('EndShowNoticeOptions', array($this));
206 * start a single notice.
212 if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
213 $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
214 $class = 'h-entry notice';
215 if ($this->notice->scope != 0 && $this->notice->scope != 1) {
216 $class .= ' limited-scope';
218 if (!empty($this->notice->source)) {
219 $class .= ' notice-source-'.$this->notice->source;
221 $id_prefix = (strlen($this->id_prefix) ? $this->id_prefix . '-' : '');
222 $this->out->elementStart('li', array('class' => $class,
223 'id' => "${id_prefix}notice-${id}"));
224 Event::handle('EndOpenNoticeListItemElement', array($this));
229 * show the author of a notice
231 * By default, this shows the avatar and (linked) nickname of the author.
236 function showAuthor()
238 $attrs = array('href' => $this->profile->profileurl,
239 'class' => 'h-card p-author',
240 'title' => $this->profile->getNickname());
242 if (Event::handle('StartShowNoticeItemAuthor', array($this->profile, $this->out, &$attrs))) {
243 $this->out->elementStart('a', $attrs);
244 $this->showAvatar($this->profile);
245 $this->out->text($this->profile->getStreamName());
246 $this->out->elementEnd('a');
247 Event::handle('EndShowNoticeItemAuthor', array($this->profile, $this->out));
251 function showAddressees()
253 $pa = $this->getProfileAddressees();
256 $this->out->elementStart('ul', 'addressees');
258 foreach ($pa as $addr) {
259 $this->out->elementStart('li', 'h-card');
260 $text = $addr['text'];
261 unset($addr['text']);
262 $this->out->element('a', $addr, $text);
263 $this->out->elementEnd('li');
265 $this->out->elementEnd('ul', 'addressees');
269 function getProfileAddressees()
273 $attentions = $this->getReplyProfiles();
275 foreach ($attentions as $attn) {
276 $class = $attn->isGroup() ? 'group' : 'account';
277 $pa[] = array('href' => $attn->profileurl,
278 'title' => $attn->getNickname(),
279 'class' => "addressee {$class}",
280 'text' => $attn->getStreamName());
286 function getReplyProfiles()
288 return $this->notice->getReplyProfiles();
292 * show the nickname of the author
294 * Links to the author's profile page
298 function showNickname()
300 $this->out->raw('<span class="p-name">' .
301 htmlspecialchars($this->profile->getNickname()) .
306 * show the content of the notice
308 * Shows the content of the notice. This is pre-rendered for efficiency
309 * at save time. Some very old notices might not be pre-rendered, so
310 * they're rendered on the spot.
314 function showContent()
316 // FIXME: URL, image, video, audio
317 $this->out->elementStart('article', array('class' => 'e-content'));
318 if (Event::handle('StartShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()))) {
319 if ($this->maxchars > 0 && mb_strlen($this->notice->content) > $this->maxchars) {
320 $this->out->text(mb_substr($this->notice->content, 0, $this->maxchars) . '[…]');
321 } elseif ($this->notice->rendered) {
322 $this->out->raw($this->notice->rendered);
324 // XXX: may be some uncooked notices in the DB,
325 // we cook them right now. This should probably disappear in future
326 // versions (>> 0.4.x)
327 $this->out->raw(common_render_content($this->notice->content, $this->notice));
329 Event::handle('EndShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()));
331 $this->out->elementEnd('article');
334 function showNoticeAttachments() {
335 if (common_config('attachments', 'show_thumbs')) {
336 $al = new InlineAttachmentList($this->notice, $this->out);
342 * show the link to the main page for the notice
344 * Displays a local link to the rendered notice, with "relative" time.
348 function showNoticeLink()
350 $this->out->elementStart('a', array('rel' => 'bookmark',
351 'class' => 'timestamp',
352 'href' => Conversation::getUrlFromNotice($this->notice)));
353 $this->out->element('time', array('class' => 'dt-published',
354 'datetime' => common_date_iso8601($this->notice->created),
355 // TRANS: Timestamp title (tooltip text) for NoticeListItem
356 'title' => common_exact_date($this->notice->created)),
357 common_date_string($this->notice->created));
358 $this->out->elementEnd('a');
362 * show the notice location
364 * shows the notice location in the correct language.
366 * If an URL is available, makes a link. Otherwise, just a span.
370 function showNoticeLocation()
372 $id = $this->notice->id;
374 $location = $this->notice->getLocation();
376 if (empty($location)) {
380 $name = $location->getName();
382 $lat = $this->notice->lat;
383 $lon = $this->notice->lon;
384 $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
387 $latdms = $this->decimalDegreesToDMS(abs($lat));
388 $londms = $this->decimalDegreesToDMS(abs($lon));
389 // TRANS: Used in coordinates as abbreviation of north.
391 // TRANS: Used in coordinates as abbreviation of south.
393 // TRANS: Used in coordinates as abbreviation of east.
395 // TRANS: Used in coordinates as abbreviation of west.
398 // TRANS: Coordinates message.
399 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
400 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
401 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
402 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
403 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
404 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
405 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
408 $url = $location->getUrl();
410 $this->out->text(' ');
411 $this->out->elementStart('span', array('class' => 'location'));
412 // TRANS: Followed by geo location.
413 $this->out->text(_('at'));
414 $this->out->text(' ');
416 $this->out->element('abbr', array('class' => 'geo',
420 $xstr = new XMLStringer(false);
421 $xstr->elementStart('a', array('href' => $url,
422 'rel' => 'external'));
423 $xstr->element('abbr', array('class' => 'geo',
426 $xstr->elementEnd('a');
427 $this->out->raw($xstr->getString());
429 $this->out->elementEnd('span');
433 * @param number $dec decimal degrees
434 * @return array split into 'deg', 'min', and 'sec'
436 function decimalDegreesToDMS($dec)
439 $tempma = abs($dec) - abs($deg);
441 $tempma = $tempma * 3600;
442 $min = floor($tempma / 60);
443 $sec = $tempma - ($min*60);
445 return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
449 * Show the source of the notice
451 * Either the name (and link) of the API client that posted the notice,
452 * or one of other other channels.
456 function showNoticeSource()
458 $ns = $this->notice->getSource();
460 if (!$ns instanceof Notice_source) {
464 // TRANS: A possible notice source (web interface).
465 $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
466 $this->out->text(' ');
467 $this->out->elementStart('span', 'source');
468 // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
469 // TRANS: Followed by notice source.
470 $this->out->text(_('from'));
471 $this->out->text(' ');
473 $name = $source_name;
477 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
478 $name = $source_name;
481 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
483 // if $ns->name and $ns->url are populated we have
484 // configured a source attr somewhere
485 if (!empty($name) && !empty($url)) {
486 $this->out->elementStart('span', 'device');
493 if (!empty($title)) {
494 $attrs['title'] = $title;
497 $this->out->element('a', $attrs, $name);
498 $this->out->elementEnd('span');
500 $this->out->element('span', 'device', $name);
503 $this->out->elementEnd('span');
507 * show link to single-notice view for this notice item
509 * A permalink that goes to this specific object and nothing else
513 function showPermalink()
515 $class = 'permalink u-url';
516 if (!$this->notice->isLocal()) {
517 $class .= ' external';
520 $this->out->element('a',
521 array('href' => $this->notice->getUrl(),
523 // TRANS: Addition in notice list item for single-notice view.
525 } catch (InvalidUrlException $e) {
526 // no permalink available
531 * show a link to the author of repeat
535 function showRepeat()
537 if (!empty($this->repeat)) {
539 $repeater = Profile::getKV('id', $this->repeat->profile_id);
541 $attrs = array('href' => $repeater->profileurl,
542 'class' => 'h-card p-author',
543 'title' => $repeater->getFancyName());
545 $this->out->elementStart('span', 'repeat h-entry');
547 // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
548 $this->out->raw(_('Repeated by').' ');
550 $this->out->element('a', $attrs, $repeater->getNickname());
552 $this->out->elementEnd('span');
557 * show a link to reply to the current notice
559 * Should either do the reply in the current notice form (if available), or
560 * link out to the notice-posting form. A little flakey, doesn't always work.
564 function showReplyLink()
566 if (common_logged_in()) {
567 $this->out->text(' ');
568 $reply_url = common_local_url('newnotice',
569 array('replyto' => $this->profile->getNickname(), 'inreplyto' => $this->notice->id));
570 $this->out->elementStart('a', array('href' => $reply_url,
571 'class' => 'notice_reply',
572 // TRANS: Link title in notice list item to reply to a notice.
573 'title' => _('Reply to this notice.')));
574 // TRANS: Link text in notice list item to reply to a notice.
575 $this->out->text(_('Reply'));
576 $this->out->text(' ');
577 $this->out->element('span', 'notice_id', $this->notice->id);
578 $this->out->elementEnd('a');
583 * if the user is the author, let them delete the notice
587 function showDeleteLink()
589 $user = common_current_user();
591 $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
594 ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
595 $this->out->text(' ');
596 $deleteurl = common_local_url('deletenotice',
597 array('notice' => $todel->id));
598 $this->out->element('a', array('href' => $deleteurl,
599 'class' => 'notice_delete',
600 // TRANS: Link title in notice list item to delete a notice.
601 'title' => _('Delete this notice from the timeline.')),
602 // TRANS: Link text in notice list item to delete a notice.
608 * show the form to repeat a notice
612 function showRepeatForm()
614 if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
615 $this->notice->scope == Notice::SITE_SCOPE) {
616 $user = common_current_user();
618 $user->id != $this->notice->profile_id) {
619 $this->out->text(' ');
620 $profile = $user->getProfile();
621 if ($profile->hasRepeated($this->notice)) {
622 $this->out->element('span', array('class' => 'repeated',
623 // TRANS: Title for repeat form status in notice list when a notice has been repeated.
624 'title' => _('Notice repeated.')),
625 // TRANS: Repeat form status in notice list when a notice has been repeated.
628 $rf = new RepeatForm($this->out, $this->notice);
638 * Close the last elements in the notice list item
644 if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
645 $this->out->elementEnd('li');
646 Event::handle('EndCloseNoticeListItemElement', array($this));
651 * Get the notice in question
653 * For hooks, etc., this may be useful
655 * @return Notice The notice we're showing
660 return $this->notice;