<?php
namespace App\Entity\Media;
use App\Entity\Federation\Boat;
use App\Entity\Federation\Skipper;
use App\Entity\User\Alert;
use App\Entity\User\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use App\Entity\Federation\Team;
use App\Entity\Federation\Classe;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use App\Entity\Federation\Race;
use App\Entity\Media\Import;
use App\Entity\Site\AccessSite;
/**
* @ORM\Entity(repositoryClass="App\Repository\Media\DocumentRepository")
* @ORM\Table(name="documents")
* @ORM\HasLifecycleCallbacks
*/
class Document
{
const THUMBNAIL_NAME = 'thumb.0000000.jpg';
/**
* @ORM\Id
* @ORM\Column(type="integer", name="id")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true, name="title")
* @Assert\NotBlank(groups={"DocumentTypeSingle"})
*/
private $title;
/**
* @ORM\Column(type="text", nullable=true, name="description")
*/
private $description;
/**
* @ORM\Column(type="datetime", nullable=true, name="publicationDate")
*/
private $publicationDate;
/**
* @ORM\Column(type="string", length=255, nullable=true, name="copyright")
*/
private $copyright;
/**
* @ORM\Column(type="string", length=500, nullable=true, name="legend")
*/
private $legend;
/**
* @ORM\Column(type="string", length=255, nullable=true, name="author")
*/
private $author;
/**
* @ORM\Column(type="string", length=255, nullable=true, name="localization")
*/
private $localization;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $originalName;
/**
* @ORM\Column(type="string", length=255, nullable=true, unique=true)
*/
private $directoryName;
/**
* @ORM\Column(type="array", nullable=true)
*/
private $file;
/**
* @ORM\Column(type="array", nullable=true)
*/
private $attachment;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Media\Document", inversedBy="documentsChild", cascade={"persist"})
*/
private $parentDocument;
/**
* @ORM\Column(type="datetime", nullable=true, name="date")
*/
private $date;
/**
* @ORM\Column(type="boolean", nullable=true, name="notify")
*/
private $notify = 0;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Media\Share", mappedBy="document", cascade={"remove"})
*/
private $shares;
/**
* @ORM\Column(type="datetime", nullable=false, name="created_date")
* @Gedmo\Timestampable(on="create")
*/
private $createdDate;
/**
* @ORM\Column(type="datetime", nullable=true, name="updated_date")
* @Gedmo\Timestampable(on="update")
*/
private $updatedDate;
/**
* @ORM\Column(type="integer", nullable=false, name="status")
*/
private $status = 0;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Media\Conversion", mappedBy="document", cascade={"persist", "remove"})
*/
private $conversions;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Media\Document", mappedBy="parentDocument", cascade={"persist", "remove"})
*/
private $documentsChild;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Federation\Race", inversedBy="documents")
* @ORM\JoinColumn(name="race_id", referencedColumnName="id")
*/
private $race;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Media\DocumentType", inversedBy="documents")
* @ORM\JoinColumn(name="document_type_id", referencedColumnName="id")
*/
private $documentType;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Federation\Skipper", inversedBy="documents")
* @ORM\JoinTable(
* name="documents_skippers",
* joinColumns={@ORM\JoinColumn(name="document_id", referencedColumnName="id", nullable=false)},
* inverseJoinColumns={@ORM\JoinColumn(name="skipper_id", referencedColumnName="id", nullable=false)}
* )
*/
private $skippers;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Federation\Boat", inversedBy="documents")
* @ORM\JoinTable(
* name="documents_boats",
* joinColumns={@ORM\JoinColumn(name="document_id", referencedColumnName="id", nullable=false)},
* inverseJoinColumns={@ORM\JoinColumn(name="boat_id", referencedColumnName="id", nullable=false)}
* )
*/
private $boats;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Federation\Team", inversedBy="documents")
* @ORM\JoinTable(
* name="documents_teams",
* joinColumns={@ORM\JoinColumn(name="document_id", referencedColumnName="id", nullable=false)},
* inverseJoinColumns={@ORM\JoinColumn(name="team_id", referencedColumnName="id", nullable=false)}
* )
*/
private $teams;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Federation\Classe", inversedBy="documents")
* @ORM\JoinTable(
* name="documents_classes",
* joinColumns={@ORM\JoinColumn(name="document_id", referencedColumnName="id", nullable=false)},
* inverseJoinColumns={@ORM\JoinColumn(name="classe_id", referencedColumnName="id", nullable=false)}
* )
*/
private $classes;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Media\Category", inversedBy="documents")
* @ORM\JoinTable(
* name="documents_categories",
* joinColumns={@ORM\JoinColumn(name="document_id", referencedColumnName="id", nullable=false)},
* inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=false)}
* )
* @Assert\Count(min = 1, groups={"DocumentTypeSingle"})
*/
private $categories;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Media\Tag", inversedBy="documents")
* @ORM\JoinTable(
* name="documents_tags",
* joinColumns={@ORM\JoinColumn(name="document_id", referencedColumnName="id", nullable=false)},
* inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id", nullable=false)}
* )
*/
private $tags;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $proofSendDate;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User\User", inversedBy="proofValidations")
*/
private $proofValidationUser;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $proofValidationDate;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Media\Import", inversedBy="documents")
* @ORM\JoinColumn(name="import_id", referencedColumnName="id")
*/
private $import;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Media\Quality", inversedBy="documents")
* @ORM\JoinColumn(name="quality_id", referencedColumnName="id")
*/
private $quality;
/**
* @ORM\OneToMany(targetEntity="App\Entity\User\Alert", mappedBy="document", cascade={"persist", "remove"})
*/
private $alerts;
public function __construct()
{
$this->conversions = new ArrayCollection();
$this->skippers = new ArrayCollection();
$this->boats = new ArrayCollection();
$this->teams = new ArrayCollection();
$this->classes = new ArrayCollection();
$this->categories = new ArrayCollection();
$this->tags = new ArrayCollection();
$this->documentsChild = new ArrayCollection();
$this->shares = new ArrayCollection();
$this->alerts = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getCopyright(): ?string
{
return $this->copyright;
}
public function setCopyright(?string $copyright): self
{
$this->copyright = $copyright;
return $this;
}
public function getDirectoryName(): ?string
{
return $this->directoryName;
}
public function setDirectoryName(?string $directoryName): self
{
$this->directoryName = $directoryName;
return $this;
}
public function getOriginalName(): ?string
{
return $this->originalName;
}
public function setOriginalName(?string $originalName): self
{
$this->originalName = $originalName;
return $this;
}
public function getCreatedDate(): ?\DateTimeInterface
{
return $this->createdDate;
}
public function getUpdatedDate(): ?\DateTimeInterface
{
return $this->updatedDate;
}
public function getStatus(): ?int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->status = $status;
return $this;
}
public function isNotify(): ?bool
{
return $this->notify;
}
public function setNotify(bool $notify): self
{
$this->notify = $notify;
return $this;
}
/**
* @return Collection|Conversion[]
*/
public function getConversions(): Collection
{
return $this->conversions;
}
public function addConversion(Conversion $conversion): self
{
if (!$this->conversions->contains($conversion)) {
$this->conversions[] = $conversion;
$conversion->setDocument($this);
}
return $this;
}
public function removeConversion(Conversion $conversion): self
{
if ($this->conversions->contains($conversion)) {
$this->conversions->removeElement($conversion);
// set the owning side to null (unless already changed)
if ($conversion->getDocument() === $this) {
$conversion->setDocument(null);
}
}
return $this;
}
public function getInProgressConversions()
{
$result = 0;
if(!empty($this->getConversions()))
{
foreach($this->getConversions() as $conversion)
{
if($conversion->getStatus() == 0 && empty($conversion->getFile()) && !empty($conversion->getJob()))
$result ++;
}
}
return $result;
}
public function getFinishedConversions()
{
$result = 0;
if(!empty($this->getConversions()))
{
foreach($this->getConversions() as $conversion)
{
if($conversion->getStatus() == 1 && !empty($conversion->getFile() && !empty($conversion->getJob())))
$result ++;
if($conversion->isInitialFormat())
$result ++;
}
}
return $result;
}
public function getRace(): ?Race
{
return $this->race;
}
public function setRace(?Race $race): self
{
$this->race = $race;
return $this;
}
public function getDocumentType(): ?DocumentType
{
return $this->documentType;
}
public function setDocumentType(?DocumentType $documentType): self
{
$this->documentType = $documentType;
return $this;
}
/**
* @return Collection|Skipper[]
*/
public function getSkippers(): Collection
{
return $this->skippers;
}
public function addSkipper(Skipper $skipper): self
{
if (!$this->skippers->contains($skipper)) {
$this->skippers[] = $skipper;
}
return $this;
}
public function removeSkipper(Skipper $skipper): self
{
if ($this->skippers->contains($skipper)) {
$this->skippers->removeElement($skipper);
}
return $this;
}
/**
* @return Collection|Boat[]
*/
public function getBoats(): Collection
{
return $this->boats;
}
public function addBoat(Boat $boat): self
{
if (!$this->boats->contains($boat)) {
$this->boats[] = $boat;
}
return $this;
}
public function removeBoat(Boat $boat): self
{
if ($this->boats->contains($boat)) {
$this->boats->removeElement($boat);
}
return $this;
}
/**
* @return Collection|Team[]
*/
public function getTeams(): Collection
{
return $this->teams;
}
public function addTeam(Team $team): self
{
if (!$this->teams->contains($team)) {
$this->teams[] = $team;
}
return $this;
}
public function removeTeam(Team $team): self
{
if ($this->teams->contains($team)) {
$this->teams->removeElement($team);
}
return $this;
}
/**
* @return Collection|Classe[]
*/
public function getClasses(): Collection
{
return $this->classes;
}
public function addClass(Classe $class): self
{
if (!$this->classes->contains($class)) {
$this->classes[] = $class;
}
return $this;
}
public function removeClass(Classe $class): self
{
if ($this->classes->contains($class)) {
$this->classes->removeElement($class);
}
return $this;
}
/**
* @return Collection|Category[]
*/
public function getCategories(): Collection
{
return $this->categories;
}
public function addCategory(Category $category): self
{
if (!$this->categories->contains($category)) {
$this->categories[] = $category;
$category->addDocument($this);
}
return $this;
}
public function removeCategory(Category $category): self
{
if ($this->categories->contains($category)) {
$this->categories->removeElement($category);
$category->removeDocument($this);
}
return $this;
}
/**
* @return Collection|Tag[]
*/
public function getTags(): Collection
{
return $this->tags;
}
public function addTag(Tag $tag): self
{
if (!$this->tags->contains($tag)) {
$this->tags[] = $tag;
$tag->addDocument($this);
}
return $this;
}
public function removeTag(Tag $tag): self
{
if ($this->tags->contains($tag)) {
$this->tags->removeElement($tag);
$tag->removeDocument($this);
}
return $this;
}
public function getProofValidationDate(): ?\DateTimeInterface
{
return $this->proofValidationDate;
}
public function setProofValidationDate(?\DateTimeInterface $proofValidationDate): self
{
$this->proofValidationDate = $proofValidationDate;
return $this;
}
public function getProofValidationUser(): ?User
{
return $this->proofValidationUser;
}
public function setProofValidationUser(?User $proofValidationUser): self
{
$this->proofValidationUser = $proofValidationUser;
return $this;
}
public function getLegend(): ?string
{
return $this->legend;
}
public function setLegend(?string $legend): self
{
$this->legend = $legend;
return $this;
}
public function getAuthor(): ?string
{
return $this->author;
}
public function setAuthor(?string $author): self
{
$this->author = $author;
return $this;
}
public function getLocalization(): ?string
{
return $this->localization;
}
public function setLocalization(?string $localization): self
{
$this->localization = $localization;
return $this;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(\DateTimeInterface $date = null): self
{
$this->date = $date;
return $this;
}
public function getPublicationDate(): ?\DateTimeInterface
{
return $this->publicationDate;
}
public function setPublicationDate(\DateTimeInterface $publicationDate = null): self
{
$this->publicationDate = $publicationDate;
return $this;
}
public function getProofSendDate(): ?\DateTimeInterface
{
return $this->proofSendDate;
}
public function setProofSendDate(\DateTimeInterface $proofSendDate = null): self
{
$this->proofSendDate = $proofSendDate;
return $this;
}
public function getFile(): ?array
{
return $this->file;
}
public function setFile(?array $file): self
{
$this->file = $file;
return $this;
}
public function getAttachment(): ?array
{
return $this->attachment;
}
public function setAttachment(?array $attachment): self
{
$this->attachment = $attachment;
return $this;
}
public function getParentDocument(): ?self
{
return $this->parentDocument;
}
public function setParentDocument(?self $parentDocument): self
{
$this->parentDocument = $parentDocument;
return $this;
}
/**
* @return Collection|Document[]
*/
public function getDocumentsChild(): Collection
{
return $this->documentsChild;
}
public function addDocumentsChild(Document $documentsChild): self
{
if (!$this->documentsChild->contains($documentsChild)) {
$this->documentsChild[] = $documentsChild;
$documentsChild->setParentDocument($this);
}
return $this;
}
public function removeDocumentsChild(Document $documentsChild): self
{
if ($this->documentsChild->contains($documentsChild)) {
$this->documentsChild->removeElement($documentsChild);
// set the owning side to null (unless already changed)
if ($documentsChild->getParentDocument() === $this) {
$documentsChild->setParentDocument(null);
}
}
return $this;
}
/**
* @return Collection|Share[]
*/
public function getShares(): Collection
{
return $this->shares;
}
public function addShare(Share $share): self
{
if (!$this->shares->contains($share)) {
$this->shares[] = $share;
}
return $this;
}
public function removeShare(Share $share): self
{
if ($this->shares->contains($share)) {
$this->shares->removeElement($share);
}
return $this;
}
/**
* @return Collection|Alert[]
*/
public function getAlerts(): Collection
{
return $this->alerts;
}
public function addAlert(Alert $alert): self
{
if (!$this->alerts->contains($alert)) {
$this->alerts[] = $alert;
}
return $this;
}
public function removeAlert(Alert $alert): self
{
if ($this->alerts->contains($alert)) {
$this->alerts->removeElement($alert);
}
return $this;
}
public function getImport(): ?Import
{
return $this->import;
}
public function setImport(?Import $import): self
{
$this->import = $import;
return $this;
}
public function getQuality(): ?Quality
{
return $this->quality;
}
public function setQuality(?Quality $quality): self
{
$this->quality = $quality;
return $this;
}
public function getRootUri()
{
return 'https://'.$this->getRace()->getDirectoryName().'.s3.amazonaws.com';
}
public function getSecondThumb()
{
return $this->getRootUri().'/'.$this->getDirectoryName().'/'.self::THUMBNAIL_NAME;
}
public function getUri()
{
return $this->getRootUri().'/'.$this->getFile()['Key'];
}
public function getCopyrightLabel()
{
return '© '.$this->getCopyright();
}
public function getThumbnail($relative = false)
{
$thumbnail = false;
if($this->getDocumentType()->getDirectoryName() == 'pictures')
{
if(!empty($this->getDocumentsChild()))
{
//Search Thumbnail
foreach($this->getDocumentsChild() as $child)
{
if($child->getQuality()->getCode() == 'BD')
$thumbnail .= $child->getFile()['Key'];
}
if(!$thumbnail)
{
foreach($this->getDocumentsChild() as $child)
$thumbnail .= $child->getFile()['Key'];
}
}
}
if(!empty($thumbnail) && $relative == false)
$thumbnail = $this->getRootUri().'/'.$thumbnail;
return $thumbnail;
}
public function __clone()
{
$this->id = null;
}
public function checkAccessByAccessSite(AccessSite $accessSite)
{
$access = false;
$restrictiveCategory = false;
foreach($this->getCategories() as $category)
if($category->isRestrictiveCategory())
$restrictiveCategory = true;
if($restrictiveCategory)
{
$boatsCompare = true;
if(sizeof($accessSite->getBoats()) > 0)
foreach($this->getBoats() as $boat)
if(!$accessSite->getBoats()->contains($boat))
$boatsCompare = false;
$skippersCompare = true;
if(sizeof($accessSite->getSkippers()) > 0)
foreach($this->getSkippers() as $boat)
if(!$accessSite->getSkippers()->contains($boat))
$skippersCompare = false;
$classesCompare = true;
if(sizeof($accessSite->getClasses()) > 0)
foreach($this->getClasses() as $boat)
if(!$accessSite->getClasses()->contains($boat))
$classesCompare = false;
if($boatsCompare && $skippersCompare && $classesCompare)
$access = true;
} else {
$access = true;
}
return $access;
}
public function getReferents(): array
{
$referents = [];
if(!empty($this->getBoats()))
{
foreach($this->getBoats() as $boat)
foreach($boat->getParticipations() as $participation)
if($participation->getRace() == $this->getRace())
foreach($participation->getReferents() as $referent)
$referents[] = $referent;
}
return $referents;
}
}