You are here

function scald_youtube_register in Scald YouTube 7

Scald_youtube_register.

Creates an atom based on a video ID or an object containing the video informations.

Parameters

mixed $video: Unique identifier of the video on YouTube, or object returned by scald_youtube_video.

Return value

int Unique identifier of the new atom.

File

./scald_youtube.module, line 392
Defines a YouTube provider for Scald.

Code

function scald_youtube_register($video) {

  // Fetch the needed informations from YouTube.
  if (is_object($video)) {
    $infos = $video;
  }
  else {
    $infos = scald_youtube_video($video);
  }

  // Check if the video has already been imported to prevent duplicate.
  $old = scald_youtube_already_imported($infos->id);
  if ($old) {
    return FALSE;
  }

  // Download a copy of the video thumbnail. This makes it possible
  // to do interesting manipulation with image styles presets.
  $thumb = drupal_http_request($infos->thumbnail['src']);
  $dir = 'public://youtube';
  if ($thumb->code == 200 && file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) {
    $dest = $dir . '/' . $infos->id . '.jpg';
    $file = file_save_data($thumb->data, $dest);
  }

  // Create an atom.
  $atom = new ScaldAtom('video', 'scald_youtube', array(
    'base_id' => $infos->id,
    'title' => $infos->title,
  ));

  // Save video width and height.
  if (!isset($atom->data)) {
    $atom->data = array();
  }
  $atom->data['video_width'] = $infos->width;
  $atom->data['video_height'] = $infos->height;

  // Set file.
  if ($file) {
    db_update('file_managed')
      ->condition('fid', $file->fid)
      ->fields(array(
      'status' => 0,
    ))
      ->execute();
    $langcode = field_language('scald_atom', $atom, 'scald_thumbnail');
    $atom->scald_thumbnail[$langcode][0] = (array) $file;
  }

  // And save it.
  $atom_sid = scald_atom_save($atom);

  // Finally, return this id.
  return $atom_sid;
}