You are here

function _sf_notifications_handle_message in Salesforce Suite 7.2

Same name and namespace in other branches
  1. 6.2 sf_notifications/sf_notifications.module \_sf_notifications_handle_message()

Loop through an array of SObjects from Salesforce and save them according to any existing sf fieldmaps, notification settings, and data.

Parameters

array $objects: A numerically indexed array of SObjects (as returned by _sf_notifications_parse_message())

Return value

(boolean) FALSE if there were errors. TRUE otherwise.

See also

sf_notifications_fieldmap_settings()

sf_notifications_settings_form()

1 call to _sf_notifications_handle_message()
_sf_notifications_parse_handle_message in sf_notifications/sf_notifications.module
Parses and handles an outbound message from Salesforce.

File

sf_notifications/sf_notifications.module, line 182

Code

function _sf_notifications_handle_message($objects) {
  $success = TRUE;

  // For each object received from Salesforce, gather all relevant fieldmaps.
  // For each relevant fieldmap, perform the appropriate C(r)UD operation.
  $new_records = $objects['salesforce'];
  $active = variable_get('sf_notifications_active_maps', array());
  $active = array_filter($active);
  foreach ($objects['drupal'] as $object_record) {
    $sfid = $object_record['sfid'];
    $obj = $objects['salesforce'][$sfid];

    // We'll handle inserts later on
    unset($new_records[$sfid]);

    // Break on fieldmap-specific conditions
    $map = salesforce_api_salesforce_fieldmap_load($object_record['name']);
    if (empty($active[$map->name])) {
      continue;
    }
    $operation = $obj->fields->IsDeleted == 'true' ? 'delete' : 'update';
    $object_record['fields'] = $obj->fields;
    $object_record['operation'] = $operation;

    // Check if any implementations of hook_sf_notifications_check_condition()
    // tell us we can't process this item with this map.
    $results = module_invoke_all('sf_notifications_check_condition', $operation, $object_record, $map);
    foreach ($results as $result) {
      if (!$result) {
        continue 2;
      }
    }
    switch ($operation) {
      case 'delete':
        $success = $success && sf_notifications_delete_record($object_record);
        break;
      case 'update':
        $success = $success && sf_notifications_update_record($object_record);
        break;
    }
  }
  foreach ($new_records as $sfid => $obj) {
    $maps = salesforce_api_salesforce_fieldmap_load_by(array(
      'salesforce' => $obj->type,
    ));
    if (empty($maps)) {
      salesforce_api_log(SALESFORCE_LOG_SOME, 'Salesforce Notifications: No fieldmap found.
            <pre>' . print_r($obj, TRUE) . '</pre>');
      $success = FALSE;
      continue;
    }

    // For each map, check active, check conditions and insert.
    foreach ($maps as $map) {
      if (empty($active[$map->name])) {
        continue;
      }

      // Forge an object record to proceed.
      // Insert is the same as update, just without an oid.
      $object_record = array(
        'oid' => NULL,
        'name' => $map->name,
        'drupal_entity' => $map->drupal_entity,
        'drupal_bundle' => $map->drupal_bundle,
        'fields' => $obj->fields,
        'operation' => 'insert',
      );

      // Check if any implementations of hook_sf_notifications_check_condition()
      // tell us we can't process this item with this map.
      $results = module_invoke_all('sf_notifications_check_condition', 'insert', $object_record, $map);
      foreach ($results as $result) {
        if (!$result) {
          continue 2;
        }
      }
      $success = $success && sf_notifications_update_record($object_record);
    }
  }
  return $success;
}