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 if ($this->notice->hasConversation()) {
143 $this->showContext();
146 Event::handle('EndShowNoticeInfo', array($this));
149 $this->out->elementEnd('div');
152 function showNoticeOptions()
154 if (Event::handle('StartShowNoticeOptions', array($this))) {
155 $user = common_current_user();
157 $this->out->elementStart('div', 'notice-options');
158 if (Event::handle('StartShowNoticeOptionItems', array($this))) {
159 $this->showReplyLink();
160 $this->showRepeatForm();
161 $this->showDeleteLink();
162 Event::handle('EndShowNoticeOptionItems', array($this));
164 $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' => 'u-url timestamp',
314 'href' => $this->notice->getLocalUrl()));
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();
423 // TRANS: A possible notice source (web interface).
424 $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
425 $this->out->text(' ');
426 $this->out->elementStart('span', 'source');
427 // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
428 // TRANS: Followed by notice source.
429 $this->out->text(_('from'));
430 $this->out->text(' ');
432 $name = $source_name;
436 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
437 $name = $source_name;
440 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
442 // if $ns->name and $ns->url are populated we have
443 // configured a source attr somewhere
444 if (!empty($name) && !empty($url)) {
445 $this->out->elementStart('span', 'device');
452 if (!empty($title)) {
453 $attrs['title'] = $title;
456 $this->out->element('a', $attrs, $name);
457 $this->out->elementEnd('span');
459 $this->out->element('span', 'device', $name);
462 $this->out->elementEnd('span');
467 * show link to notice this notice is a reply to
469 * If this notice is a reply, show a link to the notice it is replying to. The
470 * heavy lifting for figuring out replies happens at save time.
474 function showContext()
476 $this->out->element('a',
477 array('href' => $this->notice->getConversationUrl(),
478 'class' => 'conversation'),
479 // TRANS: Addition in notice list item if notice is part of a conversation.
484 * show a link to the author of repeat
488 function showRepeat()
490 if (!empty($this->repeat)) {
492 $repeater = Profile::getKV('id', $this->repeat->profile_id);
494 $attrs = array('href' => $repeater->profileurl,
495 'class' => 'h-card p-author',
496 'title' => $repeater->getFancyName());
498 $this->out->elementStart('span', 'repeat h-entry');
500 // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
501 $this->out->raw(_('Repeated by').' ');
503 $this->out->element('a', $attrs, $repeater->getNickname());
505 $this->out->elementEnd('span');
510 * show a link to reply to the current notice
512 * Should either do the reply in the current notice form (if available), or
513 * link out to the notice-posting form. A little flakey, doesn't always work.
517 function showReplyLink()
519 if (common_logged_in()) {
520 $this->out->text(' ');
521 $reply_url = common_local_url('newnotice',
522 array('replyto' => $this->profile->getNickname(), 'inreplyto' => $this->notice->id));
523 $this->out->elementStart('a', array('href' => $reply_url,
524 'class' => 'notice_reply',
525 // TRANS: Link title in notice list item to reply to a notice.
526 'title' => _('Reply to this notice.')));
527 // TRANS: Link text in notice list item to reply to a notice.
528 $this->out->text(_('Reply'));
529 $this->out->text(' ');
530 $this->out->element('span', 'notice_id', $this->notice->id);
531 $this->out->elementEnd('a');
536 * if the user is the author, let them delete the notice
540 function showDeleteLink()
542 $user = common_current_user();
544 $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
547 ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
548 $this->out->text(' ');
549 $deleteurl = common_local_url('deletenotice',
550 array('notice' => $todel->id));
551 $this->out->element('a', array('href' => $deleteurl,
552 'class' => 'notice_delete',
553 // TRANS: Link title in notice list item to delete a notice.
554 'title' => _('Delete this notice from the timeline.')),
555 // TRANS: Link text in notice list item to delete a notice.
561 * show the form to repeat a notice
565 function showRepeatForm()
567 if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
568 $this->notice->scope == Notice::SITE_SCOPE) {
569 $user = common_current_user();
571 $user->id != $this->notice->profile_id) {
572 $this->out->text(' ');
573 $profile = $user->getProfile();
574 if ($profile->hasRepeated($this->notice)) {
575 $this->out->element('span', array('class' => 'repeated',
576 // TRANS: Title for repeat form status in notice list when a notice has been repeated.
577 'title' => _('Notice repeated.')),
578 // TRANS: Repeat form status in notice list when a notice has been repeated.
581 $rf = new RepeatForm($this->out, $this->notice);
591 * Close the last elements in the notice list item
597 if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
598 $this->out->elementEnd('li');
599 Event::handle('EndCloseNoticeListItemElement', array($this));
604 * Get the notice in question
606 * For hooks, etc., this may be useful
608 * @return Notice The notice we're showing
613 return $this->notice;