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('src' => common_path('js/facebookapp.js') . '?ts=' . $ts));
129 * Start an Facebook ready HTML document
131 * For Facebook we don't want to actually output any headers,
134 * If $type isn't specified, will attempt to do content negotiation.
136 * @param string $type MIME type to use; default is to do negotation.
141 function startHTML($type=null)
143 $this->elementStart('div', array('class' => 'facebook-page'));
147 * Ends a Facebook ready HTML document
153 $this->elementEnd('div');
160 * MAY overload if no notice form needed... or direct message box????
164 function showNoticeForm()
166 // don't do it for most of the Facebook pages
171 $this->elementStart('div', 'wrap');
175 $this->elementEnd('div');
182 function showHead($error, $success)
184 $this->showStylesheets();
185 $this->showScripts();
188 $this->element("h1", null, $error);
192 $this->element("h1", null, $success);
195 $this->elementStart('fb:if-section-not-added', array('section' => 'profile'));
196 $this->elementStart('span', array('id' => 'add_to_profile'));
197 $this->element('fb:add-section-button', array('section' => 'profile'));
198 $this->elementEnd('span');
199 $this->elementEnd('fb:if-section-not-added');
204 // Make this into a widget later
205 function showLocalNav()
208 $this->elementStart('ul', array('class' => 'nav'));
210 $this->elementStart('li', array('class' =>
211 ($this->action == 'facebookhome') ? 'current' : 'facebook_home'));
213 array('href' => 'index.php', 'title' => _('Home')), _('Home'));
214 $this->elementEnd('li');
216 $this->elementStart('li',
218 ($this->action == 'facebookinvite') ? 'current' : 'facebook_invite'));
220 array('href' => 'invite.php', 'title' => _('Invite')), _('Invite'));
221 $this->elementEnd('li');
223 $this->elementStart('li',
225 ($this->action == 'facebooksettings') ? 'current' : 'facebook_settings'));
227 array('href' => 'settings.php',
228 'title' => _('Settings')), _('Settings'));
229 $this->elementEnd('li');
231 $this->elementEnd('ul');
236 * Show primary navigation.
240 function showPrimaryNav()
242 // we don't want to show anything for this
246 * Show header of the page.
248 * Calls template methods
252 function showHeader()
254 $this->elementStart('div', array('id' => 'header'));
256 $this->showNoticeForm();
257 $this->showPrimaryNav();
258 $this->elementEnd('div');
262 * Show page, a template method.
266 function showPage($error = null, $success = null)
269 $this->showHead($error, $success);
275 function showInstructions()
278 $this->elementStart('div', array('class' => 'facebook_guide'));
280 $this->elementStart('dl', array('class' => 'system_notice'));
281 $this->element('dt', null, 'Page Notice');
283 $loginmsg_part1 = _('To use the %s Facebook Application you need to login ' .
284 'with your username and password. Don\'t have a username yet? ');
286 $loginmsg_part2 = _(' a new account.');
288 $this->elementStart('dd');
289 $this->elementStart('p');
290 $this->text(sprintf($loginmsg_part1, common_config('site', 'name')));
292 array('href' => common_local_url('register')), _('Register'));
293 $this->text($loginmsg_part2);
294 $this->elementEnd('dd');
295 $this->elementEnd('dl');
297 $this->elementEnd('div');
302 function showLoginForm($msg = null)
305 $this->elementStart('div', array('class' => 'content'));
306 $this->element('h1', null, _('Login'));
309 $this->element('fb:error', array('message' => $msg));
312 $this->showInstructions();
314 $this->elementStart('div', array('id' => 'content_inner'));
316 $this->elementStart('form', array('method' => 'post',
317 'class' => 'form_settings',
319 'action' => 'index.php'));
321 $this->elementStart('fieldset');
323 $this->elementStart('ul', array('class' => 'form_datas'));
324 $this->elementStart('li');
325 $this->input('nickname', _('Nickname'));
326 $this->elementEnd('li');
327 $this->elementStart('li');
328 $this->password('password', _('Password'));
329 $this->elementEnd('li');
330 $this->elementEnd('ul');
332 $this->submit('submit', _('Login'));
333 $this->elementEnd('form');
335 $this->elementStart('p');
336 $this->element('a', array('href' => common_local_url('recoverpassword')),
337 _('Lost or forgotten password?'));
338 $this->elementEnd('p');
340 $this->elementEnd('div');
345 function updateProfileBox($notice)
348 // Need to include inline CSS for styling the Profile box
351 .entry-title .vcard .photo {
355 .entry-title .vcard .nickname {
359 .entry-title p.entry-content {
364 div.entry-content dl,
365 div.entry-content dt,
366 div.entry-content dd {
370 div.entry-content dt,
371 div.entry-content dd {
375 div.entry-content dl.timestamp dt {
378 div.entry-content dd a {
379 display:inline-block;
383 $this->xw->openMemory();
385 $item = new FacebookNoticeListItem($notice, $this);
388 $fbml = "<fb:wide>$style " . $this->xw->outputMemory(false) . "</fb:wide>";
389 $fbml .= "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
391 $fbml_main = "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
393 $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main);
395 $this->xw->openURI('php://output');
400 * Generate pagination links
402 * @param boolean $have_before is there something before?
403 * @param boolean $have_after is there something after?
404 * @param integer $page current page
405 * @param string $action current action
406 * @param array $args rest of query arguments
410 function pagination($have_before, $have_after, $page, $action, $args=null)
412 // Does a little before-after block for next/prev page
413 if ($have_before || $have_after) {
414 $this->elementStart('div', array('class' => 'pagination'));
415 $this->elementStart('dl', null);
416 $this->element('dt', null, _('Pagination'));
417 $this->elementStart('dd', null);
418 $this->elementStart('ul', array('class' => 'nav'));
421 $pargs = array('page' => $page-1);
422 $newargs = $args ? array_merge($args, $pargs) : $pargs;
423 $this->elementStart('li', array('class' => 'nav_prev'));
424 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
426 $this->elementEnd('li');
429 $pargs = array('page' => $page+1);
430 $newargs = $args ? array_merge($args, $pargs) : $pargs;
431 $this->elementStart('li', array('class' => 'nav_next'));
432 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
434 $this->elementEnd('li');
436 if ($have_before || $have_after) {
437 $this->elementEnd('ul');
438 $this->elementEnd('dd');
439 $this->elementEnd('dl');
440 $this->elementEnd('div');
447 class FacebookNoticeForm extends NoticeForm
450 var $post_action = null;
455 * @param HTMLOutputter $out output channel
456 * @param string $action action to return to, if any
457 * @param string $content content to pre-fill
460 function __construct($out=null, $action=null, $content=null,
461 $post_action=null, $user=null)
463 parent::__construct($out, $action, $content, $user);
464 $this->post_action = $post_action;
470 * @return string URL of the action
475 return $this->post_action;
480 class FacebookNoticeList extends NoticeList
483 * show the list of notices
485 * "Uses up" the stream by looping through it. So, probably can't
486 * be called twice on the same list.
488 * @return int count of notices listed.
493 $this->out->elementStart('div', array('id' =>'notices_primary'));
494 $this->out->element('h2', null, _('Notices'));
495 $this->out->elementStart('ul', array('class' => 'notices'));
499 while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
502 if ($cnt > NOTICES_PER_PAGE) {
506 $item = $this->newListItem($this->notice);
510 $this->out->elementEnd('ul');
511 $this->out->elementEnd('div');
517 * returns a new list item for the current notice
519 * Overridden to return a Facebook specific list item.
521 * @param Notice $notice the current notice
523 * @return FacebookNoticeListItem a list item for displaying the notice
524 * formatted for display in the Facebook App.
527 function newListItem($notice)
529 return new FacebookNoticeListItem($notice, $this);
534 class FacebookNoticeListItem extends NoticeListItem
537 * recipe function for displaying a single notice in the Facebook App.
539 * Overridden to strip out some of the controls that we don't
540 * want to be available.
549 $this->out->elementStart('div', 'entry-title');
551 $this->showContent();
552 $this->out->elementEnd('div');
554 $this->out->elementStart('div', 'entry-content');
555 $this->showNoticeLink();
556 $this->showNoticeSource();
557 $this->showReplyTo();
558 $this->out->elementEnd('div');
563 function showNoticeLink()
565 $noticeurl = common_local_url('shownotice',
566 array('notice' => $this->notice->id));
567 // XXX: we need to figure this out better. Is this right?
568 if (strcmp($this->notice->uri, $noticeurl) != 0 &&
569 preg_match('/^http/', $this->notice->uri)) {
570 $noticeurl = $this->notice->uri;
573 $this->out->elementStart('dl', 'timestamp');
574 $this->out->element('dt', null, _('Published'));
575 $this->out->elementStart('dd', null);
576 $this->out->elementStart('a', array('rel' => 'bookmark',
577 'href' => $noticeurl));
578 $dt = common_date_iso8601($this->notice->created);
579 $this->out->element('abbr', array('class' => 'published',
581 common_date_string($this->notice->created));
582 $this->out->elementEnd('a');
583 $this->out->elementEnd('dd');
584 $this->out->elementEnd('dl');