<?php
namespace App\Entity\Users\User;
use App\Repository\Users\User\NewsletterRepository;
use Doctrine\ORM\Mapping as ORM;
use App\Validator\Validatorfile\Yourfile;
use App\Service\Servicetext\GeneralServicetext;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @ORM\Table("newsletter")
* @ORM\Entity(repositoryClass=NewsletterRepository::class)
** @ORM\HasLifecycleCallbacks
*/
class Newsletter
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $nom;
/**
* @ORM\Column(type="string", length=255)
*/
private $email;
/**
* @ORM\Column(type="datetime")
*/
private $date;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $contact;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $sujet;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $contenu;
/**
* @var string
*
* @ORM\Column(name="src", type="string", length=255,nullable=true)
*/
private $src;
/**
* @var string
*
* @ORM\Column(name="alt", type="string", length=255,nullable=true)
*/
private $alt;
/**
*@Yourfile(taillemax=1500000, message="la taille de l'image %string% est grande.")
*/
private $file;
// permet le stocage temporaire du nom du fichier
private $tempFilename;
private $servicetext;
public function __construct()
{
$this->date = new \Datetime();
}
public function getServicetext()
{
return $this->servicetext;
}
public function setServicetext(GeneralServicetext $service)
{
$this->servicetext = $service;
return $this;
}
public function getId(): ?int
{
return $this->id;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom): self
{
$this->nom = $nom;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getContact(): ?bool
{
return $this->contact;
}
public function setContact(bool $contact=null): self
{
$this->contact = $contact;
return $this;
}
public function getSujet(): ?string
{
return $this->sujet;
}
public function setSujet(?string $sujet): self
{
$this->sujet = $sujet;
return $this;
}
public function getContenu(): ?string
{
return $this->contenu;
}
public function setContenu(?string $contenu): self
{
$this->contenu = $contenu;
return $this;
}
//permet la récupération du nom du fichier temporaire
public function getTempFilename()
{
return $this->tempFilename;
}
//permet de modifier le contenu de la variable tempFilename
public function setTempFilename($temp)
{
$this->tempFilename=$temp;
}
// permet la récupération du nom du fiechier
public function getFile()
{
return $this->file;
}
public function getUploadDir()
{
// On retourne le chemin relatif vers l'image pour un navigateur
return 'bundles/users/user/document/newsletter';
}
protected function getUploadRootDir()
{
// On retourne le chemin relatif vers l'image pour notre codePHP
return __DIR__.'/../../../../public/'.$this->getUploadDir();
}
public function setFile(UploadedFile $file)
{
$this->file = $file;
// On vérifie si on avait déjà un fichier pour cette entité
if (null !== $this->src) {
// On sauvegarde l'extension du fichier pour le supprimer plus tard
$this->tempFilename = $this->src;
// On réinitialise les valeurs des attributs url et alt
$this->src = null;
$this->alt = null;
}
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null === $this->file) {
return;
}
$text = $this->file->getClientOriginalName();
$this->src = $this->servicetext->normaliseText($text);
$this->alt = $this->src;
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
// Si jamais il n'y a pas de fichier (champ facultatif)
if (null === $this->file) {
return;
}
if (null !== $this->tempFilename) {
$oldFile = $this->getUploadRootDir().'/'.$this->id.'.'.$this->tempFilename;
if (file_exists($oldFile)) {
unlink($oldFile);
}
}
$this->file->move( $this->getUploadRootDir(), $this->id.'.'.$this->src);
}
/**
*@ORM\PreRemove()
*/
public function preRemoveUpload()
{
$this->tempFilename = $this->getUploadRootDir().'/'.$this->id.'.'.$this->src;
}
/**
* @ORM\PostRemove()
*/
public function postRemoveUpload()
{
// En PostRemove, on n'a pas accès à l'id, on utilise notre nom sauvegardé
if (file_exists($this->tempFilename)) {
// On supprime le fichier
unlink($this->tempFilename);
}
}
public function getWebPath()
{
return $this->getUploadDir().'/'.$this->getId().'.'.$this->getSrc();
}
/**
* Set src
*
* @param string $src
* @return Lecon
*/
public function setSrc($src)
{
$this->src = $src;
return $this;
}
/**
* Get src
*
* @return string
*/
public function getSrc()
{
return $this->src;
}
/**
* Set alt
*
* @param string $alt
* @return Lecon
*/
public function setAlt($alt)
{
$this->alt = $alt;
return $this;
}
/**
* Get alt
*
* @return string
*/
public function getAlt()
{
return $this->alt;
}
}