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->element('span', array('class' => 'nickname fn'),
344 $this->profile->nickname);
348 * show the content of the notice
350 * Shows the content of the notice. This is pre-rendered for efficiency
351 * at save time. Some very old notices might not be pre-rendered, so
352 * they're rendered on the spot.
357 function showContent()
359 // FIXME: URL, image, video, audio
360 $this->out->elementStart('p', array('class' => 'entry-content'));
361 if ($this->notice->rendered) {
362 $this->out->raw($this->notice->rendered);
364 // XXX: may be some uncooked notices in the DB,
365 // we cook them right now. This should probably disappear in future
366 // versions (>> 0.4.x)
367 $this->out->raw(common_render_content($this->notice->content, $this->notice));
369 $this->out->elementEnd('p');
373 * show the link to the main page for the notice
375 * Displays a link to the page for a notice, with "relative" time. Tries to
376 * get remote notice URLs correct, but doesn't always succeed.
381 function showNoticeLink()
383 $noticeurl = $this->notice->bestUrl();
385 // above should always return an URL
387 assert(!empty($noticeurl));
389 $this->out->elementStart('a', array('rel' => 'bookmark',
390 'class' => 'timestamp',
391 'href' => $noticeurl));
392 $dt = common_date_iso8601($this->notice->created);
393 $this->out->element('abbr', array('class' => 'published',
395 common_date_string($this->notice->created));
396 $this->out->elementEnd('a');
400 * show the notice location
402 * shows the notice location in the correct language.
404 * If an URL is available, makes a link. Otherwise, just a span.
409 function showNoticeLocation()
411 $id = $this->notice->id;
413 $location = $this->notice->getLocation();
415 if (empty($location)) {
419 $name = $location->getName();
421 $lat = $this->notice->lat;
422 $lon = $this->notice->lon;
423 $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
426 $latdms = $this->decimalDegreesToDMS(abs($lat));
427 $londms = $this->decimalDegreesToDMS(abs($lon));
429 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
430 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0?_('N'):_('S')),
431 $londms['deg'],$londms['min'], $londms['sec'],($lon>0?_('E'):_('W')));
434 $url = $location->getUrl();
436 $this->out->text(' ');
437 $this->out->elementStart('span', array('class' => 'location'));
438 $this->out->text(_('at'));
439 $this->out->text(' ');
441 $this->out->element('abbr', array('class' => 'geo',
445 $this->out->elementStart('a', array('href' => $url));
446 $this->out->element('abbr', array('class' => 'geo',
449 $this->out->elementEnd('a');
451 $this->out->elementEnd('span');
454 function decimalDegreesToDMS($dec)
457 $vars = explode(".",$dec);
459 $tempma = "0.".$vars[1];
461 $tempma = $tempma * 3600;
462 $min = floor($tempma / 60);
463 $sec = $tempma - ($min*60);
465 return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
469 * Show the source of the notice
471 * Either the name (and link) of the API client that posted the notice,
472 * or one of other other channels.
477 function showNoticeSource()
479 if ($this->notice->source) {
480 $this->out->text(' ');
481 $this->out->elementStart('span', 'source');
482 $this->out->text(_('from'));
483 $source_name = _($this->notice->source);
484 $this->out->text(' ');
485 switch ($this->notice->source) {
492 $this->out->element('span', 'device', $source_name);
496 $name = $source_name;
499 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
500 $ns = Notice_source::staticGet($this->notice->source);
506 $app = Oauth_application::staticGet('name', $this->notice->source);
509 $url = $app->source_url;
513 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
515 if (!empty($name) && !empty($url)) {
516 $this->out->elementStart('span', 'device');
517 $this->out->element('a', array('href' => $url,
521 $this->out->elementEnd('span');
523 $this->out->element('span', 'device', $name);
527 $this->out->elementEnd('span');
532 * show link to notice this notice is a reply to
534 * If this notice is a reply, show a link to the notice it is replying to. The
535 * heavy lifting for figuring out replies happens at save time.
540 function showContext()
542 $hasConversation = false;
543 if (!empty($this->notice->conversation)) {
544 $conversation = Notice::conversationStream(
545 $this->notice->conversation,
549 if ($conversation->N > 0) {
550 $hasConversation = true;
553 if ($hasConversation) {
554 $conv = Conversation::staticGet(
556 $this->notice->conversation
558 $convurl = $conv->uri;
559 if (!empty($convurl)) {
560 $this->out->text(' ');
564 'href' => $convurl.'#notice-'.$this->notice->id,
565 'class' => 'response'),
570 "Couldn't find Conversation ID %d to make 'in context'"
571 . "link for Notice ID %d",
572 $this->notice->conversation,
575 common_log(LOG_WARNING, $msg);
581 * show a link to the author of repeat
586 function showRepeat()
588 if (!empty($this->repeat)) {
590 $repeater = Profile::staticGet('id', $this->repeat->profile_id);
592 $attrs = array('href' => $repeater->profileurl,
595 if (!empty($repeater->fullname)) {
596 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
599 $this->out->elementStart('span', 'repeat vcard');
601 $this->out->raw(_('Repeated by'));
603 $this->out->elementStart('a', $attrs);
604 $this->out->element('span', 'fn nickname', $repeater->nickname);
605 $this->out->elementEnd('a');
607 $this->out->elementEnd('span');
612 * show a link to reply to the current notice
614 * Should either do the reply in the current notice form (if available), or
615 * link out to the notice-posting form. A little flakey, doesn't always work.
620 function showReplyLink()
622 if (common_logged_in()) {
623 $this->out->text(' ');
624 $reply_url = common_local_url('newnotice',
625 array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
626 $this->out->elementStart('a', array('href' => $reply_url,
627 'class' => 'notice_reply',
628 'title' => _('Reply to this notice')));
629 $this->out->text(_('Reply'));
630 $this->out->text(' ');
631 $this->out->element('span', 'notice_id', $this->notice->id);
632 $this->out->elementEnd('a');
637 * if the user is the author, let them delete the notice
642 function showDeleteLink()
644 $user = common_current_user();
646 $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
649 ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
650 $this->out->text(' ');
651 $deleteurl = common_local_url('deletenotice',
652 array('notice' => $todel->id));
653 $this->out->element('a', array('href' => $deleteurl,
654 'class' => 'notice_delete',
655 'title' => _('Delete this notice')), _('Delete'));
660 * show the form to repeat a notice
665 function showRepeatForm()
667 $user = common_current_user();
668 if ($user && $user->id != $this->notice->profile_id) {
669 $this->out->text(' ');
670 $profile = $user->getProfile();
671 if ($profile->hasRepeated($this->notice->id)) {
672 $this->out->element('span', array('class' => 'repeated',
673 'title' => _('Notice repeated')),
676 $rf = new RepeatForm($this->out, $this->notice);
685 * Close the last elements in the notice list item
692 $this->out->elementEnd('li');