src/Entity/Document.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\DBAL\Types\Types;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7. /**
  8.  * @ORM\Entity
  9.  * @ORM\Table(name="mmpp_document")
  10.  * @ORM\HasLifecycleCallbacks
  11.  */
  12. class Document
  13. {
  14.     /**
  15.      * @ORM\Id
  16.      * @ORM\Column(type="integer")
  17.      * @ORM\GeneratedValue(strategy="IDENTITY")
  18.      */
  19.     protected $id;
  20.     /**
  21.      * @ORM\Column(type="string", length=255)
  22.      */
  23.     protected $title;
  24.     /**
  25.      * @Gedmo\Slug(fields={"title"}, updatable=true, separator="-", unique=false)
  26.      * @ORM\Column(type="string", length=255, unique=false)
  27.      */
  28.     protected $slug;
  29.     /**
  30.      * @ORM\Column(type="string", length=255)
  31.      */
  32.     protected $path;
  33.     /**
  34.      * @ORM\Column(name="is_active", type="boolean")
  35.      */
  36.     protected $isActive true;
  37.     /**
  38.      * @Assert\File(
  39.      *      maxSize="10M",
  40.      *   mimeTypes = {
  41.       *     "application/pdf",
  42.       *     "application/x-pdf"
  43.       *   }
  44.      * )
  45.      */
  46.     public $file;
  47.     /**
  48.      * @Gedmo\Timestampable(on="create")
  49.      * @ORM\Column(type="datetime", name="created_on")
  50.      * @var \DateTime
  51.      */
  52.     protected $createdOn;
  53.     /**
  54.      * @Gedmo\Timestampable(on="update")
  55.      * @ORM\Column(type="datetime", name="updated_on")
  56.      * @var \DateTime
  57.      */
  58.     protected $updatedOn;
  59.     /**
  60.      * @ORM\ManyToOne(targetEntity="User", inversedBy="documents")
  61.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  62.      */
  63.     protected $user;
  64.     /**
  65.      * @ORM\ManyToOne(targetEntity="Formation", inversedBy="documents")
  66.      * @ORM\JoinColumn(name="formation_id", referencedColumnName="id")
  67.      */
  68.     protected $formation;
  69.     public function getAbsolutePath()
  70.     {
  71.         return null === $this->path null $this->getUploadRootDir().'/'.$this->path;
  72.     }
  73.     public function getWebPath()
  74.     {
  75.         return null === $this->path null $this->getUploadDir().'/'.$this->path;
  76.     }
  77.     protected function getUploadRootDir()
  78.     {
  79.         // le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
  80.         $rootDir __DIR__.'/../../../../web/'.$this->getUploadDir();
  81.         if ( ! is_dir($rootDir)) {
  82.             mkdir($rootDir0777true);
  83.         }
  84.         return __DIR__.'/../../../../web/'.$this->getUploadDir();
  85.     }
  86.     protected function getUploadDir()
  87.     {
  88.         // on se débarrasse de « __DIR__ » afin de ne pas avoir de problème lorsqu'on affiche
  89.         // le document/image dans la vue.
  90.         return 'uploads/sliders';
  91.     }
  92.     /**
  93.      * @ORM\PrePersist()
  94.      * @ORM\PreUpdate()
  95.      */
  96.     public function preUpload()
  97.     {
  98.         if (null !== $this->file) {
  99.             // faites ce que vous voulez pour générer un nom unique
  100.             $this->path sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension();
  101.         }
  102.     }
  103.     /**
  104.      * @ORM\PostPersist()
  105.      * @ORM\PostUpdate()
  106.      */
  107.     public function upload()
  108.     {
  109.         if (null === $this->file) {
  110.             return;
  111.         }
  112.         // s'il y a une erreur lors du déplacement du fichier, une exception
  113.         // va automatiquement être lancée par la méthode move(). Cela va empêcher
  114.         // proprement l'entité d'être persistée dans la base de données si
  115.         // erreur il y a
  116.         $this->file->move($this->getUploadRootDir(), $this->path);
  117.         unset($this->file);
  118.     }
  119.     /**
  120.      * @ORM\PostRemove()
  121.      */
  122.     public function removeUpload()
  123.     {
  124.         if ($file $this->getAbsolutePath()) {
  125.             unlink($file);
  126.         }
  127.     }
  128.     public function getId(): ?int
  129.     {
  130.         return $this->id;
  131.     }
  132.     public function getTitle(): ?string
  133.     {
  134.         return $this->title;
  135.     }
  136.     public function setTitle(string $title): self
  137.     {
  138.         $this->title $title;
  139.         return $this;
  140.     }
  141.     public function getSlug(): ?string
  142.     {
  143.         return $this->slug;
  144.     }
  145.     public function setSlug(string $slug): self
  146.     {
  147.         $this->slug $slug;
  148.         return $this;
  149.     }
  150.     public function getPath(): ?string
  151.     {
  152.         return $this->path;
  153.     }
  154.     public function setPath(string $path): self
  155.     {
  156.         $this->path $path;
  157.         return $this;
  158.     }
  159.     public function isIsActive(): ?bool
  160.     {
  161.         return $this->isActive;
  162.     }
  163.     public function setIsActive(bool $isActive): self
  164.     {
  165.         $this->isActive $isActive;
  166.         return $this;
  167.     }
  168.     public function getCreatedOn(): ?\DateTimeInterface
  169.     {
  170.         return $this->createdOn;
  171.     }
  172.     public function setCreatedOn(\DateTimeInterface $createdOn): self
  173.     {
  174.         $this->createdOn $createdOn;
  175.         return $this;
  176.     }
  177.     public function getUpdatedOn(): ?\DateTimeInterface
  178.     {
  179.         return $this->updatedOn;
  180.     }
  181.     public function setUpdatedOn(\DateTimeInterface $updatedOn): self
  182.     {
  183.         $this->updatedOn $updatedOn;
  184.         return $this;
  185.     }
  186.     public function getUser(): ?User
  187.     {
  188.         return $this->user;
  189.     }
  190.     public function setUser(?User $user): self
  191.     {
  192.         $this->user $user;
  193.         return $this;
  194.     }
  195.     public function getFormation(): ?Formation
  196.     {
  197.         return $this->formation;
  198.     }
  199.     public function setFormation(?Formation $formation): self
  200.     {
  201.         $this->formation $formation;
  202.         return $this;
  203.     }
  204. }