src/Entity/Document.php line 25

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use Doctrine\DBAL\Types\Types;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7. use Symfony\Component\HttpFoundation\File\File;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  10. /**
  11. * Document.
  12. *
  13. * @ORM\Table(name="Document")
  14. *
  15. * @ORM\Entity()
  16. *
  17. * @ORM\HasLifecycleCallbacks
  18. *
  19. * @Vich\Uploadable
  20. */
  21. class Document
  22. {
  23. /**
  24. * @var int
  25. *
  26. * @ORM\Column(name="id", type="integer")
  27. *
  28. * @ORM\Id
  29. *
  30. * @ORM\GeneratedValue(strategy="AUTO")
  31. */
  32. private $id;
  33. /**
  34. * @var string
  35. *
  36. * Assert\NotBlank
  37. *
  38. * @ORM\Column(name="filename", type="string", length=255)
  39. */
  40. private $filename;
  41. /**
  42. * @var string
  43. *
  44. * @ORM\Column(type="string", length=255, nullable=true)
  45. */
  46. private $path;
  47. #[Assert\File(maxSize: '2M')]
  48. private ?File $file = null;
  49. /**
  50. * @var bool
  51. *
  52. * @ORM\Column(name="deleteFile", type="boolean", nullable=true)
  53. */
  54. private $deleteFile;
  55. /**
  56. * @var \DateTime
  57. *
  58. * @Gedmo\Timestampable(on="create")
  59. *
  60. * @ORM\Column(name="createdAt", type="datetime", nullable=true)
  61. */
  62. private $createdAt;
  63. /**
  64. * @var \DateTime
  65. *
  66. * @Gedmo\Timestampable(on="update")
  67. *
  68. * @ORM\Column(name="updatedAt", type="datetime", nullable=true)
  69. */
  70. private $updatedAt;
  71. /**
  72. * @ORM\OneToOne(targetEntity="FicheAction", mappedBy="document1")
  73. */
  74. private $ficheActionDoc1;
  75. /**
  76. * @ORM\OneToOne(targetEntity="FicheAction", mappedBy="document2")
  77. */
  78. private $ficheActionDoc2;
  79. /**
  80. * @ORM\OneToOne(targetEntity="FicheAction", mappedBy="document3")
  81. */
  82. private $ficheActionDoc3;
  83. /**
  84. * @ORM\OneToOne(targetEntity="FicheAction", mappedBy="document4")
  85. */
  86. private $ficheActionDoc4;
  87. /**
  88. * Get id.
  89. *
  90. * @return int
  91. */
  92. public function getId()
  93. {
  94. return $this->id;
  95. }
  96. /**
  97. * Set doc.
  98. *
  99. * @param File $doc
  100. */
  101. public function setFile($doc = null): static
  102. {
  103. if (!is_null($doc) && ($doc instanceof File)) {
  104. $this->setUpdatedAt(new \DateTime());
  105. $this->file = $doc;
  106. }
  107. return $this;
  108. }
  109. /**
  110. * Get doc.
  111. *
  112. * @return string
  113. */
  114. public function getFile(): ?File
  115. {
  116. return $this->file;
  117. }
  118. /**
  119. * Set filename.
  120. *
  121. * @param string $filename
  122. */
  123. public function setFilename($filename): static
  124. {
  125. $this->filename = $filename;
  126. return $this;
  127. }
  128. /**
  129. * Get filename.
  130. *
  131. * @return string
  132. */
  133. public function getFilename()
  134. {
  135. return $this->filename;
  136. }
  137. /**
  138. * Set path.
  139. *
  140. * @param string $path
  141. */
  142. public function setPath($path): static
  143. {
  144. $this->path = $path;
  145. return $this;
  146. }
  147. /**
  148. * Get path.
  149. *
  150. * @return string
  151. */
  152. public function getPath()
  153. {
  154. return $this->path;
  155. }
  156. /**
  157. * Set createdAt.
  158. *
  159. * @param \DateTime $createdAt
  160. */
  161. public function setCreatedAt($createdAt): static
  162. {
  163. $this->createdAt = $createdAt;
  164. return $this;
  165. }
  166. /**
  167. * Get createdAt.
  168. *
  169. * @return \DateTime
  170. */
  171. public function getCreatedAt()
  172. {
  173. return $this->createdAt;
  174. }
  175. /**
  176. * Set updatedAt.
  177. *
  178. * @param \DateTime $updatedAt
  179. */
  180. public function setUpdatedAt($updatedAt): static
  181. {
  182. $this->updatedAt = $updatedAt;
  183. return $this;
  184. }
  185. /**
  186. * Get updatedAt.
  187. *
  188. * @return \DateTime
  189. */
  190. public function getUpdatedAt()
  191. {
  192. return $this->updatedAt;
  193. }
  194. /**
  195. * Set ficheActionDoc1.
  196. */
  197. public function setFicheActionDoc1(?FicheAction $ficheActionDoc1 = null): static
  198. {
  199. $this->ficheActionDoc1 = $ficheActionDoc1;
  200. return $this;
  201. }
  202. /**
  203. * Get ficheActionDoc1.
  204. *
  205. * @return FicheAction
  206. */
  207. public function getFicheActionDoc1()
  208. {
  209. return $this->ficheActionDoc1;
  210. }
  211. /**
  212. * Set ficheActionDoc2.
  213. */
  214. public function setFicheActionDoc2(?FicheAction $ficheActionDoc2 = null): static
  215. {
  216. $this->ficheActionDoc2 = $ficheActionDoc2;
  217. return $this;
  218. }
  219. /**
  220. * Get ficheActionDoc2.
  221. *
  222. * @return FicheAction
  223. */
  224. public function getFicheActionDoc2()
  225. {
  226. return $this->ficheActionDoc2;
  227. }
  228. /**
  229. * Set ficheActionDoc3.
  230. */
  231. public function setFicheActionDoc3(?FicheAction $ficheActionDoc3 = null): static
  232. {
  233. $this->ficheActionDoc3 = $ficheActionDoc3;
  234. return $this;
  235. }
  236. /**
  237. * Get ficheActionDoc3.
  238. *
  239. * @return FicheAction
  240. */
  241. public function getFicheActionDoc3()
  242. {
  243. return $this->ficheActionDoc3;
  244. }
  245. /**
  246. * Set ficheActionDoc4.
  247. */
  248. public function setFicheActionDoc4(?FicheAction $ficheActionDoc4 = null): static
  249. {
  250. $this->ficheActionDoc4 = $ficheActionDoc4;
  251. return $this;
  252. }
  253. /**
  254. * Get ficheActionDoc4.
  255. *
  256. * @return FicheAction
  257. */
  258. public function getFicheActionDoc4()
  259. {
  260. return $this->ficheActionDoc4;
  261. }
  262. #[\Override]
  263. public function __toString(): string
  264. {
  265. $result = '';
  266. $filename = explode('_', $this->filename);
  267. if (count($filename) > 2) {
  268. $result = $filename[1];
  269. }
  270. return $result;
  271. }
  272. public function getAbsolutePath()
  273. {
  274. return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
  275. }
  276. public function getWebPath()
  277. {
  278. return null === $this->path ? null : $this->getUploadDir().'/'.$this->path;
  279. }
  280. protected function getUploadRootDir(): string
  281. {
  282. // le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
  283. return __DIR__.'/../../public/'.$this->getUploadDir();
  284. }
  285. protected function getUploadDir(): string
  286. {
  287. // on se débarrasse de « __DIR__ » afin de ne pas avoir de problème lorsqu'on affiche
  288. // le document/image dans la vue.
  289. // return 'uploads/files/documents/uploaded';
  290. return 'uploads/fiches_action';
  291. }
  292. /**
  293. * @ORM\PrePersist()
  294. *
  295. * @ORM\PreUpdate()
  296. */
  297. public function preUpload(): void
  298. {
  299. if ($this->file instanceof File) {
  300. $this->path = sha1(uniqid(random_int(0, mt_getrandmax()), true)).'.'.$this->file->guessExtension();
  301. $this->filename = $this->file->getClientOriginalName();
  302. }
  303. }
  304. /**
  305. * @ORM\PostPersist()
  306. *
  307. * @ORM\PostUpdate()
  308. */
  309. public function upload(): void
  310. {
  311. if (!$this->file instanceof File) {
  312. return;
  313. }
  314. // s'il y a une erreur lors du déplacement du fichier, une exception
  315. // va automatiquement être lancée par la méthode move(). Cela va empêcher
  316. // proprement l'entité d'être persistée dans la base de données si
  317. // erreur il y a
  318. $this->file->move($this->getUploadRootDir(), $this->path);
  319. unset($this->file);
  320. }
  321. /**
  322. * @ORM\PostRemove()
  323. */
  324. public function removeUpload(): void
  325. {
  326. if ($file = $this->getAbsolutePath()) {
  327. unlink($file);
  328. }
  329. }
  330. public function getDocumentSize(): ?string
  331. {
  332. if (file_exists($this->getAbsolutePath())) {
  333. $bytes = filesize($this->getAbsolutePath());
  334. $human_filesize = $this->human_filesize($bytes);
  335. } else {
  336. $human_filesize = null;
  337. }
  338. return $human_filesize;
  339. }
  340. private function human_filesize(int|bool $bytes, $decimals = 2): string
  341. {
  342. $sz = 'BKMGTP';
  343. $factor = floor((strlen($bytes) - 1) / 3);
  344. return sprintf(sprintf('%%.%sf', $decimals), $bytes / 1024 ** $factor).@$sz[$factor];
  345. }
  346. /**
  347. * Set deleteFile.
  348. *
  349. * @param bool $deleteFile
  350. */
  351. public function setDeleteFile($deleteFile): static
  352. {
  353. $this->deleteFile = $deleteFile;
  354. return $this;
  355. }
  356. /**
  357. * Get deleteFile.
  358. *
  359. * @return bool
  360. */
  361. public function getDeleteFile()
  362. {
  363. return $this->deleteFile;
  364. }
  365. public function isDeleteFile(): ?bool
  366. {
  367. return $this->deleteFile;
  368. }
  369. }