You are here

fbconnect_profile.module in Facebook Connect 5

This module allows users to import their personal information from Facebook.

File

modules/fbconnect_profile/fbconnect_profile.module
View source
<?php

/**
 * @file
 * This module allows users to import their personal information from Facebook.
 */
define('FBCONNECT_USER_CACHE_EXPIRE', 24);

/**
 * Implementation of hook_menu().
 */
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;
}

/**
 * This function build the user import page.
 */
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;
}

/**
 * Return the import form.
 */
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');
}

/**
 * Validate user information available from facebook
 *
 * @return array
 */
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;
  }
}

/**
 * Implementation of hook_form_alter().
 */
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);
}

/**
 * Implementation of hook_user().
 */
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;
  }
}

/**
 * Implementation of hook_theme().
 */
function fbconnect_profile_theme() {
  return array(
    'fb_user_profile' => array(
      'arguments' => array(
        'data' => NULL,
        'account' => NULL,
      ),
    ),
  );
}

/**
 * Return themed user profile
 *
 * @param array $data
 *   An array containing the data to display
 * @param object $account
 *   Drupal user object
 * @return formated HTML
 */
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);
  }
}

/**
 * Query fbconnect user information.
 *
 * @return array
 */
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;
  }
}

/**
 * Implementation of hook_cron().
 */
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);
}

/**
 * Recupere les parametres d'importation de l'utilisateur
 */
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);
  }
}

/**
 * Enregistre les informations du profile facebook.
 * @param Array $fields_setting
 */
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;
  }

  // Drupal uid
  $fields[] = 'uid';
  $values[] = $uid;
  $s[] = "%d";
  foreach ($fb_user_profile as $key => $value) {
    $fields[] = $key;
    $values[] = is_array($value) ? serialize($value) : $value;
    $s[] = "'%s'";
  }

  // Save user information imported from Facebook
  db_query('REPLACE INTO {fbconnect_profile} (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $s) . ')', $values);

  // Update the 24h cache timer and save fields settings.
  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.'));
    }
  }
}

Functions

Namesort descending Description
fbconnect_profile_available_import Validate user information available from facebook
fbconnect_profile_check_profile_cache
fbconnect_profile_cron Implementation of hook_cron().
fbconnect_profile_form_alter Implementation of hook_form_alter().
fbconnect_profile_get_profile Query fbconnect user information.
fbconnect_profile_get_user_import_setting Recupere les parametres d'importation de l'utilisateur
fbconnect_profile_import_form Return the import form.
fbconnect_profile_import_form_submit
fbconnect_profile_import_page This function build the user import page.
fbconnect_profile_insert_user_info Enregistre les informations du profile facebook.
fbconnect_profile_menu Implementation of hook_menu().
fbconnect_profile_theme Implementation of hook_theme().
fbconnect_profile_user Implementation of hook_user().
fbconnect_profile_user_settings_submit
theme_fb_user_profile Return themed user profile

Constants

Namesort descending Description
FBCONNECT_USER_CACHE_EXPIRE @file This module allows users to import their personal information from Facebook.