You are here

janrain_capture_mapping.module in Janrain Registration 7.4

This module implements a data mapping UI for Janrain Capture

File

janrain_capture_mapping/janrain_capture_mapping.module
View source
<?php

/**
 * @file
 * This module implements a data mapping UI for Janrain Capture
 */

/**
 * Implements hook_menu().
 */
function janrain_capture_mapping_menu() {
  $items['admin/config/people/janrain_capture/mapping'] = array(
    'title' => t('Field Mapping'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'janrain_capture_settings_mapping',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -8,
    'file' => 'janrain_capture_mapping.admin.inc',
  );
  $items["admin/config/people/janrain_capture/fields"] = array(
    'title' => t('Manage fields'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'janrain_capture_settings_fields',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -7,
    'file' => 'janrain_capture_mapping.admin.inc',
  );
  $items['admin/config/people/janrain_capture/fields/reset'] = array(
    'title' => t('Reset to defaults'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'janrain_capture_settings_fields_reset',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'janrain_capture_mapping.admin.inc',
  );
  $items['admin/config/people/janrain_capture/fields/edit'] = array(
    'title' => t('Edit field'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'janrain_capture_settings_fields_edit',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'janrain_capture_mapping.admin.inc',
  );
  $items['admin/config/people/janrain_capture/fields/delete'] = array(
    'title' => t('Delete field'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'janrain_capture_settings_fields_delete',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'janrain_capture_mapping.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function janrain_capture_mapping_theme() {
  return array(
    'janrain_capture_settings_mapping' => array(
      'render element' => 'form',
      'file' => 'janrain_capture_mapping.admin.inc',
    ),
    'janrain_capture_settings_fields' => array(
      'render element' => 'form',
      'file' => 'janrain_capture_mapping.admin.inc',
    ),
  );
}

/**
 * Implements hook_janrain_capture_profile_sync().
 */
function janrain_capture_mapping_janrain_capture_profile_sync($account, $profile) {
  $map = variable_get('janrain_capture_mapping_map', array());
  $wrapper = entity_metadata_wrapper('user', $account);
  $capture_fields = db_query("SELECT fid, path FROM {janrain_capture_mapping_field}")
    ->fetchAllKeyed();
  foreach ($map as $mapping) {
    try {
      $fields = explode('.', $capture_fields[$mapping['fid']]);
      $profile_current = $profile;
      foreach ($fields as $field) {
        if (array_key_exists($field, $profile_current)) {
          $profile_current = $profile_current[$field];
        }
        else {
          throw new EntityMetadataWrapperException();
        }
      }
      $value = $profile_current;

      // Date fields must be properly formatted as a timestamp
      if ($wrapper->{$mapping['field']}
        ->type() == 'date') {
        $value = $value ?: strtotime($value);
      }
      $wrapper->{$mapping['field']} = $value;
    } catch (EntityMetadataWrapperException $e) {
      watchdog('janrain_capture_mapping', 'Attempt to map attr @attr to field @field failed', array(
        '@attr' => $field,
        '@field' => $mapping['field'],
      ));
    }
  }
}

/********************************* Helper Functions ************************************************/

/**
 * Inserts the default capture profile fields into the database, ready to be mapped
 * to user properties and fields.
 */
function _janrain_capture_mapping_insert_defaults() {
  $fields = array(
    'uuid' => array(
      'name' => 'UUID',
    ),
    'email' => array(
      'name' => 'Email',
    ),
    'givenName' => array(
      'name' => 'First Name',
    ),
    'familyName' => array(
      'name' => 'Last Name',
    ),
    'displayName' => array(
      'name' => 'Display Name',
    ),
    'gender' => array(
      'name' => 'Gender',
    ),
    'birthday' => array(
      'name' => 'Birthday',
    ),
  );
  foreach ($fields as $attr => $field) {
    $fid = db_insert('janrain_capture_mapping_field')
      ->fields(array(
      'title' => $field['name'],
      'path' => $attr,
    ))
      ->execute();
    $fields[$attr]['fid'] = $fid;
  }
}

Functions

Namesort descending Description
janrain_capture_mapping_janrain_capture_profile_sync Implements hook_janrain_capture_profile_sync().
janrain_capture_mapping_menu Implements hook_menu().
janrain_capture_mapping_theme Implementation of hook_theme().
_janrain_capture_mapping_insert_defaults Inserts the default capture profile fields into the database, ready to be mapped to user properties and fields.