entity_share_medias_scald.import.inc in Entity Share 7
Class for handling scald Medias in RTE.
File
modules/entity_share_medias/modules/entity_share_medias_scald/includes/entity_share_medias_scald.import.incView source
<?php
/**
* @file
* Class for handling scald Medias in RTE.
*/
/**
* Class to manage scald atoms import.
*/
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;
}
}
Classes
Name | Description |
---|---|
EntityShareMediasScaldImport | Class to manage scald atoms import. |