You are here

function _services_raw_node_update in Services Client 7

Same name and namespace in other branches
  1. 7.2 services_raw/services_raw.inc \_services_raw_node_update()

Update node calling raw node_save.

Parameters

$nid: Nid of saved node

$node: Array of node that needs to be saved

1 string reference to '_services_raw_node_update'
_services_raw_services_resources in services_raw/services_raw.inc
Provides API definition of provided services objects and operations.

File

services_raw/services_raw.inc, line 469
Custom services definition and implementation of all callbacks.

Code

function _services_raw_node_update($nid, $node) {

  // Load original node resource
  module_load_include('inc', 'services', 'resources/node_resource');

  // Adds backwards compatability with regression fixed in #1083242
  $node = _services_arg_value($node, 'node');
  $node['nid'] = $nid;
  $old_node = node_load($nid);
  if (empty($old_node->nid)) {
    return services_error(t('Node @nid not found', array(
      '@nid' => $nid,
    )), 404);
  }

  // Add missing attributes from original node
  $node += (array) $old_node;

  // If revision is not included load revision of existing node
  if (empty($node['vid']) && $old_node->vid) {
    $node['vid'] = $old_node->vid;
  }

  // Assign username to the node from $user created at auth step.
  if (isset($node['name']) && !isset($node['uid'])) {
    $account = user_load_by_name($node['name']);
    if (!empty($account)) {
      $node['uid'] = $account->uid;
    }
    else {
      unset($node['name']);
    }
  }
  if (!isset($node['name']) && !isset($node['uid'])) {
    global $user;
    $node['name'] = $user->name;
    $node['uid'] = $user->uid;
  }

  // Validate the node. If there is validation error Exception will be thrown
  // so code below won't be executed.
  _node_resource_validate_type($node);

  // Check if call isn't trying to change node type
  if ($old_node->type != $node['type']) {
    return services_error(t("You can't change node type via services"), 406);
  }
  try {

    // Prepare node defaults
    $node = (object) $node;

    // Prevent loops
    // this is apparently over-zealous as it stands and prevents insight
    // from getting updated at all.

    //$node->_services_client['visted'][] = services_client_get_id();
    node_object_prepare($node);
    node_save($node);
    if (empty($node->nid)) {
      return services_error(t('Error when saving node.'), 406);
    }
  } catch (Exception $e) {
    return services_error(t('Error when saving node.'), 406, array(
      'error' => $e
        ->getMessage(),
    ));
  }
  $result = array(
    'nid' => $node->nid,
  );
  if ($uri = services_resource_uri(array(
    'node',
    $node->nid,
  ))) {
    $result['uri'] = $uri;
  }
  return $result;
}