You are here

function services_client_data_process in Services Client 7

This function takes inbound data objects and a type and determines if there are tasks for them. If there are, it checks conditions and then generates connections and organizes the data to pass to the calling functions

Parameters

object $src_data: This is the source data object. Typically a $node or $user object

string $type: This is an identifier for the $src_data object. Options are: node_save, user_save

string $name: Optionally fire only specific hook.

10 calls to services_client_data_process()
drush_services_client_sync_nodes in ./services_client.drush.inc
Sync all nodes to specific connection
drush_services_client_sync_users in ./services_client.drush.inc
Sync all user references to insight
services_ccc_node_send in services_ccc/services_ccc.module
Get the remote connections listed in the node and send to them
services_client_node_delete in ./services_client.module
Implements hook_node_delete().
services_client_node_insert in ./services_client.module
Implements of hook_node_insert().

... See full list

File

./services_client.module, line 385
Services client module allows to push different types of objects on different types of events such as node_save, user_save to remote masters.

Code

function services_client_data_process($src_data, $type, $name = NULL) {
  ctools_include('export');

  // If sync is not forced and some module is not allowing to sync data
  // return from function
  if (services_client_sync_exclude($src_data, services_client_get_data_type($type))) {
    return TRUE;
  }

  // Module can process data in queue if data are comming from remote site. To
  // determine remote data use _services_client property which is added by services_client
  // module on remote origin site.
  if (services_client_queue_data($src_data, $type)) {
    return;
  }

  // Allow other modules to introduce their own variables and allow them to
  // alter the src_data object. An example would be to remove the visted array
  // and move it to visited so we can get backwards compatibility or if we know
  // that data will be altered and we do not want to stop the processing. Eg.
  // remove a value from the visited array.
  $data_type = services_client_get_data_type($type);
  drupal_alter('services_client_data', $src_data, $data_type);

  // If origin of data is current site and data are going to be sent to remote
  // recipients add _services_client property to tell remote site that data
  // are synced by services_client module.
  $src_data->_services_client = isset($src_data->_services_client) ? $src_data->_services_client : array();
  $src_data->_services_client['origin'] = services_client_get_id();
  $src_data->_services_client['visted'] = isset($src_data->_services_client['visted']) ? $src_data->_services_client['visted'] : array();

  // Don't send 'queued' control key to remote site, let it handle queueing by themselves
  unset($src_data->_services_client['queued']);
  if (!in_array(services_client_get_id(), $src_data->_services_client['visted'])) {
    $src_data->_services_client['visted'][] = services_client_get_id();
  }
  else {
    watchdog('sc', "Possible loop in system. Type: @type, data: <pre>@data</pre>", array(
      '@type' => $type,
      '@data' => print_r($src_data, TRUE),
    ));
    return FALSE;
  }

  // Declare some of the arrays we'll be using.
  $tasks = array();
  $conns = array();
  $hooks = array();
  $hooks_all = array();

  // Get all enabled hooks
  $hooks_all = services_client_get_existing_hooks();

  // API call can limit firing only specific hook
  if (!empty($name)) {
    if (!empty($hooks_all[$name])) {
      $hooks[] = array(
        $hooks_all[$name],
      );
    }
  }
  else {
    foreach ($hooks_all as $hook) {
      if (isset($hook->hook) && $hook->hook == $type) {
        $hooks[] = $hook;
      }
    }
  }
  foreach ($hooks as $hook) {
    $conds = $hook->config['condition']['config'];
    $mappings = $hook->config['mapping']['config'];

    // If the type is node_save, and there are conditions...
    if (in_array($type, array(
      'node_save',
      'node_delete',
    )) && count($conds) > 0) {
      if ($conds['published'] != 'e' && $conds['published'] != $src_data->status) {
        continue;
      }
      if ($conds['node_type'] != $src_data->type) {
        continue;
      }
    }

    // Check if connection has been already visited
    $connection = services_client_connection_load($hook->conn_name);
    if (isset($connection->services_client_id) && in_array($connection->services_client_id, $src_data->_services_client['visted'])) {
      watchdog('sc', 'Loop prevention. Not sending type: @type to connection @conn. data: <pre>@data</pre>', array(
        '@type' => $type,
        '@data' => print_r($src_data, TRUE),
        '@conn' => $connection->name,
      ));
      continue;
    }

    // Generate the tasks and conns
    $tasks[$hook->conn_name][] = $hook;
  }

  // Store results in array
  $results = array(
    'success' => array(),
    'errors' => array(),
  );
  if (count($tasks) > 0) {
    foreach ($tasks as $hook_conn) {
      foreach ($hook_conn as $task) {
        $success = TRUE;

        // Operation result.
        $result = array(
          'data' => $src_data,
          'type' => services_client_get_data_type($type),
          'hook' => $type,
          'task' => $task,
          'entity_type' => services_client_get_data_type($type),
          'entity_id' => services_client_get_data_id($src_data, $type),
        );
        module_invoke_all('sc_process_data', $result['entity_type'], $src_data, $task);
        try {
          services_client_make_call($src_data, $type, $task);
        } catch (ServicesClientConnectionResponseException $e) {
          $e
            ->log();
          $success = FALSE;
          $result += array(
            'code' => $e
              ->getErrorCode(),
            'message' => $e
              ->getErrorMessage(),
          );
        }

        // Store operation result for further processing.
        $results[$success ? 'success' : 'errors'][] = $result;
      }
    }
  }
  if (count($results['errors'])) {
    services_client_process_errors($results['errors']);
  }
  return $results;
}