You are here

public function ServicesEntityResourceController::index in Services Entity API 7.2

Implements ServicesResourceControllerInterface::index().

Overrides ServicesResourceControllerInterface::index

1 call to ServicesEntityResourceController::index()
ServicesEntityResourceControllerClean::index in plugins/services_entity_resource_clean.inc
Implements ServicesResourceControllerInterface::index().
1 method overrides ServicesEntityResourceController::index()
ServicesEntityResourceControllerClean::index in plugins/services_entity_resource_clean.inc
Implements ServicesResourceControllerInterface::index().

File

plugins/services_entity_resource.inc, line 112

Class

ServicesEntityResourceController
Generic controller for entity-bases resources.

Code

public function index($entity_type, $fields, $parameters, $page, $pagesize, $sort, $direction) {

  // Make sure the pagesize is not too large.
  $max_pagesize = variable_get('services_entity_max_pagesize', 100);
  $pagesize = $max_pagesize < $pagesize ? $max_pagesize : $pagesize;

  // Build an EFQ based on the arguments.
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', $entity_type)
    ->range($page * $pagesize, $pagesize);
  if (!empty($parameters)) {
    foreach ($parameters as $field => $value) {
      $this
        ->propertyQueryOperation($entity_type, $query, 'Condition', $field, $value);
    }
  }
  if ($sort != '') {
    $direction = $direction == 'DESC' ? 'DESC' : 'ASC';

    // Ensure a valid direction
    $this
      ->propertyQueryOperation($entity_type, $query, 'OrderBy', $sort, $direction);
  }
  $result = $query
    ->execute();
  if (empty($result)) {
    return services_error(t('No entities found.'), 404);
  }

  // Convert to actual entities.
  $entities = entity_load($entity_type, array_keys($result[$entity_type]));
  foreach ($entities as $id => $entity) {
    if (entity_access('view', $entity_type, $entity)) {

      // Users get special treatment to remove sensitive data.
      if ($entity_type == 'user') {

        // Use the helper that Services module already has.
        services_remove_user_data($entity);
      }
      $return[] = $this
        ->limit_fields($entity, $fields);
    }
  }

  // The access check may have resulted in there being no entities left.
  if (empty($return)) {
    return services_error(t('No entities found.'), 404);
  }
  return $return;
}