Newer
Older
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
<?php
function fb_cron_form_alter($form_id, &$form) {
//drupal_set_message("fb_user_form_alter($form_id) " . dpr($form, 1));
// Add our settings to the fb_app edit form.
if (is_array($form['fb_app_data'])) {
$node = $form['#node'];
$fb_app_data = fb_app_get_data($node->fb_app);
$fb_cron_data = $fb_app_data['fb_cron'];
$form['fb_app_data']['fb_cron'] = array('#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Facebook cron settings'),
'#description' => t('An infinite session key is required for cron functions to work.'));
$form['fb_app_data']['fb_cron']['fbu'] = array('#type' => textfield,
'#title' => t('Facebook User ID'),
'#default_value' => $fb_cron_data['fbu'],
);
$form['fb_app_data']['fb_cron']['key'] = array('#type' => textfield,
'#title' => t('Infinite Session Key'),
'#default_value' => $fb_cron_data['key'],
'#description' => t('If you do not have a key, you can get one !here.',
// XXX FIX THIS LINK
array('!here' => l(t('here'), 'http://www.facebook.com/code_gen.php?v=1.0&api_key='.$node->fb_app->apikey))),
);
// TODO: give this field an input filter
$form['fb_app_data']['fb_cron']['ref_fbml'] =
array('#type' => 'textarea',
'#title' => t('Reference FBML'),
'#description' => t('You may refer to this markup via the FBML tag <fb:ref handle=LABEL />, where LABEL is the value you specified above.'),
'#default_value' => $fb_cron_data['ref_fbml'],
);
$form['fb_app_data']['fb_cron']['ref_fbml_filter'] =
filter_form($fb_cron_data['ref_fbml_filter'], NULL, array('fb_app_data', 'fb_cron', 'ref_fbml_filter'));
$form['fb_app_data']['fb_cron']['profile_fbml'] =
array('#type' => 'textarea',
'#title' => t('Profile FBML'),
'#description' => t('Enter FBML for each application user\'s profile page. This may be a place to use the FBML tag <fb:ref handle=LABEL />.'),
'#default_value' => $fb_cron_data['profile_fbml'],
);
$form['fb_app_data']['fb_cron']['profile_fbml_filter'] =
filter_form($fb_cron_data['profile_fbml_filter'], NULL, array('fb_app_data', 'fb_cron', 'profile_fbml_filter'));
}
}
function fb_cron_cron() {
global $fb, $custom_theme;
global $user;
// TODO: replace this db query with a hook invoke to discover apps. This is more than we should have to know about fb_app's database table.
$query = "SELECT fb.* FROM {fb_app} fb LEFT JOIN {node} n ON n.nid=fb.nid WHERE n.status=1";
$result = db_query($query);
// remember the original user (probably Anonymous)
$orig_user = $user;
// Iterate through all apps
while ($fb_app = db_fetch_object($result)) {
fb_api_init($fb_app);
fb_init_path();
$fb_app_data = unserialize($fb_app->data);
$fb_cron_data = $fb_app_data['fb_cron'];
if ($fb_cron_data && $fb_cron_data['fbu']) {
watchdog('fb_cron', t('Performing cron jobs for facebook application !link',
array('!link' => l($fb_app->label, 'node/'.$fb_app->nid))));
$fb->set_user($fb_cron_data['fbu'], $fb_cron_data['key']);
// If reference FBML has been given, update it.
if ($output = $fb_cron_data['ref_fbml']) {
$custom_theme = variable_get('fb_fbml_theme', 'fb1');
$blocks = theme('blocks', 'ref_fbml');
$output .= $blocks;
$fb->api_client->fbml_setRefHandle($fb_app->label, $output);
}
// If profile FBML is set, set each user
// TODO: log in as each user to generate user specific content.
if ($fb_cron_data['profile_fbml']) {
$custom_theme = variable_get('fb_fbml_theme', 'fb1');
// Get users of this app. TODO: throttle by configurable amount
$result = db_query("SELECT * FROM {fb_app_user} WHERE nid=%d AND fbu>0 AND added > 0 ORDER BY time_cron ASC LIMIT 10", $fb_app->nid);
while ($data = db_fetch_object($result)) {
if ($data->fbu) {
$account = fb_user_get_local_user($data->fbu, $fb_app);
watchdog('fb_cron', t('Setting profile FBML for facebook user %user',
array('%user' => $data->fbu)));
print("<pre>");
print("fbu is $data->fbu\n");
//print_r($account);
print_r($fb_cron_data);
print("</pre>");
// generate ouput for this user
$user = $account;
$output = check_markup($fb_cron_data['profile_fbml'], $fb_cron_data['profile_fbml_filter'], FALSE);
$blocks = theme('blocks', 'profile_fbml');
$output .= $blocks;
dpr($output);
$fb->api_client->profile_setFBML($output, $data->fbu);
// Keep track of how recently we updated this user.
db_query("UPDATE {fb_app_user} SET time_cron=%d WHERE nid=%d and fbu=%d",
time(), $fb_app->nid, $data->fbu);
}
}
}
if ($error_code = $fb->api_client->error_code) {
watchdog('fb_cron', t('Error code %code calling facebook during cron job.',
array('%code' => $error_code), 'error'));
}
}
// restore original user
if ($orig_user)
$user = $orig_user;
fb_init_path(TRUE); // Restore old path settings so other cron hooks will succeed.
}
}
?>