View source
<?php
define('FBCONNECT_USER_CACHE_EXPIRE', 24);
function fbconnect_profile_menu($may_cache) {
global $user;
$items[] = array(
'path' => 'fbconnect/register/import',
'title' => t('Import your profile information from Facebook'),
'callback' => 'fbconnect_profile_import_page',
'type' => MENU_CALLBACK,
'access' => !$user->uid,
);
return $items;
}
function fbconnect_profile_import_page() {
if (!($fbuid = fbconnect_get_fbuid())) {
drupal_set_message(t('Your Facebook session has expired, try to reconnect'));
drupal_goto();
}
$output = fbconnect_render_avatar($fbuid);
$output .= drupal_get_form('fbconnect_profile_import_form');
return $output;
}
function fbconnect_profile_import_form() {
$form['imp'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
);
$fbuid = fbconnect_get_fbuid();
$form['imp']['field_import'] = array(
'#type' => 'checkboxes',
'#options' => fbconnect_profile_available_import($fbuid),
'#default_value' => variable_get('fbconnect_profile_fields', NULL),
'#description' => t('Select the public Facebook profile information you wish to import.
This information will be displayed on your public profile.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Next'),
);
return $form;
}
function fbconnect_profile_import_form_submit($form, &$form_state) {
$fields = (array) $form_state['field_import'];
if ($fields = array_filter($fields, 'fbconnect_import_filter')) {
$_SESSION['fb_reg_import'] = $fields;
}
drupal_goto('fbconnect/register/create');
}
function fbconnect_profile_available_import($fbuid) {
$conf = variable_get('fbconnect_profile_fields', NULL);
$fields = implode(', ', array_filter($conf, 'fbconnect_import_filter'));
if ($res = fbconnect_get_info_from_fb($fbuid, $fields)) {
$label = variable_get('facebook_user_fields', NULL);
foreach ($res as $key => $value) {
if ($value) {
if (is_array($value)) {
$field[$key] .= $label[$key];
}
else {
$field[$key] .= $label[$key] . ' : ' . check_plain($value);
}
}
}
return $field;
}
}
function fbconnect_profile_form_alter($form_id, &$form) {
if ($form_id == 'fbconnect_admin_settings') {
$form['import'] = array(
'#type' => 'fieldset',
'#title' => t('Facebook profile settings'),
'#collapsible' => TRUE,
'#weight' => -1,
);
$form['import']['fbconnect_profile_fields'] = array(
'#type' => 'checkboxes',
'#options' => variable_get('facebook_user_fields', NULL),
'#default_value' => variable_get('fbconnect_profile_fields', array_keys(variable_get('facebook_user_fields', NULL))),
);
}
if ($form_id == 'fbconnect_user_settings_form') {
if ($fbuid = fbconnect_get_fbuid()) {
$form['import'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#weight' => -1,
);
$user = user_load(array(
'uid' => arg(1),
));
$default = (array) fbconnect_profile_get_user_import_setting($user->uid);
$options = fbconnect_profile_available_import($fbuid);
$form['import']['import_setting'] = array(
'#type' => 'checkboxes',
'#description' => t('This information is displayed on my profile.'),
'#options' => $options,
'#default_value' => $default,
);
$form['#submit']['fbconnect_profile_user_settings_submit'] = array();
}
}
}
function fbconnect_profile_user_settings_submit($form, &$form_state) {
$user = user_load(array(
'uid' => arg(1),
));
$conf = (array) $form_state['import_setting'];
if ($fields = array_filter($conf, 'fbconnect_import_filter')) {
fbconnect_profile_insert_user_info($user->uid, $fields);
}
else {
db_query("UPDATE {fbconnect_users} SET import_setting = '' WHERE uid = %d", $user->uid);
db_query('DELETE FROM {fbconnect_profile} WHERE uid = %d', $user->uid);
}
drupal_set_message('Your Facebook connect settings have been saved.');
drupal_goto('user/' . $user->uid);
}
function fbconnect_profile_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'view':
if ($info = fbconnect_profile_get_profile($account->uid)) {
$items['fbconnect_profile'] = array(
'value' => theme('fb_user_profile', $info, $account),
'class' => 'member',
);
return array(
t('Personal information') => $items,
);
}
break;
}
}
function fbconnect_profile_theme() {
return array(
'fb_user_profile' => array(
'arguments' => array(
'data' => NULL,
'account' => NULL,
),
),
);
}
function theme_fb_user_profile($data, $account) {
if (!empty($data) && !empty($account)) {
$label = variable_get('facebook_user_fields', '');
foreach ($data as $key => $value) {
if (!is_array($value)) {
$item[] = array(
'data' => $label[$key] . ': ' . $value,
'class' => $key,
);
}
else {
switch ($key) {
case 'affiliations':
$item[] = array(
'data' => $label[$key] . ' : ' . $value[0]['name'],
'class' => $key,
);
break;
case 'hometown_location':
$location = $label[$key] . ': ' . $value['city'] . ', ' . $value['state'] . ', ' . $value['country'];
$item[] = array(
'data' => $location,
'class' => $key,
);
break;
}
}
}
return theme('item_list', $item);
}
}
function fbconnect_profile_get_profile($uid) {
$res = db_query('SELECT * FROM {fbconnect_profile} WHERE uid = %d', $uid);
$fields = db_fetch_array($res);
if ($fields) {
$serialized = array(
'affiliations',
'hometown_location',
'current_location',
'meeting_sex',
'meeting_for',
);
array_shift($fields);
foreach ($fields as $key => $value) {
if (in_array($key, $serialized) && $value) {
$data[$key] = unserialize($value);
}
else {
if ($value) {
$data[$key] = $value;
}
}
}
return $data;
}
}
function fbconnect_profile_cron() {
$expire = time() - FBCONNECT_USER_CACHE_EXPIRE * 3600;
$query = 'DELETE {fbconnect_profile} FROM {fbconnect_profile} INNER JOIN {fbconnect_users}
WHERE {fbconnect_users}.uid = {fbconnect_profile}.uid
AND timestamp < %d';
db_query($query, $expire);
}
function fbconnect_profile_get_user_import_setting($uid) {
$data = db_result(db_query('SELECT import_setting FROM {fbconnect_users} WHERE uid = %d', $uid));
if ($data != NULL) {
return unserialize($data);
}
}
function fbconnect_profile_insert_user_info($uid, $fields_setting) {
$fbuid = fbconnect_get_fbuid();
$fb_user_profile = fbconnect_get_info_from_fb($fbuid, implode(',', $fields_setting));
if (count($fields_setting) != count($fb_user_profile)) {
watchdog('fbconnect', 'Error importing from facebook for fbuid : %fbuid', array(
'%fbuid' => $fbuid,
));
drupal_set_message(t('Error importing from facebook'), 'error');
return FALSE;
}
$fields[] = 'uid';
$values[] = $uid;
$s[] = "%d";
foreach ($fb_user_profile as $key => $value) {
$fields[] = $key;
$values[] = is_array($value) ? serialize($value) : $value;
$s[] = "'%s'";
}
db_query('REPLACE INTO {fbconnect_profile} (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $s) . ')', $values);
db_query("UPDATE {fbconnect_users} SET timestamp = %d, import_setting = '%s' WHERE uid = %d", time(), serialize($fields_setting), $uid);
return TRUE;
}
function fbconnect_profile_check_profile_cache($uid) {
$last_update = db_result(db_query('SELECT timestamp FROM {fbconnect_users} WHERE uid = %d', $uid));
$expire = time() - FBCONNECT_RENEW_CACHE * 3600;
if ($last_update < $expire) {
if (!($fields = fbconnect_profile_get_user_import_setting($uid))) {
return;
}
if (fbconnect_profile_insert_user_info($uid, $fields)) {
drupal_set_message(t('Your Facebook imported information has been updated.'));
}
}
}