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
30 function prepare($argarray)
32 parent::prepare($argarray);
34 $this->page = $this->trimmed('page');
43 function handle($args)
45 parent::handle($args);
47 // If the user has opted not to initially allow the app to have
48 // Facebook status update permission, store that preference. Only
49 // promt the user the first time she uses the app
50 if ($this->arg('skip') || $args['fb_sig_request_method'] == 'GET') {
51 $this->facebook->api_client->data_setUserPreference(
52 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
84 // User hasn't authenticated yet, prompt for creds
91 $this->showStylesheets();
93 $nickname = common_canonical_nickname($this->trimmed('nickname'));
94 $password = $this->arg('password');
99 if (common_check_user($nickname, $password)) {
100 $user = User::staticGet('nickname', $nickname);
103 $this->showLoginForm(_m("Server error: Couldn't get user!"));
106 $flink = DB_DataObject::factory('foreign_link');
107 $flink->user_id = $user->id;
108 $flink->foreign_id = $this->fbuid;
109 $flink->service = FACEBOOK_SERVICE;
110 $flink->created = common_sql_now();
111 $flink->set_flags(true, false, false, false);
113 $flink_id = $flink->insert();
115 // XXX: Do some error handling here
117 $this->setDefaults();
119 $this->getUpdatePermission();
122 $msg = _m('Incorrect username or password.');
126 $this->showLoginForm($msg);
130 function setDefaults()
132 $this->facebook->api_client->data_setUserPreference(
133 FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
136 function showNoticeForm()
138 $post_action = "$this->app_uri/index.php";
140 $notice_form = new FacebookNoticeForm($this, $post_action, null,
141 $post_action, $this->user);
142 $notice_form->show();
147 if ($this->page > 1) {
148 // @todo FIXME: Core should have methods to get "Full name (nickname)" in a localised form
149 // so that this can be used consistenly throughout StatusNet without having to implement it
151 // TRANS: Page title.
152 // TRANS: %1$s is a user nickname, %2$s is a page number.
153 return sprintf(_m('%1$s and friends, page %2$d'), $this->user->nickname, $this->page);
155 // TRANS: Page title.
156 // TRANS: %s is a user nickname
157 return sprintf(_m("%s and friends"), $this->user->nickname);
161 function showContent()
163 $notice = $this->user->noticeInbox(($this->page-1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
165 $nl = new NoticeList($notice, $this);
169 $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
170 $this->page, 'index.php', array('nickname' => $this->user->nickname));
173 function showNoticeList($notice)
175 $nl = new NoticeList($notice, $this);
179 function getUpdatePermission() {
180 $this->showStylesheets();
182 $this->elementStart('div', array('class' => 'facebook_guide'));
184 // TRANS: Instructions. %s is the application name.
185 $instructions = sprintf(_m('If you would like the %s app to automatically update ' .
186 'your Facebook status with your latest notice, you need ' .
187 'to give it permission.'), $this->app_name);
189 $this->elementStart('p');
190 $this->element('span', array('id' => 'permissions_notice'), $instructions);
191 $this->elementEnd('p');
193 $this->elementStart('form', array('method' => 'post',
194 'action' => "index.php",
195 'id' => 'facebook-skip-permissions'));
197 $this->elementStart('ul', array('id' => 'fb-permissions-list'));
198 $this->elementStart('li', array('id' => 'fb-permissions-item'));
200 $next = urlencode("$this->app_uri/index.php");
201 $api_key = common_config('facebook', 'apikey');
203 $auth_url = 'http://www.facebook.com/authorize.php?api_key=' .
204 $api_key . '&v=1.0&ext_perm=publish_stream&next=' . $next .
205 '&next_cancel=' . $next . '&submit=skip';
207 $this->elementStart('span', array('class' => 'facebook-button'));
208 // @todo FIXME: sprintf not needed here?
209 $this->element('a', array('href' => $auth_url),
210 sprintf(_m('Okay, do it!'), $this->app_name));
211 $this->elementEnd('span');
213 $this->elementEnd('li');
215 $this->elementStart('li', array('id' => 'fb-permissions-item'));
216 // TRANS: Button text. Clicking the button will skip updating Facebook permissions.
217 $this->submit('skip', _m('BUTTON','Skip'));
218 $this->elementEnd('li');
219 $this->elementEnd('ul');
221 $this->elementEnd('form');
222 $this->elementEnd('div');
226 * Generate pagination links
228 * @param boolean $have_before is there something before?
229 * @param boolean $have_after is there something after?
230 * @param integer $page current page
231 * @param string $action current action
232 * @param array $args rest of query arguments
236 function pagination($have_before, $have_after, $page, $action, $args=null)
238 // Does a little before-after block for next/prev page
240 // XXX: Fix so this uses common_local_url() if possible.
242 if ($have_before || $have_after) {
243 $this->elementStart('dl', 'pagination');
244 $this->element('dt', null, _m('Pagination'));
245 $this->elementStart('dd', null);
246 $this->elementStart('ul', array('class' => 'nav'));
249 $pargs = array('page' => $page-1);
250 $newargs = $args ? array_merge($args, $pargs) : $pargs;
251 $this->elementStart('li', array('class' => 'nav_prev'));
252 $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'prev'),
253 // TRANS: Pagination link.
255 $this->elementEnd('li');
258 $pargs = array('page' => $page+1);
259 $newargs = $args ? array_merge($args, $pargs) : $pargs;
260 $this->elementStart('li', array('class' => 'nav_next'));
261 $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'next'),
262 // TRANS: Pagination link.
264 $this->elementEnd('li');
266 if ($have_before || $have_after) {
267 $this->elementEnd('ul');
268 $this->elementEnd('dd');
269 $this->elementEnd('dl');