Minor: Format code

pull/4805/head
Angel Fernando Quiroz Campos 2 years ago
parent f1803e1563
commit 6e9392dd5c
  1. 137
      src/CoreBundle/Entity/ResourceLink.php
  2. 256
      src/CoreBundle/Entity/ResourceNode.php

@ -1,9 +1,8 @@
<?php
/* For licensing terms, see /license.txt */
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Entity;
use ApiPlatform\Metadata\ApiResource;
@ -17,75 +16,108 @@ use LogicException;
use Stringable;
use Symfony\Component\Serializer\Annotation\Groups;
use function in_array;
#[ApiResource]
#[ORM\Table(name: 'resource_link')]
#[ORM\Entity]
class ResourceLink implements Stringable
{
use TimestampableTypedEntity;
public const VISIBILITY_DRAFT = 0;
public const VISIBILITY_PENDING = 1;
public const VISIBILITY_PUBLISHED = 2;
public const VISIBILITY_DELETED = 3;
#[ORM\Id]
#[ORM\Column(type: 'bigint')]
#[ORM\GeneratedValue]
protected ?int $id = null;
#[ORM\ManyToOne(targetEntity: ResourceNode::class, inversedBy: 'resourceLinks')]
#[ORM\JoinColumn(name: 'resource_node_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
protected ResourceNode $resourceNode;
#[ORM\ManyToOne(targetEntity: Course::class)]
#[ORM\JoinColumn(name: 'c_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
protected ?Course $course = null;
#[ORM\ManyToOne(targetEntity: \Chamilo\CoreBundle\Entity\Session::class, inversedBy: 'resourceLinks')]
#[ORM\ManyToOne(targetEntity: Session::class, inversedBy: 'resourceLinks')]
#[ORM\JoinColumn(name: 'session_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
protected ?Session $session = null;
#[ORM\ManyToOne(targetEntity: CGroup::class)]
#[ORM\JoinColumn(name: 'group_id', referencedColumnName: 'iid', nullable: true, onDelete: 'CASCADE')]
protected ?CGroup $group = null;
#[ORM\ManyToOne(targetEntity: Usergroup::class)]
#[ORM\JoinColumn(name: 'usergroup_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
protected ?Usergroup $userGroup = null;
#[ORM\ManyToOne(targetEntity: \Chamilo\CoreBundle\Entity\User::class)]
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
protected ?User $user = null;
/**
* @var Collection|ResourceRight[]
* @var Collection<int, ResourceRight>
*/
#[ORM\OneToMany(targetEntity: \Chamilo\CoreBundle\Entity\ResourceRight::class, mappedBy: 'resourceLink', cascade: ['persist', 'remove'], orphanRemoval: true)]
#[ORM\OneToMany(
targetEntity: ResourceRight::class,
mappedBy: 'resourceLink',
cascade: ['persist', 'remove'],
orphanRemoval: true
)]
protected Collection $resourceRights;
#[Groups(['ctool:read', 'c_tool_intro:read'])]
#[Groups([
'ctool:read',
'c_tool_intro:read',
])]
#[ORM\Column(name: 'visibility', type: 'integer', nullable: false)]
protected int $visibility;
#[Groups(['resource_node:read', 'resource_node:write', 'document:write', 'document:read'])]
#[ORM\Column(name: 'start_visibility_at', type: 'datetime', nullable: true)]
protected ?DateTimeInterface $startVisibilityAt = null;
#[Groups(['resource_node:read', 'resource_node:write', 'document:write', 'document:read'])]
#[ORM\Column(name: 'end_visibility_at', type: 'datetime', nullable: true)]
protected ?DateTimeInterface $endVisibilityAt = null;
public function __construct()
{
$this->resourceRights = new ArrayCollection();
$this->visibility = self::VISIBILITY_DRAFT;
}
public function __toString(): string
{
return (string) $this->getId();
}
public function getId(): ?int
{
return $this->id;
}
public function getStartVisibilityAt(): ?DateTimeInterface
{
return $this->startVisibilityAt;
}
public function setStartVisibilityAt(?DateTimeInterface $startVisibilityAt): self
{
$this->startVisibilityAt = $startVisibilityAt;
return $this;
}
public function getEndVisibilityAt(): ?DateTimeInterface
{
return $this->endVisibilityAt;
}
public function setEndVisibilityAt(?DateTimeInterface $endVisibilityAt): self
{
$this->endVisibilityAt = $endVisibilityAt;
@ -93,19 +125,6 @@ class ResourceLink implements Stringable
return $this;
}
/**
* @param Collection<int, ResourceRight> $rights
*
* @return ResourceLink
*/
public function setResourceRights(Collection $rights): self
{
$this->resourceRights = $rights;
/*foreach ($rights as $right) {
$this->addResourceRight($right);
}*/
return $this;
}
public function addResourceRight(ResourceRight $right): self
{
if (!$this->resourceRights->contains($right)) {
@ -115,103 +134,131 @@ class ResourceLink implements Stringable
return $this;
}
/**
* @return Collection|ResourceRight[]
* @return Collection<int, ResourceRight>
*/
public function getResourceRights(): Collection|array
public function getResourceRights(): Collection
{
return $this->resourceRights;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
public function setUser(User $user = null): self
public function setResourceRights(Collection $rights): self
{
$this->user = $user;
$this->resourceRights = $rights;
/*foreach ($rights as $right) {
$this->addResourceRight($right);
}*/
return $this;
}
public function getCourse(): ?Course
{
return $this->course;
}
public function setCourse(Course $course = null): self
{
$this->course = $course;
return $this;
}
public function getSession(): ?Session
{
return $this->session;
}
public function setSession(Session $session = null): self
{
$this->session = $session;
return $this;
}
public function hasSession(): bool
{
return null !== $this->session;
}
public function hasCourse(): bool
{
return null !== $this->course;
}
public function hasGroup(): bool
{
return null !== $this->group;
}
public function getGroup(): ?CGroup
{
return $this->group;
}
public function setGroup(CGroup $group = null): self
{
$this->group = $group;
return $this;
}
public function getUserGroup(): ?Usergroup
{
return $this->userGroup;
}
public function setUserGroup(Usergroup $group = null): self
{
$this->userGroup = $group;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(User $user = null): self
{
$this->user = $user;
return $this;
}
public function hasUser(): bool
{
return null !== $this->user;
}
public function getResourceNode(): ResourceNode
{
return $this->resourceNode;
}
public function setResourceNode(ResourceNode $resourceNode): self
{
$this->resourceNode = $resourceNode;
return $this;
}
public function getResourceNode(): ResourceNode
public function isPublished(): bool
{
return $this->resourceNode;
return self::VISIBILITY_PUBLISHED === $this->getVisibility();
}
public function getVisibility(): int
{
return $this->visibility;
}
public function setVisibility(int $visibility): self
{
if (!\in_array($visibility, self::getVisibilityList(), true)) {
if (!in_array($visibility, self::getVisibilityList(), true)) {
$message = sprintf(
'The visibility is not valid. Valid options: %s',
print_r(self::getVisibilityList(), true)
@ -223,27 +270,27 @@ class ResourceLink implements Stringable
return $this;
}
public function isPublished(): bool
public static function getVisibilityList(): array
{
return self::VISIBILITY_PUBLISHED === $this->getVisibility();
return [
'Draft' => self::VISIBILITY_DRAFT,
'Pending' => self::VISIBILITY_PENDING,
'Published' => self::VISIBILITY_PUBLISHED,
'Deleted' => self::VISIBILITY_DELETED,
];
}
public function isPending(): bool
{
return self::VISIBILITY_PENDING === $this->getVisibility();
}
public function isDraft(): bool
{
return self::VISIBILITY_DRAFT === $this->getVisibility();
}
public static function getVisibilityList(): array
{
return [
'Draft' => self::VISIBILITY_DRAFT,
'Pending' => self::VISIBILITY_PENDING,
'Published' => self::VISIBILITY_PUBLISHED,
'Deleted' => self::VISIBILITY_DELETED,
];
}
public function getVisibilityName(): string
{
return array_flip(static::getVisibilityList())[$this->getVisibility()];

@ -36,6 +36,7 @@ use Symfony\Component\Uid\UuidV4;
use Symfony\Component\Validator\Constraints as Assert;
//* attributes={"security"="is_granted('ROLE_ADMIN')"},
/**
* Base entity for all resources.
*/
@ -72,21 +73,26 @@ class ResourceNode implements Stringable
{
use TimestampableTypedEntity;
use TimestampableAgoTrait;
public const PATH_SEPARATOR = '/';
#[Groups(['resource_node:read', 'document:read', 'ctool:read', 'user_json:read'])]
#[ORM\Id]
#[ORM\Column(type: 'bigint')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
protected ?int $id = null;
#[Groups(['resource_node:read', 'resource_node:write', 'document:read', 'document:write'])]
#[Assert\NotBlank]
#[Gedmo\TreePathSource]
#[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)]
protected string $title;
#[Assert\NotBlank]
#[Gedmo\Slug(fields: ['title'])]
#[ORM\Column(name: 'slug', type: 'string', length: 255, nullable: false)]
protected string $slug;
#[Groups(['resource_node:read'])]
#[Assert\NotNull]
#[ORM\ManyToOne(targetEntity: ResourceType::class, inversedBy: 'resourceNodes')]
@ -107,35 +113,42 @@ class ResourceNode implements Stringable
/**
* ResourceFile available file for this node.
*/
#[Groups(['resource_node:read', 'resource_node:write', 'document:read', 'document:write', 'message:read'])]
#[ORM\OneToOne(inversedBy: 'resourceNode', targetEntity: ResourceFile::class, orphanRemoval: true)]
#[ORM\JoinColumn(name: 'resource_file_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
protected ?ResourceFile $resourceFile = null;
#[Assert\NotNull]
#[Groups(['resource_node:read', 'resource_node:write', 'document:write'])]
#[ORM\ManyToOne(targetEntity: \Chamilo\CoreBundle\Entity\User::class, inversedBy: 'resourceNodes')]
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'resourceNodes')]
#[ORM\JoinColumn(name: 'creator_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
protected User $creator;
#[Groups(['resource_node:read'])]
#[MaxDepth(1)]
#[ORM\JoinColumn(name: 'parent_id', onDelete: 'CASCADE')]
#[Gedmo\TreeParent]
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
#[Gedmo\TreeParent]
#[Gedmo\SortableGroup]
protected ?ResourceNode $parent = null;
/**
* @var Collection|ResourceNode[]
* @var Collection<int, ResourceNode>
*/
#[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')]
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
#[ORM\OrderBy(['id' => 'ASC'])]
protected Collection $children;
#[Gedmo\TreeLevel]
#[ORM\Column(name: 'level', type: 'integer', nullable: true)]
protected ?int $level = null;
#[Groups(['resource_node:read', 'document:read'])]
#[Gedmo\TreePath(appendId: true, separator: '/')]
#[Gedmo\TreePath(separator: '/', appendId: true)]
#[ORM\Column(name: 'path', type: 'text', nullable: true)]
protected ?string $path = null;
/**
* Shortcut to access Course resource from ResourceNode.
* Groups({"resource_node:read", "course:read"}).
@ -143,30 +156,39 @@ class ResourceNode implements Stringable
* ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Illustration", mappedBy="resourceNode")
*/
//protected $illustration;
/**
* @var Collection|ResourceComment[]
* @var Collection<int, ResourceComment>
*/
#[ORM\OneToMany(targetEntity: \Chamilo\CoreBundle\Entity\ResourceComment::class, mappedBy: 'resourceNode', cascade: ['persist', 'remove'])]
#[ORM\OneToMany(mappedBy: 'resourceNode', targetEntity: ResourceComment::class, cascade: ['persist', 'remove'])]
protected Collection $comments;
#[Groups(['resource_node:read', 'document:read'])]
#[Gedmo\Timestampable(on: 'create')]
#[ORM\Column(type: 'datetime')]
protected DateTime $createdAt;
#[Groups(['resource_node:read', 'document:read'])]
#[Gedmo\Timestampable(on: 'update')]
#[ORM\Column(type: 'datetime')]
protected DateTime $updatedAt;
#[Groups(['resource_node:read', 'document:read'])]
protected bool $fileEditableText;
#[Groups(['resource_node:read', 'document:read'])]
#[ORM\Column(type: 'boolean')]
protected bool $public;
protected ?string $content = null;
#[ORM\OneToOne(targetEntity: \Chamilo\CourseBundle\Entity\CShortcut::class, mappedBy: 'shortCutNode', cascade: ['persist', 'remove'])]
#[ORM\OneToOne(mappedBy: 'shortCutNode', targetEntity: CShortcut::class, cascade: ['persist', 'remove'])]
protected ?CShortcut $shortCut = null;
#[Groups(['resource_node:read', 'document:read'])]
#[ORM\Column(type: 'uuid', unique: true)]
protected ?UuidV4 $uuid = null;
#[ORM\Column(name: 'display_order', type: 'integer', nullable: false)]
#[Gedmo\SortablePosition]
protected int $displayOrder;
@ -182,46 +204,62 @@ class ResourceNode implements Stringable
$this->fileEditableText = false;
$this->displayOrder = 0;
}
public function __toString(): string
{
return $this->getPathForDisplay();
}
public function getUuid(): ?UuidV4
{
return $this->uuid;
}
/**
* Returns the resource id.
*
* @return int
* Returns the path cleaned from its ids.
* Eg.: "Root/subdir/file.txt".
*/
public function getId()
public function getPathForDisplay(): string
{
return $this->id;
return $this->path;
//return $this->convertPathForDisplay($this->path);
}
public function getUuid(): ?UuidV4
{
return $this->uuid;
}
public function hasCreator(): bool
{
return null !== $this->creator;
}
public function getCreator(): ?User
{
return $this->creator;
}
public function setCreator(User $creator): self
{
$this->creator = $creator;
return $this;
}
/**
* Returns the children resource instances.
*
* @return Collection|ResourceNode[]
* @return Collection<int, ResourceNode>
*/
public function getChildren(): Collection|array
public function getChildren(): Collection
{
return $this->children;
}
/**
* Returns the parent resource.
*/
public function getParent(): ?self
{
return $this->parent;
}
/**
* Sets the parent resource.
*/
@ -231,13 +269,7 @@ class ResourceNode implements Stringable
return $this;
}
/**
* Returns the parent resource.
*/
public function getParent(): ?self
{
return $this->parent;
}
/**
* Return the lvl value of the resource in the tree.
*/
@ -245,17 +277,17 @@ class ResourceNode implements Stringable
{
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()
public function getPath(): ?string
{
return $this->path;
}
/**
* @return Collection|ResourceComment[]
*/
@ -263,6 +295,7 @@ class ResourceNode implements Stringable
{
return $this->comments;
}
public function addComment(ResourceComment $comment): self
{
$comment->setResourceNode($this);
@ -270,15 +303,7 @@ class ResourceNode implements Stringable
return $this;
}
/**
* Returns the path cleaned from its ids.
* Eg.: "Root/subdir/file.txt".
*/
public function getPathForDisplay(): string
{
return $this->path;
//return $this->convertPathForDisplay($this->path);
}
public function getPathForDisplayToArray(?int $baseRoot = null): array
{
$parts = explode(self::PATH_SEPARATOR, $this->path);
@ -298,38 +323,14 @@ class ResourceNode implements Stringable
return $list;
}
public function getPathForDisplayRemoveBase(string $base): string
{
$path = str_replace($base, '', $this->path);
return $this->convertPathForDisplay($path);
}
public function getSlug(): string
{
return $this->slug;
}
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): self
{
$title = str_replace('/', '-', $title);
$this->title = $title;
return $this;
}
public function setSlug(string $slug): self
{
if (str_contains(self::PATH_SEPARATOR, $slug)) {
$message = 'Invalid character "'.self::PATH_SEPARATOR.'" in resource name';
throw new InvalidArgumentException($message);
}
$this->slug = $slug;
return $this;
}
/**
* Convert a path for display: remove ids.
*/
@ -351,13 +352,33 @@ class ResourceNode implements Stringable
return $pathForDisplay;
}
public function getResourceType(): ResourceType
public function getSlug(): string
{
return $this->resourceType;
return $this->slug;
}
public function setResourceType(ResourceType $resourceType): self
public function setSlug(string $slug): self
{
$this->resourceType = $resourceType;
if (str_contains(self::PATH_SEPARATOR, $slug)) {
$message = 'Invalid character "'.self::PATH_SEPARATOR.'" in resource name';
throw new InvalidArgumentException($message);
}
$this->slug = $slug;
return $this;
}
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): self
{
$title = str_replace('/', '-', $title);
$this->title = $title;
return $this;
}
@ -374,10 +395,21 @@ class ResourceNode implements Stringable
return $this;
}
/**
* @return Collection<int, ResourceLink>
*/
public function getResourceLinks(): Collection
{
return $this->resourceLinks;
}
public function setResourceLinks(Collection $resourceLinks): self
{
$this->resourceLinks = $resourceLinks;
return $this;
}
public function addResourceLink(ResourceLink $link): self
{
$link->setResourceNode($this);
@ -385,31 +417,55 @@ class ResourceNode implements Stringable
return $this;
}
public function setResourceLinks(Collection $resourceLinks): self
public function hasEditableTextContent(): bool
{
$this->resourceLinks = $resourceLinks;
if ($this->hasResourceFile()) {
$mimeType = $this->getResourceFile()->getMimeType();
return $this;
if (str_contains($mimeType, 'text')) {
return true;
}
}
return false;
}
public function hasResourceFile(): bool
{
return null !== $this->resourceFile;
}
public function getResourceFile(): ?ResourceFile
{
return $this->resourceFile;
}
public function hasEditableTextContent(): bool
public function setResourceFile(?ResourceFile $resourceFile = null): self
{
$this->resourceFile = $resourceFile;
$resourceFile?->setResourceNode($this);
return $this;
}
public function getIcon(): string
{
$class = 'fa fa-folder';
if ($this->hasResourceFile()) {
$mimeType = $this->getResourceFile()->getMimeType();
if (str_contains($mimeType, 'text')) {
return true;
$class = 'far fa-file';
if ($this->isResourceFileAnImage()) {
$class = 'far fa-file-image';
}
if ($this->isResourceFileAVideo()) {
$class = 'far fa-file-video';
}
}
return false;
return '<i class="'.$class.'"></i>';
}
public function isResourceFileAnImage(): bool
{
if ($this->hasResourceFile()) {
@ -421,6 +477,7 @@ class ResourceNode implements Stringable
return false;
}
public function isResourceFileAVideo(): bool
{
if ($this->hasResourceFile()) {
@ -432,30 +489,7 @@ class ResourceNode implements Stringable
return false;
}
public function setResourceFile(?ResourceFile $resourceFile = null): self
{
$this->resourceFile = $resourceFile;
if (null !== $resourceFile) {
$resourceFile->setResourceNode($this);
}
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 '<i class="'.$class.'"></i>';
}
public function getThumbnail(RouterInterface $router): string
{
$size = 'fa-3x';
@ -481,30 +515,56 @@ class ResourceNode implements Stringable
return '<i class="'.$class.'"></i>';
}
/**
* Returns the resource id.
*/
public function getId(): ?int
{
return $this->id;
}
public function getResourceType(): ResourceType
{
return $this->resourceType;
}
public function setResourceType(ResourceType $resourceType): self
{
$this->resourceType = $resourceType;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getShortCut(): ?CShortcut
{
return $this->shortCut;
}
public function setShortCut(?CShortcut $shortCut): self
{
$this->shortCut = $shortCut;
return $this;
}
public function isPublic(): bool
{
return $this->public;
}
public function setPublic(bool $public): self
{
$this->public = $public;

Loading…
Cancel
Save