You are here

function salesforce_api_fieldmap_export_create in Salesforce Suite 7.2

Same name and namespace in other branches
  1. 5.2 salesforce_api/salesforce_api.module \salesforce_api_fieldmap_export_create()
  2. 6.2 salesforce_api/salesforce_api.module \salesforce_api_fieldmap_export_create()
  3. 7 salesforce_api/salesforce_api.module \salesforce_api_fieldmap_export_create()

Creates an object for export to Salesforce based on the supplied Drupal object and fieldmap.

Parameters

$name: The name of the fieldmap used to process the Drupal object for the export.

$drupal_data: The Drupal object used to generate the export.

Return value

An object containing data ready for export to Salesforce or FALSE if the operation failed.

2 calls to salesforce_api_fieldmap_export_create()
sf_entity_export in sf_entity/sf_entity.module
Exports an entity to Salesforce using the specified fieldmap and stores the ID of the Salesforce object for the entity.
sf_entity_salesforce_form in sf_entity/sf_entity.module
Displays the Salesforce synchronization form.

File

salesforce_api/salesforce_api.module, line 1068
Defines an API that enables modules to interact with the Salesforce server.

Code

function salesforce_api_fieldmap_export_create($name, $drupal_data = NULL) {

  // Load the fieldmap from the database.
  $map = salesforce_api_salesforce_fieldmap_load($name);

  // Fail if the fieldmap does not exist.
  if (!$map) {
    return FALSE;
  }
  $drupal_object_definition = salesforce_api_fieldmap_objects_load('drupal', $map->drupal_entity, $map->drupal_bundle);
  $sf_object_definition = salesforce_api_fieldmap_objects_load('salesforce', 'salesforce', $map->salesforce);
  $object = new stdClass();

  // Loop through the fields on the fieldmap.
  foreach ($map->fields as $sf_fieldname => $drupal_fieldname) {
    $value = '';
    $sf_field_definition = $sf_object_definition['fields'][$sf_fieldname];
    $updateable = $sf_field_definition['type'] & SALESFORCE_FIELD_UPDATEABLE;
    $createable = $sf_field_definition['type'] & SALESFORCE_FIELD_CREATEABLE;
    $nillable = $sf_field_definition['type'] & SALESFORCE_FIELD_NILLABLE;

    // Don't try to update or create fields to which those actions do not apply.
    if (!$updateable && !$createable || empty($drupal_data->salesforce->sfid) && !$createable || !empty($drupal_data->salesforce->sfid) && !$updateable) {
      continue;
    }

    // See if it's a special field
    if (is_array($map->fields[$sf_fieldname])) {
      switch ($map->fields[$sf_fieldname]['type']) {
        case 'fixed':
          if (isset($map->fields[$sf_fieldname]['value'])) {
            $value = $map->fields[$sf_fieldname]['value'];
          }
          break;
        case 'tokens':
          if (isset($map->fields[$sf_fieldname]['value'])) {
            $value = token_replace($map->fields[$sf_fieldname]['value'], array(
              $map->drupal_entity => $drupal_data,
            ), array(
              'clear' => TRUE,
              'sanitize' => FALSE,
            ));
          }
          break;
        case 'php':
          if (isset($map->fields[$sf_fieldname]['value'])) {
            $value = eval($map->fields[$sf_fieldname]['value']);
          }
          break;
      }
    }
    elseif (isset($drupal_object_definition['fields'][$drupal_fieldname]['export'])) {
      $drupal_field_export_handler = $drupal_object_definition['fields'][$drupal_fieldname]['export'];
      $drupal_field_definition = $drupal_object_definition['fields'][$drupal_fieldname];

      // Get the value for the field from the handler function.
      $value = $drupal_field_export_handler($drupal_data, $drupal_fieldname, $drupal_field_definition, $sf_field_definition);
    }
    elseif (isset($drupal_data->{$drupal_fieldname})) {
      $value = $drupal_data->{$drupal_fieldname};
    }

    // Ignore null values for non-nillable fields. We also explicitly set all
    // empty strings to NULL so they too can be subject to these checks.
    if ($value === "") {
      $value = NULL;
    }
    if (is_null($value) && !$nillable) {
      continue;
    }
    elseif (is_null($value) && $nillable && !empty($map->fields[$sf_fieldname])) {
      $fieldsToNull[] = $sf_fieldname;
      continue;
    }

    // Evaluate field-type-specific syntax rules. The point here is not to
    // massage data in any way - that should have been done in the export
    // handler. All we want to do is avoid creating a syntactically invalid
    // object for export. For example, an "id" or "reference" field must contain
    // a Salesforce ID. We do not check to make sure the given Salesforce ID
    // exists, merely that the format matches that of a Salesforce ID.
    //
    // For any errors, just continue to the next iteration and log the error.
    $type = $sf_field_definition['salesforce']['type'];
    $errors = array();
    switch ($type) {
      case 'boolean':
        if (empty($value)) {
          $object->{$sf_fieldname} = 0;
        }
        else {
          $object->{$sf_fieldname} = 1;
        }
        break;
      case 'time':
      case 'date':
      case 'datetime':
        $time = strtotime($value);
        if (empty($time)) {
          $errors[] = array(
            'message' => 'Salesforce cannot accept empty values for @type fields on fieldname @fieldname: "@value" value provided.',
            'vars' => array(
              '@type' => $type,
              '@value' => $value,
              '@fieldname' => $sf_fieldname,
            ),
          );
        }
        else {

          // The export handler should have handled this, but reformat to
          // DATE_ATOM, just in case.
          $object->{$sf_fieldname} = gmdate(DATE_ATOM, $time);
        }
        break;
      case 'email':

        // Remove spaces.
        $value = trim($value);
        if (salesforce_api_valid_email_address($value)) {
          $object->{$sf_fieldname} = $value;
        }
        else {
          $errors[] = array(
            'message' => 'Invalid email address provided for Salesforce export on fieldname @fieldname: @value',
            'vars' => array(
              '@fieldname' => $sf_fieldname,
              '@value' => $value,
            ),
          );
          continue;
        }
        break;
      case 'percent':
      case 'currency':
      case 'int':
      case 'double':
        if (is_numeric($value)) {
          $object->{$sf_fieldname} = $value;
        }
        else {
          $errors[] = array(
            'message' => 'Invalid value provided for Salesforce @type field on fieldname @fieldname: "@value".',
            'vars' => array(
              '@type' => $type,
              '@value' => $value,
              '@fieldname' => $sf_fieldname,
            ),
          );
        }
        break;
      case 'reference':
      case 'id':
        if (!is_sfid($value)) {
          $errors[] = array(
            'message' => 'Invalid value provided for Salesforce @type field on fieldname @fieldname: "@value".',
            'vars' => array(
              '@type' => $type,
              '@value' => $value,
              '@fieldname' => $sf_fieldname,
            ),
          );
        }
        else {
          $object->{$sf_fieldname} = $value;
        }
        break;
      case 'string':
      case 'picklist':
      case 'multipicklist':
      case 'combobox':
      case 'base64':
      case 'textarea':
      case 'phone':
      case 'url':
      case 'encryptedstring':
      default:
        $object->{$sf_fieldname} = $value;
        break;
    }
    $max_len = $sf_field_definition['salesforce']['length'];
    if ($max_len && isset($object->{$sf_fieldname}) && drupal_strlen($object->{$sf_fieldname}) > $max_len) {
      $object->{$sf_fieldname} = drupal_substr($object->{$sf_fieldname}, 0, $max_len);
    }
  }
  if (!empty($errors)) {
    foreach ($errors as $error) {
      salesforce_api_log(SALESFORCE_LOG_SOME, $error['message'], $error['vars'], WATCHDOG_ERROR);
    }
  }
  if (!empty($fieldsToNull)) {
    $object->fieldsToNull = $fieldsToNull;
  }
  return $object;
}