You are here

function services_client_make_node_call in Services Client 7

Make the actual node create/update call for each connection and task

Parameters

object $node: This is the node object which was just saved

object $task: Contains the field mappings and other data

Return value

boolean TRUE or FALSE for whether or not the request was successful

1 call to services_client_make_node_call()
services_client_make_call in ./services_client.module
Make call to remote site by event $type

File

./services_client.module, line 704
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_make_node_call($node, $task) {

  // Generate our connection object. If false, then we failed login.
  $client = services_client_connection_get($task->conn_name);

  // Lets load up that mapping and do our thing
  $conds = $task->config['condition']['config'];
  $mapping = $task->config['mapping']['config'];
  $fields = explode("\n", $mapping['field_mapping']);
  $fields_empty = services_client_process_mapping_prepare_empty(isset($mapping['field_mapping_empty']) ? $mapping['field_mapping_empty'] : array());
  $types = explode("\n", $mapping['node_type_mapping']);

  // Process the field mapping and assign to the data object we are passing to services server
  $node_data = services_client_process_mapping($node, $fields, $fields_empty);

  // Override field mapping of types to allow substitution/transformation
  if (count($types) > 0) {
    $node_data->type = services_client_type_override($types, $node->type);
  }

  // UUID is always passed and is deliberately overridden
  $node_data->uuid = $node->uuid;

  // Get remote UID
  $account = user_load($node->uid);
  $uid = services_client_scalar_result($client
    ->get('uuid', 'user', array(
    'uuid' => $account->uuid,
  )));
  if (is_numeric($uid) && !empty($uid)) {
    $node_data->uid = $uid;
  }
  if (isset($node->_services_client)) {
    $node_data->_services_client = $node->_services_client;
  }

  // Node data needs to be an array containing an object.
  $data = (array) $node_data;

  // TODO DEBUGGING REMOVE ME
  watchdog('sc_node', 'Sending node to %conn: <pre>@node</pre>', array(
    '%conn' => $task->conn_name,
    '@node' => print_r($data, TRUE),
  ));

  // Find out if there is already an object on the master server with this UUID
  $nid = services_client_scalar_result($client
    ->get('uuid', 'node', array(
    'uuid' => $node->uuid,
  )));

  // We have a result. We need to update the node
  if (is_numeric($nid) && !empty($nid)) {
    watchdog('sc_node', 'Got @nid for uuid @uuid', array(
      '@nid' => $nid,
      '@uuid' => $node->uuid,
    ));

    // Update the node on the services master
    $client
      ->update('node_raw', $nid, $data);
  }
  else {

    // Create the node on the services master
    $client
      ->create('node_raw', $data);
  }
}