src/Entity/InscriptionFile.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\DBAL\Types\Types;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Gedmo\Mapping\Annotation as Gedmo;
  9. /**
  10.  * @ORM\Entity
  11.  * @ORM\Table(name="mmpp_inscription_file")
  12.  * @ORM\HasLifecycleCallbacks
  13.  * @ORM\Entity(repositoryClass="App\Repository\InscriptionFileRepository")
  14.  */
  15. class InscriptionFile
  16. {
  17.     /**
  18.      * @ORM\Id
  19.      * @ORM\Column(type="integer")
  20.      * @ORM\GeneratedValue(strategy="AUTO")
  21.      */
  22.     protected $id;
  23.     /**
  24.      * @ORM\Column(type="string", length=255)
  25.      */
  26.     protected $title;
  27.     /**
  28.      * @Gedmo\Slug(fields={"title"}, updatable=true, separator="-", unique=false)
  29.      * @ORM\Column(type="string", length=255, unique=false)
  30.      */
  31.     protected $slug;
  32.     /**
  33.      * @ORM\Column(type="string", length=255)
  34.      */
  35.     protected $path;
  36.     /**
  37.      * @ORM\Column(name="is_active", type="boolean")
  38.      */
  39.     protected $isActive true;
  40.     /**
  41.      * @Assert\File(
  42.      *      maxSize="20M",
  43.      *   mimeTypes = {
  44.       *     "application/pdf",
  45.       *     "application/x-pdf"
  46.       *   }
  47.      * )
  48.      */
  49.     public $file;
  50.     /**
  51.      * @Gedmo\Timestampable(on="create")
  52.      * @ORM\Column(type="datetime", name="created_on")
  53.      * @var \DateTime
  54.      */
  55.     protected $createdOn;
  56.     /**
  57.      * @Gedmo\Timestampable(on="update")
  58.      * @ORM\Column(type="datetime", name="updated_on")
  59.      * @var \DateTime
  60.      */
  61.     protected $updatedOn;
  62.     /**
  63.      * @ORM\ManyToOne(targetEntity="User", inversedBy="tarifs")
  64.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  65.      */
  66.     protected $user;
  67.     /**
  68.      * @ORM\ManyToOne(targetEntity="Formulaire", inversedBy="files")
  69.      * @ORM\JoinColumn(name="formulaire_id", referencedColumnName="id")
  70.      */
  71.     protected $formulaire;
  72.     /**
  73.      * @ORM\OneToMany(targetEntity="InscriptionFileLog", mappedBy="inscriptionFile", cascade={"remove"})
  74.      * @var unknown
  75.      */
  76.     protected $logs;
  77.     /**
  78.      * Constructor
  79.      */
  80.     public function __construct()
  81.     {
  82.         $this->logs = new ArrayCollection();
  83.     }
  84.     public function getAbsolutePath()
  85.     {
  86.         return null === $this->path null $this->getUploadRootDir().'/'.$this->path;
  87.     }
  88.     public function getWebPath()
  89.     {
  90.         return null === $this->path null $this->getUploadDir().'/'.$this->path;
  91.     }
  92.     protected function getUploadRootDir()
  93.     {
  94.         // le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
  95.         $rootDir __DIR__.'/../../../../web/'.$this->getUploadDir();
  96.         if ( ! is_dir($rootDir)) {
  97.             mkdir($rootDir0777true);
  98.         }
  99.         return __DIR__.'/../../../../web/'.$this->getUploadDir();
  100.     }
  101.     protected function getUploadDir()
  102.     {
  103.         // on se débarrasse de « __DIR__ » afin de ne pas avoir de problème lorsqu'on affiche
  104.         // le document/image dans la vue.
  105.         return 'uploads/sliders';
  106.     }
  107.     /**
  108.      * @ORM\PrePersist()
  109.      * @ORM\PreUpdate()
  110.      */
  111.     public function preUpload()
  112.     {
  113.         if (null !== $this->file) {
  114.             // faites ce que vous voulez pour générer un nom unique
  115.             $this->path sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension();
  116.         }
  117.     }
  118.     /**
  119.      * @ORM\PostPersist()
  120.      * @ORM\PostUpdate()
  121.      */
  122.     public function upload()
  123.     {
  124.         if (null === $this->file) {
  125.             return;
  126.         }
  127.         // s'il y a une erreur lors du déplacement du fichier, une exception
  128.         // va automatiquement être lancée par la méthode move(). Cela va empêcher
  129.         // proprement l'entité d'être persistée dans la base de données si
  130.         // erreur il y a
  131.         $this->file->move($this->getUploadRootDir(), $this->path);
  132.         unset($this->file);
  133.     }
  134.     /**
  135.      * @ORM\PostRemove()
  136.      */
  137.     public function removeUpload()
  138.     {
  139.         if ($file $this->getAbsolutePath()) {
  140.             unlink($file);
  141.         }
  142.     }
  143.     /**
  144.      * Get id
  145.      *
  146.      * @return integer
  147.      */
  148.     public function getId()
  149.     {
  150.         return $this->id;
  151.     }
  152.     /**
  153.      * Set title
  154.      *
  155.      * @param string $title
  156.      * @return InscriptionFile
  157.      */
  158.     public function setTitle($title)
  159.     {
  160.         $this->title $title;
  161.         return $this;
  162.     }
  163.     /**
  164.      * Get title
  165.      *
  166.      * @return string
  167.      */
  168.     public function getTitle()
  169.     {
  170.         return $this->title;
  171.     }
  172.     /**
  173.      * Set slug
  174.      *
  175.      * @param string $slug
  176.      * @return InscriptionFile
  177.      */
  178.     public function setSlug($slug)
  179.     {
  180.         $this->slug $slug;
  181.         return $this;
  182.     }
  183.     /**
  184.      * Get slug
  185.      *
  186.      * @return string
  187.      */
  188.     public function getSlug()
  189.     {
  190.         return $this->slug;
  191.     }
  192.     /**
  193.      * Set path
  194.      *
  195.      * @param string $path
  196.      * @return InscriptionFile
  197.      */
  198.     public function setPath($path)
  199.     {
  200.         $this->path $path;
  201.         return $this;
  202.     }
  203.     /**
  204.      * Get path
  205.      *
  206.      * @return string
  207.      */
  208.     public function getPath()
  209.     {
  210.         return $this->path;
  211.     }
  212.     /**
  213.      * Set isActive
  214.      *
  215.      * @param boolean $isActive
  216.      * @return InscriptionFile
  217.      */
  218.     public function setIsActive($isActive)
  219.     {
  220.         $this->isActive $isActive;
  221.         return $this;
  222.     }
  223.     /**
  224.      * Get isActive
  225.      *
  226.      * @return boolean
  227.      */
  228.     public function getIsActive()
  229.     {
  230.         return $this->isActive;
  231.     }
  232.     /**
  233.      * Set createdOn
  234.      *
  235.      * @param \DateTime $createdOn
  236.      * @return InscriptionFile
  237.      */
  238.     public function setCreatedOn($createdOn)
  239.     {
  240.         $this->createdOn $createdOn;
  241.         return $this;
  242.     }
  243.     /**
  244.      * Get createdOn
  245.      *
  246.      * @return \DateTime
  247.      */
  248.     public function getCreatedOn()
  249.     {
  250.         return $this->createdOn;
  251.     }
  252.     /**
  253.      * Set updatedOn
  254.      *
  255.      * @param \DateTime $updatedOn
  256.      * @return InscriptionFile
  257.      */
  258.     public function setUpdatedOn($updatedOn)
  259.     {
  260.         $this->updatedOn $updatedOn;
  261.         return $this;
  262.     }
  263.     /**
  264.      * Get updatedOn
  265.      *
  266.      * @return \DateTime
  267.      */
  268.     public function getUpdatedOn()
  269.     {
  270.         return $this->updatedOn;
  271.     }
  272.     /**
  273.      * Set user
  274.      *
  275.      * @param \App\Entity\User $user
  276.      * @return InscriptionFile
  277.      */
  278.     public function setUser(\App\Entity\User $user null)
  279.     {
  280.         $this->user $user;
  281.         return $this;
  282.     }
  283.     /**
  284.      * Get user
  285.      *
  286.      * @return \App\Entity\User
  287.      */
  288.     public function getUser()
  289.     {
  290.         return $this->user;
  291.     }
  292.     /**
  293.      * Set formulaire
  294.      *
  295.      * @param \App\Entity\Formulaire $formulaire
  296.      * @return InscriptionFile
  297.      */
  298.     public function setFormulaire(\App\Entity\Formulaire $formulaire null)
  299.     {
  300.         $this->formulaire $formulaire;
  301.         return $this;
  302.     }
  303.     /**
  304.      * Get formulaire
  305.      *
  306.      * @return \App\Entity\Formulaire
  307.      */
  308.     public function getFormulaire()
  309.     {
  310.         return $this->formulaire;
  311.     }
  312.     /**
  313.      * Add logs
  314.      *
  315.      * @param \App\Entity\InscriptionFileLog $logs
  316.      * @return InscriptionFile
  317.      */
  318.     public function addLog(\App\Entity\InscriptionFileLog $logs)
  319.     {
  320.         $this->logs[] = $logs;
  321.         return $this;
  322.     }
  323.     /**
  324.      * Remove logs
  325.      *
  326.      * @param \App\Entity\InscriptionFileLog $logs
  327.      */
  328.     public function removeLog(\App\Entity\InscriptionFileLog $logs)
  329.     {
  330.         $this->logs->removeElement($logs);
  331.     }
  332.     /**
  333.      * Get logs
  334.      *
  335.      * @return \Doctrine\Common\Collections\Collection
  336.      */
  337.     public function getLogs()
  338.     {
  339.         return $this->logs;
  340.     }
  341.     public function isIsActive(): ?bool
  342.     {
  343.         return $this->isActive;
  344.     }
  345. }