You are here

public function RestWSEntityResourceController::query in RESTful Web Services 7.2

Implements RestWSQueryResourceControllerInterface::query().

Overrides RestWSQueryResourceControllerInterface::query

File

./restws.entity.inc, line 237
RESTful web services module integration for entities.

Class

RestWSEntityResourceController
Controller for entity-bases resources.

Code

public function query($filters = array(), $meta_controls = array()) {
  $limit = variable_get('restws_query_max_limit', 100);
  $offset = 0;
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', $this->entityType);
  foreach ($filters as $filter => $value) {
    $this
      ->propertyQueryOperation($query, 'Condition', $filter, $value);
  }
  $rest_controls = restws_meta_controls();
  foreach ($meta_controls as $control_name => $value) {
    switch ($control_name) {
      case $rest_controls['sort']:
        if (isset($meta_controls[$rest_controls['direction']]) && strtolower($meta_controls[$rest_controls['direction']]) == 'desc') {
          $direction = 'DESC';
        }
        else {
          $direction = 'ASC';
        }
        $this
          ->propertyQueryOperation($query, 'OrderBy', $value, $direction);
        break;
      case $rest_controls['limit']:
        $limit = $this
          ->limit($value);
        break;
      case $rest_controls['page']:
        $offset = $value > 0 ? $value : $offset;
        break;
    }
  }

  // Calculate the offset.
  $offset *= $limit;
  $query
    ->range($offset, $limit);
  $this
    ->nodeAccess($query);

  // Catch any errors, like wrong keywords or properties.
  try {
    $query_result = $query
      ->execute();
  } catch (PDOException $exception) {
    throw new RestWSException('Query failed.', 400);
  }
  $query_result = isset($query_result[$this->entityType]) ? $query_result[$this->entityType] : array();
  $result = array_keys($query_result);
  return $result;
}