src/Entity/Tarif.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_tarif")
  10.  * @ORM\HasLifecycleCallbacks
  11.  */
  12. class Tarif
  13. {
  14.     /**
  15.      * @ORM\Id
  16.      * @ORM\Column(type="integer")
  17.      * @ORM\GeneratedValue(strategy="AUTO")
  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="tarifs")
  61.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  62.      */
  63.     protected $user;
  64.     /**
  65.      * @ORM\ManyToOne(targetEntity="Formation", inversedBy="tarifs")
  66.      * @ORM\JoinColumn(name="formation_id", referencedColumnName="id")
  67.      */
  68.     protected $formation;
  69.     /**
  70.      * Constructor
  71.      */
  72.     public function __construct()
  73.     {
  74.     }
  75.     public function getAbsolutePath()
  76.     {
  77.         return null === $this->path null $this->getUploadRootDir().'/'.$this->path;
  78.     }
  79.     public function getWebPath()
  80.     {
  81.         return null === $this->path null $this->getUploadDir().'/'.$this->path;
  82.     }
  83.     protected function getUploadRootDir()
  84.     {
  85.         // le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
  86.         $rootDir __DIR__.'/../../../../web/'.$this->getUploadDir();
  87.         if ( ! is_dir($rootDir)) {
  88.             mkdir($rootDir0777true);
  89.         }
  90.         return __DIR__.'/../../../../web/'.$this->getUploadDir();
  91.     }
  92.     protected function getUploadDir()
  93.     {
  94.         // on se débarrasse de « __DIR__ » afin de ne pas avoir de problème lorsqu'on affiche
  95.         // le document/image dans la vue.
  96.         return 'uploads/sliders';
  97.     }
  98.     /**
  99.      * @ORM\PrePersist()
  100.      * @ORM\PreUpdate()
  101.      */
  102.     public function preUpload()
  103.     {
  104.         if (null !== $this->file) {
  105.             // faites ce que vous voulez pour générer un nom unique
  106.             $this->path sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension();
  107.         }
  108.     }
  109.     /**
  110.      * @ORM\PostPersist()
  111.      * @ORM\PostUpdate()
  112.      */
  113.     public function upload()
  114.     {
  115.         if (null === $this->file) {
  116.             return;
  117.         }
  118.         // s'il y a une erreur lors du déplacement du fichier, une exception
  119.         // va automatiquement être lancée par la méthode move(). Cela va empêcher
  120.         // proprement l'entité d'être persistée dans la base de données si
  121.         // erreur il y a
  122.         $this->file->move($this->getUploadRootDir(), $this->path);
  123.         unset($this->file);
  124.     }
  125.     /**
  126.      * @ORM\PostRemove()
  127.      */
  128.     public function removeUpload()
  129.     {
  130.         if ($file $this->getAbsolutePath()) {
  131.             unlink($file);
  132.         }
  133.     }
  134.     /**
  135.      * Get id
  136.      *
  137.      * @return integer
  138.      */
  139.     public function getId()
  140.     {
  141.         return $this->id;
  142.     }
  143.     /**
  144.      * Set title
  145.      *
  146.      * @param string $title
  147.      * @return Tarif
  148.      */
  149.     public function setTitle($title)
  150.     {
  151.         $this->title $title;
  152.         return $this;
  153.     }
  154.     /**
  155.      * Get title
  156.      *
  157.      * @return string
  158.      */
  159.     public function getTitle()
  160.     {
  161.         return $this->title;
  162.     }
  163.     /**
  164.      * Set path
  165.      *
  166.      * @param string $path
  167.      * @return Tarif
  168.      */
  169.     public function setPath($path)
  170.     {
  171.         $this->path $path;
  172.         return $this;
  173.     }
  174.     /**
  175.      * Get path
  176.      *
  177.      * @return string
  178.      */
  179.     public function getPath()
  180.     {
  181.         return $this->path;
  182.     }
  183.     /**
  184.      * Set isActive
  185.      *
  186.      * @param boolean $isActive
  187.      * @return Tarif
  188.      */
  189.     public function setIsActive($isActive)
  190.     {
  191.         $this->isActive $isActive;
  192.         return $this;
  193.     }
  194.     /**
  195.      * Get isActive
  196.      *
  197.      * @return boolean
  198.      */
  199.     public function getIsActive()
  200.     {
  201.         return $this->isActive;
  202.     }
  203.     /**
  204.      * Set createdOn
  205.      *
  206.      * @param \DateTime $createdOn
  207.      * @return Tarif
  208.      */
  209.     public function setCreatedOn($createdOn)
  210.     {
  211.         $this->createdOn $createdOn;
  212.         return $this;
  213.     }
  214.     /**
  215.      * Get createdOn
  216.      *
  217.      * @return \DateTime
  218.      */
  219.     public function getCreatedOn()
  220.     {
  221.         return $this->createdOn;
  222.     }
  223.     /**
  224.      * Set updatedOn
  225.      *
  226.      * @param \DateTime $updatedOn
  227.      * @return Tarif
  228.      */
  229.     public function setUpdatedOn($updatedOn)
  230.     {
  231.         $this->updatedOn $updatedOn;
  232.         return $this;
  233.     }
  234.     /**
  235.      * Get updatedOn
  236.      *
  237.      * @return \DateTime
  238.      */
  239.     public function getUpdatedOn()
  240.     {
  241.         return $this->updatedOn;
  242.     }
  243.     /**
  244.      * Set user
  245.      *
  246.      * @param \App\Entity\User $user
  247.      * @return Tarif
  248.      */
  249.     public function setUser(\App\Entity\User $user null)
  250.     {
  251.         $this->user $user;
  252.         return $this;
  253.     }
  254.     /**
  255.      * Get user
  256.      *
  257.      * @return \App\Entity\User
  258.      */
  259.     public function getUser()
  260.     {
  261.         return $this->user;
  262.     }
  263.     /**
  264.      * Set formation
  265.      *
  266.      * @param \App\Entity\Formation $formation
  267.      * @return Tarif
  268.      */
  269.     public function setFormation(\App\Entity\Formation $formation null)
  270.     {
  271.         $this->formation $formation;
  272.         return $this;
  273.     }
  274.     /**
  275.      * Get formation
  276.      *
  277.      * @return \App\Entity\Formation
  278.      */
  279.     public function getFormation()
  280.     {
  281.         return $this->formation;
  282.     }
  283.     /**
  284.      * Set slug
  285.      *
  286.      * @param string $slug
  287.      * @return Tarif
  288.      */
  289.     public function setSlug($slug)
  290.     {
  291.         $this->slug $slug;
  292.         return $this;
  293.     }
  294.     /**
  295.      * Get slug
  296.      *
  297.      * @return string
  298.      */
  299.     public function getSlug()
  300.     {
  301.         return $this->slug;
  302.     }
  303.     public function isIsActive(): ?bool
  304.     {
  305.         return $this->isActive;
  306.     }
  307. }