src/Entity/Produit/Produit/Categorie.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Produit\Produit;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use App\Validator\Validatortext\Taillemin;
  5. use App\Validator\Validatortext\Taillemax;
  6. use App\Service\Servicetext\GeneralServicetext;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use App\Validator\Validatorfile\Image;
  9. use Symfony\Component\HttpFoundation\File\UploadedFile;
  10. use App\Repository\Produit\Produit\CategorieRepository;
  11. use App\Entity\Users\User\User;
  12. use App\Entity\Produit\Produit\Souscategorie;
  13. use Doctrine\Common\Collections\Collection;
  14. /**
  15.  * Categorie
  16.  *
  17.  * @ORM\Table("categorie")
  18.  * @ORM\Entity(repositoryClass=CategorieRepository::class)
  19.  * @UniqueEntity(fields="nom", message="Cette catégorie existe déjà.")
  20.  * @UniqueEntity(fields="typeservice", message="Cette catégorie existe déjà.")
  21.  ** @ORM\HasLifecycleCallbacks
  22.  */
  23. class Categorie
  24. {
  25.     /**
  26.      * @var integer
  27.      *
  28.      * @ORM\Column(name="id", type="integer")
  29.      * @ORM\Id
  30.      * @ORM\GeneratedValue(strategy="AUTO")
  31.      */
  32.     private $id;
  33.     /**
  34.      * @var string
  35.      *
  36.      * @ORM\Column(name="nom", type="string", length=255,unique=true)
  37.      * @Taillemin(valeur=3, message="Au moins 3 caractères")
  38.      * @Taillemax(valeur=70, message="Au plus 70 caractès")
  39.     */
  40.     private $nom;
  41.     
  42.     /**
  43.      * @var string
  44.      *
  45.      * @ORM\Column(name="typeservice", type="string", length=255, nullable=true)
  46.      */
  47.     private $typeservice;
  48.     
  49.     /**
  50.      * @var text
  51.      *
  52.      * @ORM\Column(name="description", type="text")
  53.      * @Taillemin(valeur=3, message="Au moins 3 caractères")
  54.      * @Taillemax(valeur=500, message="Au plus 500 caractès")
  55.      */
  56.     private $description;
  57.     /**
  58.      * @var \DateTime
  59.      *
  60.      * @ORM\Column(name="date", type="datetime")
  61.      */
  62.     private $date;
  63.     
  64.     /**
  65.      * @var integer
  66.      *
  67.      * @ORM\Column(name="rang", type="integer")
  68.      */
  69.     private $rang;
  70.     
  71.     /**
  72.       * @ORM\ManyToOne(targetEntity=User::class)
  73.       * @ORM\JoinColumn(nullable=false)
  74.       */
  75.     private $user;
  76.     
  77.     /**
  78.      * @ORM\OneToMany(targetEntity=Souscategorie::class, mappedBy="categorie")
  79.      */
  80.     private $souscategories;
  81.     
  82.      /**
  83.      * @var string
  84.      *
  85.      * @ORM\Column(name="src", type="string", length=255,nullable=true)
  86.      */
  87.     private $src;
  88.     /**
  89.      * @var string
  90.      *
  91.      * @ORM\Column(name="alt", type="string", length=255,nullable=true)
  92.      */
  93.     private $alt;
  94.     
  95.     /**
  96.     *@Image(taillemax=1500000, message="la taille de l'image  %string% est grande.")
  97.     */
  98.     private $file;
  99.     
  100.     // permet le stocage temporaire du nom du fichier
  101.     private $tempFilename;
  102.     
  103.     private $servicetext;
  104.     
  105.     private $em;
  106.     
  107.     public function __construct(GeneralServicetext $service)
  108.     {
  109.         $this->src ="source";
  110.         $this->alt ="alternatif";
  111.         $this->rang 0;
  112.         $this->servicetext $service;
  113.         $this->date = new \Datetime();
  114.         $this->souscategories = new \Doctrine\Common\Collections\ArrayCollection();
  115.     }
  116.     
  117.     public function getServicetext()
  118.     {
  119.         return $this->servicetext;
  120.     }
  121.     
  122.     public function setServicetext(GeneralServicetext $service)
  123.     {
  124.         $this->servicetext $service;
  125.         return $this;
  126.     }
  127.     
  128.     public function getEm()
  129.     {
  130.         return $this->em;
  131.     }
  132.     
  133.     public function setEm($em)
  134.     {
  135.         $this->em $em;
  136.         return $this;
  137.     }
  138.     /**
  139.      * Get id
  140.      *
  141.      * @return integer 
  142.      */
  143.     public function getId()
  144.     {
  145.         return $this->id;
  146.     }
  147.     /**
  148.      * Set nom
  149.      *
  150.      * @param string $nom
  151.      * @return Categorie
  152.      */
  153.     public function setNom($nom)
  154.     {
  155.         $this->nom $nom;
  156.         return $this;
  157.     }
  158.     /**
  159.      * Get nom
  160.      *
  161.      * @return string 
  162.      */
  163.     public function getNom()
  164.     {
  165.         return $this->nom;
  166.     }
  167.     /**
  168.      * Set date
  169.      *
  170.      * @param \DateTime $date
  171.      * @return Categorie
  172.      */
  173.     public function setDate($date)
  174.     {
  175.         $this->date $date;
  176.         return $this;
  177.     }
  178.     /**
  179.      * Get date
  180.      *
  181.      * @return \DateTime 
  182.      */
  183.     public function getDate()
  184.     {
  185.         return $this->date;
  186.     }
  187.     /**
  188.      * Add souscategories
  189.      * @return Categorie
  190.      */
  191.     public function addSouscategory(Souscategorie $souscategories): self
  192.     {
  193.         $this->souscategories[] = $souscategories;
  194.         return $this;
  195.     }
  196.     /**
  197.      * Remove souscategories
  198.      */
  199.     public function removeSouscategory(Souscategorie $souscategories)
  200.     {
  201.         $this->souscategories->removeElement($souscategories);
  202.     }
  203.     /**
  204.      * Get souscategories 
  205.      */
  206.     public function getSouscategories(): ?Collection
  207.     {
  208.         return $this->souscategories;
  209.     }
  210.     /**
  211.      * Set user
  212.      * @return Categorie
  213.      */
  214.     public function setUser(User $user): self
  215.     {
  216.         $this->user $user;
  217.         return $this;
  218.     }
  219.     /**
  220.      * Get user 
  221.      */
  222.     public function getUser(): ?User
  223.     {
  224.         return $this->user;
  225.     }
  226.     
  227.     //permet la récupération du nom du fichier temporaire
  228.     public function getTempFilename()
  229.     {
  230.         return $this->tempFilename;
  231.     }
  232.     
  233.     //permet de modifier le contenu de la variable tempFilename
  234.     public function setTempFilename($temp)
  235.     {
  236.     $this->tempFilename=$temp;
  237.     }
  238.     // permet la récupération du nom du fiechier
  239.     public function getFile()
  240.     {
  241.     return $this->file;
  242.     }
  243.     
  244.     public function getUploadDir()
  245.     {
  246.     // On retourne le chemin relatif vers l'image pour un navigateur
  247.     return 'bundles/produit/produit/images/categorie';
  248.     }
  249.     protected function getUploadRootDir()
  250.     {
  251.     // On retourne le chemin relatif vers l'image pour notre codePHP
  252.     return  __DIR__.'/../../../../public/'.$this->getUploadDir();
  253.     }
  254.     
  255.     public function setFile(UploadedFile $file)
  256.     {
  257.         $this->file $file;
  258.         // On vérifie si on avait déjà un fichier pour cette entité
  259.         if (null !== $this->src) {
  260.         // On sauvegarde l'extension du fichier pour le supprimer plus tard
  261.         $this->tempFilename $this->src;
  262.         // On réinitialise les valeurs des attributs url et alt
  263.         $this->src null;
  264.         $this->alt null;
  265.     }
  266.     }
  267.     
  268.     /**
  269.     * @ORM\PrePersist()
  270.     * @ORM\PreUpdate()
  271.     */
  272.     public function preUpload()
  273.     {
  274.         if (null === $this->file) {
  275.         return;
  276.         }
  277.         $text $this->file->getClientOriginalName();
  278.         $this->src $this->servicetext->normaliseText($text);
  279.         $this->alt $this->src;
  280.     }
  281.     
  282.     /**
  283.     * @ORM\PostPersist()
  284.     * @ORM\PostUpdate()
  285.     */
  286.     public function upload()
  287.     {
  288.     // Si jamais il n'y a pas de fichier (champ facultatif)
  289.     if (null === $this->file) {
  290.     return;
  291.     }
  292.     if (null !== $this->tempFilename) {
  293.     $oldFile $this->getUploadRootDir().'/'.$this->id.'.'.$this->tempFilename;
  294.     if (file_exists($oldFile)) {
  295.     unlink($oldFile);
  296.     }
  297.     }
  298.     $this->file->move$this->getUploadRootDir(), $this->id.'.'.$this->src);
  299.     }
  300.     /**
  301.     *@ORM\PreRemove()
  302.     */
  303.     public function preRemoveUpload()
  304.     {
  305.     $this->tempFilename $this->getUploadRootDir().'/'.$this->id.'.'.$this->src;
  306.     }
  307.     
  308.     /**
  309.     * @ORM\PostRemove()
  310.     */
  311.     public function postRemoveUpload()
  312.     {
  313.     // En PostRemove, on n'a pas accès à l'id, on utilise notre nom sauvegardé
  314.     if (file_exists($this->tempFilename)) {
  315.     // On supprime le fichier
  316.     unlink($this->tempFilename);
  317.     }
  318.     }
  319.     
  320.     public function getWebPath()
  321.     {
  322.     return $this->getUploadDir().'/'.$this->getId().'.'.$this->getSrc();
  323.     }
  324.     /**
  325.      * Set src
  326.      *
  327.      * @param string $src
  328.      * @return Categorie
  329.      */
  330.     public function setSrc($src)
  331.     {
  332.         $this->src $src;
  333.         return $this;
  334.     }
  335.     /**
  336.      * Get src
  337.      *
  338.      * @return string 
  339.      */
  340.     public function getSrc()
  341.     {
  342.         return $this->src;
  343.     }
  344.     /**
  345.      * Set alt
  346.      *
  347.      * @param string $alt
  348.      * @return Categorie
  349.      */
  350.     public function setAlt($alt)
  351.     {
  352.         $this->alt $alt;
  353.         return $this;
  354.     }
  355.     /**
  356.      * Get alt
  357.      *
  358.      * @return string 
  359.      */
  360.     public function getAlt()
  361.     {
  362.         return $this->alt;
  363.     }
  364.     /**
  365.      * Set rang
  366.      *
  367.      * @param integer $rang
  368.      * @return Categorie
  369.      */
  370.     public function setRang($rang)
  371.     {
  372.         $this->rang $rang;
  373.         return $this;
  374.     }
  375.     /**
  376.      * Get rang
  377.      *
  378.      * @return integer 
  379.      */
  380.     public function getRang()
  381.     {
  382.         return $this->rang;
  383.     }
  384.     /**
  385.      * Set description
  386.      *
  387.      * @param string $description
  388.      * @return Categorie
  389.      */
  390.     public function setDescription($description)
  391.     {
  392.         $this->description $description;
  393.         return $this;
  394.     }
  395.     /**
  396.      * Get description
  397.      *
  398.      * @return string 
  399.      */
  400.     public function getDescription()
  401.     {
  402.         return $this->description;
  403.     }
  404.     /**
  405.      * Set typeservice
  406.      *
  407.      * @param string $typeservice
  408.      * @return Categorie
  409.      */
  410.     public function setTypeservice($typeservice)
  411.     {
  412.         $this->typeservice $typeservice;
  413.         return $this;
  414.     }
  415.     /**
  416.      * Get typeservice
  417.      *
  418.      * @return string 
  419.      */
  420.     public function getTypeservice()
  421.     {
  422.         return $this->typeservice;
  423.     }
  424.     
  425.     public function getScatProduits()
  426.     {
  427.         $liste_scat $this->em->getRepository(Souscategorie::class)
  428.                                ->findBy(array('categorie'=>$this), array('rang'=>'asc'));
  429.         return $liste_scat;
  430.     }
  431. }