3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET')) { exit(1); }
22 require_once INSTALLDIR.'/lib/facebookaction.php';
24 class FacebookhomeAction extends FacebookAction
29 function prepare($argarray)
31 parent::prepare($argarray);
33 $this->page = $this->trimmed('page');
42 function handle($args)
44 parent::handle($args);
46 // If the user has opted not to initially allow the app to have
47 // Facebook status update permission, store that preference. Only
48 // promt the user the first time she uses the app
49 if ($this->arg('skip') || $args['fb_sig_request_method'] == 'GET') {
50 $this->facebook->api_client->data_setUserPreference(
51 FACEBOOK_PROMPTED_UPDATE_PREF, 'true');
56 $this->user = $this->flink->getUser();
58 // If this is the first time the user has started the app
59 // prompt for Facebook status update permission
60 if (!$this->facebook->api_client->users_hasAppPermission('publish_stream')) {
62 if ($this->facebook->api_client->data_getUserPreference(
63 FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
64 $this->getUpdatePermission();
69 // Make sure the user's profile box has the lastest notice
70 $notice = $this->user->getCurrentNotice();
72 $this->updateProfileBox($notice);
75 if ($this->arg('status_submit') == 'Send') {
76 $this->saveNewNotice();
79 // User is authenticated and has already been prompted once for
80 // Facebook status update permission? Then show the main page
86 // User hasn't authenticated yet, prompt for creds
95 $this->showStylesheets();
97 $nickname = common_canonical_nickname($this->trimmed('nickname'));
98 $password = $this->arg('password');
104 if (common_check_user($nickname, $password)) {
106 $user = User::staticGet('nickname', $nickname);
109 $this->showLoginForm(_("Server error - couldn't get user!"));
112 $flink = DB_DataObject::factory('foreign_link');
113 $flink->user_id = $user->id;
114 $flink->foreign_id = $this->fbuid;
115 $flink->service = FACEBOOK_SERVICE;
116 $flink->created = common_sql_now();
117 $flink->set_flags(true, false, false, false);
119 $flink_id = $flink->insert();
121 // XXX: Do some error handling here
123 $this->setDefaults();
125 $this->getUpdatePermission();
129 $msg = _('Incorrect username or password.');
133 $this->showLoginForm($msg);
138 function setDefaults()
140 $this->facebook->api_client->data_setUserPreference(
141 FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
144 function showNoticeForm()
146 $post_action = "$this->app_uri/index.php";
148 $notice_form = new FacebookNoticeForm($this, $post_action, null,
149 $post_action, $this->user);
150 $notice_form->show();
155 if ($this->page > 1) {
156 return sprintf(_("%s and friends, page %d"), $this->user->nickname, $this->page);
158 return sprintf(_("%s and friends"), $this->user->nickname);
162 function showContent()
164 $notice = $this->user->noticeInbox(($this->page-1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
166 $nl = new NoticeList($notice, $this);
170 $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
171 $this->page, 'index.php', array('nickname' => $this->user->nickname));
174 function showNoticeList($notice)
177 $nl = new NoticeList($notice, $this);
181 function getUpdatePermission() {
183 $this->showStylesheets();
185 $this->elementStart('div', array('class' => 'facebook_guide'));
187 $instructions = sprintf(_('If you would like the %s app to automatically update ' .
188 'your Facebook status with your latest notice, you need ' .
189 'to give it permission.'), $this->app_name);
191 $this->elementStart('p');
192 $this->element('span', array('id' => 'permissions_notice'), $instructions);
193 $this->elementEnd('p');
195 $this->elementStart('form', array('method' => 'post',
196 'action' => "index.php",
197 'id' => 'facebook-skip-permissions'));
199 $this->elementStart('ul', array('id' => 'fb-permissions-list'));
200 $this->elementStart('li', array('id' => 'fb-permissions-item'));
202 $next = urlencode("$this->app_uri/index.php");
203 $api_key = common_config('facebook', 'apikey');
205 $auth_url = 'http://www.facebook.com/authorize.php?api_key=' .
206 $api_key . '&v=1.0&ext_perm=publish_stream&next=' . $next .
207 '&next_cancel=' . $next . '&submit=skip';
209 $this->elementStart('span', array('class' => 'facebook-button'));
210 $this->element('a', array('href' => $auth_url),
211 sprintf(_('Okay, do it!'), $this->app_name));
212 $this->elementEnd('span');
214 $this->elementEnd('li');
216 $this->elementStart('li', array('id' => 'fb-permissions-item'));
217 $this->submit('skip', _('Skip'));
218 $this->elementEnd('li');
219 $this->elementEnd('ul');
221 $this->elementEnd('form');
222 $this->elementEnd('div');
227 * Generate pagination links
229 * @param boolean $have_before is there something before?
230 * @param boolean $have_after is there something after?
231 * @param integer $page current page
232 * @param string $action current action
233 * @param array $args rest of query arguments
237 function pagination($have_before, $have_after, $page, $action, $args=null)
240 // Does a little before-after block for next/prev page
242 // XXX: Fix so this uses common_local_url() if possible.
244 if ($have_before || $have_after) {
245 $this->elementStart('div', array('class' => 'pagination'));
246 $this->elementStart('dl', null);
247 $this->element('dt', null, _('Pagination'));
248 $this->elementStart('dd', null);
249 $this->elementStart('ul', array('class' => 'nav'));
252 $pargs = array('page' => $page-1);
253 $newargs = $args ? array_merge($args, $pargs) : $pargs;
254 $this->elementStart('li', array('class' => 'nav_prev'));
255 $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'prev'),
257 $this->elementEnd('li');
260 $pargs = array('page' => $page+1);
261 $newargs = $args ? array_merge($args, $pargs) : $pargs;
262 $this->elementStart('li', array('class' => 'nav_next'));
263 $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'next'),
265 $this->elementEnd('li');
267 if ($have_before || $have_after) {
268 $this->elementEnd('ul');
269 $this->elementEnd('dd');
270 $this->elementEnd('dl');
271 $this->elementEnd('div');