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 if($this->notice->is_local == Notice::LOCAL_PUBLIC || $this->notice->is_local == Notice::LOCAL_NONPUBLIC){
384 $noticeurl = common_local_url('shownotice',
385 array('notice' => $this->notice->id));
387 $noticeurl = $this->notice->uri;
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('span', array('class' => 'geo',
445 $this->out->element('a', array('class' => 'geo',
450 $this->out->elementEnd('span');
453 function decimalDegreesToDMS($dec)
456 $vars = explode(".",$dec);
458 $tempma = "0.".$vars[1];
460 $tempma = $tempma * 3600;
461 $min = floor($tempma / 60);
462 $sec = $tempma - ($min*60);
464 return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
468 * Show the source of the notice
470 * Either the name (and link) of the API client that posted the notice,
471 * or one of other other channels.
476 function showNoticeSource()
478 if ($this->notice->source) {
479 $this->out->text(' ');
480 $this->out->elementStart('span', 'source');
481 $this->out->text(_('from'));
482 $source_name = _($this->notice->source);
483 $this->out->text(' ');
484 switch ($this->notice->source) {
491 $this->out->element('span', 'device', $source_name);
495 $name = $source_name;
498 if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
499 $ns = Notice_source::staticGet($this->notice->source);
505 $app = Oauth_application::staticGet('name', $this->notice->source);
508 $url = $app->source_url;
512 Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
514 if (!empty($name) && !empty($url)) {
515 $this->out->elementStart('span', 'device');
516 $this->out->element('a', array('href' => $url,
520 $this->out->elementEnd('span');
522 $this->out->element('span', 'device', $name);
526 $this->out->elementEnd('span');
531 * show link to notice this notice is a reply to
533 * If this notice is a reply, show a link to the notice it is replying to. The
534 * heavy lifting for figuring out replies happens at save time.
539 function showContext()
541 $hasConversation = false;
542 if( !empty($this->notice->conversation)
543 && $this->notice->conversation != $this->notice->id){
544 $hasConversation = true;
546 $conversation = Notice::conversationStream($this->notice->id, 1, 1);
547 if($conversation->N > 0){
548 $hasConversation = true;
551 if ($hasConversation){
552 $this->out->text(' ');
553 $convurl = common_local_url('conversation',
554 array('id' => $this->notice->conversation));
555 $this->out->element('a', array('href' => $convurl.'#notice-'.$this->notice->id,
556 'class' => 'response'),
562 * show a link to the author of repeat
567 function showRepeat()
569 if (!empty($this->repeat)) {
571 $repeater = Profile::staticGet('id', $this->repeat->profile_id);
573 $attrs = array('href' => $repeater->profileurl,
576 if (!empty($repeater->fullname)) {
577 $attrs['title'] = $repeater->fullname . ' (' . $repeater->nickname . ')';
580 $this->out->elementStart('span', 'repeat vcard');
582 $this->out->raw(_('Repeated by'));
584 $this->out->elementStart('a', $attrs);
585 $this->out->element('span', 'fn nickname', $repeater->nickname);
586 $this->out->elementEnd('a');
588 $this->out->elementEnd('span');
593 * show a link to reply to the current notice
595 * Should either do the reply in the current notice form (if available), or
596 * link out to the notice-posting form. A little flakey, doesn't always work.
601 function showReplyLink()
603 if (common_logged_in()) {
604 $this->out->text(' ');
605 $reply_url = common_local_url('newnotice',
606 array('replyto' => $this->profile->nickname, 'inreplyto' => $this->notice->id));
607 $this->out->elementStart('a', array('href' => $reply_url,
608 'class' => 'notice_reply',
609 'title' => _('Reply to this notice')));
610 $this->out->text(_('Reply'));
611 $this->out->text(' ');
612 $this->out->element('span', 'notice_id', $this->notice->id);
613 $this->out->elementEnd('a');
618 * if the user is the author, let them delete the notice
623 function showDeleteLink()
625 $user = common_current_user();
627 $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
630 ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
631 $this->out->text(' ');
632 $deleteurl = common_local_url('deletenotice',
633 array('notice' => $todel->id));
634 $this->out->element('a', array('href' => $deleteurl,
635 'class' => 'notice_delete',
636 'title' => _('Delete this notice')), _('Delete'));
641 * show the form to repeat a notice
646 function showRepeatForm()
648 $user = common_current_user();
649 if ($user && $user->id != $this->notice->profile_id) {
650 $this->out->text(' ');
651 $profile = $user->getProfile();
652 if ($profile->hasRepeated($this->notice->id)) {
653 $this->out->element('span', array('class' => 'repeated',
654 'title' => _('Notice repeated')),
657 $rf = new RepeatForm($this->out, $this->notice);
666 * Close the last elements in the notice list item
673 $this->out->elementEnd('li');