entity_share_medias.inc in Entity Share 7
Class for handling Medias.
File
modules/entity_share_medias/includes/entity_share_medias.incView source
<?php
/**
* @file
* Class for handling Medias.
*/
/**
* Interface EntityShareMediasProviderInterface.
*/
interface EntityShareMediasProviderInterface {
/**
* Check if the field type is managed or not.
*
* @return bool
* Check if the field type is managed.
*/
public function isManagedFieldType();
}
/**
* Interface EntityShareMediasExportInterface.
*/
interface EntityShareMediasExportInterface extends EntityShareMediasProviderInterface {
/**
* Export the medias.
*/
public function exportMedias();
}
/**
* Interface EntityShareMediasImportInterface.
*/
interface EntityShareMediasImportInterface extends EntityShareMediasProviderInterface {
/**
* Import the medias.
*/
public function importMedias();
}
/**
* Class EntityShareMediasAbstract.
*/
abstract class EntityShareMediasAbstract {
/**
* Field data.
*
* @var array
* Data of the field.
*/
protected $fieldData;
/**
* Name of the field.
*
* @var string
* Machine name of the field.
*/
protected $fieldName;
/**
* Type of the field.
*
* @var string
* Type of the field.
*/
protected $fieldType;
/**
* Entity object.
*
* @var object
* Entity.
*/
protected $entity;
/**
* Metadatas of the field.
*
* @var array
* Field info.
*/
protected $fieldInfo;
/**
* Constructor. Initialize properties.
*
* @param array $field_data
* Datas of the field.
* @param string $field_name
* Name of the field.
* @param string $field_type
* Type of the field.
* @param object $entity
* Entity to export/import.
* @param array $field_info
* Informations o the field.
*/
public function __construct(array &$field_data, $field_name, $field_type, $entity, array $field_info) {
$this->fieldData =& $field_data;
$this->fieldName = $field_name;
$this->fieldType = $field_type;
$this->entity = $entity;
$this->fieldInfo = $field_info;
}
}
/**
* Class EntityShareMediasProviderAbstract.
*/
abstract class EntityShareMediasProviderAbstract extends EntityShareMediasAbstract implements EntityShareMediasProviderInterface {
const WATCHDOG_TYPE = 'entity_share_medias';
/**
* Type of field managed by the class.
*
* @var array
* Type of field types managed.
*/
protected $managedFieldTypes = array(
'text',
'text_long',
'text_with_summary',
);
/**
* Values to treat for embedded RTE.
*
* @var array
* Value sub dimensions to treat for embedded medias in RTE.
*/
protected $rteKeys = array(
'value',
'safe_value',
'summary',
'safe_summary',
);
/**
* Check if RTE Field.
*
* @return bool
* TRUE if RTE Field, FALSE otherwise.
*/
protected function isRteField() {
return $this->fieldInfo['module'] == 'text';
}
/**
* Check if the field type is managed.
*
* @return bool
* TRUE if the field type is managed, FALSE otherwise.
*/
public function isManagedFieldType() {
return in_array($this->fieldType, $this->managedFieldTypes);
}
/**
* Manage RTE content.
*/
protected abstract function rteManagement();
/**
* Manage field media reference content.
*/
protected abstract function fieldManagement();
/**
* Treatment on the RTE field to match fid, etc.
*
* @param array $field_data
* Data of the field for a language.
* @param Closure $callback
* Callback called for each embedded medias.
* @param string $pattern
* Pattern to match in th RTE data to find embedded media.
*/
protected function rteEmbeddedMedia(array &$field_data, Closure $callback, $pattern = '/"fid":"(.+?)"/') {
foreach ($field_data as $key => &$value) {
if (in_array($key, $this->rteKeys)) {
// Match all the embedded files.
if (preg_match_all($pattern, $value, $matches, PREG_SET_ORDER)) {
$callback($matches, $value);
}
}
}
}
/**
* Create the file.
*
* @param string $url
* Url of the remote file.
* @param object $file_entity
* Object of the original file.
* @param string $filename
* Name of the file.
*
* @return bool|object
* File.
*/
protected function createFile($url, $file_entity, $filename = NULL) {
if (empty($filename)) {
$filename = drupal_basename($url);
}
// Get file folder.
$relative_path = $this
->getRelativePathFromUrl($url);
$file_scheme_dir = file_default_scheme() . '://' . $relative_path;
// Get the media.
$media = drupal_http_request($url);
if ($media->code == 200 && file_prepare_directory($file_scheme_dir, FILE_CREATE_DIRECTORY)) {
$dest = $file_scheme_dir . '/' . $filename;
$file = file_save_data($media->data, $dest, FILE_EXISTS_REPLACE);
if ($file) {
// Set original uuid.
$entity_info = entity_get_info('file');
$uuid_key = $entity_info['entity keys']['uuid'];
$file->{$uuid_key} = $file_entity->{$uuid_key};
file_save($file);
// Not added by file_save and necessary to have
// dynamic type in the code.
$file->entity_type = 'file';
return $file;
}
else {
return FALSE;
}
}
else {
watchdog(static::WATCHDOG_TYPE, "The file %file couldn't be created !", array(
'%file' => $file_scheme_dir . '/' . $filename,
), WATCHDOG_ERROR);
return FALSE;
}
}
/**
* Get the relative drupal stream path from a full drupal URL.
*
* @param string $url
* Url of the media.
*
* @return string
* Relative path of the media.
*/
protected function getRelativePathFromUrl($url) {
$path = parse_url($url, PHP_URL_PATH);
$path = explode('/', $path);
// Remove filename.
array_pop($path);
$relative_path = array();
for ($i = count($path) - 1; $i > 0; $i--) {
if ($path[$i] == 'files') {
break;
}
$relative_path[] = $path[$i];
}
$relative_path = implode('/', array_reverse($relative_path));
return $relative_path;
}
}
/**
* Class EntityShareMedias.
*/
class EntityShareMedias extends EntityShareMediasAbstract {
const WATCHDOG_TYPE = 'entity_share_medias';
const HOOK_PREFIX = 'es_medias_';
/**
* Configuration of the medias to manage.
*
* @var array
* Class configuration of the medias to handle.
*/
protected $mediaTypesConfig = array();
/**
* Get the Media Types Configuration.
*
* @return array
* Class configuration of the medias to handle.
*/
public function getMediaTypesConfig() {
// Alter the media types configurations.
drupal_alter(self::HOOK_PREFIX . 'media_types_config', $this->mediaTypesConfig);
return $this->mediaTypesConfig;
}
/**
* Export the medias of the providers.
*/
public function exportMedias() {
foreach ($this
->getMediaTypesConfig() as $type => $config) {
$class = $config['export'];
$provider = new $class($this->fieldData, $this->fieldName, $this->fieldType, $this->entity, $this->fieldInfo);
if (!$provider instanceof EntityShareMediasExportInterface) {
watchdog(self::WATCHDOG_TYPE, 'The media type %type class must implement EntityShareMediasProviderInterface interface !', array(
'%type' => $type,
), WATCHDOG_WARNING);
continue;
}
if ($provider
->isManagedFieldType()) {
$provider
->exportMedias();
}
}
}
/**
* Import the medias of the providers.
*/
public function importMedias() {
foreach ($this
->getMediaTypesConfig() as $type => $config) {
$class = $config['import'];
$provider = new $class($this->fieldData, $this->fieldName, $this->fieldType, $this->entity, $this->fieldInfo);
if (!$provider instanceof EntityShareMediasImportInterface) {
watchdog(self::WATCHDOG_TYPE, 'The media type %type class must implement EntityShareMediasProviderInterface interface !', array(
'%type' => $type,
), WATCHDOG_WARNING);
continue;
}
if ($provider
->isManagedFieldType()) {
$provider
->importMedias();
}
}
}
}
Classes
Name![]() |
Description |
---|---|
EntityShareMedias | Class EntityShareMedias. |
EntityShareMediasAbstract | Class EntityShareMediasAbstract. |
EntityShareMediasProviderAbstract | Class EntityShareMediasProviderAbstract. |
Interfaces
Name![]() |
Description |
---|---|
EntityShareMediasExportInterface | Interface EntityShareMediasExportInterface. |
EntityShareMediasImportInterface | Interface EntityShareMediasImportInterface. |
EntityShareMediasProviderInterface | Interface EntityShareMediasProviderInterface. |