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) {
99 $item = $this->newListItem($this->notice);
103 $this->out->elementEnd('ol');
104 $this->out->elementEnd('div');
110 * returns a new list item for the current notice
112 * Recipe (factory?) method; overridden by sub-classes to give
113 * a different list item class.
115 * @param Notice $notice the current notice
117 * @return NoticeListItem a list item for displaying the notice
120 function newListItem($notice)
122 return new NoticeListItem($notice, $this->out);
127 * widget for displaying a single notice
129 * This widget has the core smarts for showing a single notice: what to display,
130 * where, and under which circumstances. Its key method is show(); this is a recipe
131 * that calls all the other show*() methods to build up a single notice. The
132 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
133 * author info (since that's implicit by the data in the page).
137 * @author Evan Prodromou <evan@status.net>
138 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
139 * @link http://status.net/
141 * @see ProfileNoticeListItem
144 class NoticeListItem extends Widget
146 /** The notice this item will show. */
150 /** The notice that was repeated. */
154 /** The profile of the author of the notice, extracted once for convenience. */
161 * Also initializes the profile attribute.
163 * @param Notice $notice The notice we'll display
166 function __construct($notice, $out=null)
168 parent::__construct($out);
169 if (!empty($notice->repeat_of)) {
170 $original = Notice::staticGet('id', $notice->repeat_of);
171 if (empty($original)) { // could have been deleted
172 $this->notice = $notice;
174 $this->notice = $original;
175 $this->repeat = $notice;
178 $this->notice = $notice;
180 $this->profile = $this->notice->getProfile();
184 * recipe function for displaying a single notice.
186 * This uses all the other methods to correctly display a notice. Override
187 * it or one of the others to fine-tune the output.
194 if (empty($this->notice)) {
195 common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
197 } else if (empty($this->profile)) {
198 common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
203 if (Event::handle('StartShowNoticeItem', array($this))) {
205 $this->showNoticeInfo();
206 $this->showNoticeOptions();
207 Event::handle('EndShowNoticeItem', array($this));
212 function showNotice()
214 $this->out->elementStart('div', 'entry-title');
216 $this->showContent();
217 $this->out->elementEnd('div');
220 function showNoticeInfo()
222 $this->out->elementStart('div', 'entry-content');
223 $this->showNoticeLink();
224 $this->showNoticeSource();
225 $this->showNoticeLocation();
226 $this->showContext();
228 $this->out->elementEnd('div');
231 function showNoticeOptions()
233 $user = common_current_user();
235 $this->out->elementStart('div', 'notice-options');
236 $this->showFaveForm();
237 $this->showReplyLink();
238 $this->showRepeatForm();
239 $this->showDeleteLink();
240 $this->out->elementEnd('div');
245 * start a single notice.
253 // TODO: add notice_type class e.g., notice_video, notice_image
254 $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
255 $this->out->elementStart('li', array('class' => 'hentry notice',
256 'id' => 'notice-' . $id));
260 * show the "favorite" form
265 function showFaveForm()
267 $user = common_current_user();
269 if ($user->hasFave($this->notice)) {
270 $disfavor = new DisfavorForm($this->out, $this->notice);
273 $favor = new FavorForm($this->out, $this->notice);
280 * show the author of a notice
282 * By default, this shows the avatar and (linked) nickname of the author.
287 function showAuthor()
289 $this->out->elementStart('span', 'vcard author');
290 $attrs = array('href' => $this->profile->profileurl,
292 if (!empty($this->profile->fullname)) {
293 $attrs['title'] = $this->profile->fullname . ' (' . $this->profile->nickname . ')';
295 $this->out->elementStart('a', $attrs);
297 $this->out->text(' ');
298 $this->showNickname();
299 $this->out->elementEnd('a');
300 $this->out->elementEnd('span');
304 * show the avatar of the notice's author
306 * This will use the default avatar if no avatar is assigned for the author.
307 * It makes a link to the author's profile.
312 function showAvatar()
314 if ('shownotice' === $this->out->trimmed('action')) {
315 $avatar_size = AVATAR_PROFILE_SIZE;
317 $avatar_size = AVATAR_STREAM_SIZE;
319 $avatar = $this->profile->getAvatar($avatar_size);
321 $this->out->element('img', array('src' => ($avatar) ?
322 $avatar->displayUrl() :
323 Avatar::defaultImage($avatar_size),
324 'class' => 'avatar photo',
325 'width' => $avatar_size,
326 'height' => $avatar_size,
328 ($this->profile->fullname) ?
329 $this->profile->fullname :
330 $this->profile->nickname));
334 * show the nickname of the author
336 * Links to the author's profile page
341 function showNickname()
343 $this->out->raw('<span class="nickname fn">' .
344 htmlspecialchars($this->profile->nickname) .
349 * show the content of the notice
351 * Shows the content of the notice. This is pre-rendered for efficiency
352 * at save time. Some very old notices might not be pre-rendered, so
353 * they're rendered on the spot.
358 function showContent()
360 // FIXME: URL, image, video, audio
361 $this->out->elementStart('p', array('class' => 'entry-content'));
362 if ($this->notice->rendered) {
363 $this->out->raw($this->notice->rendered);
365 // XXX: may be some uncooked notices in the DB,
366 // we cook them right now. This should probably disappear in future
367 // versions (>> 0.4.x)
368 $this->out->raw(common_render_content($this->notice->content, $this->notice));
370 $this->out->elementEnd('p');
374 * show the link to the main page for the notice
376 * Displays a link to the page for a notice, with "relative" time. Tries to
377 * get remote notice URLs correct, but doesn't always succeed.
382 function showNoticeLink()
384 $noticeurl = $this->notice->bestUrl();
386 // above should always return an URL
388 assert(!empty($noticeurl));
390 $this->out->elementStart('a', array('rel' => 'bookmark',
391 'class' => 'timestamp',
392 'href' => $noticeurl));
393 $dt = common_date_iso8601($this->notice->created);
394 $this->out->element('abbr', array('class' => 'published',
396 common_date_string($this->notice->created));
397 $this->out->elementEnd('a');
401 * show the notice location
403 * shows the notice location in the correct language.
405 * If an URL is available, makes a link. Otherwise, just a span.
410 function showNoticeLocation()
412 $id = $this->notice->id;
414 $location = $this->notice->getLocation();
416 if (empty($location)) {
420 $name = $location->getName();
422 $lat = $this->notice->lat;
423 $lon = $this->notice->lon;
424 $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
427 $latdms = $this->decimalDegreesToDMS(abs($lat));
428 $londms = $this->decimalDegreesToDMS(abs($lon));
429 // TRANS: Used in coordinates as abbreviation of north
431 // TRANS: Used in coordinates as abbreviation of south
433 // TRANS: Used in coordinates as abbreviation of east
435 // TRANS: Used in coordinates as abbreviation of west
438 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
439 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south)),
440 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
443 $url = $location->getUrl();
445 $this->out->text(' ');
446 $this->out->elementStart('span', array('class' => 'location'));
447 $this->out->text(_('at'));
448 $this->out->text(' ');
450 $this->out->element('abbr', array('class' => 'geo',
454 $xstr = new XMLStringer(false);
455 $xstr->elementStart('a', array('href' => $url,
456 'rel' => 'external'));
457 $xstr->element('abbr', array('class' => 'geo',
460 $xstr->elementEnd('a');
461 $this->out->raw($xstr->getString());
463 $this->out->elementEnd('span');
466 function decimalDegreesToDMS($dec)
469 $vars = explode(".",$dec);
471 $tempma = "0.".$vars[1];
473 $tempma = $tempma * 3600;
474 $min = floor($tempma / 60);
475 $sec = $tempma - ($min*60);
477 return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
481 * Show the source of the notice
483 * Either the name (and link) of the API client that posted the notice,
484 * or one of other other channels.
489 function showNoticeSource()
491 if ($this->notice->source) {
492 $this->out->text(' ');
493 $this->out->elementStart('span', 'source');
494 $this->out->text(_('from'));
495 $source_name = _($this->notice->source);
496 $this->out->text(' ');
497 switch ($this->notice->source) {
504 $this->out->element('span', 'device', $source_name);
508 $name = $source_name;
511 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
512 $ns = Notice_source::staticGet($this->notice->source);
518 $app = Oauth_application::staticGet('name', $this->notice->source);
521 $url = $app->source_url;
525 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
527 if (!empty($name) && !empty($url)) {
528 $this->out->elementStart('span', 'device');
529 $this->out->element('a', array('href' => $url,
533 $this->out->elementEnd('span');
535 $this->out->element('span', 'device', $name);
539 $this->out->elementEnd('span');
544 * show link to notice this notice is a reply to
546 * If this notice is a reply, show a link to the notice it is replying to. The
547 * heavy lifting for figuring out replies happens at save time.
552 function showContext()
554 if ($this->notice->hasConversation()) {
555 $conv = Conversation::staticGet(
557 $this->notice->conversation
559 $convurl = $conv->uri;
560 if (!empty($convurl)) {
561 $this->out->text(' ');
565 'href' => $convurl.'#notice-'.$this->notice->id,
566 'class' => 'response'),
571 "Couldn't find Conversation ID %d to make 'in context'"
572 . "link for Notice ID %d",
573 $this->notice->conversation,
576 common_log(LOG_WARNING, $msg);
582 * show a link to the author of repeat
587 function showRepeat()
589 if (!empty($this->repeat)) {
591 $repeater = Profile::staticGet('id', $this->repeat->profile_id);
593 $attrs = array('href' => $repeater->profileurl,
596 if (!empty($repeater->fullname)) {
597 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
600 $this->out->elementStart('span', 'repeat vcard');
602 $this->out->raw(_('Repeated by'));
604 $this->out->elementStart('a', $attrs);
605 $this->out->element('span', 'fn nickname', $repeater->nickname);
606 $this->out->elementEnd('a');
608 $this->out->elementEnd('span');
613 * show a link to reply to the current notice
615 * Should either do the reply in the current notice form (if available), or
616 * link out to the notice-posting form. A little flakey, doesn't always work.
621 function showReplyLink()
623 if (common_logged_in()) {
624 $this->out->text(' ');
625 $reply_url = common_local_url('newnotice',
626 array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
627 $this->out->elementStart('a', array('href' => $reply_url,
628 'class' => 'notice_reply',
629 'title' => _('Reply to this notice')));
630 $this->out->text(_('Reply'));
631 $this->out->text(' ');
632 $this->out->element('span', 'notice_id', $this->notice->id);
633 $this->out->elementEnd('a');
638 * if the user is the author, let them delete the notice
643 function showDeleteLink()
645 $user = common_current_user();
647 $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
650 ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
651 $this->out->text(' ');
652 $deleteurl = common_local_url('deletenotice',
653 array('notice' => $todel->id));
654 $this->out->element('a', array('href' => $deleteurl,
655 'class' => 'notice_delete',
656 'title' => _('Delete this notice')), _('Delete'));
661 * show the form to repeat a notice
666 function showRepeatForm()
668 $user = common_current_user();
669 if ($user && $user->id != $this->notice->profile_id) {
670 $this->out->text(' ');
671 $profile = $user->getProfile();
672 if ($profile->hasRepeated($this->notice->id)) {
673 $this->out->element('span', array('class' => 'repeated',
674 'title' => _('Notice repeated')),
677 $rf = new RepeatForm($this->out, $this->notice);
686 * Close the last elements in the notice list item
693 $this->out->elementEnd('li');