152 lines
3.7 KiB
PHP
152 lines
3.7 KiB
PHP
<?php
|
|
|
|
class DocumentManager
|
|
{
|
|
/**
|
|
* @var DocTarget
|
|
*/
|
|
private $target;
|
|
|
|
/**
|
|
* @var DocProvider
|
|
*/
|
|
private $provider;
|
|
|
|
public function __construct(DocTarget $target)
|
|
{
|
|
$this->target = $target;
|
|
$this->provider = new DbDocProvider();
|
|
}
|
|
|
|
public function createDir($parent, $name)
|
|
{
|
|
$parent = $parent ? $this->provider->getDirById($this->target, $parent) : null;
|
|
$this->provider->createDir($this->target, $name, $parent);
|
|
}
|
|
|
|
public function saveFileTo($name, $path, $parent = 0)
|
|
{
|
|
$parent = $parent ? $this->provider->getDirById($this->target, $parent) : null;
|
|
return $this->provider->createFile($this->target, $name, $path, $parent);
|
|
}
|
|
|
|
public function rename($id, $newName)
|
|
{
|
|
if (!trim($newName)) {
|
|
throw new \InvalidArgumentException('New name is empty');
|
|
}
|
|
|
|
if ($entity = $this->provider->getEntityById($this->target, $id)) {
|
|
$entity->name = $newName;
|
|
$entity->saveNewName();
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @param $id
|
|
* @return bool
|
|
* @throws \Exception
|
|
* @throws DocNotFoundException
|
|
*/
|
|
public function delete($id)
|
|
{
|
|
$entity = $this->provider->getEntityById($this->target, $id);
|
|
|
|
if ($entity->isDir()) {
|
|
$children = $this->provider->getChildren($this->target, $entity->id);
|
|
foreach ($children as $child) {
|
|
$this->delete($child->id);
|
|
}
|
|
}
|
|
|
|
return (bool)$entity->delete();
|
|
}
|
|
|
|
/**
|
|
* Удалить содержимое каталога
|
|
*/
|
|
public function cleanDir()
|
|
{
|
|
/** @var DocCollection $collection */
|
|
$collection = $this->getChildren();
|
|
/** @var File $file */
|
|
foreach ($collection->getFiles() as $file) {
|
|
$this->delete($file->getId());
|
|
}
|
|
/** @var Dir $dir */
|
|
foreach ($collection->getDirs() as $dir) {
|
|
$this->delete($dir->getId());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return DocTarget
|
|
*/
|
|
public function getTarget()
|
|
{
|
|
return $this->target;
|
|
}
|
|
|
|
public function getChildren($dirId = 0)
|
|
{
|
|
$files = [];
|
|
$dirs = [];
|
|
|
|
$documents = $this->provider->getChildren($this->target, $dirId);
|
|
|
|
foreach ($documents as $doc) {
|
|
if ($doc->isDir()) {
|
|
$dirs[] = new Dir($doc->id, $doc->name, $doc->parent);
|
|
} else {
|
|
$files[] = new File($doc->id, $doc->name, $doc->file, $doc->parent);
|
|
}
|
|
}
|
|
return new DocCollection($dirs, $files);
|
|
}
|
|
|
|
/**
|
|
* Врзыращает массив родительских папок
|
|
* @param $dirId
|
|
* @return array
|
|
*/
|
|
public function getParents($dirId)
|
|
{
|
|
try {
|
|
$result = [];
|
|
while ($dir = $this->provider->getDirById($this->target, $dirId)) {
|
|
$result[] = [
|
|
'name' => $dir->name,
|
|
'id' => $dir->id,
|
|
];
|
|
$dirId = $dir->parent;
|
|
}
|
|
} catch (DocNotFoundException $e) {
|
|
array_shift($result);
|
|
|
|
return array_reverse($result);
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* @param $id
|
|
* @return Dir
|
|
* @throws DocNotFoundException
|
|
*/
|
|
public function getDir($id)
|
|
{
|
|
if ((int)$id === 0) {
|
|
return new RootDir();
|
|
}
|
|
$dir = $this->provider->getDirById($this->target, $id);
|
|
if (!$dir) {
|
|
throw new DocNotFoundException();
|
|
}
|
|
|
|
return new Dir($dir->id, $dir->name, $dir->parent);
|
|
}
|
|
} |