src/AdminBundle/Entity/TicketMessage.php line 130

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Entity;
  3. use DateTime;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\HttpFoundation\File\UploadedFile;
  6. #[ORM\Table(name: 'ticket_message')]
  7. #[ORM\Entity(repositoryClass: \AdminBundle\Repository\TicketsMessageRepository::class)]
  8. #[ORM\HasLifecycleCallbacks]
  9. class TicketMessage extends BaseEntity
  10. {
  11. const FILE_FOLDER = 'upload/ticketsMessages/';
  12. /**
  13. * @var integer
  14. */
  15. #[ORM\Column(name: 'id', type: 'integer', nullable: false)]
  16. #[ORM\Id]
  17. #[ORM\GeneratedValue(strategy: 'IDENTITY')]
  18. protected $id;
  19. /**
  20. * @var string
  21. */
  22. #[ORM\Column(name: 'message', type: 'text', nullable: false)]
  23. protected $message;
  24. /**
  25. * @var string
  26. */
  27. #[ORM\Column(name: 'created_date', type: 'datetime', nullable: true)]
  28. protected $createdDate;
  29. #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id')]
  30. #[ORM\ManyToOne(targetEntity: \AdminBundle\Entity\User::class)]
  31. protected $user;
  32. #[ORM\JoinColumn(name: 'ticket_id', referencedColumnName: 'id')]
  33. #[ORM\ManyToOne(targetEntity: \AdminBundle\Entity\Ticket::class, inversedBy: 'ticketMessages')]
  34. protected $ticket;
  35. /**
  36. * @var string
  37. */
  38. #[ORM\Column(name: 'filename', type: 'string', length: 255, nullable: true)]
  39. protected $filename;
  40. private $attachmentFile;
  41. public function __construct()
  42. {
  43. $this->createdDate = new DateTime();
  44. }
  45. /**
  46. * @return int
  47. */
  48. public function getId(): ?int
  49. {
  50. return $this->id;
  51. }
  52. /**
  53. * @return string
  54. */
  55. public function getMessage(): ?string
  56. {
  57. return $this->message;
  58. }
  59. /**
  60. * @param string $message
  61. */
  62. public function setMessage(string $message)
  63. {
  64. $this->message = $message;
  65. return $this;
  66. }
  67. /**
  68. * @return DateTime
  69. */
  70. public function getCreatedDate()
  71. {
  72. return $this->createdDate;
  73. }
  74. /**
  75. * @param DateTime $createdDate
  76. */
  77. public function setCreatedDate(DateTime $createdDate)
  78. {
  79. $this->createdDate = $createdDate;
  80. return $this;
  81. }
  82. /**
  83. * @return User
  84. */
  85. public function getUser()
  86. {
  87. return $this->user;
  88. }
  89. /**
  90. * @param User $user
  91. */
  92. public function setUser(User $user)
  93. {
  94. $this->user = $user;
  95. return $this;
  96. }
  97. /**
  98. * @return Ticket
  99. */
  100. public function getTicket()
  101. {
  102. return $this->ticket;
  103. }
  104. /**
  105. * @param Ticket $ticket
  106. */
  107. public function setTicket(Ticket $ticket = null)
  108. {
  109. $this->ticket = $ticket;
  110. return $this;
  111. }
  112. /**
  113. * @param UploadedFile $attachmentFile
  114. */
  115. public function setAttachmentFile(UploadedFile $attachmentFile = null)
  116. {
  117. $this->attachmentFile = $attachmentFile;
  118. return $this;
  119. }
  120. /**
  121. * @return UploadedFile
  122. */
  123. public function getAttachmentFile()
  124. {
  125. return $this->attachmentFile;
  126. }
  127. public function getUploadFilePath()
  128. {
  129. if (!is_dir(self::FILE_FOLDER . $this->id)) {
  130. mkdir(self::FILE_FOLDER . $this->id, 0777, true);
  131. }
  132. return self::FILE_FOLDER . $this->id . '/';
  133. }
  134. public function getAttachmentFilePath()
  135. {
  136. if (!$this->filename) {
  137. return;
  138. }
  139. return $this->getUploadFilePath() . $this->filename;
  140. }
  141. public function uploadFile()
  142. {
  143. $attachmentFile = $this->getAttachmentFile();
  144. if (!$attachmentFile) {
  145. return;
  146. }
  147. $this->removeAttachmentFile();
  148. $nDate = new \DateTime();
  149. $filename = 'TICKET_MESSAGE_FILE_' . $nDate->format('Y-m-d-H:i:s');
  150. $this->filename = $filename . '.' . $attachmentFile->guessExtension();
  151. $attachmentFile->move(
  152. $this->getUploadFilePath(),
  153. $this->filename
  154. );
  155. $this->setAttachmentFile(null);
  156. }
  157. #[ORM\PreUpdate]
  158. #[ORM\PrePersist]
  159. public function lifecycleFileUpload()
  160. {
  161. $this->uploadFile();
  162. }
  163. #[ORM\PostRemove]
  164. public function removeUpload()
  165. {
  166. $this->removeAttachmentFile();
  167. }
  168. #[ORM\PostPersist]
  169. public function onPostPersist()
  170. {
  171. rename(
  172. self::FILE_FOLDER . $this->filename,
  173. $this->getUploadFilePath() . $this->filename
  174. );
  175. }
  176. public function removeAttachmentFile()
  177. {
  178. if ($attachmentFile = $this->getAttachmentFilePath()) {
  179. @unlink($attachmentFile);
  180. }
  181. }
  182. /**
  183. * @return string
  184. */
  185. public function getFilename()
  186. {
  187. return $this->filename;
  188. }
  189. }