You are here

function field_test_field_storage_query in SimpleTest 7

Implement hook_field_storage_query().

File

tests/field_test.module, line 924

Code

function field_test_field_storage_query($field_id, $conditions, $count, &$cursor = NULL, $age) {
  $data = _field_test_storage_data();
  $load_current = $age == FIELD_LOAD_CURRENT;
  $field = field_info_field_by_id($field_id);
  $field_columns = array_keys($field['columns']);
  $field_data = $data[$field['id']];
  $sub_table = $load_current ? 'current' : 'revisions';

  // We need to sort records by object type and object id.
  usort($field_data[$sub_table], '_field_test_field_storage_query_sort_helper');

  // Initialize results array.
  $return = array();
  $obj_count = 0;
  $rows_count = 0;
  $rows_total = count($field_data[$sub_table]);
  $skip = $cursor;
  $skipped = 0;
  foreach ($field_data[$sub_table] as $row) {
    if ($count != FIELD_QUERY_NO_LIMIT && $obj_count >= $count) {
      break;
    }
    if ($row->field_id == $field['id']) {
      $match = TRUE;
      $condition_deleted = FALSE;

      // Add conditions.
      foreach ($conditions as $condition) {
        @(list($column, $value, $operator) = $condition);
        if (empty($operator)) {
          $operator = is_array($value) ? 'IN' : '=';
        }
        switch ($operator) {
          case '=':
            $match = $match && $row->{$column} == $value;
            break;
          case '!=':
          case '<':
          case '<=':
          case '>':
          case '>=':
            eval('$match = $match && ' . $row->{$column} . ' ' . $operator . ' ' . $value);
            break;
          case 'IN':
            $match = $match && in_array($row->{$column}, $value);
            break;
          case 'NOT IN':
            $match = $match && !in_array($row->{$column}, $value);
            break;
          case 'BETWEEN':
            $match = $match && $row->{$column} >= $value[0] && $row->{$column} <= $value[1];
            break;
          case 'STARTS_WITH':
          case 'ENDS_WITH':
          case 'CONTAINS':

            // Not supported.
            $match = FALSE;
            break;
        }

        // Track condition on 'deleted'.
        if ($column == 'deleted') {
          $condition_deleted = TRUE;
        }
      }

      // Exclude deleted data unless we have a condition on it.
      if (!$condition_deleted && $row->deleted) {
        $match = FALSE;
      }
      if ($match) {
        if (is_null($skip) || $skipped >= $skip) {
          $cursor++;

          // If querying all revisions and the entity type has revisions, we need
          // to key the results by revision_ids.
          $entity_type = field_info_fieldable_types($row->type);
          $id = $load_current || empty($entity_type['object keys']['revision']) ? $row->entity_id : $row->revision_id;
          if (!isset($return[$row->type][$id])) {
            $return[$row->type][$id] = field_create_stub_entity($row->type, array(
              $row->entity_id,
              $row->revision_id,
              $row->bundle,
            ));
            $obj_count++;
          }
        }
        else {
          $skipped++;
        }
      }
    }
    $rows_count++;

    // The query is complete if we walked the whole array.
    if ($count != FIELD_QUERY_NO_LIMIT && $rows_count >= $rows_total) {
      $cursor = FIELD_QUERY_COMPLETE;
    }
  }
  return $return;
}