You are here

abstract class EntityShareMediasProviderAbstract in Entity Share 7

Class EntityShareMediasProviderAbstract.

Hierarchy

Expanded class hierarchy of EntityShareMediasProviderAbstract

File

modules/entity_share_medias/includes/entity_share_medias.inc, line 118
Class for handling Medias.

View source
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;
  }

}

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::$managedFieldTypes protected property Type of field managed by the class. 2
EntityShareMediasProviderAbstract::$rteKeys protected property Values to treat for embedded RTE.
EntityShareMediasProviderAbstract::createFile protected function Create the file.
EntityShareMediasProviderAbstract::fieldManagement abstract protected function Manage field media reference content. 4
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.
EntityShareMediasProviderAbstract::rteManagement abstract protected function Manage RTE content. 4
EntityShareMediasProviderAbstract::WATCHDOG_TYPE constant 2