src/Entity/Users/User/Newsletter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Users\User;
  3. use App\Repository\Users\User\NewsletterRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use App\Validator\Validatorfile\Yourfile;
  6. use App\Service\Servicetext\GeneralServicetext;
  7. use Symfony\Component\HttpFoundation\File\UploadedFile;
  8. /**
  9.  * @ORM\Table("newsletter")
  10.  * @ORM\Entity(repositoryClass=NewsletterRepository::class)
  11.  ** @ORM\HasLifecycleCallbacks
  12.  */
  13. class Newsletter
  14. {
  15.     /**
  16.      * @ORM\Id
  17.      * @ORM\GeneratedValue
  18.      * @ORM\Column(type="integer")
  19.     */
  20.     private $id;
  21.     /**
  22.      * @ORM\Column(type="string", length=255)
  23.     */
  24.     private $nom;
  25.     /**
  26.      * @ORM\Column(type="string", length=255)
  27.      */
  28.     private $email;
  29.     /**
  30.      * @ORM\Column(type="datetime")
  31.      */
  32.     private $date;
  33.     /**
  34.      * @ORM\Column(type="boolean", nullable=true)
  35.      */
  36.     private $contact;
  37.     /**
  38.      * @ORM\Column(type="string", length=255, nullable=true)
  39.      */
  40.     private $sujet;
  41.     /**
  42.      * @ORM\Column(type="text", nullable=true)
  43.      */
  44.     private $contenu;
  45.     /**
  46.      * @var string
  47.      *
  48.      * @ORM\Column(name="src", type="string", length=255,nullable=true)
  49.      */
  50.     private $src;
  51.     /**
  52.      * @var string
  53.      *
  54.      * @ORM\Column(name="alt", type="string", length=255,nullable=true)
  55.      */
  56.     private $alt;
  57.     
  58.     /**
  59.     *@Yourfile(taillemax=1500000, message="la taille de l'image  %string% est grande.")
  60.     */
  61.     private $file;
  62.     
  63.     // permet le stocage temporaire du nom du fichier
  64.     private $tempFilename;
  65.     private $servicetext;
  66.     public function __construct()
  67.     {
  68.         $this->date = new \Datetime();
  69.     }
  70.     public function getServicetext()
  71.     {
  72.         return $this->servicetext;
  73.     }
  74.     
  75.     public function setServicetext(GeneralServicetext $service)
  76.     {
  77.         $this->servicetext $service;
  78.         return $this;
  79.     }
  80.     public function getId(): ?int
  81.     {
  82.         return $this->id;
  83.     }
  84.     public function getNom(): ?string
  85.     {
  86.         return $this->nom;
  87.     }
  88.     public function setNom(string $nom): self
  89.     {
  90.         $this->nom $nom;
  91.         return $this;
  92.     }
  93.     public function getEmail(): ?string
  94.     {
  95.         return $this->email;
  96.     }
  97.     public function setEmail(string $email): self
  98.     {
  99.         $this->email $email;
  100.         return $this;
  101.     }
  102.     public function getDate(): ?\DateTimeInterface
  103.     {
  104.         return $this->date;
  105.     }
  106.     public function setDate(\DateTimeInterface $date): self
  107.     {
  108.         $this->date $date;
  109.         return $this;
  110.     }
  111.     public function getContact(): ?bool
  112.     {
  113.         return $this->contact;
  114.     }
  115.     public function setContact(bool $contact=null): self
  116.     {
  117.         $this->contact $contact;
  118.         return $this;
  119.     }
  120.     public function getSujet(): ?string
  121.     {
  122.         return $this->sujet;
  123.     }
  124.     public function setSujet(?string $sujet): self
  125.     {
  126.         $this->sujet $sujet;
  127.         return $this;
  128.     }
  129.     public function getContenu(): ?string
  130.     {
  131.         return $this->contenu;
  132.     }
  133.     public function setContenu(?string $contenu): self
  134.     {
  135.         $this->contenu $contenu;
  136.         return $this;
  137.     }
  138.     //permet la récupération du nom du fichier temporaire
  139.     public function getTempFilename()
  140.     {
  141.         return $this->tempFilename;
  142.     }
  143.     //permet de modifier le contenu de la variable tempFilename
  144.     public function setTempFilename($temp)
  145.     {
  146.         $this->tempFilename=$temp;
  147.     }
  148.     // permet la récupération du nom du fiechier
  149.     public function getFile()
  150.     {
  151.         return $this->file;
  152.     }
  153.     
  154.     public function getUploadDir()
  155.     {
  156.         // On retourne le chemin relatif vers l'image pour un navigateur
  157.         return 'bundles/users/user/document/newsletter';
  158.     }
  159.     protected function getUploadRootDir()
  160.     {
  161.         // On retourne le chemin relatif vers l'image pour notre codePHP
  162.         return  __DIR__.'/../../../../public/'.$this->getUploadDir();
  163.     }
  164.     public function setFile(UploadedFile $file)
  165.     {
  166.         $this->file $file;
  167.         // On vérifie si on avait déjà un fichier pour cette entité
  168.         if (null !== $this->src) {
  169.             // On sauvegarde l'extension du fichier pour le supprimer plus tard
  170.             $this->tempFilename $this->src;
  171.             // On réinitialise les valeurs des attributs url et alt
  172.             $this->src null;
  173.             $this->alt null;
  174.         }
  175.     }
  176.     /**
  177.     * @ORM\PrePersist()
  178.     * @ORM\PreUpdate()
  179.     */
  180.     public function preUpload()
  181.     {
  182.         if (null === $this->file) {
  183.          return;
  184.         }
  185.         $text $this->file->getClientOriginalName();
  186.         $this->src $this->servicetext->normaliseText($text);
  187.         $this->alt $this->src;
  188.     }
  189.     
  190.     /**
  191.     * @ORM\PostPersist()
  192.     * @ORM\PostUpdate()
  193.     */
  194.     public function upload()
  195.     {
  196.         // Si jamais il n'y a pas de fichier (champ facultatif)
  197.         if (null === $this->file) {
  198.             return;
  199.         }
  200.         if (null !== $this->tempFilename) {
  201.             $oldFile $this->getUploadRootDir().'/'.$this->id.'.'.$this->tempFilename;
  202.             if (file_exists($oldFile)) {
  203.                 unlink($oldFile);
  204.             }
  205.         }
  206.         $this->file->move$this->getUploadRootDir(), $this->id.'.'.$this->src);
  207.     }
  208.     /**
  209.     *@ORM\PreRemove()
  210.     */
  211.     public function preRemoveUpload()
  212.     {
  213.         $this->tempFilename $this->getUploadRootDir().'/'.$this->id.'.'.$this->src;
  214.     }
  215.     
  216.     /**
  217.     * @ORM\PostRemove()
  218.     */
  219.     public function postRemoveUpload()
  220.     {
  221.         // En PostRemove, on n'a pas accès à l'id, on utilise notre nom sauvegardé
  222.         if (file_exists($this->tempFilename)) {
  223.             // On supprime le fichier
  224.             unlink($this->tempFilename);
  225.         }
  226.     }
  227.     
  228.     public function getWebPath()
  229.     {
  230.         return $this->getUploadDir().'/'.$this->getId().'.'.$this->getSrc();
  231.     }
  232.     /**
  233.      * Set src
  234.      *
  235.      * @param string $src
  236.      * @return Lecon
  237.      */
  238.     public function setSrc($src)
  239.     {
  240.         $this->src $src;
  241.         return $this;
  242.     }
  243.     /**
  244.      * Get src
  245.      *
  246.      * @return string 
  247.      */
  248.     public function getSrc()
  249.     {
  250.         return $this->src;
  251.     }
  252.     /**
  253.      * Set alt
  254.      *
  255.      * @param string $alt
  256.      * @return Lecon
  257.      */
  258.     public function setAlt($alt)
  259.     {
  260.         $this->alt $alt;
  261.         return $this;
  262.     }
  263.     /**
  264.      * Get alt
  265.      *
  266.      * @return string 
  267.      */
  268.     public function getAlt()
  269.     {
  270.         return $this->alt;
  271.     }
  272. }