3 * Laconica, the distributed open-source microblogging tool
5 * Low-level generator for HTML
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 Zach Copley <zach@controlyourself.ca>
25 * @copyright 2008 Control Yourself, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://laconi.ca/
30 if (!defined('LACONICA'))
35 require_once INSTALLDIR.'/lib/facebookutil.php';
36 require_once INSTALLDIR.'/lib/noticeform.php';
39 class FacebookAction extends Action
52 * Just wraps the HTMLOutputter constructor.
54 * @param string $output URI to output to, default = stdout
55 * @param boolean $indent Whether to indent output, default true
57 * @see XMLOutputter::__construct
58 * @see HTMLOutputter::__construct
60 function __construct($output='php://output', $indent=true, $facebook=null, $flink=null)
62 parent::__construct($output, $indent);
64 $this->facebook = $facebook;
65 $this->flink = $flink;
68 $this->fbuid = $flink->foreign_id;
69 $this->user = $flink->getUser();
72 $this->args = array();
75 function prepare($argarray)
77 parent::prepare($argarray);
79 $this->facebook = getFacebook();
80 $this->fbuid = $this->facebook->require_login();
82 $this->action = $this->trimmed('action');
84 $app_props = $this->facebook->api_client->Admin_getAppProperties(
85 array('canvas_name', 'application_name'));
87 $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
88 $this->app_name = $app_props['application_name'];
90 $this->flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE);
96 function showStylesheets()
98 // Add a timestamp to the file so Facebook cache wont ignore our changes
99 $ts = filemtime(INSTALLDIR.'/theme/base/css/display.css');
101 $this->element('link', array('rel' => 'stylesheet',
102 'type' => 'text/css',
103 'href' => theme_path('css/display.css', 'base') . '?ts=' . $ts));
105 $theme = common_config('site', 'theme');
107 $ts = filemtime(INSTALLDIR. '/theme/' . $theme .'/css/display.css');
109 $this->element('link', array('rel' => 'stylesheet',
110 'type' => 'text/css',
111 'href' => theme_path('css/display.css', null) . '?ts=' . $ts));
113 $ts = filemtime(INSTALLDIR.'/theme/base/css/facebookapp.css');
115 $this->element('link', array('rel' => 'stylesheet',
116 'type' => 'text/css',
117 'href' => theme_path('css/facebookapp.css', 'base') . '?ts=' . $ts));
120 function showScripts()
122 // Add a timestamp to the file so Facebook cache wont ignore our changes
123 $ts = filemtime(INSTALLDIR.'/js/facebookapp.js');
125 $this->element('script', array('type' => 'text/javascript',
126 'src' => common_path('js/facebookapp.js') . '?ts=' . $ts));
130 * Start an Facebook ready HTML document
132 * For Facebook we don't want to actually output any headers,
135 * If $type isn't specified, will attempt to do content negotiation.
137 * @param string $type MIME type to use; default is to do negotation.
142 function startHTML($type=null)
144 $this->elementStart('div', array('class' => 'facebook-page'));
148 * Ends a Facebook ready HTML document
154 $this->elementEnd('div');
161 * MAY overload if no notice form needed... or direct message box????
165 function showNoticeForm()
167 // don't do it for most of the Facebook pages
172 $this->elementStart('div', 'wrap');
176 $this->elementEnd('div');
183 function showHead($error, $success)
185 $this->showStylesheets();
186 $this->showScripts();
189 $this->element("h1", null, $error);
193 $this->element("h1", null, $success);
196 $this->elementStart('fb:if-section-not-added', array('section' => 'profile'));
197 $this->elementStart('span', array('id' => 'add_to_profile'));
198 $this->element('fb:add-section-button', array('section' => 'profile'));
199 $this->elementEnd('span');
200 $this->elementEnd('fb:if-section-not-added');
205 // Make this into a widget later
206 function showLocalNav()
209 $this->elementStart('ul', array('class' => 'nav'));
211 $this->elementStart('li', array('class' =>
212 ($this->action == 'facebookhome') ? 'current' : 'facebook_home'));
214 array('href' => 'index.php', 'title' => _('Home')), _('Home'));
215 $this->elementEnd('li');
217 $this->elementStart('li',
219 ($this->action == 'facebookinvite') ? 'current' : 'facebook_invite'));
221 array('href' => 'invite.php', 'title' => _('Invite')), _('Invite'));
222 $this->elementEnd('li');
224 $this->elementStart('li',
226 ($this->action == 'facebooksettings') ? 'current' : 'facebook_settings'));
228 array('href' => 'settings.php',
229 'title' => _('Settings')), _('Settings'));
230 $this->elementEnd('li');
232 $this->elementEnd('ul');
237 * Show primary navigation.
241 function showPrimaryNav()
243 // we don't want to show anything for this
247 * Show header of the page.
249 * Calls template methods
253 function showHeader()
255 $this->elementStart('div', array('id' => 'header'));
257 $this->showNoticeForm();
258 $this->showPrimaryNav();
259 $this->elementEnd('div');
263 * Show page, a template method.
267 function showPage($error = null, $success = null)
270 $this->showHead($error, $success);
276 function showInstructions()
279 $this->elementStart('div', array('class' => 'facebook_guide'));
281 $this->elementStart('dl', array('class' => 'system_notice'));
282 $this->element('dt', null, 'Page Notice');
284 $loginmsg_part1 = _('To use the %s Facebook Application you need to login ' .
285 'with your username and password. Don\'t have a username yet? ');
287 $loginmsg_part2 = _(' a new account.');
289 $this->elementStart('dd');
290 $this->elementStart('p');
291 $this->text(sprintf($loginmsg_part1, common_config('site', 'name')));
293 array('href' => common_local_url('register')), _('Register'));
294 $this->text($loginmsg_part2);
295 $this->elementEnd('dd');
296 $this->elementEnd('dl');
298 $this->elementEnd('div');
303 function showLoginForm($msg = null)
306 $this->elementStart('div', array('class' => 'content'));
307 $this->element('h1', null, _('Login'));
310 $this->element('fb:error', array('message' => $msg));
313 $this->showInstructions();
315 $this->elementStart('div', array('id' => 'content_inner'));
317 $this->elementStart('form', array('method' => 'post',
318 'class' => 'form_settings',
320 'action' => 'index.php'));
322 $this->elementStart('fieldset');
324 $this->elementStart('ul', array('class' => 'form_datas'));
325 $this->elementStart('li');
326 $this->input('nickname', _('Nickname'));
327 $this->elementEnd('li');
328 $this->elementStart('li');
329 $this->password('password', _('Password'));
330 $this->elementEnd('li');
331 $this->elementEnd('ul');
333 $this->submit('submit', _('Login'));
334 $this->elementEnd('form');
336 $this->elementStart('p');
337 $this->element('a', array('href' => common_local_url('recoverpassword')),
338 _('Lost or forgotten password?'));
339 $this->elementEnd('p');
341 $this->elementEnd('div');
346 function updateProfileBox($notice)
349 // Need to include inline CSS for styling the Profile box
352 .entry-title .vcard .photo {
356 .entry-title .vcard .nickname {
360 .entry-title p.entry-content {
365 div.entry-content dl,
366 div.entry-content dt,
367 div.entry-content dd {
371 div.entry-content dt,
372 div.entry-content dd {
376 div.entry-content dl.timestamp dt {
379 div.entry-content dd a {
380 display:inline-block;
384 $this->xw->openMemory();
386 $item = new FacebookNoticeListItem($notice, $this);
389 $fbml = "<fb:wide>$style " . $this->xw->outputMemory(false) . "</fb:wide>";
390 $fbml .= "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
392 $fbml_main = "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
394 $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main);
396 $this->xw->openURI('php://output');
401 * Generate pagination links
403 * @param boolean $have_before is there something before?
404 * @param boolean $have_after is there something after?
405 * @param integer $page current page
406 * @param string $action current action
407 * @param array $args rest of query arguments
411 function pagination($have_before, $have_after, $page, $action, $args=null)
413 // Does a little before-after block for next/prev page
414 if ($have_before || $have_after) {
415 $this->elementStart('div', array('class' => 'pagination'));
416 $this->elementStart('dl', null);
417 $this->element('dt', null, _('Pagination'));
418 $this->elementStart('dd', null);
419 $this->elementStart('ul', array('class' => 'nav'));
422 $pargs = array('page' => $page-1);
423 $newargs = $args ? array_merge($args, $pargs) : $pargs;
424 $this->elementStart('li', array('class' => 'nav_prev'));
425 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
427 $this->elementEnd('li');
430 $pargs = array('page' => $page+1);
431 $newargs = $args ? array_merge($args, $pargs) : $pargs;
432 $this->elementStart('li', array('class' => 'nav_next'));
433 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
435 $this->elementEnd('li');
437 if ($have_before || $have_after) {
438 $this->elementEnd('ul');
439 $this->elementEnd('dd');
440 $this->elementEnd('dl');
441 $this->elementEnd('div');
448 class FacebookNoticeForm extends NoticeForm
451 var $post_action = null;
456 * @param HTMLOutputter $out output channel
457 * @param string $action action to return to, if any
458 * @param string $content content to pre-fill
461 function __construct($out=null, $action=null, $content=null,
462 $post_action=null, $user=null)
464 parent::__construct($out, $action, $content, $user);
465 $this->post_action = $post_action;
471 * @return string URL of the action
476 return $this->post_action;
481 class FacebookNoticeList extends NoticeList
484 * show the list of notices
486 * "Uses up" the stream by looping through it. So, probably can't
487 * be called twice on the same list.
489 * @return int count of notices listed.
494 $this->out->elementStart('div', array('id' =>'notices_primary'));
495 $this->out->element('h2', null, _('Notices'));
496 $this->out->elementStart('ul', array('class' => 'notices'));
500 while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
503 if ($cnt > NOTICES_PER_PAGE) {
507 $item = $this->newListItem($this->notice);
511 $this->out->elementEnd('ul');
512 $this->out->elementEnd('div');
518 * returns a new list item for the current notice
520 * Overridden to return a Facebook specific list item.
522 * @param Notice $notice the current notice
524 * @return FacebookNoticeListItem a list item for displaying the notice
525 * formatted for display in the Facebook App.
528 function newListItem($notice)
530 return new FacebookNoticeListItem($notice, $this);
535 class FacebookNoticeListItem extends NoticeListItem
538 * recipe function for displaying a single notice in the Facebook App.
540 * Overridden to strip out some of the controls that we don't
541 * want to be available.
550 $this->out->elementStart('div', 'entry-title');
552 $this->showContent();
553 $this->out->elementEnd('div');
555 $this->out->elementStart('div', 'entry-content');
556 $this->showNoticeLink();
557 $this->showNoticeSource();
558 $this->showReplyTo();
559 $this->out->elementEnd('div');
564 function showNoticeLink()
566 $noticeurl = common_local_url('shownotice',
567 array('notice' => $this->notice->id));
568 // XXX: we need to figure this out better. Is this right?
569 if (strcmp($this->notice->uri, $noticeurl) != 0 &&
570 preg_match('/^http/', $this->notice->uri)) {
571 $noticeurl = $this->notice->uri;
574 $this->out->elementStart('dl', 'timestamp');
575 $this->out->element('dt', null, _('Published'));
576 $this->out->elementStart('dd', null);
577 $this->out->elementStart('a', array('rel' => 'bookmark',
578 'href' => $noticeurl));
579 $dt = common_date_iso8601($this->notice->created);
580 $this->out->element('abbr', array('class' => 'published',
582 common_date_string($this->notice->created));
583 $this->out->elementEnd('a');
584 $this->out->elementEnd('dd');
585 $this->out->elementEnd('dl');