-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfb_tab.module
More file actions
366 lines (313 loc) · 10.4 KB
/
fb_tab.module
File metadata and controls
366 lines (313 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/**
* @file
*
* This module provides support for "Profile Tabs" that can be added to
* facebook pages (no longer allowed for user profiles).
*
* http://developers.facebook.com/docs/guides/canvas/#tabs
*
*/
// hook_fb
define('FB_TAB_HOOK', 'fb_tab');
//// Hook_fb_tab operations
define('FB_TAB_OP_VIEW', 'fb_tab_view');
define('FB_TAB_OP_FORM', 'fb_tab_form');
define('FB_TAB_PATH_VIEW', 'fb_tab/view');
define('FB_TAB_PATH_FORM', 'fb_tab/config');
define('FB_TAB_PATH_FORM_ARGS', 2);
define('FB_TAB_VAR_PROCESS', 'fb_tab_process_page');
function fb_tab_menu() {
$items = array();
$items[FB_TAB_PATH_VIEW] = array(
'page callback' => 'fb_tab_view',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items[FB_TAB_PATH_FORM] = array(
'title' => 'Configure Tabs',
'page callback' => 'fb_tab_pages',
'access arguments' => array('access content'),
);
$items[FB_TAB_PATH_FORM . '/%'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('fb_tab_config_form', FB_TAB_PATH_FORM_ARGS),
'access arguments' => array('access content'),
);
return $items;
}
/**
* Implementation of hook_fb
*
* @param $op
* @param $data -- data payload
* @param $return
*/
function fb_tab_fb($op, $data, &$return) {
$fb = isset($data['fb']) ? $data['fb'] : NULL;
$fb_app = isset($data['fb_app']) ? $data['fb_app'] : NULL;
if ($op == FB_OP_POST_INIT) {
// Include our admin hooks.
if (fb_is_fb_admin_page()) {
require drupal_get_path('module', 'fb_tab') . '/fb_tab.admin.inc';
}
}
elseif ($op == FB_OP_CURRENT_APP) {
if (fb_is_tab() && isset($_REQUEST['fb_sig_api_key'])) {
$return = fb_get_app(array('apikey' => $_REQUEST['fb_sig_api_key']));
}
}
elseif ($op == FB_OP_INITIALIZE) {
if (fb_is_tab()) {
$config = _fb_tab_get_config($fb_app);
if (!isset($GLOBALS['custom_theme'])) {
$GLOBALS['custom_theme'] = $config['custom_theme'];
}
$use_ob = variable_get(FB_TAB_VAR_PROCESS, TRUE);
// Hack to init the theme before _drupal_maintenance_theme initializes the wrong one.
if (variable_get('site_offline', FALSE)) {
$dummy = theme('dummy');
}
// Store entire page in output buffer. Will post-process on exit.
if ($use_ob) {
ob_start();
$GLOBALS['fb_tab_post_process'] = TRUE;
}
}
}
elseif ($op == FB_OP_EXIT && fb_is_tab()) {
if (isset($GLOBALS['fb_tab_post_process']) && $GLOBALS['fb_tab_post_process']) {
$output = ob_get_contents();
ob_end_clean();
//print '<pre>' . htmlentities($output) . '</pre>'; flush();
$output = fb_canvas_process($output, array(
'add_target' => FALSE,
'absolute_links' => TRUE,
));
if (isset($output)) {
print($output);
}
}
}
}
/**
* Implements fb_tab_form_alter.
*/
function fb_tab_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['fb_app_data']) && is_array($form['fb_app_data'])) {
// Add our settings to the fb_app edit form.
//require 'fb_canvas.admin.inc';
fb_tab_admin_form_alter($form, $form_state, $form_id);
}
if (fb_is_tab()) {
// Most Drupal forms have an action that assumes Drupal's base_path.
// But in the case of FBML, we need to make it explicit.
$form['#action'] = url($_GET['q'], array('absolute' => TRUE));
//dpm(__FUNCTION__ . " changed action to " . $form['#action']);
}
}
function _fb_tab_make_form_action_local($action = NULL) {
global $base_path;
if (!isset($action)) {
// Is this ever reached?
if (function_exists('dpm')) dpm("_fb_canvas_make_form_action_local($action)"); // XXX
$action = $_GET['q'];
}
// If action is fully qualified, do not change it
if (strpos($action, ':')) {
if (function_exists('dpm')) dpm($action, "leaving form action untouched"); // XXX
return $action;
}
$relative = url('');
$absolute = url('', array('absolute' => TRUE));
$base_path = base_path(); // By default, '#action' will be request_uri(), which starts with base_path();
global $_fb_app;
if (strpos($action, $relative) === 0) {
// If here, the action was made by call to url() and we prepended
// canvas_name. Now remove the canvas name for a proper absolute url.
// (Comment forms reach this clause but node forms do not.)
$action = substr($action, strlen($relative));
}
elseif ($base_path && strpos($action, $base_path) === 0) {
$action = substr($action, strlen($base_path));
}
$action = url(ltrim($action, '/'), array('absolute' => TRUE));
return $action;
}
/**
* Helper returns configuration for this module, on a per-app basis.
*/
function _fb_tab_get_config($fb_app) {
$fb_app_data = fb_get_app_data($fb_app);
$config = isset($fb_app_data['fb_tab']) ? $fb_app_data['fb_tab'] : array();
// Merge in defaults
$config += array(
'custom_theme' => NULL,
'tab_default_name' => isset($fb_app->title) ? $fb_app->title : NULL,
'profile_tab_url' => FB_TAB_PATH_VIEW,
);
return $config;
}
/**
* Another module provides the contents of the tab depending
* on the context that we give it (who is calling it, etc.)
*/
function fb_tab_view() {
$fb_app = $GLOBALS['_fb_app'];
$fb = $GLOBALS['_fb'];
$profile_id = fb_settings(FB_SETTINGS_PROFILE_ID);
$config = fb_tab_fetch_config($fb_app, $profile_id);
$data = array(
'fb_app' => $fb_app,
'fb' => $fb,
'profile_id' => $profile_id,
'config' => isset($config['data']) ? $config['data'] : NULL,
);
$content = fb_invoke(FB_TAB_OP_VIEW, $data, array(), FB_TAB_HOOK);
//print('<pre>' . print_r($data, 1) . '</pre>'); flush();
//print(print_r($content, 1)); flush();
return drupal_render($content);
}
/**
* Build the tab config form. Invokes hook_fb_tab() to get the custom settings.
*/
function fb_tab_config_form($form_state, $profile_id) {
$fb_app = $GLOBALS['_fb_app'];
$fb = $GLOBALS['_fb'];
$config = fb_tab_fetch_config($fb_app, $profile_id);
$data = array(
'fb_app' => $fb_app,
'fb' => $fb,
'profile_id' => $profile_id,
'config' => isset($config['data']) ? $config['data'] : NULL,
);
$page_info = $fb->api($profile_id);
$fbu = fb_facebook_user($fb);
$admin_info = fb_fql_query($fb, "SELECT uid, type FROM page_admin WHERE uid=$fbu AND page_id=$profile_id"); // FQL not SQL, no {curly_brackets}
//dpm($admin_info, __FUNCTION__);
// @TODO: if not page admin, prompt user to login or deny access.
$form = array(
'info' => array(
'#type' => 'markup',
'#value' => t('Tab confiugration for <a href="!href">%page</a>', array(
'!href' => $page_info['link'],
'%page' => $page_info['name'],
)),
'#prefix' => '<h3>',
'#suffix' => '</h3>',
'#weight' => -99,
),
'label' => array(
'#type' => 'value',
'#value' => $fb_app->label,
),
'profile_id' => array(
'#type' => 'value',
'#value' => $profile_id,
),
'data' => array(
// Modules will add their fields here.
'#tree' => TRUE,
),
'submit' => array(
'#type' => 'submit',
'#value' => t('Submit'),
'#weight' => 90,
),
);
if (isset($config['fb_tab_id'])) {
$form['fb_tab_id'] = array(
'#type' => 'value',
'#value' => $config['fb_tab_id'],
);
}
$form['data'] = fb_invoke(FB_TAB_OP_FORM, $data, $form['data'], FB_TAB_HOOK);
//print('<pre>' . print_r($data, 1) . '</pre>'); flush();
//print(print_r($content, 1)); flush();
return $form;
}
/**
* Submit handler for tab config form.
*/
function fb_tab_config_form_submit($form, &$form_state) {
if ($profile_id = $form_state['values']['profile_id']) {
fb_tab_write_config($form_state['values']);
$data = fb_fql_query($GLOBALS['_fb'], "SELECT page_id, name, pic_small, page_url, type FROM page WHERE page_id=$profile_id"); // FQL, no {curly_brackets}
drupal_set_message(t('The tab settings for <a href="!url">%page</a> have been updated.', array(
'!url' => $data[0]['page_url'],
'%page' => $data[0]['name'],
)));
}
}
/*
* Store configuration data for a tab by application.
*/
function fb_tab_write_config(&$row) {
if (!isset($row['created']))
$row['created'] = time();
$row['data'] = serialize($row['data']);
return drupal_write_record('fb_tab', $row,
isset($row['fb_tab_id']) ? array('fb_tab_id') : NULL);
}
/*
* Return configuration of a tab by application (access via 'label') and page ID.
* Page ID exists because an app can present different views on different pages.
*/
function fb_tab_fetch_config($fb_app, $profile_id) {
$result = db_query("SELECT * FROM {fb_tab} WHERE label = '%s' AND profile_id = %d" , $fb_app->label, $profile_id);
$row = db_fetch_array($result);
if ($row) {
$row['data'] = unserialize($row['data']);
return $row;
}
else {
return array();
}
}
/**
* This page callback will show the user a list of pages they have authority to configure.
*/
function fb_tab_pages() {
if ($fbu = fb_facebook_user()) {
$result = fb_fql_query($GLOBALS['_fb'], "SELECT page_id, name, pic_small, page_url, type FROM page WHERE has_added_app=1 AND page_id IN (SELECT page_id FROM page_admin WHERE uid=$fbu)"); // FQL, no {curly_brackets}
//dpm($result, __FUNCTION__);
if (count($result)) {
$output['pages'] = array(
'#prefix' => '<ul>',
'#suffix' => '</ul>',
);
foreach ($result as $data) {
$output['pages'][$data['page_id']] = array(
'#value' => l($data['name'],fb_tab_config_url($data['page_id'], array('fb_canvas' => fb_is_canvas()))),
'#prefix' => '<li>',
'#suffix' => '</li>',
);
}
$output['intro'] = array(
'#value' => t('Select one of your pages to configure:'),
'#prefix' => '<p>',
'#suffix' => '</p>',
);
}
else {
$output['intro'] = array(
'#value' => t('Found no pages to configure.'),
'#prefix' => '<p>',
'#suffix' => '</p>',
);
}
return drupal_render($output);
}
else {
fb_access_denied();
}
}
/**
* Provides the URL of the settings page for a given facebook page.
*/
function fb_tab_config_url($profile_id = NULL, $options = array()) {
if (!$profile_id) {
$profile_id = fb_settings(FB_SETTINGS_PROFILE_ID);
}
return url(FB_TAB_PATH_FORM . '/' . $profile_id, $options);
}