You are here

protected function EntityShareMediasScaldImport::importScaldAtom in Entity Share 7

Import the scald Atom.

Parameters

object $scald_atom: Scald atom.

string $uuid: UUID of the scald atom.

string $media_url: The remote URL of the Scald atom media.

Return value

int Scald atom id.

Throws

EntityShareImportException Throw an exception in case of problem when importing atom.

1 call to EntityShareMediasScaldImport::importScaldAtom()
EntityShareMediasScaldImport::fieldManagement in modules/entity_share_medias/modules/entity_share_medias_scald/includes/entity_share_medias_scald.import.inc
Manage field media reference content.

File

modules/entity_share_medias/modules/entity_share_medias_scald/includes/entity_share_medias_scald.import.inc, line 141
Class for handling scald Medias in RTE.

Class

EntityShareMediasScaldImport
Class to manage scald atoms import.

Code

protected function importScaldAtom($scald_atom, $uuid, $media_url) {

  // Test if scald atom already exists.
  $sid = $this
    ->getScaldIdFromUuid($uuid);

  // Update the id with existing id if exists.
  if (isset($sid)) {
    $scald_atom->sid = $sid;
    $local_scald_atom = scald_atom_load($sid);
    $local_file_name = isset($local_scald_atom->base_entity->filename) ? $local_scald_atom->base_entity->filename : NULL;
    $local_fid = isset($local_scald_atom->base_entity->fid) ? $local_scald_atom->base_entity->fid : NULL;
  }
  else {

    // Empty the remote sid to create a local one.
    $scald_atom->sid = NULL;
  }

  // If the base_entity exists, get the media from the url if available.
  if (!empty($scald_atom->base_entity)) {

    // Create the file if not exists or the filename is different.
    if (!isset($local_file_name) || (isset($local_file_name) && $local_file_name != $scald_atom->base_entity['filename'] || empty($local_fid))) {
      $file = $this
        ->createFile($media_url, (object) $scald_atom->base_entity);
      if (empty($file)) {
        watchdog(self::WATCHDOG_TYPE, "Couldn't import base entity for scald_atom");
        throw new EntityShareImportException(t("Couldn't import base entity for scald_atom"));
      }
      else {

        // Update the base_id, the base_entity,
        // the file_source , thumbnail_source etc.
        if ($scald_atom->base_id == $scald_atom->base_entity['fid']) {
          $scald_atom->base_id = $file->fid;
        }
        $scald_atom->base_entity = $file;
        $scald_atom->file_source = $file->uri;

        // Scald will regenerate the thumbnail if unset.
        unset($scald_atom->scald_thumbnail[$scald_atom->language]);
        unset($scald_atom->thumbnail_source);
        if (isset($scald_atom->data['fid'])) {
          $scald_atom->data['fid'] = $file->fid;
        }
      }
    }
  }

  // @todo Manage custom fields.
  // @todo Warning: "scald_thumbnail" is treated specifically depending on the provider.
  // @todo Warning: for example scald_image provider will automatically sync scald_thumbnail and base_entity.
  // @todo Expose data for provider import sub module (scald way).
  // Save the atom.
  $sid = scald_atom_save($scald_atom);
  if (!empty($file)) {

    // Update the file_usage table to keep the relation with the sid.
    $file_usage_list = file_usage_list($file);
    if (!isset($file_usage_list['file']['scald_atom'][$sid])) {
      file_usage_add($file, $scald_atom->base_entity->entity_type, 'scald_atom', $sid);
    }
  }
  return $sid;
}