You are here

function scald_search in Scald: Media Management made easy 6

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

Find Atoms matching a given set of characteristics.

Parameters

$query: A keyed array with one or more of the following keys: 'provider' 'type' 'base_id' 'publisher' 'actions' 'title'

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

$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

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

4 calls to scald_search()
mee_field in mee/mee.module
Implementation of hook_field().
scald_composite_nodeapi in scald_composite/scald_composite.module
Implementation of hook_nodeapi().
scald_dailymotion_already_imported in scald_dailymotion/scald_dailymotion.module
Checks if a video has already been imported, based on its video id.
scald_example_content_type_node_load in scald_example_content_type/scald_example_content_type.module
Implements hook_node_load (D7)

File

./scald.module, line 1475

Code

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

  // Argument validation
  if (empty($query) || !is_array($query)) {
    return FALSE;
  }
  $scald_config = variable_get('scald_config', 0);
  $scald_providers = variable_get('scald_providers', 0);
  if (!$fuzzy && !empty($query['type']) && !is_array($query['type']) && !in_array($query['type'], array_keys($scald_config->types))) {
    unset($query['type']);
  }
  if (!empty($query['providers']) && !in_array($query['provider'], $scald_providers)) {
    unset($query['provider']);
  }

  // Build the query & execute
  $wheres = array();
  $data = array();
  foreach ($query as $field => $value) {
    switch ($field) {
      case 'title':
      case 'type':
      case 'base_id':
      case 'provider':
        $placeholder = $fuzzy ? "'%%%s%%'" : "'%s'";
        if (is_array($value)) {
          $condition = db_placeholders($value, 'text');
          $wheres[] = $field . " IN({$condition})";
          $data = array_merge($data, $value);
        }
        else {
          if ($fuzzy) {
            $wheres[] = $field . " LIKE '%%%s%%'";
          }
          else {
            $wheres[] = $field . " = '%s'";
          }
          $data[] = $value;
        }
        break;

      // end 'title/type/base_id/provider'
      case 'publisher':
        $wheres[] = $field . " = %d";
        $data[] = $value;
        break;

      // end 'publisher'
      case 'actions':
        break;

      // end 'actions'
      default:
        continue;
    }
  }

  // @@@TODO: Use a pager_query() call here?
  $search_results = db_query("\n      SELECT\n        sid\n      FROM\n        {scald_atoms}\n      WHERE " . implode(' AND ', $wheres) . ($singular ? " LIMIT 1" : ''), $data);

  // Compose and return results
  if (!$search_results) {
    return FALSE;
  }
  if ($singular) {
    return db_result($search_results);
  }
  $sids = array();
  while ($sid_raw = db_fetch_array($search_results)) {
    $sids[] = $sid_raw['sid'];
  }
  return $sids;
}