You are here

entity_share_medias_scald.export.inc in Entity Share 7

Class for handling scald Medias Export.

File

modules/entity_share_medias/modules/entity_share_medias_scald/includes/entity_share_medias_scald.export.inc
View source
<?php

/**
 * @file
 * Class for handling scald Medias Export.
 */

/**
 * Class for scald atoms export.
 */
class EntityShareMediasScaldExport extends EntityShareMediasScaldAbstract implements EntityShareMediasExportInterface {

  /**
   * Load Scald Atom.
   *
   * @param int $sid
   *   ScaldID.
   *
   * @return object
   *   Scald atom.
   */
  protected function loadScaldAtom($sid) {
    $scald_atom = scald_atom_load($sid);

    // Add entity_share property to the entity.
    $this->entity->entity_share->scald_atoms[$sid] = $scald_atom;
    return $scald_atom;
  }

  /**
   * Export the medias of the provider.
   */
  public function exportMedias() {

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

  /**
   * Alter RTE datas.
   */
  protected function rteManagement() {
    $this
      ->rteEmbeddedMedia($this->fieldData, $this
      ->getExportRteFieldCallback(), '/\\[scald=([0-9]+):([^\\s]+)?(.*?)]/');
  }

  /**
   * Manage field media reference content.
   */
  protected function fieldManagement() {
    if (is_object($this->fieldData)) {
      $this->fieldData = (array) $this->fieldData;
    }
    $sid = $this->fieldData['sid'];
    if (!empty($sid)) {
      $scald_atom = $this
        ->loadScaldAtom($sid);
      if (!empty($scald_atom->base_entity)) {

        // Add the file url in a property of the scald object.
        $scald_atom->file_url = file_create_url($scald_atom->base_entity->uri);
      }
      $this->fieldData = $scald_atom;
    }
  }

  /**
   * Generate the callback to treat the scald content from the field data.
   *
   * @return callable
   *   The callback to treat RTE.
   */
  protected function getExportRteFieldCallback() {
    $instance = $this;
    return function ($matches, &$field_value) use ($instance) {
      foreach ($matches as $match) {
        $full_pattern_match = $match[0];
        $sid = $match[1];
        $context = $match[2];
        $infos = $match[3];

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

  /**
   * Generate the export string for the scald atom.
   *
   * @param int $sid
   *   Scald atom id.
   * @param string $context
   *   Scald context.
   * @param string $infos
   *   Scald additional information.
   *
   * @return string
   *   The export string.
   */
  protected function generateExportRteMediaString($sid, $context, $infos) {

    // Load scald atom.
    $scald_atom = $this
      ->loadScaldAtom($sid);

    // Uuid.
    $uuid = $this
      ->getUniqueIdFromAtom($scald_atom);

    // Add the file url in a dimension of the object.
    $file_url = '';
    if (!empty($scald_atom->base_entity)) {
      $file_url = file_create_url($scald_atom->base_entity->uri);
    }
    return <<<EXPORT
[scald={<span class="php-variable">$uuid</span>}:{<span class="php-variable">$context</span>} url={<span class="php-variable">$file_url</span>} orisid={<span class="php-variable">$sid</span>} entity_share=1 {<span class="php-variable">$infos</span>}]
EXPORT;
  }

}

Classes

Namesort descending Description
EntityShareMediasScaldExport Class for scald atoms export.