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') && !defined('LACONICA')) {
24 require_once INSTALLDIR . '/plugins/Facebook/facebookaction.php';
26 class FacebookhomeAction extends FacebookAction
31 function prepare($argarray)
33 parent::prepare($argarray);
35 $this->page = $this->trimmed('page');
44 function handle($args)
46 parent::handle($args);
48 // If the user has opted not to initially allow the app to have
49 // Facebook status update permission, store that preference. Only
50 // promt the user the first time she uses the app
51 if ($this->arg('skip') || $args['fb_sig_request_method'] == 'GET') {
52 $this->facebook->api_client->data_setUserPreference(
53 FACEBOOK_PROMPTED_UPDATE_PREF, 'true');
58 $this->user = $this->flink->getUser();
60 // If this is the first time the user has started the app
61 // prompt for Facebook status update permission
62 if (!$this->facebook->api_client->users_hasAppPermission('publish_stream')) {
64 if ($this->facebook->api_client->data_getUserPreference(
65 FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
66 $this->getUpdatePermission();
71 // Make sure the user's profile box has the lastest notice
72 $notice = $this->user->getCurrentNotice();
74 $this->updateProfileBox($notice);
77 if ($this->arg('status_submit') == 'Send') {
78 $this->saveNewNotice();
81 // User is authenticated and has already been prompted once for
82 // Facebook status update permission? Then show the main page
88 // User hasn't authenticated yet, prompt for creds
97 $this->showStylesheets();
99 $nickname = common_canonical_nickname($this->trimmed('nickname'));
100 $password = $this->arg('password');
106 if (common_check_user($nickname, $password)) {
108 $user = User::staticGet('nickname', $nickname);
111 $this->showLoginForm(_m("Server error - couldn't get user!"));
114 $flink = DB_DataObject::factory('foreign_link');
115 $flink->user_id = $user->id;
116 $flink->foreign_id = $this->fbuid;
117 $flink->service = FACEBOOK_SERVICE;
118 $flink->created = common_sql_now();
119 $flink->set_flags(true, false, false, false);
121 $flink_id = $flink->insert();
123 // XXX: Do some error handling here
125 $this->setDefaults();
127 $this->getUpdatePermission();
131 $msg = _m('Incorrect username or password.');
135 $this->showLoginForm($msg);
140 function setDefaults()
142 $this->facebook->api_client->data_setUserPreference(
143 FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
146 function showNoticeForm()
148 $post_action = "$this->app_uri/index.php";
150 $notice_form = new FacebookNoticeForm($this, $post_action, null,
151 $post_action, $this->user);
152 $notice_form->show();
157 if ($this->page > 1) {
158 return sprintf(_m("%s and friends, page %d"), $this->user->nickname, $this->page);
160 return sprintf(_m("%s and friends"), $this->user->nickname);
164 function showContent()
166 $notice = $this->user->noticeInbox(($this->page-1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
168 $nl = new NoticeList($notice, $this);
172 $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
173 $this->page, 'index.php', array('nickname' => $this->user->nickname));
176 function showNoticeList($notice)
179 $nl = new NoticeList($notice, $this);
183 function getUpdatePermission() {
185 $this->showStylesheets();
187 $this->elementStart('div', array('class' => 'facebook_guide'));
189 $instructions = sprintf(_m('If you would like the %s app to automatically update ' .
190 'your Facebook status with your latest notice, you need ' .
191 'to give it permission.'), $this->app_name);
193 $this->elementStart('p');
194 $this->element('span', array('id' => 'permissions_notice'), $instructions);
195 $this->elementEnd('p');
197 $this->elementStart('form', array('method' => 'post',
198 'action' => "index.php",
199 'id' => 'facebook-skip-permissions'));
201 $this->elementStart('ul', array('id' => 'fb-permissions-list'));
202 $this->elementStart('li', array('id' => 'fb-permissions-item'));
204 $next = urlencode("$this->app_uri/index.php");
205 $api_key = common_config('facebook', 'apikey');
207 $auth_url = 'http://www.facebook.com/authorize.php?api_key=' .
208 $api_key . '&v=1.0&ext_perm=publish_stream&next=' . $next .
209 '&next_cancel=' . $next . '&submit=skip';
211 $this->elementStart('span', array('class' => 'facebook-button'));
212 $this->element('a', array('href' => $auth_url),
213 sprintf(_m('Okay, do it!'), $this->app_name));
214 $this->elementEnd('span');
216 $this->elementEnd('li');
218 $this->elementStart('li', array('id' => 'fb-permissions-item'));
219 $this->submit('skip', _m('Skip'));
220 $this->elementEnd('li');
221 $this->elementEnd('ul');
223 $this->elementEnd('form');
224 $this->elementEnd('div');
229 * Generate pagination links
231 * @param boolean $have_before is there something before?
232 * @param boolean $have_after is there something after?
233 * @param integer $page current page
234 * @param string $action current action
235 * @param array $args rest of query arguments
239 function pagination($have_before, $have_after, $page, $action, $args=null)
242 // Does a little before-after block for next/prev page
244 // XXX: Fix so this uses common_local_url() if possible.
246 if ($have_before || $have_after) {
247 $this->elementStart('dl', 'pagination');
248 $this->element('dt', null, _m('Pagination'));
249 $this->elementStart('dd', null);
250 $this->elementStart('ul', array('class' => 'nav'));
253 $pargs = array('page' => $page-1);
254 $newargs = $args ? array_merge($args, $pargs) : $pargs;
255 $this->elementStart('li', array('class' => 'nav_prev'));
256 $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'prev'),
258 $this->elementEnd('li');
261 $pargs = array('page' => $page+1);
262 $newargs = $args ? array_merge($args, $pargs) : $pargs;
263 $this->elementStart('li', array('class' => 'nav_next'));
264 $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'next'),
266 $this->elementEnd('li');
268 if ($have_before || $have_after) {
269 $this->elementEnd('ul');
270 $this->elementEnd('dd');
271 $this->elementEnd('dl');