src/Entity/Projet/Projet/Projet.php line 16

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