View source
<?php
namespace Drupal\imce;
class ImceFolder extends ImceItem {
public $type = 'folder';
public $conf;
public $scanned;
public $items = [];
public $files = [];
public $subfolders = [];
public function __construct($name, array $conf = NULL) {
parent::__construct($name);
$this
->setConf($conf);
}
public function getConf() {
if (isset($this->conf)) {
return $this->conf;
}
if ($parent = $this->parent) {
if ($conf = $parent
->getConf()) {
if (Imce::permissionInFolderConf('browse_subfolders', $conf)) {
return $conf + [
'inherited' => TRUE,
];
}
}
}
}
public function setConf(array $conf = NULL) {
$this->conf = $conf;
}
public function getPermission($name) {
return Imce::permissionInFolderConf($name, $this
->getConf());
}
public function setPath($path) {
$oldpath = $this->path;
if ($path !== $oldpath) {
if (isset($oldpath)) {
unset($this
->fm()->tree[$oldpath]);
foreach ($this->subfolders as $name => $item) {
$item
->setPath(NULL);
}
}
$this->path = $path;
if (isset($path)) {
$this
->fm()->tree[$path] = $this;
foreach ($this->subfolders as $name => $item) {
$item
->setPath(Imce::joinPaths($path, $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;
}
}
public function checkItem($name) {
if (!($item = $this
->getItem($name))) {
if (!$this->scanned) {
$this
->scan();
$item = $this
->getItem($name);
}
}
return $item;
}
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;
}
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;
}
}
public function createItem($type, $name, $conf = NULL) {
$item = $this
->fm()
->createItem($type, $name, $conf);
$this
->appendItem($item);
return $item;
}
public function addFile($name) {
return $this
->createItem('file', $name);
}
public function addSubfolder($name, $conf = NULL) {
return $this
->createItem('folder', $name, $conf);
}
public function isPredefined() {
return isset($this->conf);
}
public function hasPredefinedPath() {
if ($this
->isPredefined()) {
return $this;
}
foreach ($this->subfolders as $folder) {
if ($folder = $folder
->hasPredefinedPath()) {
return $folder;
}
}
return FALSE;
}
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);
$this->files = $this->items = $content['files'];
$subfolders = $this->subfolders;
$this->subfolders = [];
foreach ($content['subfolders'] as $name => $uri) {
if (isset($subfolders[$name]) && is_object($subfolders[$name])) {
$this->subfolders[$name] = $this->items[$name] = $subfolders[$name];
}
else {
$this
->addSubfolder($name);
}
}
}
}
}