You are here

function _services_raw_fields_update in Services Client 7

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

Update entity calling array of _save functions.

Parameters

$uuid: UUID of saved entity

$entity_type: Currently node or user.

$field: Field of entity that needs to be saved

$data: Array of fields with their respective values that needs to be saved

1 string reference to '_services_raw_fields_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 376
Custom services definition and implementation of all callbacks.

Code

function _services_raw_fields_update($uuid, $entity_type, $data) {
  switch ($entity_type) {
    case 'node':
      $id_name = 'nid';
      $id = uuid_node_find($uuid);
      $save_functions = array(
        'node_object_prepare',
        'node_save',
      );
      break;
    case 'user':
      $id_name = 'uid';
      $id = uuid_user_find($uuid);
      $save_functions = array(
        'user_save',
      );
      break;
    default:
      return services_error(t('Entity type @type not supported', array(
        '@type' => $entity_type,
      )), 404);
  }
  module_load_include('inc', 'services', 'resources/' . $entity_type . '_resource');
  $entity_list = entity_load($entity_type, array(
    $id,
  ));
  if (is_array($entity_list)) {
    $entity = reset($entity_list);
  }
  if (empty($entity->{$id_name})) {
    return services_error(t('@type @id not found', array(
      '@type' => $entity_type,
      '@id' => $id,
    )), 404);
  }

  // Get all the fields for this entity type
  $fields_info = field_info_instances($entity_type, $entity->type);

  // Loop over all the new updates
  foreach ($data as $new_field => $new_field_values) {

    // Validate that the passed field is on this entity
    $math = FALSE;
    foreach (array_keys($fields_info) as $cur_field) {
      if ($cur_name == $new_field) {
        $match == TRUE;
        continue;
      }
    }

    // We need to find all fields that were given
    if ($match == FALSE) {
      return services_error(t('Field @field not found', array(
        '@field' => $new_field,
      )), 404);
    }

    // Prepare entity defaults
    $lang = field_language($entity_type, $entity, $new_field);

    // Get field values in a numeric list
    $i = 0;
    $field_value = NULL;
    foreach ($new_field_values as $new_field_value) {
      $field_value[$i]['value'] = $new_field_value;
      $i++;
    }
    $entity->{$new_field}[$lang] = $field_value;
  }

  // Register where we come from
  $entity->_services_client['visted'][] = services_client_get_id();
  try {

    // Save the data
    foreach ($save_functions as $function) {
      $function($entity);
    }

    // If the identifier cannot be found, quit with a notice
    if (empty($entity->{$id_name})) {
      return services_error(t('Error when saving entity.'), 406);
    }
  } catch (Exception $e) {
    return services_error(t('Error when saving entity.'), 406, array(
      'error' => $e
        ->getMessage(),
    ));
  }
  $result = array(
    $id => $entity->{$id_name},
  );
  if ($uri = services_resource_uri(array(
    $entity_type,
    $entity->{$id_name},
  ))) {
    $result['uri'] = $uri;
  }
  return $result;
}