You are here

function scald_search in Scald: Media Management made easy 7

Same name and namespace in other branches
  1. 6 scald.module \scald_search()

Find Atoms matching a given set of characteristics.

Parameters

array $query: A keyed array with one or more of the following keys:

  • 'provider'
  • 'type'
  • 'base_id'
  • 'publisher'
  • 'actions'
  • 'title'.

bool $fuzzy: A boolean that if set to TRUE broadens the search to include partial matches on all parameters.

bool $singular: A boolean that if set to TRUE ensures that only one result is returned. The return value is a single SID (scalar, not in an array).

Return value

mixed An array of SIDs that match the characteristics specified FALSE on bad input or no results

1 call to scald_search()
ScaldBaseTestCase::testScaldSearch in tests/scald.test
Test Scald search.

File

./scald.module, line 762
The Scald Core, which handles all Scald Registries and dispatch.

Code

function scald_search($query = array(), $fuzzy = FALSE, $singular = FALSE) {

  // Validate the arguments.
  if (empty($query) || !is_array($query)) {
    return FALSE;
  }
  $types = scald_types();
  $providers = scald_atom_providers();
  if (!$fuzzy && !empty($query['type']) && !is_array($query['type']) && !in_array($query['type'], array_keys($types))) {
    unset($query['type']);
  }
  if (!empty($query['providers']) && !in_array($query['provider'], $providers)) {
    unset($query['provider']);
  }

  // Ensure that there's still some filters left.
  if (empty($query)) {
    return FALSE;
  }
  $efq = new EntityFieldQuery();
  $efq
    ->entityCondition('entity_type', 'scald_atom');
  foreach ($query as $field => $value) {
    switch ($field) {
      case 'title':
      case 'type':
      case 'base_id':
      case 'provider':
        if (is_array($value) || !$fuzzy) {
          $efq
            ->propertyCondition($field, $value);
        }
        else {
          $efq
            ->propertyCondition($field, '%' . $value . '%', 'LIKE');
        }
        break;
      case 'publisher':
      case 'actions':
        $efq
          ->propertyCondition($field, $value);
        break;
    }
  }
  if ($singular) {
    $efq
      ->range(0, 1);
  }
  $results = $efq
    ->execute();

  // Compose and return result.
  $sids = !empty($results['scald_atom']) ? array_keys($results['scald_atom']) : array();
  if (!count($sids)) {
    return FALSE;
  }
  return $singular ? $sids[0] : $sids;
}