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 (!empty($this->flink)) {
48 $this->user = $this->flink->getUser();
50 // If this is the first time the user has started the app
51 // prompt for Facebook status update permission
52 if (!$this->facebook->api_client->users_hasAppPermission('publish_stream')) {
53 $this->getUpdatePermission();
57 if ($this->arg('status_submit') == 'Send') {
58 $this->saveNewNotice();
61 // User is authenticated and has already been prompted once for
62 // Facebook status update permission? Then show the main page
66 // User hasn't authenticated yet, prompt for creds
73 $this->showStylesheets();
75 $nickname = common_canonical_nickname($this->trimmed('nickname'));
76 $password = $this->arg('password');
81 if (common_check_user($nickname, $password)) {
82 $user = User::staticGet('nickname', $nickname);
85 $this->showLoginForm(_m("Server error: Couldn't get user!"));
88 $flink = DB_DataObject::factory('foreign_link');
89 $flink->user_id = $user->id;
90 $flink->foreign_id = $this->fbuid;
91 $flink->service = FACEBOOK_SERVICE;
92 $flink->created = common_sql_now();
93 $flink->set_flags(true, false, false, false);
95 $flink_id = $flink->insert();
97 // XXX: Do some error handling here
99 $this->getUpdatePermission();
102 $msg = _m('Incorrect username or password.');
106 $this->showLoginForm($msg);
110 function showNoticeForm()
112 $post_action = "$this->app_uri/index.php";
114 $notice_form = new FacebookNoticeForm($this, $post_action, null,
115 $post_action, $this->user);
116 $notice_form->show();
121 if ($this->page > 1) {
122 // @todo FIXME: Core should have methods to get "Full name (nickname)" in a localised form
123 // so that this can be used consistenly throughout StatusNet without having to implement it
125 // TRANS: Page title.
126 // TRANS: %1$s is a user nickname, %2$s is a page number.
127 return sprintf(_m('%1$s and friends, page %2$d'), $this->user->nickname, $this->page);
129 // TRANS: Page title.
130 // TRANS: %s is a user nickname
131 return sprintf(_m("%s and friends"), $this->user->nickname);
135 function showContent()
137 $notice = $this->user->noticeInbox(($this->page-1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
139 $nl = new NoticeList($notice, $this);
143 $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
144 $this->page, 'index.php', array('nickname' => $this->user->nickname));
147 function showNoticeList($notice)
149 $nl = new NoticeList($notice, $this);
153 function getUpdatePermission() {
154 $this->showStylesheets();
156 $this->elementStart('div', array('class' => 'facebook_guide'));
158 // TRANS: Instructions. %s is the application name.
159 $instructions = sprintf(_m('If you would like the %s app to automatically update ' .
160 'your Facebook status with your latest notice, you need ' .
161 'to give it permission.'), $this->app_name);
163 $this->elementStart('p');
164 $this->element('span', array('id' => 'permissions_notice'), $instructions);
165 $this->elementEnd('p');
167 $this->elementStart('form', array('method' => 'post',
168 'action' => "index.php",
169 'id' => 'facebook-skip-permissions'));
171 $this->elementStart('ul', array('id' => 'fb-permissions-list'));
172 $this->elementStart('li', array('id' => 'fb-permissions-item'));
174 $next = urlencode("$this->app_uri/index.php");
175 $api_key = common_config('facebook', 'apikey');
177 $auth_url = 'http://www.facebook.com/authorize.php?api_key=' .
178 $api_key . '&v=1.0&ext_perm=publish_stream&next=' . $next .
179 '&next_cancel=' . $next . '&submit=skip';
181 $this->elementStart('span', array('class' => 'facebook-button'));
182 // @todo FIXME: sprintf not needed here?
183 $this->element('a', array('href' => $auth_url),
184 sprintf(_m('Okay, do it!'), $this->app_name));
185 $this->elementEnd('span');
187 $this->elementEnd('li');
189 $this->elementStart('li', array('id' => 'fb-permissions-item'));
190 // TRANS: Button text. Clicking the button will skip updating Facebook permissions.
191 $this->submit('skip', _m('BUTTON','Skip'));
192 $this->elementEnd('li');
193 $this->elementEnd('ul');
195 $this->elementEnd('form');
196 $this->elementEnd('div');
200 * Generate pagination links
202 * @param boolean $have_before is there something before?
203 * @param boolean $have_after is there something after?
204 * @param integer $page current page
205 * @param string $action current action
206 * @param array $args rest of query arguments
210 function pagination($have_before, $have_after, $page, $action, $args=null)
212 // Does a little before-after block for next/prev page
214 // XXX: Fix so this uses common_local_url() if possible.
216 if ($have_before || $have_after) {
217 $this->elementStart('dl', 'pagination');
218 $this->element('dt', null, _m('Pagination'));
219 $this->elementStart('dd', null);
220 $this->elementStart('ul', array('class' => 'nav'));
223 $pargs = array('page' => $page-1);
224 $newargs = $args ? array_merge($args, $pargs) : $pargs;
225 $this->elementStart('li', array('class' => 'nav_prev'));
226 $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'prev'),
227 // TRANS: Pagination link.
229 $this->elementEnd('li');
232 $pargs = array('page' => $page+1);
233 $newargs = $args ? array_merge($args, $pargs) : $pargs;
234 $this->elementStart('li', array('class' => 'nav_next'));
235 $this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'next'),
236 // TRANS: Pagination link.
238 $this->elementEnd('li');
240 if ($have_before || $have_after) {
241 $this->elementEnd('ul');
242 $this->elementEnd('dd');
243 $this->elementEnd('dl');