You are here

class ImceFolder in IMCE 8

Same name and namespace in other branches
  1. 8.2 src/ImceFolder.php \Drupal\imce\ImceFolder

Imce Folder.

Hierarchy

Expanded class hierarchy of ImceFolder

4 files declare their use of ImceFolder
DeleteTest.php in tests/src/Kernel/Plugin/ImcePlugin/DeleteTest.php
ImceFolderTest.php in tests/src/Unit/ImceFolderTest.php
ImceFolderTest.php in tests/src/Kernel/ImceFolderTest.php
ResizeTest.php in tests/src/Kernel/Plugin/ImcePlugin/ResizeTest.php

File

src/ImceFolder.php, line 8

Namespace

Drupal\imce
View source
class ImceFolder extends ImceItem {

  /**
   * {@inheritdoc}
   */
  public $type = 'folder';

  /**
   * Inheritable folder configuration including permissions.
   *
   * @var array
   */
  public $conf;

  /**
   * Scan status.
   *
   * @var bool
   */
  public $scanned;

  /**
   * Items.
   *
   * @var array
   */
  public $items = [];

  /**
   * Files.
   *
   * @var array
   */
  public $files = [];

  /**
   * Subfolders.
   *
   * @var array
   */
  public $subfolders = [];

  /**
   * Constructs the folder.
   *
   * @param string $name
   *   Folder name.
   * @param array $conf
   *   Folder configuration.
   */
  public function __construct($name, array $conf = NULL) {
    parent::__construct($name);
    $this
      ->setConf($conf);
  }

  /**
   * Returns folder configuration.
   */
  public function getConf() {
    if (isset($this->conf)) {
      return $this->conf;
    }

    // Inherit parent conf.
    if ($parent = $this->parent) {
      if ($conf = $parent
        ->getConf()) {
        if (Imce::permissionInFolderConf('browse_subfolders', $conf)) {
          return $conf + [
            'inherited' => TRUE,
          ];
        }
      }
    }
  }

  /**
   * Sets folder configuration.
   */
  public function setConf(array $conf = NULL) {
    $this->conf = $conf;
  }

  /**
   * Returns a permission value.
   */
  public function getPermission($name) {
    return Imce::permissionInFolderConf($name, $this
      ->getConf());
  }

  /**
   * Sets the folder path.
   */
  public function setPath($path) {
    $oldpath = $this->path;
    if ($path !== $oldpath) {

      // Remove oldpath references.
      if (isset($oldpath)) {
        unset($this
          ->fm()->tree[$oldpath]);
        foreach ($this->subfolders as $name => $item) {
          $item
            ->setPath(NULL);
        }
      }

      // Add new path references.
      $this->path = $path;
      if (isset($path)) {
        $this
          ->fm()->tree[$path] = $this;
        foreach ($this->subfolders as $name => $item) {
          $item
            ->setPath(Imce::joinPaths($path, $name));
        }
      }
    }
  }

  /**
   * Returns an item by name.
   */
  public function getItem($name) {
    if (isset($this->items[$name])) {
      $item = $this->items[$name];
      if (!is_object($item)) {
        $item = isset($this->subfolders[$name]) ? $this
          ->addSubfolder($name) : $this
          ->addFile($name);
      }
      return $item;
    }
  }

  /**
   * Returns an item by name.
   *
   * Scans the folder if needed.
   */
  public function checkItem($name) {
    if (!($item = $this
      ->getItem($name))) {
      if (!$this->scanned) {
        $this
          ->scan();
        $item = $this
          ->getItem($name);
      }
    }
    return $item;
  }

  /**
   * Appends an item to the item list.
   */
  public function appendItem(ImceItem $item) {
    $parent = $item->parent;
    if ($item !== $this && $parent !== $this) {
      if ($parent) {
        $parent
          ->removeItem($item);
      }
      $item->parent = $this;
      $name = $item->name;
      $this->items[$name] = $item;
      if ($item->type === 'folder') {
        $this->subfolders[$name] = $item;
        $path = ($this->parent ? $this
          ->getPath() . '/' : '') . $name;
        $item
          ->setPath($path);
      }
      else {
        $this->files[$name] = $item;
      }
    }
    return $item;
  }

  /**
   * Removes an item from the item list.
   */
  public function removeItem(ImceItem $item) {
    if ($this === $item->parent) {
      $item
        ->deselect();
      $item->parent = NULL;
      $name = $item->name;
      unset($this->items[$name]);
      if ($item->type === 'folder') {
        unset($this->subfolders[$name]);
        $item
          ->setPath(NULL);
      }
      else {
        unset($this->files[$name]);
      }
      return $item;
    }
  }

  /**
   * Creates and returns a child file/folder object by name.
   */
  public function createItem($type, $name, $conf = NULL) {
    $item = $this
      ->fm()
      ->createItem($type, $name, $conf);
    $this
      ->appendItem($item);
    return $item;
  }

  /**
   * Adds a file by name.
   */
  public function addFile($name) {
    return $this
      ->createItem('file', $name);
  }

  /**
   * Adds a subfolder by name.
   */
  public function addSubfolder($name, $conf = NULL) {
    return $this
      ->createItem('folder', $name, $conf);
  }

  /**
   * Checks if the folder is predefined.
   */
  public function isPredefined() {
    return isset($this->conf);
  }

  /**
   * Returns the first predefined descendent including itself.
   */
  public function hasPredefinedPath() {
    if ($this
      ->isPredefined()) {
      return $this;
    }
    foreach ($this->subfolders as $folder) {
      if ($folder = $folder
        ->hasPredefinedPath()) {
        return $folder;
      }
    }
    return FALSE;
  }

  /**
   * Scans folder content.
   */
  public function scan() {
    if (!$this->scanned) {
      $this->scanned = TRUE;
      $options = [
        'browse_files' => $this
          ->getPermission('browse_files'),
        'browse_subfolders' => $this
          ->getPermission('browse_subfolders'),
      ];
      $content = $this
        ->fm()
        ->scanDir($this
        ->getUri(), $options);

      // Add files as raw data. We create the objects when needed.
      $this->files = $this->items = $content['files'];

      // Create the subfolder objects.
      $subfolders = $this->subfolders;
      $this->subfolders = [];
      foreach ($content['subfolders'] as $name => $uri) {

        // Check if previously created.
        if (isset($subfolders[$name]) && is_object($subfolders[$name])) {
          $this->subfolders[$name] = $this->items[$name] = $subfolders[$name];
        }
        else {
          $this
            ->addSubfolder($name);
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ImceFolder::$conf public property Inheritable folder configuration including permissions.
ImceFolder::$files public property Files.
ImceFolder::$items public property Items.
ImceFolder::$scanned public property Scan status.
ImceFolder::$subfolders public property Subfolders.
ImceFolder::$type public property Item type. Overrides ImceItem::$type
ImceFolder::addFile public function Adds a file by name.
ImceFolder::addSubfolder public function Adds a subfolder by name.
ImceFolder::appendItem public function Appends an item to the item list.
ImceFolder::checkItem public function Returns an item by name.
ImceFolder::createItem public function Creates and returns a child file/folder object by name.
ImceFolder::getConf public function Returns folder configuration.
ImceFolder::getItem public function Returns an item by name.
ImceFolder::getPermission public function Returns a permission value.
ImceFolder::hasPredefinedPath public function Returns the first predefined descendent including itself.
ImceFolder::isPredefined public function Checks if the folder is predefined.
ImceFolder::removeItem public function Removes an item from the item list.
ImceFolder::scan public function Scans folder content.
ImceFolder::setConf public function Sets folder configuration.
ImceFolder::setPath public function Sets the folder path.
ImceFolder::__construct public function Constructs the folder. Overrides ImceItem::__construct
ImceItem::$fm protected property File manager.
ImceItem::$name public property Item name.
ImceItem::$parent public property Item parent.
ImceItem::$path protected property Item path relative to the root.
ImceItem::$selected public property Selected status.
ImceItem::addToJs public function Adds the item to js.
ImceItem::deselect public function Deselects the item.
ImceItem::fm public function Returns the file manager.
ImceItem::getPath public function Returns the item path relative to the root.
ImceItem::getUri public function Returns the item uri.
ImceItem::remove public function Removes the item from its parent.
ImceItem::removeFromJs public function Removes the item from js.
ImceItem::select public function Selects the item.
ImceItem::setFm public function Sets the file manager.