src/Entity/Clientdocument.php line 15

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