You are here

function scald_author_get_id in Scald: Media Management made easy 6

Get an Author ID from Scald Core. This will first check if an author matching the $data passed as an argument exists, and return it if it does. If it doesn't, then it will try to create a new author from this data.

Parameters

$data: A keyed array with the following possible keys: 'name', the full name; no first/last/etc. distinction. (required) 'uid', A Drupal User ID. (optional) 'url', A URL by which to find out more about the Author. (optional) NOTE: If this element is omitted for an array which has 'uid' set, the path 'user/<uid>' is assumed and supplied. Set 'url' to an empty string to avoid this behavior.

Return value

The Author ID upon success. FALSE upon failure

1 call to scald_author_get_id()
scald_dailymotion_register in scald_dailymotion/scald_dailymotion.module
Creates an atom based on a DailyMotion video id or an object containing the video informations..

File

./scald.module, line 1590

Code

function scald_author_get_id($data = array()) {

  // Validate arguments
  if (empty($data) || empty($data['name'])) {
    return FALSE;
  }

  // Build SQL conditions
  $sets = array();
  $sql_data = array();
  foreach ($data as $field => $value) {
    switch ($field) {
      case 'name':
        $sets[] = 'name = \'%s\'';
        $sql_data[] = $value;
        break;

      // end 'name'
      case 'uid':
        $sets[] = 'uid = %d';
        $sql_data[] = $value;
        if (!isset($data['url'])) {
          $sets[] = 'url = \'' . url('user') . '/%d\'';
          $sql_data[] = $value;
        }
        break;

      // end 'uid'
      case 'url':
        $sets[] = 'url = \'%s\'';
        $sql_data[] = $value;
        break;

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

  // Look for an existing author in the database.
  $aid = db_result(db_query("\n      SELECT aid FROM\n        {scald_authors}\n      WHERE " . implode(' AND ', $sets), $sql_data));
  if ($aid) {
    return $aid;
  }

  // If no author matched those criteria, then, try to insert a new one.
  if (!db_query("\n      INSERT INTO\n        {scald_authors}\n      SET " . implode(', ', $sets), $sql_data)) {
    return FALSE;
  }
  return db_last_insert_id('scald_authors', 'aid');
}