src/Entity/Documents/Document.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Documents;
  3. use App\Annotation\SiteAware;
  4. use App\Entity\AbstractBase;
  5. use App\Entity\Interfaces\DocumentInterface;
  6. use App\Entity\Interfaces\SiteInterface;
  7. use App\Entity\MiniAbstractBase;
  8. use App\Entity\Traits\HasDocumentTrait;
  9. use App\Entity\Traits\NameTrait;
  10. use App\Entity\Traits\PublishedTrait;
  11. use App\Entity\Traits\SiteTrait;
  12. use App\Entity\User;
  13. use App\Entity\UserGroup;
  14. use App\Repository\Documents\DocumentRepository;
  15. use Doctrine\Common\Collections\ArrayCollection;
  16. use Doctrine\Common\Collections\Collection;
  17. use Doctrine\ORM\Mapping as ORM;
  18. use Gedmo\Mapping\Annotation as Gedmo;
  19. use Symfony\Component\HttpFoundation\File\File;
  20. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  21. /**
  22.  * @ORM\Table(name="vulco_document", indexes={@ORM\Index(name="document_site_idx", columns={"site"})})
  23.  * @ORM\Entity(repositoryClass=DocumentRepository::class)
  24.  * @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
  25.  * @SiteAware(siteFieldName="site")
  26.  * @Vich\Uploadable
  27.  */
  28. class Document extends AbstractBase implements SiteInterfaceDocumentInterface
  29. {
  30.     use HasDocumentTrait;
  31.     use NameTrait;
  32.     use PublishedTrait;
  33.     use SiteTrait;
  34.     /**
  35.      * @ORM\Column(type="string", length=255, nullable=false)
  36.      */
  37.     private string $name;
  38.     /**
  39.      * @ORM\Column(type="integer", nullable=false, options={"default": 0})
  40.      */
  41.     private int $numDownloads 0;
  42.     /**
  43.      * @Vich\UploadableField(mapping="document_document", fileNameProperty="documentName")
  44.      */
  45.     private ?File $document null;
  46.     /**
  47.      * @ORM\ManyToOne(targetEntity="App\Entity\Documents\DocumentCategory", inversedBy="documents")
  48.      * @ORM\JoinColumn(name="document_category_id", referencedColumnName="id")
  49.      */
  50.     private ?DocumentCategory $documentCategory null;
  51.     /**
  52.      * @ORM\OneToMany(targetEntity="App\Entity\Documents\DocumentDownload", mappedBy="document", cascade={"persist", "remove"}, orphanRemoval=true)
  53.      * @ORM\OrderBy({"createdAt": "DESC"})
  54.      */
  55.     private ?Collection $downloads;
  56.     /**
  57.      * @ORM\ManyToMany(targetEntity="App\Entity\UserGroup", inversedBy="documents")
  58.      * @ORM\JoinTable(name="vulco_documents_user_groups",
  59.      *     joinColumns={@ORM\JoinColumn(name="document_id", referencedColumnName="id")},
  60.      *     inverseJoinColumns={@ORM\JoinColumn(name="user_group_id", referencedColumnName="id")}
  61.      * )
  62.      */
  63.     private ?Collection $userGroups;
  64.     /**
  65.      * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="pointMovements")
  66.      *
  67.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  68.      */
  69.     private ?User $user null;
  70.     public function __construct()
  71.     {
  72.         $this->downloads = new ArrayCollection();
  73.         $this->userGroups = new ArrayCollection();
  74.     }
  75.     public function getNumDownloads(): int
  76.     {
  77.         return $this->numDownloads;
  78.     }
  79.     public function setNumDownloads(int $numDownloads): self
  80.     {
  81.         $this->numDownloads $numDownloads;
  82.         return $this;
  83.     }
  84.     public function getDocumentCategory(): ?DocumentCategory
  85.     {
  86.         return $this->documentCategory;
  87.     }
  88.     public function setDocumentCategory(?DocumentCategory $documentCategory): self
  89.     {
  90.         $this->documentCategory $documentCategory;
  91.         return $this;
  92.     }
  93.     public function getDownloads(): ?Collection
  94.     {
  95.         return $this->downloads;
  96.     }
  97.     public function setDownloads(?Collection $downloads): self
  98.     {
  99.         $this->downloads $downloads;
  100.         return $this;
  101.     }
  102.     public function existsDownload(User $user): bool
  103.     {
  104.         $result false;
  105.         $download = new DocumentDownload();
  106.         $download->setDocument($this);
  107.         $download->setUser($user);
  108.         $predicate = function ($keyDocumentDownload $element) use ($download) {
  109.             return ($element->getDocument()->getId() === $download->getDocument()->getId()) &&
  110.                 ($element->getUser()->getId() === $download->getUser()->getId());
  111.         };
  112.         if ($this->downloads->exists($predicate)) {
  113.             $result true;
  114.         }
  115.         return $result;
  116.     }
  117.     public function addDownload(DocumentDownload $download): self
  118.     {
  119.         $predicate = function ($keyDocumentDownload $element) use ($download) {
  120.             return ($element->getDocument()->getId() === $download->getDocument()->getId()) &&
  121.                 ($element->getUser()->getId() === $download->getUser()->getId());
  122.         };
  123.         if (!$this->downloads->exists($predicate)) {
  124.             $this->downloads->add($download);
  125.             $this->setNumDownloads($this->getNumDownloads() + 1);
  126.         }
  127.         return $this;
  128.     }
  129.     public function removeDownload(DocumentDownload $download): self
  130.     {
  131.         if ($this->downloads->contains($download)) {
  132.             $this->downloads->removeElement($download);
  133.             $this->setNumDownloads($this->getNumDownloads() - 1);
  134.         }
  135.         return $this;
  136.     }
  137.     public function getUserGroups(): ?Collection
  138.     {
  139.         return $this->userGroups;
  140.     }
  141.     public function setUserGroups(?Collection $userGroups): self
  142.     {
  143.         $this->userGroups $userGroups;
  144.         return $this;
  145.     }
  146.     public function getUser(): ?User
  147.     {
  148.         return $this->user;
  149.     }
  150.     public function setUser(?User $user): self
  151.     {
  152.         $this->user $user;
  153.         return $this;
  154.     }
  155.     public function isPrivate(): bool
  156.     {
  157.         return (bool)$this->user;
  158.     }
  159.     public function addUserGroup(UserGroup $userGroup): self
  160.     {
  161.         if (!$this->userGroups->contains($userGroup)) {
  162.             $this->userGroups->add($userGroup);
  163.             $userGroup->addDocument($this);
  164.         }
  165.         return $this;
  166.     }
  167.     public function removeUserGroup(UserGroup $userGroup): self
  168.     {
  169.         if ($this->userGroups->contains($userGroup)) {
  170.             $this->userGroups->removeElement($userGroup);
  171.         }
  172.         return $this;
  173.     }
  174.     public function isExcludedFor(User $user): bool
  175.     {
  176.         $result false;
  177.         // check if the user received as an argument belongs to an excluded userGroup
  178.         if (null !== $this->userGroups) {
  179.             /** @var UserGroup $userGroup */
  180.             foreach ($this->userGroups as $userGroup) {
  181.                 if ((null !== $user->getUserGroup()) && $userGroup->getId() === $user->getUserGroup()->getId()) {
  182.                     $result true;
  183.                     break;
  184.                 }
  185.             }
  186.         }
  187.         // check if the document is exclusive to a specific user other than the user received as an argument.
  188.         if ((null !== $this->user) && $this->user->getId() !== $user->getId()) {
  189.             $result true;
  190.         }
  191.         return $result;
  192.     }
  193.     public function __toString(): string
  194.     {
  195.         return $this->id $this->getName() : MiniAbstractBase::DEFAULT_EMPTY_STRING;
  196.     }
  197. }