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 if (Event::handle('StartShowNoticeItemNotice', array($this))) {
117 $this->showNoticeTitle();
119 $this->showAddressees();
120 $this->showContent();
121 Event::handle('EndShowNoticeItemNotice', array($this));
125 function showNoticeTitle()
127 if (Event::handle('StartShowNoticeTitle', array($this))) {
128 $this->element('a', array('href' => $this->notice->getUrl(),
129 'class' => 'p-name metadata'),
130 $this->notice->getTitle());
131 Event::handle('EndShowNoticeTitle', array($this));
135 function showNoticeInfo()
137 $this->out->elementStart('div', 'entry-metadata');
138 if (Event::handle('StartShowNoticeInfo', array($this))) {
139 $this->showNoticeLink();
140 $this->showNoticeSource();
141 $this->showNoticeLocation();
142 $this->showPermalink();
144 Event::handle('EndShowNoticeInfo', array($this));
147 $this->out->elementEnd('div');
150 function showNoticeOptions()
152 if (Event::handle('StartShowNoticeOptions', array($this))) {
153 $user = common_current_user();
155 if ($user instanceof User) {
156 $this->out->elementStart('div', 'notice-options');
157 if (Event::handle('StartShowNoticeOptionItems', array($this))) {
158 $this->showReplyLink();
159 $this->showRepeatForm();
160 $this->showDeleteLink();
161 Event::handle('EndShowNoticeOptionItems', array($this));
163 $this->out->elementEnd('div');
166 Event::handle('EndShowNoticeOptions', array($this));
171 * start a single notice.
177 if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
178 $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
179 $class = 'h-entry notice';
180 if ($this->notice->scope != 0 && $this->notice->scope != 1) {
181 $class .= ' limited-scope';
183 if (!empty($this->notice->source)) {
184 $class .= ' notice-source-'.$this->notice->source;
186 $this->out->elementStart('li', array('class' => $class,
187 'id' => 'notice-' . $id));
188 Event::handle('EndOpenNoticeListItemElement', array($this));
193 * show the author of a notice
195 * By default, this shows the avatar and (linked) nickname of the author.
200 function showAuthor()
202 $attrs = array('href' => $this->profile->profileurl,
203 'class' => 'h-card p-author',
204 'title' => $this->profile->getNickname());
206 if (Event::handle('StartShowNoticeItemAuthor', array($this->profile, $this->out, &$attrs))) {
207 $this->out->elementStart('a', $attrs);
208 $this->showAvatar($this->profile);
209 $this->out->text($this->profile->getStreamName());
210 $this->out->elementEnd('a');
211 Event::handle('EndShowNoticeItemAuthor', array($this->profile, $this->out));
215 function showAddressees()
217 $pa = $this->getProfileAddressees();
220 $this->out->elementStart('ul', 'addressees');
222 foreach ($pa as $addr) {
223 $this->out->elementStart('li', 'h-card');
224 $text = $addr['text'];
225 unset($addr['text']);
226 $this->out->element('a', $addr, $text);
227 $this->out->elementEnd('li');
229 $this->out->elementEnd('ul', 'addressees');
233 function getProfileAddressees()
237 $attentions = $this->getReplyProfiles();
239 foreach ($attentions as $attn) {
240 $class = $attn->isGroup() ? 'group' : 'account';
241 $pa[] = array('href' => $attn->profileurl,
242 'title' => $attn->getNickname(),
243 'class' => "addressee {$class}",
244 'text' => $attn->getStreamName());
250 function getReplyProfiles()
252 return $this->notice->getReplyProfiles();
256 * show the nickname of the author
258 * Links to the author's profile page
262 function showNickname()
264 $this->out->raw('<span class="p-name">' .
265 htmlspecialchars($this->profile->getNickname()) .
270 * show the content of the notice
272 * Shows the content of the notice. This is pre-rendered for efficiency
273 * at save time. Some very old notices might not be pre-rendered, so
274 * they're rendered on the spot.
278 function showContent()
280 // FIXME: URL, image, video, audio
281 $this->out->elementStart('div', array('class' => 'e-content'));
282 if (Event::handle('StartShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()))) {
283 if ($this->notice->rendered) {
284 $this->out->raw($this->notice->rendered);
286 // XXX: may be some uncooked notices in the DB,
287 // we cook them right now. This should probably disappear in future
288 // versions (>> 0.4.x)
289 $this->out->raw(common_render_content($this->notice->content, $this->notice));
291 Event::handle('EndShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()));
293 $this->out->elementEnd('div');
296 function showNoticeAttachments() {
297 if (common_config('attachments', 'show_thumbs')) {
298 $al = new InlineAttachmentList($this->notice, $this->out);
304 * show the link to the main page for the notice
306 * Displays a local link to the rendered notice, with "relative" time.
310 function showNoticeLink()
312 $this->out->elementStart('a', array('rel' => 'bookmark',
313 'class' => 'timestamp',
314 'href' => Conversation::getUrlFromNotice($this->notice)));
315 $this->out->element('time', array('class' => 'dt-published',
316 'datetime' => common_date_iso8601($this->notice->created),
317 // TRANS: Timestamp title (tooltip text) for NoticeListItem
318 'title' => common_exact_date($this->notice->created)),
319 common_date_string($this->notice->created));
320 $this->out->elementEnd('a');
324 * show the notice location
326 * shows the notice location in the correct language.
328 * If an URL is available, makes a link. Otherwise, just a span.
332 function showNoticeLocation()
334 $id = $this->notice->id;
336 $location = $this->notice->getLocation();
338 if (empty($location)) {
342 $name = $location->getName();
344 $lat = $this->notice->lat;
345 $lon = $this->notice->lon;
346 $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
349 $latdms = $this->decimalDegreesToDMS(abs($lat));
350 $londms = $this->decimalDegreesToDMS(abs($lon));
351 // TRANS: Used in coordinates as abbreviation of north.
353 // TRANS: Used in coordinates as abbreviation of south.
355 // TRANS: Used in coordinates as abbreviation of east.
357 // TRANS: Used in coordinates as abbreviation of west.
360 // TRANS: Coordinates message.
361 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
362 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
363 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
364 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
365 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
366 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
367 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
370 $url = $location->getUrl();
372 $this->out->text(' ');
373 $this->out->elementStart('span', array('class' => 'location'));
374 // TRANS: Followed by geo location.
375 $this->out->text(_('at'));
376 $this->out->text(' ');
378 $this->out->element('abbr', array('class' => 'geo',
382 $xstr = new XMLStringer(false);
383 $xstr->elementStart('a', array('href' => $url,
384 'rel' => 'external'));
385 $xstr->element('abbr', array('class' => 'geo',
388 $xstr->elementEnd('a');
389 $this->out->raw($xstr->getString());
391 $this->out->elementEnd('span');
395 * @param number $dec decimal degrees
396 * @return array split into 'deg', 'min', and 'sec'
398 function decimalDegreesToDMS($dec)
401 $tempma = abs($dec) - abs($deg);
403 $tempma = $tempma * 3600;
404 $min = floor($tempma / 60);
405 $sec = $tempma - ($min*60);
407 return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
411 * Show the source of the notice
413 * Either the name (and link) of the API client that posted the notice,
414 * or one of other other channels.
418 function showNoticeSource()
420 $ns = $this->notice->getSource();
422 if (!$ns instanceof Notice_source) {
426 // TRANS: A possible notice source (web interface).
427 $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
428 $this->out->text(' ');
429 $this->out->elementStart('span', 'source');
430 // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
431 // TRANS: Followed by notice source.
432 $this->out->text(_('from'));
433 $this->out->text(' ');
435 $name = $source_name;
439 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
440 $name = $source_name;
443 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
445 // if $ns->name and $ns->url are populated we have
446 // configured a source attr somewhere
447 if (!empty($name) && !empty($url)) {
448 $this->out->elementStart('span', 'device');
455 if (!empty($title)) {
456 $attrs['title'] = $title;
459 $this->out->element('a', $attrs, $name);
460 $this->out->elementEnd('span');
462 $this->out->element('span', 'device', $name);
465 $this->out->elementEnd('span');
469 * show link to single-notice view for this notice item
471 * A permalink that goes to this specific object and nothing else
475 function showPermalink()
477 $class = 'permalink u-url';
478 if (!$this->notice->isLocal()) {
479 $class .= ' external';
482 $this->out->element('a',
483 array('href' => $this->notice->getUrl(),
485 // TRANS: Addition in notice list item for single-notice view.
487 } catch (InvalidUrlException $e) {
488 // no permalink available
493 * show a link to the author of repeat
497 function showRepeat()
499 if (!empty($this->repeat)) {
501 $repeater = Profile::getKV('id', $this->repeat->profile_id);
503 $attrs = array('href' => $repeater->profileurl,
504 'class' => 'h-card p-author',
505 'title' => $repeater->getFancyName());
507 $this->out->elementStart('span', 'repeat h-entry');
509 // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
510 $this->out->raw(_('Repeated by').' ');
512 $this->out->element('a', $attrs, $repeater->getNickname());
514 $this->out->elementEnd('span');
519 * show a link to reply to the current notice
521 * Should either do the reply in the current notice form (if available), or
522 * link out to the notice-posting form. A little flakey, doesn't always work.
526 function showReplyLink()
528 if (common_logged_in()) {
529 $this->out->text(' ');
530 $reply_url = common_local_url('newnotice',
531 array('replyto' => $this->profile->getNickname(), 'inreplyto' => $this->notice->id));
532 $this->out->elementStart('a', array('href' => $reply_url,
533 'class' => 'notice_reply',
534 // TRANS: Link title in notice list item to reply to a notice.
535 'title' => _('Reply to this notice.')));
536 // TRANS: Link text in notice list item to reply to a notice.
537 $this->out->text(_('Reply'));
538 $this->out->text(' ');
539 $this->out->element('span', 'notice_id', $this->notice->id);
540 $this->out->elementEnd('a');
545 * if the user is the author, let them delete the notice
549 function showDeleteLink()
551 $user = common_current_user();
553 $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
556 ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
557 $this->out->text(' ');
558 $deleteurl = common_local_url('deletenotice',
559 array('notice' => $todel->id));
560 $this->out->element('a', array('href' => $deleteurl,
561 'class' => 'notice_delete',
562 // TRANS: Link title in notice list item to delete a notice.
563 'title' => _('Delete this notice from the timeline.')),
564 // TRANS: Link text in notice list item to delete a notice.
570 * show the form to repeat a notice
574 function showRepeatForm()
576 if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
577 $this->notice->scope == Notice::SITE_SCOPE) {
578 $user = common_current_user();
580 $user->id != $this->notice->profile_id) {
581 $this->out->text(' ');
582 $profile = $user->getProfile();
583 if ($profile->hasRepeated($this->notice)) {
584 $this->out->element('span', array('class' => 'repeated',
585 // TRANS: Title for repeat form status in notice list when a notice has been repeated.
586 'title' => _('Notice repeated.')),
587 // TRANS: Repeat form status in notice list when a notice has been repeated.
590 $rf = new RepeatForm($this->out, $this->notice);
600 * Close the last elements in the notice list item
606 if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
607 $this->out->elementEnd('li');
608 Event::handle('EndCloseNoticeListItemElement', array($this));
613 * Get the notice in question
615 * For hooks, etc., this may be useful
617 * @return Notice The notice we're showing
622 return $this->notice;