uuid = Uuid::uuid4()->toString(); $this->children = new ArrayCollection(); $this->resourceLinks = new ArrayCollection(); $this->comments = new ArrayCollection(); $this->createdAt = new \DateTime(); $this->editableContent = false; } /** * @return string */ public function __toString() { return (string) $this->getPathForDisplay(); } /** * Returns the resource id. * * @return int */ public function getId() { return $this->id; } /** * Returns the resource creator. * * @return User */ public function getCreator(): ?User { return $this->creator; } public function setCreator(User $creator = null): self { $this->creator = $creator; return $this; } /** * Returns the children resource instances. * * @return ResourceNode[]|ArrayCollection */ public function getChildren() { return $this->children; } /** * Sets the parent resource. */ public function setParent(self $parent = null): self { $this->parent = $parent; return $this; } /** * Returns the parent resource. * * @return ResourceNode */ public function getParent() { return $this->parent; } /** * Return the lvl value of the resource in the tree. * * @return int */ public function getLevel() { return $this->level; } /** * Returns the "raw" path of the resource * (the path merge names and ids of all items). * Eg.: "Root-1/subdir-2/file.txt-3/". * * @return string */ public function getPath() { return $this->path; } /** * @return ResourceComment[]|ArrayCollection */ public function getComments() { return $this->comments; } public function addComment(ResourceComment $comment) { $comment->setResourceNode($this); return $this->comments->add($comment); } /** * Returns the path cleaned from its ids. * Eg.: "Root/subdir/file.txt". * * @return string */ public function getPathForDisplay() { return self::convertPathForDisplay($this->path); } public function getPathForDisplayToArray($baseRoot = null) { $parts = explode(self::PATH_SEPARATOR, $this->path); $list = []; foreach ($parts as $part) { $parts = explode('-', $part); if (empty($parts[1])) { continue; } $value = $parts[0]; $id = $parts[1]; if (!empty($baseRoot)) { if ($id < $baseRoot) { continue; } } $list[$id] = $value; } return $list; } public function getPathForDisplayRemoveBase(string $base): string { $path = str_replace($base, '', $this->path); return self::convertPathForDisplay($path); } public function getSlug() { return $this->slug; } public function getTitle() { return $this->title; } public function setTitle(string $title) { $this->title = $title; return $this; } public function setSlug(string $slug): self { if (false !== strpos(self::PATH_SEPARATOR, $slug)) { throw new \InvalidArgumentException('Invalid character "'.self::PATH_SEPARATOR.'" in resource name.'); } $this->slug = $slug; return $this; } /** * Convert a path for display: remove ids. * * @param string $path * * @return string */ public static function convertPathForDisplay($path) { /*$pathForDisplay = preg_replace( '/-\d+'.self::PATH_SEPARATOR.'/', ' / ', $path ); if ($pathForDisplay !== null && strlen($pathForDisplay) > 0) { $pathForDisplay = substr_replace($pathForDisplay, '', -3); } */ $pathForDisplay = preg_replace( '/-\d+'.self::PATH_SEPARATOR.'/', '/', $path ); if (null !== $pathForDisplay && strlen($pathForDisplay) > 0) { $pathForDisplay = substr_replace($pathForDisplay, '', -1); } return $pathForDisplay; } /** * @return ResourceType */ public function getResourceType() { return $this->resourceType; } public function setResourceType(ResourceType $resourceType): self { $this->resourceType = $resourceType; return $this; } /** * @return ArrayCollection|ResourceLink[] */ public function getResourceLinks() { return $this->resourceLinks; } /** * @return ResourceNode */ public function setResourceLinks($resourceLinks) { $this->resourceLinks = $resourceLinks; return $this; } /** * @param Session $session * * @return ArrayCollection */ public function hasSession(Session $session = null) { $links = $this->getResourceLinks(); $criteria = Criteria::create(); $criteria->andWhere( Criteria::expr()->eq('session', $session) ); return $links->matching($criteria); } public function hasResourceFile(): bool { return null !== $this->resourceFile; } public function getResourceFile(): ?ResourceFile { return $this->resourceFile; } public function hasEditableTextContent(): bool { if ($this->hasResourceFile()) { $mimeType = $this->getResourceFile()->getMimeType(); if (false !== strpos($mimeType, 'text')) { return true; } } return false; } public function isFileEditableText(): bool { return $this->hasEditableTextContent(); } public function isResourceFileAnImage(): bool { if ($this->hasResourceFile()) { $mimeType = $this->getResourceFile()->getMimeType(); if (false !== strpos($mimeType, 'image')) { return true; } } return false; } public function isResourceFileAVideo(): bool { if ($this->hasResourceFile()) { $mimeType = $this->getResourceFile()->getMimeType(); if (false !== strpos($mimeType, 'video')) { return true; } } return false; } public function setResourceFile(ResourceFile $resourceFile = null): self { $this->resourceFile = $resourceFile; return $this; } public function getIcon(): string { $class = 'fa fa-folder'; if ($this->hasResourceFile()) { $class = 'far fa-file'; if ($this->isResourceFileAnImage()) { $class = 'far fa-file-image'; } if ($this->isResourceFileAVideo()) { $class = 'far fa-file-video'; } } return ''; } public function getThumbnail(RouterInterface $router): string { $size = 'fa-3x'; $class = "fa fa-folder $size"; if ($this->hasResourceFile()) { $class = "far fa-file $size"; if ($this->isResourceFileAnImage()) { $class = "far fa-file-image $size"; $params = [ 'id' => $this->getId(), 'tool' => $this->getResourceType()->getTool(), 'type' => $this->getResourceType()->getName(), 'filter' => 'editor_thumbnail', ]; $url = $router->generate( 'chamilo_core_resource_view', $params ); return ""; } if ($this->isResourceFileAVideo()) { $class = "far fa-file-video $size"; } } return ''; } public function getContent() { return $this->content; } public function setContent(string $content): self { $this->content = $content; return $this; } }