src/Entity/Fournisseurdocument.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Validator\Constraints as Assert;
  5. /**
  6. * Fournisseurdocument
  7. *
  8. * @ORM\Table(name="fournisseurdocument", options={"collate"="latin1_general_ci","charset"="latin1"})
  9. * @ORM\Entity
  10. */
  11. class Fournisseurdocument {
  12. /**
  13. * @var integer
  14. *
  15. * @ORM\Column(name="id", type="integer", nullable=false)
  16. * @ORM\Id
  17. * @ORM\GeneratedValue(strategy="IDENTITY")
  18. */
  19. private $id;
  20. /**
  21. * @ORM\ManyToOne(targetEntity="App\Entity\Fournisseur", cascade={"persist"})
  22. * @ORM\JoinColumn(nullable=false)
  23. */
  24. private $idfournisseur;
  25. /**
  26. * @var integer
  27. *
  28. * @ORM\Column(name="date", type="integer", nullable=false)
  29. */
  30. private $date;
  31. /**
  32. * @var \DateTime
  33. *
  34. * @ORM\Column(name="datefr", type="datetime", nullable=false)
  35. */
  36. private $datefr;
  37. /**
  38. * @var string
  39. *
  40. * @ORM\Column(name="nom", type="string", length=150, nullable=false)
  41. */
  42. private $nom;
  43. /**
  44. * @var string
  45. *
  46. * @ORM\Column(name="fichier", type="string", length=150, nullable=false)
  47. * @Assert\File(maxSize="6000000")
  48. */
  49. private $fichier;
  50. /**
  51. * @var boolean
  52. *
  53. * @ORM\Column(name="clientall", type="boolean", nullable=false)
  54. */
  55. private $clientall;
  56. /**
  57. * @var boolean
  58. *
  59. * @ORM\Column(name="etat", type="boolean", nullable=false)
  60. */
  61. private $etat;
  62. public function __construct() {
  63. $this->etat = true;
  64. $this->clientall = false;
  65. $this->date = time();
  66. $this->datefr = new \DateTime('now');
  67. }
  68. /**
  69. * Get id
  70. *
  71. * @return integer
  72. */
  73. public function getId() {
  74. return $this->id;
  75. }
  76. /**
  77. * Set date
  78. *
  79. * @param integer $date
  80. * @return Fournisseurdocument
  81. */
  82. public function setDate($date) {
  83. $this->date = $date;
  84. return $this;
  85. }
  86. /**
  87. * Get date
  88. *
  89. * @return integer
  90. */
  91. public function getDate() {
  92. return $this->date;
  93. }
  94. /**
  95. * Set datefr
  96. *
  97. * @param \DateTime $datefr
  98. * @return Fournisseurdocument
  99. */
  100. public function setDatefr($datefr) {
  101. $this->datefr = $datefr;
  102. return $this;
  103. }
  104. /**
  105. * Get datefr
  106. *
  107. * @return \DateTime
  108. */
  109. public function getDatefr() {
  110. return $this->datefr;
  111. }
  112. /**
  113. * Set nom
  114. *
  115. * @param string $nom
  116. * @return Fournisseurdocument
  117. */
  118. public function setNom($nom) {
  119. $this->nom = $nom;
  120. return $this;
  121. }
  122. /**
  123. * Get nom
  124. *
  125. * @return string
  126. */
  127. public function getNom() {
  128. return $this->nom;
  129. }
  130. /**
  131. * Set fichier
  132. *
  133. * @param string $fichier
  134. * @return Fournisseurdocument
  135. */
  136. public function setFichier($fichier) {
  137. $this->fichier = $fichier;
  138. return $this;
  139. }
  140. /**
  141. * Get fichier
  142. *
  143. * @return string
  144. */
  145. public function getFichier() {
  146. return $this->fichier;
  147. }
  148. /**
  149. * Set clientall
  150. *
  151. * @param boolean $clientall
  152. * @return Fournisseurdocument
  153. */
  154. public function setClientall($clientall) {
  155. $this->clientall = $clientall;
  156. return $this;
  157. }
  158. /**
  159. * Get clientall
  160. *
  161. * @return boolean
  162. */
  163. public function getClientall() {
  164. return $this->clientall;
  165. }
  166. /**
  167. * Set etat
  168. *
  169. * @param boolean $etat
  170. * @return Fournisseurdocument
  171. */
  172. public function setEtat($etat) {
  173. $this->etat = $etat;
  174. return $this;
  175. }
  176. /**
  177. * Get etat
  178. *
  179. * @return boolean
  180. */
  181. public function getEtat() {
  182. return $this->etat;
  183. }
  184. /**
  185. * @param Fournisseur $idfournisseur
  186. * @return $this
  187. *
  188. */
  189. public function setIdfournisseur(Fournisseur $idfournisseur) {
  190. $this->idfournisseur = $idfournisseur;
  191. return $this;
  192. }
  193. /**
  194. * Get idclient
  195. *
  196. * @return \App\Entity\Client
  197. */
  198. public function getIdfournisseur() {
  199. return $this->idfournisseur;
  200. }
  201. protected function getUploadRootDir() {
  202. // le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
  203. return realpath(__DIR__ . '/../../../..') . '/public/' . $this->getUploadDir();
  204. }
  205. protected function getUploadDir() {
  206. // on se débarrasse de « __DIR__ » afin de ne pas avoir de problème lorsqu'on affiche
  207. // le document/image dans la vue.
  208. return 'documents';
  209. }
  210. public function upload($id_client) {
  211. // Vérifie que $this->fichier est bien un objet UploadedFile
  212. if ($this->fichier instanceof \Symfony\Component\HttpFoundation\File\UploadedFile) {
  213. // Utiliser le nom de fichier original ici mais le nettoyer pour éviter les problèmes de sécurité
  214. $name = "F" . $id_client . "-" . str_replace(" ", "_", $this->fichier->getClientOriginalName());
  215. // Déplacer le fichier dans le répertoire de destination
  216. $this->fichier->move($this->getUploadRootDir(), $name);
  217. // Mettre à jour la propriété fichier avec le nom du fichier déplacé
  218. $this->fichier = $name;
  219. } else {
  220. // Si ce n'est pas un fichier valide, lance une exception
  221. throw new \Exception("Le fichier n'est pas valide.");
  222. }
  223. }
  224. }