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 $class = 'hentry notice';
174 if ($this->notice->scope != 0 && $this->notice->scope != 1) {
175 $class .= ' limited-scope';
177 $this->out->elementStart('li', array('class' => $class,
178 'id' => 'notice-' . $id));
179 Event::handle('EndOpenNoticeListItemElement', array($this));
184 * show the "favorite" form
189 function showFaveForm()
191 if (Event::handle('StartShowFaveForm', array($this))) {
192 $user = common_current_user();
194 if ($user->hasFave($this->notice)) {
195 $disfavor = new DisfavorForm($this->out, $this->notice);
198 $favor = new FavorForm($this->out, $this->notice);
202 Event::handle('EndShowFaveForm', array($this));
207 * show the author of a notice
209 * By default, this shows the avatar and (linked) nickname of the author.
214 function showAuthor()
216 $this->out->elementStart('span', 'vcard author');
217 $attrs = array('href' => $this->profile->profileurl,
219 if (!empty($this->profile->fullname)) {
220 $attrs['title'] = $this->profile->getFancyName();
222 $this->out->elementStart('a', $attrs);
224 $this->out->text(' ');
225 $this->showNickname();
226 $this->out->elementEnd('a');
227 $this->out->elementEnd('span');
231 * show the avatar of the notice's author
233 * This will use the default avatar if no avatar is assigned for the author.
234 * It makes a link to the author's profile.
239 function showAvatar()
241 $avatar_size = $this->avatarSize();
243 $avatar = $this->profile->getAvatar($avatar_size);
245 $this->out->element('img', array('src' => ($avatar) ?
246 $avatar->displayUrl() :
247 Avatar::defaultImage($avatar_size),
248 'class' => 'avatar photo',
249 'width' => $avatar_size,
250 'height' => $avatar_size,
252 ($this->profile->fullname) ?
253 $this->profile->fullname :
254 $this->profile->nickname));
257 function avatarSize()
259 return AVATAR_STREAM_SIZE;
263 * show the nickname of the author
265 * Links to the author's profile page
270 function showNickname()
272 $this->out->raw('<span class="nickname fn">' .
273 htmlspecialchars($this->profile->nickname) .
278 * show the content of the notice
280 * Shows the content of the notice. This is pre-rendered for efficiency
281 * at save time. Some very old notices might not be pre-rendered, so
282 * they're rendered on the spot.
287 function showContent()
289 // FIXME: URL, image, video, audio
290 $this->out->elementStart('p', array('class' => 'entry-content'));
291 if ($this->notice->rendered) {
292 $this->out->raw($this->notice->rendered);
294 // XXX: may be some uncooked notices in the DB,
295 // we cook them right now. This should probably disappear in future
296 // versions (>> 0.4.x)
297 $this->out->raw(common_render_content($this->notice->content, $this->notice));
299 $this->out->elementEnd('p');
302 function showNoticeAttachments() {
303 if (common_config('attachments', 'show_thumbs')) {
304 $al = new InlineAttachmentList($this->notice, $this->out);
310 * show the link to the main page for the notice
312 * Displays a link to the page for a notice, with "relative" time. Tries to
313 * get remote notice URLs correct, but doesn't always succeed.
318 function showNoticeLink()
320 $noticeurl = $this->notice->bestUrl();
322 // above should always return an URL
324 assert(!empty($noticeurl));
326 $this->out->elementStart('a', array('rel' => 'bookmark',
327 'class' => 'timestamp',
328 'href' => $noticeurl));
329 $dt = common_date_iso8601($this->notice->created);
330 $this->out->element('abbr', array('class' => 'published',
332 common_date_string($this->notice->created));
333 $this->out->elementEnd('a');
337 * show the notice location
339 * shows the notice location in the correct language.
341 * If an URL is available, makes a link. Otherwise, just a span.
346 function showNoticeLocation()
348 $id = $this->notice->id;
350 $location = $this->notice->getLocation();
352 if (empty($location)) {
356 $name = $location->getName();
358 $lat = $this->notice->lat;
359 $lon = $this->notice->lon;
360 $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
363 $latdms = $this->decimalDegreesToDMS(abs($lat));
364 $londms = $this->decimalDegreesToDMS(abs($lon));
365 // TRANS: Used in coordinates as abbreviation of north
367 // TRANS: Used in coordinates as abbreviation of south
369 // TRANS: Used in coordinates as abbreviation of east
371 // TRANS: Used in coordinates as abbreviation of west
374 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
375 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
376 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
379 $url = $location->getUrl();
381 $this->out->text(' ');
382 $this->out->elementStart('span', array('class' => 'location'));
383 $this->out->text(_('at'));
384 $this->out->text(' ');
386 $this->out->element('abbr', array('class' => 'geo',
390 $xstr = new XMLStringer(false);
391 $xstr->elementStart('a', array('href' => $url,
392 'rel' => 'external'));
393 $xstr->element('abbr', array('class' => 'geo',
396 $xstr->elementEnd('a');
397 $this->out->raw($xstr->getString());
399 $this->out->elementEnd('span');
403 * @param number $dec decimal degrees
404 * @return array split into 'deg', 'min', and 'sec'
406 function decimalDegreesToDMS($dec)
409 $tempma = abs($dec) - abs($deg);
411 $tempma = $tempma * 3600;
412 $min = floor($tempma / 60);
413 $sec = $tempma - ($min*60);
415 return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
419 * Show the source of the notice
421 * Either the name (and link) of the API client that posted the notice,
422 * or one of other other channels.
427 function showNoticeSource()
429 $ns = $this->notice->getSource();
432 $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _('web')) : _($ns->name);
433 $this->out->text(' ');
434 $this->out->elementStart('span', 'source');
435 // FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
436 $this->out->text(_('from'));
437 $this->out->text(' ');
439 $name = $source_name;
443 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
444 $name = $source_name;
447 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
449 // if $ns->name and $ns->url are populated we have
450 // configured a source attr somewhere
451 if (!empty($name) && !empty($url)) {
453 $this->out->elementStart('span', 'device');
460 if (!empty($title)) {
461 $attrs['title'] = $title;
464 $this->out->element('a', $attrs, $name);
465 $this->out->elementEnd('span');
467 $this->out->element('span', 'device', $name);
470 $this->out->elementEnd('span');
475 * show link to notice this notice is a reply to
477 * If this notice is a reply, show a link to the notice it is replying to. The
478 * heavy lifting for figuring out replies happens at save time.
483 function showContext()
485 if ($this->notice->hasConversation()) {
486 $conv = Conversation::staticGet(
488 $this->notice->conversation
490 $convurl = $conv->uri;
491 if (!empty($convurl)) {
492 $this->out->text(' ');
496 'href' => $convurl.'#notice-'.$this->notice->id,
497 'class' => 'response'),
502 "Couldn't find Conversation ID %d to make 'in context'"
503 . "link for Notice ID %d",
504 $this->notice->conversation,
507 common_log(LOG_WARNING, $msg);
513 * show a link to the author of repeat
518 function showRepeat()
520 if (!empty($this->repeat)) {
522 $repeater = Profile::staticGet('id', $this->repeat->profile_id);
524 $attrs = array('href' => $repeater->profileurl,
527 if (!empty($repeater->fullname)) {
528 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
531 $this->out->elementStart('span', 'repeat vcard');
533 $this->out->raw(_('Repeated by'));
535 $this->out->elementStart('a', $attrs);
536 $this->out->element('span', 'fn nickname', $repeater->nickname);
537 $this->out->elementEnd('a');
539 $this->out->elementEnd('span');
544 * show a link to reply to the current notice
546 * Should either do the reply in the current notice form (if available), or
547 * link out to the notice-posting form. A little flakey, doesn't always work.
552 function showReplyLink()
554 if (common_logged_in()) {
555 $this->out->text(' ');
556 $reply_url = common_local_url('newnotice',
557 array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
558 $this->out->elementStart('a', array('href' => $reply_url,
559 'class' => 'notice_reply',
560 'title' => _('Reply to this notice')));
561 $this->out->text(_('Reply'));
562 $this->out->text(' ');
563 $this->out->element('span', 'notice_id', $this->notice->id);
564 $this->out->elementEnd('a');
569 * if the user is the author, let them delete the notice
574 function showDeleteLink()
576 $user = common_current_user();
578 $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
581 ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
582 $this->out->text(' ');
583 $deleteurl = common_local_url('deletenotice',
584 array('notice' => $todel->id));
585 $this->out->element('a', array('href' => $deleteurl,
586 'class' => 'notice_delete',
587 'title' => _('Delete this notice')), _('Delete'));
592 * show the form to repeat a notice
597 function showRepeatForm()
599 if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
600 $this->notice->scope == Notice::SITE_SCOPE) {
601 $user = common_current_user();
603 $user->id != $this->notice->profile_id) {
604 $this->out->text(' ');
605 $profile = $user->getProfile();
606 if ($profile->hasRepeated($this->notice->id)) {
607 $this->out->element('span', array('class' => 'repeated',
608 'title' => _('Notice repeated')),
611 $rf = new RepeatForm($this->out, $this->notice);
621 * Close the last elements in the notice list item
628 if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
629 $this->out->elementEnd('li');
630 Event::handle('EndCloseNoticeListItemElement', array($this));