Skip to content
Snippets Groups Projects

Issue #1910294: Ability to filter with comparison parameters (smaller/larger than)

Open Avi Schwab requested to merge issue/restws-1910294:1910294-ability-to-filter into 7.x-2.x
Files
4
+ 119
14
@@ -242,7 +242,7 @@ class RestWSEntityResourceController implements RestWSQueryResourceControllerInt
$query->entityCondition('entity_type', $this->entityType);
foreach ($filters as $filter => $value) {
$this->propertyQueryOperation($query, 'Condition', $filter, $value);
$this->propertyQueryCondition($query, $filter, $value);
}
$rest_controls = restws_meta_controls();
@@ -255,7 +255,7 @@ class RestWSEntityResourceController implements RestWSQueryResourceControllerInt
else {
$direction = 'ASC';
}
$this->propertyQueryOperation($query, 'OrderBy', $value, $direction);
$this->propertyQueryOrderBy($query, $value, $direction);
break;
case $rest_controls['limit']:
@@ -296,7 +296,7 @@ class RestWSEntityResourceController implements RestWSQueryResourceControllerInt
$query->entityCondition('entity_type', $this->entityType);
foreach ($filters as $filter => $value) {
$this->propertyQueryOperation($query, 'Condition', $filter, $value);
$this->propertyQueryCondition($query, $filter, $value);
}
$query->count();
$this->checkAccess($query);
@@ -326,7 +326,7 @@ class RestWSEntityResourceController implements RestWSQueryResourceControllerInt
if ($this->resource() == 'node') {
$query->addTag('node_access');
if (!user_access('bypass node access')) {
$this->propertyQueryOperation($query, 'Condition', 'status', 1);
$this->propertyQueryCondition($query, 'status', 1);
}
}
// Respect user access and filter out blocked users if user lacks
@@ -363,13 +363,54 @@ class RestWSEntityResourceController implements RestWSQueryResourceControllerInt
/**
* Helper function which takes care of distinguishing between fields and
* entity properties and executes the right EntityFieldQuery function for it.
* entity properties for query order-by clauses and executes the right
* EntityFieldQuery function for it.
*
* @param EntityFieldQuery $query
* The EntityFieldQuery pointer which should be used.
*
* @param string $operation
* The general function name, without the words 'property' or 'field'.
* @param string $property
* The property or field which should be used.
*
* @param string|array $value
* The value for the function.
*/
protected function propertyQueryOrderBy(EntityFieldQuery $query, $property, $value) {
$properties = $this->propertyInfo();
// If field is not set, then the filter is a property and we can extract
// the schema field from the property array.
if (empty($properties[$property]['field'])) {
$column = $properties[$property]['schema field'];
$query->propertyOrderBy($column, $value);
}
else {
// For fields we need the field info to get the right column for the
// query.
$field_info = field_info_field($property);
if (is_array($value)) {
// Specific column filters are given, so add a query condition for each
// one of them.
foreach ($value as $column => $val) {
$query->fieldOrderBy($field_info, $column, $value);
}
}
else {
// Just pick the first field column for the operation.
$columns = array_keys($field_info['columns']);
$column = $columns[0];
$query->fieldOrderBy($field_info, $column, $value);
}
}
}
/**
* Helper function which takes care of distinguishing between fields and
* entity properties for query conditions and executes the right EntityFieldQuery
* function for it.
*
* @param EntityFieldQuery $query
* The EntityFieldQuery pointer which should be used.
*
* @param string $property
* The property or field which should be used.
@@ -377,7 +418,7 @@ class RestWSEntityResourceController implements RestWSQueryResourceControllerInt
* @param string|array $value
* The value for the function.
*/
protected function propertyQueryOperation(EntityFieldQuery $query, $operation, $property, $value) {
protected function propertyQueryCondition(EntityFieldQuery $query, $property, $value) {
$properties = $this->propertyInfo();
// Check property access.
@@ -391,28 +432,92 @@ class RestWSEntityResourceController implements RestWSQueryResourceControllerInt
// the schema field from the property array.
if (empty($properties[$property]['field'])) {
$column = $properties[$property]['schema field'];
$operation = 'property' . $operation;
$query->$operation($column, $value);
$condition = $this->conditionValueComponents($value);
if (is_array($value)) {
// Operators.
foreach ($value as $operator => $operator_value) {
$oper = $this->conditionValueComponents(array($operator => $operator_value));
$query->propertyCondition($column, $oper['value'], $oper['operator']);
}
}
else {
$query->propertyCondition($column, $condition['value'], $condition['operator']);
}
}
else {
// For fields we need the field info to get the right column for the
// query.
$field_info = field_info_field($property);
$operation = 'field' . $operation;
if (is_array($value)) {
// Specific column filters are given, so add a query condition for each
// one of them.
foreach ($value as $column => $val) {
$query->$operation($field_info, $column, $val);
if (!is_array($val)) {
// No operator given so we assume =
if (trim($column) == 'format') {
$query->fieldCondition($field_info, $column, $val);
}
else {
$columns = array_keys($field_info['columns']);
$column = $columns[0];
$condition = $this->conditionValueComponents($value);
$query->fieldCondition($field_info, $column, $condition['value'], $condition['operator']);
}
}
else {
foreach ($val as $oper => $ovalue) {
$condition = $this->conditionValueComponents(array($oper => $ovalue));
$query->fieldCondition($field_info, $column, $condition['value'], $condition['operator']);
}
}
}
}
else {
// Just pick the first field column for the operation.
$columns = array_keys($field_info['columns']);
$column = $columns[0];
$query->$operation($field_info, $column, $value);
$condition = $this->conditionValueComponents($value);
$query->fieldCondition($field_info, $column, $condition['value'], $condition['operator']);
}
}
}
protected function conditionValueComponents($value) {
$allowed_operators = array(
// Orders.
'le' => '<=',
'lt' => '<',
'gt' => '>',
'ge' => '>=',
// Equality.
'eq' => '=',
'ne' => '<>',
// Strings.
'sw' => 'STARTS_WITH',
'ct' => 'CONTAINS',
);
if (variable_get('restws_filter_operators', FALSE)) {
if (is_array($value)) {
$operator = key($value);
$value = reset($value);
}
else {
$operator = 'eq';
}
if (!isset($allowed_operators[$operator])) {
throw new RestWSException(sprintf('"%s" is not a valid operator.', $operator), 400);
}
}
else {
$operator = 'eq';
}
return array(
'operator' => $allowed_operators[$operator],
'value' => $value,
);
}
/**
@@ -476,4 +581,4 @@ class RestWSEntityResourceController implements RestWSQueryResourceControllerInt
}
}
}
}
\ No newline at end of file
Loading