3 * StatusNet, the distributed open-source microblogging tool
5 * widget for displaying a list of notices
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @author Sarven Capadisli <csarven@status.net>
26 * @copyright 2008 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 require_once INSTALLDIR.'/lib/favorform.php';
36 require_once INSTALLDIR.'/lib/disfavorform.php';
37 require_once INSTALLDIR.'/lib/attachmentlist.php';
40 * widget for displaying a list of notices
42 * There are a number of actions that display a list of notices, in
43 * reverse chronological order. This widget abstracts out most of the
44 * code for UI for notice lists. It's overridden to hide some
45 * data for e.g. the profile page.
49 * @author Evan Prodromou <evan@status.net>
50 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
51 * @link http://status.net/
54 * @see ProfileNoticeList
57 class NoticeList extends Widget
59 /** the current stream of notices being displayed. */
66 * @param Notice $notice stream of notices from DB_DataObject
69 function __construct($notice, $out=null)
71 parent::__construct($out);
72 $this->notice = $notice;
76 * show the list of notices
78 * "Uses up" the stream by looping through it. So, probably can't
79 * be called twice on the same list.
81 * @return int count of notices listed.
86 $this->out->elementStart('div', array('id' =>'notices_primary'));
87 $this->out->element('h2', null, _('Notices'));
88 $this->out->elementStart('ol', array('class' => 'notices xoxo'));
92 while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
95 if ($cnt > NOTICES_PER_PAGE) {
100 $item = $this->newListItem($this->notice);
102 } catch (Exception $e) {
103 // we log exceptions and continue
104 common_log(LOG_ERR, $e->getMessage());
109 $this->out->elementEnd('ol');
110 $this->out->elementEnd('div');
116 * returns a new list item for the current notice
118 * Recipe (factory?) method; overridden by sub-classes to give
119 * a different list item class.
121 * @param Notice $notice the current notice
123 * @return NoticeListItem a list item for displaying the notice
126 function newListItem($notice)
128 return new NoticeListItem($notice, $this->out);
133 * widget for displaying a single notice
135 * This widget has the core smarts for showing a single notice: what to display,
136 * where, and under which circumstances. Its key method is show(); this is a recipe
137 * that calls all the other show*() methods to build up a single notice. The
138 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
139 * author info (since that's implicit by the data in the page).
143 * @author Evan Prodromou <evan@status.net>
144 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
145 * @link http://status.net/
147 * @see ProfileNoticeListItem
150 class NoticeListItem extends Widget
152 /** The notice this item will show. */
156 /** The notice that was repeated. */
160 /** The profile of the author of the notice, extracted once for convenience. */
167 * Also initializes the profile attribute.
169 * @param Notice $notice The notice we'll display
172 function __construct($notice, $out=null)
174 parent::__construct($out);
175 if (!empty($notice->repeat_of)) {
176 $original = Notice::staticGet('id', $notice->repeat_of);
177 if (empty($original)) { // could have been deleted
178 $this->notice = $notice;
180 $this->notice = $original;
181 $this->repeat = $notice;
184 $this->notice = $notice;
186 $this->profile = $this->notice->getProfile();
190 * recipe function for displaying a single notice.
192 * This uses all the other methods to correctly display a notice. Override
193 * it or one of the others to fine-tune the output.
200 if (empty($this->notice)) {
201 common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
203 } else if (empty($this->profile)) {
204 common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
209 if (Event::handle('StartShowNoticeItem', array($this))) {
211 $this->showNoticeInfo();
212 $this->showNoticeOptions();
213 Event::handle('EndShowNoticeItem', array($this));
218 function showNotice()
220 $this->out->elementStart('div', 'entry-title');
222 $this->showContent();
223 $this->out->elementEnd('div');
226 function showNoticeInfo()
228 $this->out->elementStart('div', 'entry-content');
229 $this->showNoticeLink();
230 $this->showNoticeSource();
231 $this->showNoticeLocation();
232 $this->showContext();
234 $this->out->elementEnd('div');
237 function showNoticeOptions()
239 if (Event::handle('StartShowNoticeOptions', array($this))) {
240 $user = common_current_user();
242 $this->out->elementStart('div', 'notice-options');
243 $this->showFaveForm();
244 $this->showReplyLink();
245 $this->showRepeatForm();
246 $this->showDeleteLink();
247 $this->out->elementEnd('div');
249 Event::handle('EndShowNoticeOptions', array($this));
254 * start a single notice.
262 // TODO: add notice_type class e.g., notice_video, notice_image
263 $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
264 $this->out->elementStart('li', array('class' => 'hentry notice',
265 'id' => 'notice-' . $id));
269 * show the "favorite" form
274 function showFaveForm()
276 if (Event::handle('StartShowFaveForm', array($this))) {
277 $user = common_current_user();
279 if ($user->hasFave($this->notice)) {
280 $disfavor = new DisfavorForm($this->out, $this->notice);
283 $favor = new FavorForm($this->out, $this->notice);
287 Event::handle('EndShowFaveForm', array($this));
292 * show the author of a notice
294 * By default, this shows the avatar and (linked) nickname of the author.
299 function showAuthor()
301 $this->out->elementStart('span', 'vcard author');
302 $attrs = array('href' => $this->profile->profileurl,
304 if (!empty($this->profile->fullname)) {
305 $attrs['title'] = $this->profile->fullname . ' (' . $this->profile->nickname . ')';
307 $this->out->elementStart('a', $attrs);
309 $this->out->text(' ');
310 $this->showNickname();
311 $this->out->elementEnd('a');
312 $this->out->elementEnd('span');
316 * show the avatar of the notice's author
318 * This will use the default avatar if no avatar is assigned for the author.
319 * It makes a link to the author's profile.
324 function showAvatar()
326 if ('shownotice' === $this->out->trimmed('action')) {
327 $avatar_size = AVATAR_PROFILE_SIZE;
329 $avatar_size = AVATAR_STREAM_SIZE;
331 $avatar = $this->profile->getAvatar($avatar_size);
333 $this->out->element('img', array('src' => ($avatar) ?
334 $avatar->displayUrl() :
335 Avatar::defaultImage($avatar_size),
336 'class' => 'avatar photo',
337 'width' => $avatar_size,
338 'height' => $avatar_size,
340 ($this->profile->fullname) ?
341 $this->profile->fullname :
342 $this->profile->nickname));
346 * show the nickname of the author
348 * Links to the author's profile page
353 function showNickname()
355 $this->out->raw('<span class="nickname fn">' .
356 htmlspecialchars($this->profile->nickname) .
361 * show the content of the notice
363 * Shows the content of the notice. This is pre-rendered for efficiency
364 * at save time. Some very old notices might not be pre-rendered, so
365 * they're rendered on the spot.
370 function showContent()
372 // FIXME: URL, image, video, audio
373 $this->out->elementStart('p', array('class' => 'entry-content'));
374 if ($this->notice->rendered) {
375 $this->out->raw($this->notice->rendered);
377 // XXX: may be some uncooked notices in the DB,
378 // we cook them right now. This should probably disappear in future
379 // versions (>> 0.4.x)
380 $this->out->raw(common_render_content($this->notice->content, $this->notice));
382 $this->out->elementEnd('p');
386 * show the link to the main page for the notice
388 * Displays a link to the page for a notice, with "relative" time. Tries to
389 * get remote notice URLs correct, but doesn't always succeed.
394 function showNoticeLink()
396 $noticeurl = $this->notice->bestUrl();
398 // above should always return an URL
400 assert(!empty($noticeurl));
402 $this->out->elementStart('a', array('rel' => 'bookmark',
403 'class' => 'timestamp',
404 'href' => $noticeurl));
405 $dt = common_date_iso8601($this->notice->created);
406 $this->out->element('abbr', array('class' => 'published',
408 common_date_string($this->notice->created));
409 $this->out->elementEnd('a');
413 * show the notice location
415 * shows the notice location in the correct language.
417 * If an URL is available, makes a link. Otherwise, just a span.
422 function showNoticeLocation()
424 $id = $this->notice->id;
426 $location = $this->notice->getLocation();
428 if (empty($location)) {
432 $name = $location->getName();
434 $lat = $this->notice->lat;
435 $lon = $this->notice->lon;
436 $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
439 $latdms = $this->decimalDegreesToDMS(abs($lat));
440 $londms = $this->decimalDegreesToDMS(abs($lon));
441 // TRANS: Used in coordinates as abbreviation of north
443 // TRANS: Used in coordinates as abbreviation of south
445 // TRANS: Used in coordinates as abbreviation of east
447 // TRANS: Used in coordinates as abbreviation of west
450 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
451 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
452 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
455 $url = $location->getUrl();
457 $this->out->text(' ');
458 $this->out->elementStart('span', array('class' => 'location'));
459 $this->out->text(_('at'));
460 $this->out->text(' ');
462 $this->out->element('abbr', array('class' => 'geo',
466 $xstr = new XMLStringer(false);
467 $xstr->elementStart('a', array('href' => $url,
468 'rel' => 'external'));
469 $xstr->element('abbr', array('class' => 'geo',
472 $xstr->elementEnd('a');
473 $this->out->raw($xstr->getString());
475 $this->out->elementEnd('span');
479 * @param number $dec decimal degrees
480 * @return array split into 'deg', 'min', and 'sec'
482 function decimalDegreesToDMS($dec)
485 $tempma = abs($dec) - abs($deg);
487 $tempma = $tempma * 3600;
488 $min = floor($tempma / 60);
489 $sec = $tempma - ($min*60);
491 return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
495 * Show the source of the notice
497 * Either the name (and link) of the API client that posted the notice,
498 * or one of other other channels.
503 function showNoticeSource()
505 $ns = $this->notice->getSource();
508 $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _('web')) : _($ns->name);
509 $this->out->text(' ');
510 $this->out->elementStart('span', 'source');
511 // FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
512 $this->out->text(_('from'));
513 $this->out->text(' ');
515 $name = $source_name;
519 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
520 $name = $source_name;
523 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
525 // if $ns->name and $ns->url are populated we have
526 // configured a source attr somewhere
527 if (!empty($name) && !empty($url)) {
529 $this->out->elementStart('span', 'device');
536 if (!empty($title)) {
537 $attrs['title'] = $title;
540 $this->out->element('a', $attrs, $name);
541 $this->out->elementEnd('span');
543 $this->out->element('span', 'device', $name);
546 $this->out->elementEnd('span');
551 * show link to notice this notice is a reply to
553 * If this notice is a reply, show a link to the notice it is replying to. The
554 * heavy lifting for figuring out replies happens at save time.
559 function showContext()
561 if ($this->notice->hasConversation()) {
562 $conv = Conversation::staticGet(
564 $this->notice->conversation
566 $convurl = $conv->uri;
567 if (!empty($convurl)) {
568 $this->out->text(' ');
572 'href' => $convurl.'#notice-'.$this->notice->id,
573 'class' => 'response'),
578 "Couldn't find Conversation ID %d to make 'in context'"
579 . "link for Notice ID %d",
580 $this->notice->conversation,
583 common_log(LOG_WARNING, $msg);
589 * show a link to the author of repeat
594 function showRepeat()
596 if (!empty($this->repeat)) {
598 $repeater = Profile::staticGet('id', $this->repeat->profile_id);
600 $attrs = array('href' => $repeater->profileurl,
603 if (!empty($repeater->fullname)) {
604 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
607 $this->out->elementStart('span', 'repeat vcard');
609 $this->out->raw(_('Repeated by'));
611 $this->out->elementStart('a', $attrs);
612 $this->out->element('span', 'fn nickname', $repeater->nickname);
613 $this->out->elementEnd('a');
615 $this->out->elementEnd('span');
620 * show a link to reply to the current notice
622 * Should either do the reply in the current notice form (if available), or
623 * link out to the notice-posting form. A little flakey, doesn't always work.
628 function showReplyLink()
630 if (common_logged_in()) {
631 $this->out->text(' ');
632 $reply_url = common_local_url('newnotice',
633 array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
634 $this->out->elementStart('a', array('href' => $reply_url,
635 'class' => 'notice_reply',
636 'title' => _('Reply to this notice')));
637 $this->out->text(_('Reply'));
638 $this->out->text(' ');
639 $this->out->element('span', 'notice_id', $this->notice->id);
640 $this->out->elementEnd('a');
645 * if the user is the author, let them delete the notice
650 function showDeleteLink()
652 $user = common_current_user();
654 $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
657 ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
658 $this->out->text(' ');
659 $deleteurl = common_local_url('deletenotice',
660 array('notice' => $todel->id));
661 $this->out->element('a', array('href' => $deleteurl,
662 'class' => 'notice_delete',
663 'title' => _('Delete this notice')), _('Delete'));
668 * show the form to repeat a notice
673 function showRepeatForm()
675 $user = common_current_user();
676 if ($user && $user->id != $this->notice->profile_id) {
677 $this->out->text(' ');
678 $profile = $user->getProfile();
679 if ($profile->hasRepeated($this->notice->id)) {
680 $this->out->element('span', array('class' => 'repeated',
681 'title' => _('Notice repeated')),
684 $rf = new RepeatForm($this->out, $this->notice);
693 * Close the last elements in the notice list item
700 $this->out->elementEnd('li');