You are here

class EntityShareMediasScaldImport in Entity Share 7

Class to manage scald atoms import.

Hierarchy

Expanded class hierarchy of EntityShareMediasScaldImport

1 string reference to 'EntityShareMediasScaldImport'
entity_share_medias_scald_es_medias_media_types_config_alter in modules/entity_share_medias/modules/entity_share_medias_scald/entity_share_medias_scald.module
Implements hook_entity_share_medias_scald_es_medias_types_config_alter().

File

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

View source
class EntityShareMediasScaldImport extends EntityShareMediasScaldAbstract implements EntityShareMediasImportInterface {
  const WATCHDOG_TYPE = 'entity_share_import_scald';

  /**
   * Import the medias of the provider.
   */
  public function importMedias() {

    // Alter the field_data.
    if ($this
      ->isRteField()) {
      $this
        ->rteManagement();
    }
    else {
      $this
        ->fieldManagement();
    }
  }

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

    // Treatment of the RTE data.
    $this
      ->rteEmbeddedMedia($this->fieldData, $this
      ->getImportRteFieldCallback(), '/\\[scald=(.+?):([^\\s]+)?\\s+url=(.+?)\\sorisid=(.+?)\\s+entity_share=1\\s(.*)]/');
  }

  /**
   * Manage field media reference content.
   */
  protected function fieldManagement() {

    // Case of invalid scald atoms that can't be
    // loaded in the remote system.
    if (empty($this->fieldData) || empty($this->fieldData['sid'])) {
      return;
    }
    $scald_atom = (object) $this->fieldData;
    $uuid = $this
      ->getUniqueIdFromAtom($scald_atom);
    $sid = $this
      ->importScaldAtom($scald_atom, $uuid, $scald_atom->file_url);
    $this->fieldData = NULL;
    $this->fieldData['sid'] = $sid;
  }

  /**
   * Generate the callback to treat the scald content from the field data.
   *
   * @return callable
   *   The callback to treat RTE.
   */
  protected function getImportRteFieldCallback() {
    $instance = $this;
    return function ($matches, &$field_value) use ($instance) {
      foreach ($matches as $match) {
        $full_pattern_match = $match[0];
        $uuid = $match[1];
        $context = $match[2];
        $url = $match[3];
        $orisid = $match[4];
        $infos = $match[5];
        $scald_atom = $instance->entity->entity_share['scald_atoms'][$orisid];
        if (!empty($scald_atom)) {
          $sid = $instance
            ->importScaldAtom((object) $scald_atom, $uuid, $url);

          // Replace sid per file uuid (alter the original
          // field_value, be careful with all references).
          $field_value = str_replace($full_pattern_match, $instance
            ->generateImportRteMediaString($sid, $context, $infos), $field_value);

          // Replace remote copyright id.
          $instance
            ->manageCopyrights($orisid, $sid, $field_value);
        }
      }
    };
  }

  /**
   * Generate the import string for the scald atom.
   *
   * @param int $sid
   *   Scald atom id.
   * @param string $context
   *   Scald context.
   * @param string $info
   *   Scald additional informations.
   *
   * @return string
   *   The import string.
   */
  protected function generateImportRteMediaString($sid, $context, $info) {
    return <<<IMPORT
[scald={<span class="php-variable">$sid</span>}:{<span class="php-variable">$context</span>} {<span class="php-variable">$info</span>}]
IMPORT;
  }

  /**
   * Manage the copyrights to update them with the new ids.
   *
   * @param int $orisid
   *   Original sid (remote).
   * @param int $sid
   *   New sid (local).
   * @param string $field_value
   *   String to patch.
   */
  protected function manageCopyrights($orisid, $sid, &$field_value) {
    $search = array(
      "<!--copyright={$orisid}-->",
      "<!--END copyright={$orisid}-->",
    );
    $replace = array(
      "<!--copyright={$sid}-->",
      "<!--END copyright={$sid}-->",
    );
    $field_value = str_replace($search, $replace, $field_value);
  }

  /**
   * Import the scald Atom.
   *
   * @param object $scald_atom
   *   Scald atom.
   * @param string $uuid
   *   UUID of the scald atom.
   * @param string $media_url
   *   The remote URL of the Scald atom media.
   *
   * @return int
   *   Scald atom id.
   *
   * @throws EntityShareImportException
   *   Throw an exception in case of problem when importing atom.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityShareMediasAbstract::$entity protected property Entity object.
EntityShareMediasAbstract::$fieldData protected property Field data.
EntityShareMediasAbstract::$fieldInfo protected property Metadatas of the field.
EntityShareMediasAbstract::$fieldName protected property Name of the field.
EntityShareMediasAbstract::$fieldType protected property Type of the field.
EntityShareMediasAbstract::__construct public function Constructor. Initialize properties.
EntityShareMediasProviderAbstract::$rteKeys protected property Values to treat for embedded RTE.
EntityShareMediasProviderAbstract::createFile protected function Create the file.
EntityShareMediasProviderAbstract::getRelativePathFromUrl protected function Get the relative drupal stream path from a full drupal URL.
EntityShareMediasProviderAbstract::isManagedFieldType public function Check if the field type is managed. Overrides EntityShareMediasProviderInterface::isManagedFieldType
EntityShareMediasProviderAbstract::isRteField protected function Check if RTE Field.
EntityShareMediasProviderAbstract::rteEmbeddedMedia protected function Treatment on the RTE field to match fid, etc.
EntityShareMediasScaldAbstract::$managedFieldTypes protected property Types of field handled by the current class. Overrides EntityShareMediasProviderAbstract::$managedFieldTypes
EntityShareMediasScaldAbstract::CUSTOM_UUID_PREFIX constant
EntityShareMediasScaldAbstract::getScaldAtomFromProvider protected function Get the scald atom from provider and base id.
EntityShareMediasScaldAbstract::getScaldIdFromBaseEntityUuid public function Get the scald atom id from base entity uuid.
EntityShareMediasScaldAbstract::getScaldIdFromUuid public function Get a scald Id from a UUID.
EntityShareMediasScaldAbstract::getUniqueIdFromAtom public function Generate manually a uuid from a scald atom.
EntityShareMediasScaldAbstract::isCustomUuid public function Is custom UUID.
EntityShareMediasScaldImport::fieldManagement protected function Manage field media reference content. Overrides EntityShareMediasProviderAbstract::fieldManagement
EntityShareMediasScaldImport::generateImportRteMediaString protected function Generate the import string for the scald atom.
EntityShareMediasScaldImport::getImportRteFieldCallback protected function Generate the callback to treat the scald content from the field data.
EntityShareMediasScaldImport::importMedias public function Import the medias of the provider. Overrides EntityShareMediasImportInterface::importMedias
EntityShareMediasScaldImport::importScaldAtom protected function Import the scald Atom.
EntityShareMediasScaldImport::manageCopyrights protected function Manage the copyrights to update them with the new ids.
EntityShareMediasScaldImport::rteManagement protected function Alter RTE datas. Overrides EntityShareMediasProviderAbstract::rteManagement
EntityShareMediasScaldImport::WATCHDOG_TYPE constant Overrides EntityShareMediasProviderAbstract::WATCHDOG_TYPE