base = false; $this->files = new ArrayCollection(); $this->children = new ArrayCollection(); $this->folderOwners = new ArrayCollection(); $this->permission = FileWebPermission::PRIVATE; $this->trash = false; $this->lastModified = new \DateTime(); $this->dateCreated = new \DateTime(); $this->regenerateGuid(); $this->visible = true; $this->systemBox = false; } /** * @ORM\PrePersist * @ORM\PreUpdate */ public function prePersist(){ $this->updateLastModified(); } /** * @param \DateTime $lastModified * @return FolderItem */ public function setLastModified(\DateTime $lastModified): FolderItem { $this->lastModified = $lastModified; return $this; } public function updateLastModified(){ $this->lastModified = new \DateTime(); return $this; } /** * @return \DateTime|null */ public function getLastModified() { return $this->lastModified; } /** * @param int $length * @return bool|string */ private function generateHumanHash($length = 64){ $hash = ""; while(strlen($hash) < $length){ try { $hash .= hash('sha512', random_bytes($length)); } catch (\Exception $e) { $hash .= hash('sha512', mt_rand(0,$length*10000)); } } return substr($hash, 0,$length); } /** * use to change the pk if its not unique */ public function regenerateGuid(){ $this->guid = $this->generateHumanHash(); } /** * Get id * * @return int */ public function getId() { return $this->id; } /** * Set name * * @param string $name * * @return FolderItem */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set description * * @param string $description * * @return FolderItem */ public function setDescription($description) { $this->description = $description; return $this; } /** * Get description * * @return string */ public function getDescription() { return $this->description; } /** * Set permission * * @param integer $permission * * @return FolderItem */ public function setPermission($permission) { $this->permission = $permission; return $this; } /** * Get permission * * @return int */ public function getPermission() { return $this->permission; } /** * Set guid * * @param string $guid * * @return FolderItem */ public function setGuid($guid) { $this->guid = $guid; return $this; } public function genGuid(){ if(empty($this->guid)){ $this->guid = time().$this->generateHumanHash(64); } } /** * Get guid * * @return string */ public function getGuid() { return $this->guid; } /** * @param ArrayCollection $files * @return FolderItem */ public function setFiles(ArrayCollection $files): FolderItem { $this->files = $files; return $this; } /** * @return ArrayCollection */ public function getFiles() { return $this->files; } /** * @param FileItem $file * @return FolderItem */ public function addFile(FileItem $file): FolderItem { $this->files[] = $file; $file->setParentFolder($this); return $this; } /** * @param FileItem $file * @return FolderItem */ public function removeFile(FileItem $file): FolderItem { $this->files->removeElement($file); return $this; } /** * @param bool $base * @return FolderItem */ public function setBase(bool $base): FolderItem { $this->base = $base; return $this; } /** * @return bool */ public function isBase(): bool { return $this->base; } /** * @param FolderItem $parent * @return FolderItem */ public function setParent(FolderItem $parent): FolderItem { if(!is_null($this->parent)) $this->previousParentFolder = $this->parent; $this->parent = $parent; return $this; } /** * @return FolderItem */ public function getParent(): ?FolderItem { return $this->parent; } /** * @return FolderItem */ public function getPreviousParentFolder(): ?FolderItem { return $this->previousParentFolder; } /** * @param FolderItem|null $previousParentFolder * @return FolderItem */ public function setPreviousParentFolder(?FolderItem $previousParentFolder): FolderItem { $this->previousParentFolder = $previousParentFolder; return $this; } /** * @param ArrayCollection $children * @return FolderItem */ public function setChildren(ArrayCollection $children): FolderItem { $this->children = $children; return $this; } /** * @return ArrayCollection|null|PersistentCollection */ public function getChildren() { return $this->children; } /** * @param FolderItem $folder * @return FolderItem */ public function addChildren(FolderItem $folder): FolderItem { $this->children[] = $folder; $folder->setParent($this); return $this; } /** * @param FolderItem $folder * @return FolderItem */ public function removeChildren(FolderItem $folder): FolderItem { $this->children->removeElement($folder); return $this; } /** * @param ArrayCollection $folderOwners * @return FolderItem */ public function setFolderOwners(ArrayCollection $folderOwners): FolderItem { $this->folderOwners = $folderOwners; return $this; } /** * @return ArrayCollection */ public function getFolderOwners() { return $this->folderOwners; } /** * @param FolderOwner $fo * @return $this */ public function addFolderOwners(FolderOwner $fo) { $this->folderOwners[] = $fo; return $this; } /*** * @param FolderOwner $fo */ public function removeFolderOwners(FolderOwner $fo) { $this->folderOwners->removeElement($fo); } /** * @param bool $trash * @return FolderItem */ public function setTrash(bool $trash): FolderItem { $this->trash = $trash; return $this; } /** * @return bool */ public function isTrash(): bool { return $this->trash; } /** * @return \DateTime */ public function getDateCreated(): \DateTime { return $this->dateCreated; } protected $tree; protected $trashed; public function getTree(){ if(isset($this->tree) && is_array($this->tree)){ return $this->tree; } $this->trashedNtree(); return $this->tree; } public function isTrashed(){ if(isset($this->trashed) && is_array($this->trashed)){ return $this->trashed; } $this->trashedNtree(); return $this->trashed; } private function trashedNtree(){ $tree = array(); $loop = true; $loopFolder = $this; $trashed = null; while($loop){ if($loopFolder instanceof FolderItem){ $tree[] = array( 'guid' => $loopFolder->getGuid(), 'name' => $loopFolder->getName() ); if($loopFolder->isBase() || $loopFolder->isTrash()){ if($loopFolder->isTrash()) $trashed = true; $loopFolder = null; } else $loopFolder = $loopFolder->getParent(); } else { $loop = false; } } $this->tree = array_reverse($tree); if(!is_null($trashed) && $trashed = true) $this->trashed = true; else $this->trashed = false; } /** * @param bool $visible * @return FolderItem */ public function setVisible(bool $visible): FolderItem { $this->visible = $visible; return $this; } /** * @return bool */ public function isVisible(): bool { return $this->visible; } /** * @param Collection|null $folderGroupPermission * @return FolderItem */ public function setFolderGroupPermission(?Collection $folderGroupPermission): FolderItem { $this->folderGroupPermission = $folderGroupPermission; return $this; } /** * @return Collection|null */ public function getFolderGroupPermission(): ?Collection { return $this->folderGroupPermission; } /** * @param FolderGroupPermission $fgp * @return $this */ public function addFolderGroupPermission(FolderGroupPermission $fgp) { $this->folderGroupPermission[] = $fgp; return $this; } /** * @param FolderGroupPermission $fgp */ public function removeFolderGroupPermission(FolderGroupPermission $fgp) { $this->folderGroupPermission->removeElement($fgp); } /** * @param bool $systemBox * @return FolderItem */ public function setSystemBox(bool $systemBox): FolderItem { $this->systemBox = $systemBox; return $this; } /** * @return bool */ public function isSystemBox(): bool { return $this->systemBox; } }