<?php
namespace App\Entity;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity
* @ORM\Table(name="mmpp_document")
* @ORM\HasLifecycleCallbacks
*/
class Document
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*/
protected $title;
/**
* @Gedmo\Slug(fields={"title"}, updatable=true, separator="-", unique=false)
* @ORM\Column(type="string", length=255, unique=false)
*/
protected $slug;
/**
* @ORM\Column(type="string", length=255)
*/
protected $path;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
protected $isActive = true;
/**
* @Assert\File(
* maxSize="10M",
* mimeTypes = {
* "application/pdf",
* "application/x-pdf"
* }
* )
*/
public $file;
/**
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime", name="created_on")
* @var \DateTime
*/
protected $createdOn;
/**
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime", name="updated_on")
* @var \DateTime
*/
protected $updatedOn;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="documents")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* @ORM\ManyToOne(targetEntity="Formation", inversedBy="documents")
* @ORM\JoinColumn(name="formation_id", referencedColumnName="id")
*/
protected $formation;
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()
{
// le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
$rootDir = __DIR__.'/../../../../web/'.$this->getUploadDir();
if ( ! is_dir($rootDir)) {
mkdir($rootDir, 0777, true);
}
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// 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/sliders';
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->file) {
// faites ce que vous voulez pour générer un nom unique
$this->path = sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->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()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
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 getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getPath(): ?string
{
return $this->path;
}
public function setPath(string $path): self
{
$this->path = $path;
return $this;
}
public function isIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getCreatedOn(): ?\DateTimeInterface
{
return $this->createdOn;
}
public function setCreatedOn(\DateTimeInterface $createdOn): self
{
$this->createdOn = $createdOn;
return $this;
}
public function getUpdatedOn(): ?\DateTimeInterface
{
return $this->updatedOn;
}
public function setUpdatedOn(\DateTimeInterface $updatedOn): self
{
$this->updatedOn = $updatedOn;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getFormation(): ?Formation
{
return $this->formation;
}
public function setFormation(?Formation $formation): self
{
$this->formation = $formation;
return $this;
}
}