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('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * widget for displaying a single notice
40 * This widget has the core smarts for showing a single notice: what to display,
41 * where, and under which circumstances. Its key method is show(); this is a recipe
42 * that calls all the other show*() methods to build up a single notice. The
43 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
44 * author info (since that's implicit by the data in the page).
48 * @author Evan Prodromou <evan@status.net>
49 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50 * @link http://status.net/
52 * @see ProfileNoticeListItem
55 class NoticeListItem extends Widget
57 /** The notice this item will show. */
61 /** The notice that was repeated. */
65 /** The profile of the author of the notice, extracted once for convenience. */
72 * Also initializes the profile attribute.
74 * @param Notice $notice The notice we'll display
77 function __construct($notice, $out=null)
79 parent::__construct($out);
80 if (!empty($notice->repeat_of)) {
81 $original = Notice::staticGet('id', $notice->repeat_of);
82 if (empty($original)) { // could have been deleted
83 $this->notice = $notice;
85 $this->notice = $original;
86 $this->repeat = $notice;
89 $this->notice = $notice;
91 $this->profile = $this->notice->getProfile();
95 * recipe function for displaying a single notice.
97 * This uses all the other methods to correctly display a notice. Override
98 * it or one of the others to fine-tune the output.
105 if (empty($this->notice)) {
106 common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
108 } else if (empty($this->profile)) {
109 common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
114 if (Event::handle('StartShowNoticeItem', array($this))) {
116 $this->showNoticeAttachments();
117 $this->showNoticeInfo();
118 $this->showNoticeOptions();
119 Event::handle('EndShowNoticeItem', array($this));
124 function showNotice()
126 $this->out->elementStart('div', 'entry-title');
128 $this->showContent();
129 $this->out->elementEnd('div');
132 function showNoticeInfo()
134 $this->out->elementStart('div', 'entry-content');
135 if (Event::handle('StartShowNoticeInfo', array($this))) {
136 $this->showNoticeLink();
137 $this->showNoticeSource();
138 $this->showNoticeLocation();
139 $this->showContext();
141 Event::handle('EndShowNoticeInfo', array($this));
144 $this->out->elementEnd('div');
147 function showNoticeOptions()
149 if (Event::handle('StartShowNoticeOptions', array($this))) {
150 $user = common_current_user();
152 $this->out->elementStart('div', 'notice-options');
153 $this->showFaveForm();
154 $this->showReplyLink();
155 $this->showRepeatForm();
156 $this->showDeleteLink();
157 $this->out->elementEnd('div');
159 Event::handle('EndShowNoticeOptions', array($this));
164 * start a single notice.
171 if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
172 $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
173 $this->out->elementStart('li', array('class' => 'hentry notice',
174 'id' => 'notice-' . $id));
175 Event::handle('EndOpenNoticeListItemElement', array($this));
180 * show the "favorite" form
185 function showFaveForm()
187 if (Event::handle('StartShowFaveForm', array($this))) {
188 $user = common_current_user();
190 if ($user->hasFave($this->notice)) {
191 $disfavor = new DisfavorForm($this->out, $this->notice);
194 $favor = new FavorForm($this->out, $this->notice);
198 Event::handle('EndShowFaveForm', array($this));
203 * show the author of a notice
205 * By default, this shows the avatar and (linked) nickname of the author.
210 function showAuthor()
212 $this->out->elementStart('span', 'vcard author');
213 $attrs = array('href' => $this->profile->profileurl,
215 if (!empty($this->profile->fullname)) {
216 $attrs['title'] = $this->profile->getFancyName();
218 $this->out->elementStart('a', $attrs);
220 $this->out->text(' ');
221 $this->showNickname();
222 $this->out->elementEnd('a');
223 $this->out->elementEnd('span');
227 * show the avatar of the notice's author
229 * This will use the default avatar if no avatar is assigned for the author.
230 * It makes a link to the author's profile.
235 function showAvatar()
237 $avatar_size = $this->avatarSize();
239 $avatar = $this->profile->getAvatar($avatar_size);
241 $this->out->element('img', array('src' => ($avatar) ?
242 $avatar->displayUrl() :
243 Avatar::defaultImage($avatar_size),
244 'class' => 'avatar photo',
245 'width' => $avatar_size,
246 'height' => $avatar_size,
248 ($this->profile->fullname) ?
249 $this->profile->fullname :
250 $this->profile->nickname));
253 function avatarSize()
255 return AVATAR_STREAM_SIZE;
259 * show the nickname of the author
261 * Links to the author's profile page
266 function showNickname()
268 $this->out->raw('<span class="nickname fn">' .
269 htmlspecialchars($this->profile->nickname) .
274 * show the content of the notice
276 * Shows the content of the notice. This is pre-rendered for efficiency
277 * at save time. Some very old notices might not be pre-rendered, so
278 * they're rendered on the spot.
283 function showContent()
285 // FIXME: URL, image, video, audio
286 $this->out->elementStart('p', array('class' => 'entry-content'));
287 if ($this->notice->rendered) {
288 $this->out->raw($this->notice->rendered);
290 // XXX: may be some uncooked notices in the DB,
291 // we cook them right now. This should probably disappear in future
292 // versions (>> 0.4.x)
293 $this->out->raw(common_render_content($this->notice->content, $this->notice));
295 $this->out->elementEnd('p');
298 function showNoticeAttachments() {
299 if (common_config('attachments', 'show_thumbs')) {
300 $al = new InlineAttachmentList($this->notice, $this->out);
306 * show the link to the main page for the notice
308 * Displays a link to the page for a notice, with "relative" time. Tries to
309 * get remote notice URLs correct, but doesn't always succeed.
314 function showNoticeLink()
316 $noticeurl = $this->notice->bestUrl();
318 // above should always return an URL
320 assert(!empty($noticeurl));
322 $this->out->elementStart('a', array('rel' => 'bookmark',
323 'class' => 'timestamp',
324 'href' => $noticeurl));
325 $dt = common_date_iso8601($this->notice->created);
326 $this->out->element('abbr', array('class' => 'published',
328 common_date_string($this->notice->created));
329 $this->out->elementEnd('a');
333 * show the notice location
335 * shows the notice location in the correct language.
337 * If an URL is available, makes a link. Otherwise, just a span.
342 function showNoticeLocation()
344 $id = $this->notice->id;
346 $location = $this->notice->getLocation();
348 if (empty($location)) {
352 $name = $location->getName();
354 $lat = $this->notice->lat;
355 $lon = $this->notice->lon;
356 $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
359 $latdms = $this->decimalDegreesToDMS(abs($lat));
360 $londms = $this->decimalDegreesToDMS(abs($lon));
361 // TRANS: Used in coordinates as abbreviation of north
363 // TRANS: Used in coordinates as abbreviation of south
365 // TRANS: Used in coordinates as abbreviation of east
367 // TRANS: Used in coordinates as abbreviation of west
370 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
371 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
372 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
375 $url = $location->getUrl();
377 $this->out->text(' ');
378 $this->out->elementStart('span', array('class' => 'location'));
379 $this->out->text(_('at'));
380 $this->out->text(' ');
382 $this->out->element('abbr', array('class' => 'geo',
386 $xstr = new XMLStringer(false);
387 $xstr->elementStart('a', array('href' => $url,
388 'rel' => 'external'));
389 $xstr->element('abbr', array('class' => 'geo',
392 $xstr->elementEnd('a');
393 $this->out->raw($xstr->getString());
395 $this->out->elementEnd('span');
399 * @param number $dec decimal degrees
400 * @return array split into 'deg', 'min', and 'sec'
402 function decimalDegreesToDMS($dec)
405 $tempma = abs($dec) - abs($deg);
407 $tempma = $tempma * 3600;
408 $min = floor($tempma / 60);
409 $sec = $tempma - ($min*60);
411 return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
415 * Show the source of the notice
417 * Either the name (and link) of the API client that posted the notice,
418 * or one of other other channels.
423 function showNoticeSource()
425 $ns = $this->notice->getSource();
428 $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _('web')) : _($ns->name);
429 $this->out->text(' ');
430 $this->out->elementStart('span', 'source');
431 // FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
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)) {
449 $this->out->elementStart('span', 'device');
456 if (!empty($title)) {
457 $attrs['title'] = $title;
460 $this->out->element('a', $attrs, $name);
461 $this->out->elementEnd('span');
463 $this->out->element('span', 'device', $name);
466 $this->out->elementEnd('span');
471 * show link to notice this notice is a reply to
473 * If this notice is a reply, show a link to the notice it is replying to. The
474 * heavy lifting for figuring out replies happens at save time.
479 function showContext()
481 if ($this->notice->hasConversation()) {
482 $conv = Conversation::staticGet(
484 $this->notice->conversation
486 $convurl = $conv->uri;
487 if (!empty($convurl)) {
488 $this->out->text(' ');
492 'href' => $convurl.'#notice-'.$this->notice->id,
493 'class' => 'response'),
498 "Couldn't find Conversation ID %d to make 'in context'"
499 . "link for Notice ID %d",
500 $this->notice->conversation,
503 common_log(LOG_WARNING, $msg);
509 * show a link to the author of repeat
514 function showRepeat()
516 if (!empty($this->repeat)) {
518 $repeater = Profile::staticGet('id', $this->repeat->profile_id);
520 $attrs = array('href' => $repeater->profileurl,
523 if (!empty($repeater->fullname)) {
524 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
527 $this->out->elementStart('span', 'repeat vcard');
529 $this->out->raw(_('Repeated by'));
531 $this->out->elementStart('a', $attrs);
532 $this->out->element('span', 'fn nickname', $repeater->nickname);
533 $this->out->elementEnd('a');
535 $this->out->elementEnd('span');
540 * show a link to reply to the current notice
542 * Should either do the reply in the current notice form (if available), or
543 * link out to the notice-posting form. A little flakey, doesn't always work.
548 function showReplyLink()
550 if (common_logged_in()) {
551 $this->out->text(' ');
552 $reply_url = common_local_url('newnotice',
553 array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
554 $this->out->elementStart('a', array('href' => $reply_url,
555 'class' => 'notice_reply',
556 'title' => _('Reply to this notice')));
557 $this->out->text(_('Reply'));
558 $this->out->text(' ');
559 $this->out->element('span', 'notice_id', $this->notice->id);
560 $this->out->elementEnd('a');
565 * if the user is the author, let them delete the notice
570 function showDeleteLink()
572 $user = common_current_user();
574 $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
577 ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
578 $this->out->text(' ');
579 $deleteurl = common_local_url('deletenotice',
580 array('notice' => $todel->id));
581 $this->out->element('a', array('href' => $deleteurl,
582 'class' => 'notice_delete',
583 'title' => _('Delete this notice')), _('Delete'));
588 * show the form to repeat a notice
593 function showRepeatForm()
595 $user = common_current_user();
596 if ($user && $user->id != $this->notice->profile_id) {
597 $this->out->text(' ');
598 $profile = $user->getProfile();
599 if ($profile->hasRepeated($this->notice->id)) {
600 $this->out->element('span', array('class' => 'repeated',
601 'title' => _('Notice repeated')),
604 $rf = new RepeatForm($this->out, $this->notice);
613 * Close the last elements in the notice list item
620 if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
621 $this->out->elementEnd('li');
622 Event::handle('EndCloseNoticeListItemElement', array($this));