src/Entity/Document.php line 13

  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Document\Block;
  4. use App\Repository\DocumentRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\DBAL\Types\Types;
  8. use Doctrine\ORM\Mapping as ORM;
  9. #[ORM\Entity(repositoryClassDocumentRepository::class)]
  10. class Document
  11. {
  12.     const TYPE_WEB 0;
  13.     const TYPE_DOCX 1;
  14.     const STATUS_DRAFT=0;
  15.     const STATUS_ON_PROGRESS=1;
  16.     const STATUS_FINALIZED=2;
  17.     const STATUSES = [
  18.         self::STATUS_DRAFT => "Draft",
  19.         self::STATUS_ON_PROGRESS => "On progress",
  20.         self::STATUS_FINALIZED => "Finalized"
  21.     ];
  22.     #[ORM\Id]
  23.     #[ORM\GeneratedValue]
  24.     #[ORM\Column]
  25.     private ?int $id null;
  26.     #[ORM\Column(length255)]
  27.     private ?string $title null;
  28.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  29.     private ?string $description null;
  30.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  31.     private ?string $tags null;
  32.     #[ORM\ManyToOne(inversedBy'docs')]
  33.     private ?User $owner null;
  34.     #[ORM\Column(nullabletrue)]
  35.     private ?bool $isTemplate null;
  36.     #[ORM\ManyToOne(targetEntityself::class)]
  37.     private ?self $sourceTemplate null;
  38.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  39.     private ?\DateTimeInterface $dateCreated null;
  40.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  41.     private ?\DateTimeInterface $dateUpdated null;
  42.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  43.     private ?\DateTimeInterface $version null;
  44.     #[ORM\ManyToOne(targetEntityself::class)]
  45.     private ?self $versionOf null;
  46.     #[ORM\Column]
  47.     private ?int $type null;
  48.     /* web specific attributes */
  49.     #[ORM\OneToMany(mappedBy'document'targetEntityBlock::class)]
  50.     #[ORM\OrderBy(["blockorder" => "ASC"])]
  51.     private Collection $blocks;
  52.     /* docx specific attributes */
  53.     #[ORM\ManyToOne]
  54.     private ?File $file null;
  55.     #[ORM\OneToMany(mappedBy'document'targetEntityDocumentPermission::class, orphanRemovaltrue)]
  56.     private Collection $documentPermissions;
  57.     #[ORM\Column]
  58.     private ?int $status 0;
  59.     #[ORM\Column]
  60.     private ?bool $isLastVersion null;
  61.     public function __construct()
  62.     {
  63.         $this->blocks = new ArrayCollection();
  64.         $this->documentPermissions = new ArrayCollection();
  65.     }
  66.     public function getId(): ?int
  67.     {
  68.         return $this->id;
  69.     }
  70.     public function getTitle(): ?string
  71.     {
  72.         return $this->title;
  73.     }
  74.     public function setTitle(string $title): self
  75.     {
  76.         $this->title $title;
  77.         return $this;
  78.     }
  79.     public function getDescription(): ?string
  80.     {
  81.         return $this->description;
  82.     }
  83.     public function setDescription(?string $description): self
  84.     {
  85.         $this->description $description;
  86.         return $this;
  87.     }
  88.     public function getTags(): ?string
  89.     {
  90.         return $this->tags;
  91.     }
  92.     public function setTags(?string $tags): self
  93.     {
  94.         $this->tags $tags;
  95.         return $this;
  96.     }
  97.     public function getOwner(): ?User
  98.     {
  99.         return $this->owner;
  100.     }
  101.     public function setOwner(?User $owner): self
  102.     {
  103.         $this->owner $owner;
  104.         return $this;
  105.     }
  106.     public function isIsTemplate(): ?bool
  107.     {
  108.         return $this->isTemplate;
  109.     }
  110.     public function setIsTemplate(?bool $isTemplate): self
  111.     {
  112.         $this->isTemplate $isTemplate;
  113.         return $this;
  114.     }
  115.     public function getSourceTemplate(): ?self
  116.     {
  117.         return $this->sourceTemplate;
  118.     }
  119.     public function setSourceTemplate(?self $sourceTemplate): self
  120.     {
  121.         $this->sourceTemplate $sourceTemplate;
  122.         return $this;
  123.     }
  124.     public function getDateCreated(): ?\DateTimeInterface
  125.     {
  126.         return $this->dateCreated;
  127.     }
  128.     public function setDateCreated(\DateTimeInterface $dateCreated): self
  129.     {
  130.         $this->dateCreated $dateCreated;
  131.         return $this;
  132.     }
  133.     public function getDateUpdated(): ?\DateTimeInterface
  134.     {
  135.         return $this->dateUpdated;
  136.     }
  137.     public function setDateUpdated(\DateTimeInterface $dateUpdated): self
  138.     {
  139.         $this->dateUpdated $dateUpdated;
  140.         return $this;
  141.     }
  142.     public function getVersion(): ?\DateTimeInterface
  143.     {
  144.         return $this->version;
  145.     }
  146.     public function setVersion(\DateTimeInterface $version): self
  147.     {
  148.         $this->version $version;
  149.         return $this;
  150.     }
  151.     public function getVersionOf(): ?self
  152.     {
  153.         return $this->versionOf;
  154.     }
  155.     public function setVersionOf(?self $versionOf): self
  156.     {
  157.         $this->versionOf $versionOf;
  158.         return $this;
  159.     }
  160.     public function getType(): ?int
  161.     {
  162.         return $this->type;
  163.     }
  164.     public function getTypeToString()
  165.     {
  166.         $strType $this->type == Document::TYPE_WEB "Web" "";
  167.         $strType $this->type == Document::TYPE_DOCX "Docx" $strType;
  168.         return $strType;
  169.     }
  170.     public function typeIsWeb()
  171.     {
  172.         return $this->type == Document::TYPE_WEB;
  173.     }
  174.     public function setType(int $type): self
  175.     {
  176.         $this->type $type;
  177.         return $this;
  178.     }
  179.     /**
  180.      * @return Collection<int, Block>
  181.      */
  182.     public function getBlocks(): Collection
  183.     {
  184.         return $this->blocks;
  185.     }
  186.     public function addBlock(Block $block): self
  187.     {
  188.         if (!$this->blocks->contains($block)) {
  189.             $this->blocks->add($block);
  190.             $block->setDocument($this);
  191.         }
  192.         return $this;
  193.     }
  194.     public function removeBlock(Block $block): self
  195.     {
  196.         if ($this->blocks->removeElement($block)) {
  197.             // set the owning side to null (unless already changed)
  198.             if ($block->getDocument() === $this) {
  199.                 $block->setDocument(null);
  200.             }
  201.         }
  202.         return $this;
  203.     }
  204.     public function getFile(): ?File
  205.     {
  206.         return $this->file;
  207.     }
  208.     public function setFile(?File $file): self
  209.     {
  210.         $this->file $file;
  211.         return $this;
  212.     }
  213.     public static function clone(Document $source)
  214.     {
  215.         $document = new Document();
  216.         $document->setTags($source->getTags());
  217.         $document->setDescription($source->getDescription());
  218.         $document->setTitle($source->getTitle());
  219.         $document->setOwner($source->getOwner());
  220.         foreach($source->getBlocks() as $block)
  221.             $document->addBlock(Block::clone($block));
  222.         return $document;
  223.     }
  224.     public function getNextBlockId() {
  225.         if(count($this->getBlocks()) == 0)
  226.             return 1;
  227.         $ids = [];
  228.         /** @var Block $block */
  229.         foreach($this->getBlocks() as $block)
  230.             array_push($ids$block->getIdInDoc());
  231.         sort($ids);
  232.         return ($ids[count($ids) - 1]) + 1;
  233.     }
  234.     /**
  235.      * @return Collection<int, DocumentPermission>
  236.      */
  237.     public function getDocumentPermissions(): Collection
  238.     {
  239.         return $this->documentPermissions;
  240.     }
  241.     public function addDocumentPermission(DocumentPermission $documentPermission): self
  242.     {
  243.         if (!$this->documentPermissions->contains($documentPermission)) {
  244.             $this->documentPermissions->add($documentPermission);
  245.             $documentPermission->setDocument($this);
  246.         }
  247.         return $this;
  248.     }
  249.     public function removeDocumentPermission(DocumentPermission $documentPermission): self
  250.     {
  251.         if ($this->documentPermissions->removeElement($documentPermission)) {
  252.             // set the owning side to null (unless already changed)
  253.             if ($documentPermission->getDocument() === $this) {
  254.                 $documentPermission->setDocument(null);
  255.             }
  256.         }
  257.         return $this;
  258.     }
  259.     public function allows(User $user) {
  260.         /** @var DocumentPermission $p */
  261.         foreach($this->documentPermissions as $p){
  262.             if($p->getUser() === $user)
  263.                 return true;
  264.         }
  265.         return false;
  266.     }
  267.     public function getStatus(): ?int
  268.     {
  269.         return $this->status;
  270.     }
  271.     public function setStatus(int $status): self
  272.     {
  273.         $this->status $status;
  274.         return $this;
  275.     }
  276.     public function toArray()
  277.     {
  278.         return  [
  279.             "id" => $this->getId(),
  280.             "tags" => $this->getTags(),
  281.             "description" => $this->getDescription(),
  282.             "title" => $this->getTitle(),
  283.         ];
  284.     }
  285.     public function isIsLastVersion(): ?bool
  286.     {
  287.         return $this->isLastVersion;
  288.     }
  289.     public function setIsLastVersion(bool $isLastVersion)
  290.     {
  291.         $this->isLastVersion $isLastVersion;
  292.         return $this;
  293.     }
  294. }