You are here

entity_share_medias_file.import.inc in Entity Share 7

Class for handling scald Medias in RTE.

File

modules/entity_share_medias/modules/entity_share_medias_file/includes/entity_share_medias_file.import.inc
View source
<?php

/**
 * @file
 * Class for handling scald Medias in RTE.
 */

/**
 * Class for file import.
 */
class EntityShareMediasFileImport extends EntityShareMediasFileAbstract implements EntityShareMediasImportInterface {
  const WATCHDOG_TYPE = 'entity_share_import_file';

  /**
   * Import the medias of the provider.
   */
  public function importMedias() {
    if ($this
      ->isRteField()) {
      $this
        ->rteManagement();
    }
    else {
      $this
        ->fieldManagement();
    }
  }

  /**
   * Alter RTE datas.
   */
  protected function rteManagement() {
  }

  /**
   * Manage field media reference content.
   */
  protected function fieldManagement() {
    $file = (object) $this->fieldData;
    $uuid = $this
      ->getUuid($file);
    $fid = $this
      ->importFile($file, $uuid, $file->file_url);
    $this->fieldData['fid'] = $fid;
  }

  /**
   * Import the scald Atom.
   *
   * @param object $file
   *   File object.
   * @param string $uuid
   *   UUID of the file.
   * @param string $media_url
   *   URL of the remote file.
   *
   * @return int
   *   Scald atom id.
   */
  protected function importFile($file, $uuid, $media_url) {

    // Test if file already exists.
    $fid = $this
      ->getFileIdFromUuid($uuid);

    // Update the id with existing id if exists.
    if (isset($fid)) {
      $file->fid = $fid;
      $local_file = file_load($fid);
      $local_file_name = $local_file->filename;
    }
    else {

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

    // If the fileexists, get the media from the url if available.
    if (!empty($file->file_url)) {

      // Create the file if not exists or the filename is different.
      if (!isset($local_file_name) || isset($local_file_name) && $local_file_name != $file->filename) {
        $file = $this
          ->createFile($media_url, (object) $file);
      }
    }
    if (isset($file)) {
      if (!isset($fid)) {
        $fid = $file->fid;
      }

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

    // Return the fid.
    return $fid;
  }

}

Classes

Namesort descending Description
EntityShareMediasFileImport Class for file import.