<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Document.
*
* @ORM\Table(name="Document")
*
* @ORM\Entity()
*
* @ORM\HasLifecycleCallbacks
*
* @Vich\Uploadable
*/
class Document
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
*
* @ORM\Id
*
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* Assert\NotBlank
*
* @ORM\Column(name="filename", type="string", length=255)
*/
private $filename;
/**
* @var string
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $path;
#[Assert\File(maxSize: '2M')]
private ?File $file = null;
/**
* @var bool
*
* @ORM\Column(name="deleteFile", type="boolean", nullable=true)
*/
private $deleteFile;
/**
* @var \DateTime
*
* @Gedmo\Timestampable(on="create")
*
* @ORM\Column(name="createdAt", type="datetime", nullable=true)
*/
private $createdAt;
/**
* @var \DateTime
*
* @Gedmo\Timestampable(on="update")
*
* @ORM\Column(name="updatedAt", type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @ORM\OneToOne(targetEntity="FicheAction", mappedBy="document1")
*/
private $ficheActionDoc1;
/**
* @ORM\OneToOne(targetEntity="FicheAction", mappedBy="document2")
*/
private $ficheActionDoc2;
/**
* @ORM\OneToOne(targetEntity="FicheAction", mappedBy="document3")
*/
private $ficheActionDoc3;
/**
* @ORM\OneToOne(targetEntity="FicheAction", mappedBy="document4")
*/
private $ficheActionDoc4;
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set doc.
*
* @param File $doc
*/
public function setFile($doc = null): static
{
if (!is_null($doc) && ($doc instanceof File)) {
$this->setUpdatedAt(new \DateTime());
$this->file = $doc;
}
return $this;
}
/**
* Get doc.
*
* @return string
*/
public function getFile(): ?File
{
return $this->file;
}
/**
* Set filename.
*
* @param string $filename
*/
public function setFilename($filename): static
{
$this->filename = $filename;
return $this;
}
/**
* Get filename.
*
* @return string
*/
public function getFilename()
{
return $this->filename;
}
/**
* Set path.
*
* @param string $path
*/
public function setPath($path): static
{
$this->path = $path;
return $this;
}
/**
* Get path.
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set createdAt.
*
* @param \DateTime $createdAt
*/
public function setCreatedAt($createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt.
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt.
*
* @param \DateTime $updatedAt
*/
public function setUpdatedAt($updatedAt): static
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt.
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set ficheActionDoc1.
*/
public function setFicheActionDoc1(?FicheAction $ficheActionDoc1 = null): static
{
$this->ficheActionDoc1 = $ficheActionDoc1;
return $this;
}
/**
* Get ficheActionDoc1.
*
* @return FicheAction
*/
public function getFicheActionDoc1()
{
return $this->ficheActionDoc1;
}
/**
* Set ficheActionDoc2.
*/
public function setFicheActionDoc2(?FicheAction $ficheActionDoc2 = null): static
{
$this->ficheActionDoc2 = $ficheActionDoc2;
return $this;
}
/**
* Get ficheActionDoc2.
*
* @return FicheAction
*/
public function getFicheActionDoc2()
{
return $this->ficheActionDoc2;
}
/**
* Set ficheActionDoc3.
*/
public function setFicheActionDoc3(?FicheAction $ficheActionDoc3 = null): static
{
$this->ficheActionDoc3 = $ficheActionDoc3;
return $this;
}
/**
* Get ficheActionDoc3.
*
* @return FicheAction
*/
public function getFicheActionDoc3()
{
return $this->ficheActionDoc3;
}
/**
* Set ficheActionDoc4.
*/
public function setFicheActionDoc4(?FicheAction $ficheActionDoc4 = null): static
{
$this->ficheActionDoc4 = $ficheActionDoc4;
return $this;
}
/**
* Get ficheActionDoc4.
*
* @return FicheAction
*/
public function getFicheActionDoc4()
{
return $this->ficheActionDoc4;
}
#[\Override]
public function __toString(): string
{
$result = '';
$filename = explode('_', $this->filename);
if (count($filename) > 2) {
$result = $filename[1];
}
return $result;
}
public function getAbsolutePath()
{
return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path ? null : $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir(): string
{
// le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
return __DIR__.'/../../public/'.$this->getUploadDir();
}
protected function getUploadDir(): string
{
// on se débarrasse de « __DIR__ » afin de ne pas avoir de problème lorsqu'on affiche
// le document/image dans la vue.
// return 'uploads/files/documents/uploaded';
return 'uploads/fiches_action';
}
/**
* @ORM\PrePersist()
*
* @ORM\PreUpdate()
*/
public function preUpload(): void
{
if ($this->file instanceof File) {
$this->path = sha1(uniqid(random_int(0, mt_getrandmax()), true)).'.'.$this->file->guessExtension();
$this->filename = $this->file->getClientOriginalName();
}
}
/**
* @ORM\PostPersist()
*
* @ORM\PostUpdate()
*/
public function upload(): void
{
if (!$this->file instanceof File) {
return;
}
// s'il y a une erreur lors du déplacement du fichier, une exception
// va automatiquement être lancée par la méthode move(). Cela va empêcher
// proprement l'entité d'être persistée dans la base de données si
// erreur il y a
$this->file->move($this->getUploadRootDir(), $this->path);
unset($this->file);
}
/**
* @ORM\PostRemove()
*/
public function removeUpload(): void
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
public function getDocumentSize(): ?string
{
if (file_exists($this->getAbsolutePath())) {
$bytes = filesize($this->getAbsolutePath());
$human_filesize = $this->human_filesize($bytes);
} else {
$human_filesize = null;
}
return $human_filesize;
}
private function human_filesize(int|bool $bytes, $decimals = 2): string
{
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf(sprintf('%%.%sf', $decimals), $bytes / 1024 ** $factor).@$sz[$factor];
}
/**
* Set deleteFile.
*
* @param bool $deleteFile
*/
public function setDeleteFile($deleteFile): static
{
$this->deleteFile = $deleteFile;
return $this;
}
/**
* Get deleteFile.
*
* @return bool
*/
public function getDeleteFile()
{
return $this->deleteFile;
}
public function isDeleteFile(): ?bool
{
return $this->deleteFile;
}
}