src/AdminBundle/Entity/Ticket.php line 331

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Entity;
  3. use DateTime;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\HttpFoundation\File\UploadedFile;
  7. #[ORM\Table(name: 'ticket')]
  8. #[ORM\Entity(repositoryClass: \AdminBundle\Repository\TicketsRepository::class)]
  9. #[ORM\HasLifecycleCallbacks]
  10. class Ticket extends BaseEntity
  11. {
  12. const STATUS_CLOSED = 0;
  13. const STATUS_NEW = 1;
  14. const STATUS_WAITING = 2;
  15. const STATUS_PROCESSING = 3;
  16. const STATUS_SOLVED = 4;
  17. const STATUS_UNSOLVED = 5;
  18. const TYPE_INTERNAL = 0;
  19. const TYPE_EXTERNAL = 1;
  20. const TICKET_LIST_PERSONAL = 'personal';
  21. const TICKET_LIST_ORGANIZATIONAL = 'organizational';
  22. const TYPES = [
  23. self::TYPE_INTERNAL => 'Internal',
  24. self::TYPE_EXTERNAL => 'External',
  25. ];
  26. public static $statusTypes = [
  27. self::STATUS_CLOSED => 'Deleted',
  28. self::STATUS_NEW => 'New ticket',
  29. self::STATUS_WAITING => 'Waiting',
  30. self::STATUS_PROCESSING => 'Processing',
  31. self::STATUS_SOLVED => 'Ticket solved',
  32. self::STATUS_UNSOLVED => 'Ticket unsolved',
  33. ];
  34. const FILE_FOLDER = 'upload/tickets/';
  35. /**
  36. * @var integer
  37. */
  38. #[ORM\Column(name: 'id', type: 'integer', nullable: false)]
  39. #[ORM\Id]
  40. #[ORM\GeneratedValue(strategy: 'IDENTITY')]
  41. protected $id;
  42. #[ORM\Column(name: 'booking', type: 'string', length: 255, nullable: true)]
  43. // #[ORM\JoinColumn(name: 'booking', referencedColumnName: 'id', nullable: true)]
  44. // #[ORM\ManyToOne(targetEntity: \AdminBundle\Entity\Booking::class, inversedBy: 'tickets')]
  45. protected $booking;
  46. /**
  47. * @var integer
  48. */
  49. #[ORM\Column(name: 'status', type: 'integer')]
  50. protected $status = self::STATUS_NEW;
  51. /**
  52. * @var string
  53. */
  54. #[ORM\Column(name: 'subject', type: 'string', length: 255, nullable: false)]
  55. protected $subject;
  56. /**
  57. * @var string
  58. */
  59. #[ORM\Column(name: 'description', type: 'text', nullable: false)]
  60. protected $description;
  61. /**
  62. * @var DateTime
  63. */
  64. #[ORM\Column(name: 'created_date', type: 'datetime', nullable: true)]
  65. protected $createdDate;
  66. /**
  67. * @var DateTime
  68. */
  69. #[ORM\Column(name: 'update_date', type: 'datetime', nullable: true)]
  70. protected $updateDate;
  71. #[ORM\ManyToMany(targetEntity: \AdminBundle\Entity\Department::class, inversedBy: 'tickets')]
  72. private $departments;
  73. #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id')]
  74. #[ORM\ManyToOne(targetEntity: \AdminBundle\Entity\User::class)]
  75. protected $user;
  76. #[ORM\JoinColumn(name: 'reply_user_id', referencedColumnName: 'id', nullable: true)]
  77. #[ORM\ManyToOne(targetEntity: \AdminBundle\Entity\User::class)]
  78. protected $replyUser;
  79. #[ORM\OneToMany(targetEntity: \AdminBundle\Entity\TicketMessage::class, mappedBy: 'ticket', cascade: ['persist', 'remove'], orphanRemoval: true)]
  80. private $ticketMessages;
  81. /**
  82. * @var string
  83. */
  84. #[ORM\Column(name: 'filename', type: 'string', length: 255, nullable: true)]
  85. protected $filename;
  86. /**
  87. * @var integer
  88. */
  89. #[ORM\Column(name: 'type', type: 'integer')]
  90. protected $type;
  91. private $attachmentFile;
  92. public function __construct()
  93. {
  94. $this->type = 0;
  95. $this->createdDate = new DateTime();
  96. $this->departments = new ArrayCollection();
  97. $this->ticketMessages = new ArrayCollection();
  98. }
  99. /**
  100. * @return int
  101. */
  102. public function getId(): ?int
  103. {
  104. return $this->id;
  105. }
  106. /**
  107. * @param int $id
  108. */
  109. public function setId(int $id)
  110. {
  111. $this->id = $id;
  112. return $this;
  113. }
  114. /**
  115. * @return string
  116. */
  117. public function getBooking(): ?string
  118. {
  119. return $this->booking;
  120. }
  121. /**
  122. * @param string $booking
  123. */
  124. public function setBooking($booking)
  125. {
  126. $this->booking = $booking;
  127. return $this;
  128. }
  129. /**
  130. * @return int
  131. */
  132. public function getStatus(): int
  133. {
  134. return $this->status;
  135. }
  136. /**
  137. * @param int $status
  138. */
  139. public function setStatus(int $status)
  140. {
  141. $this->status = $status;
  142. return $this;
  143. }
  144. /**
  145. * @return string|null
  146. */
  147. public function getSubject(): ?string
  148. {
  149. return $this->subject;
  150. }
  151. /**
  152. * @param string $subject
  153. */
  154. public function setSubject(string $subject)
  155. {
  156. $this->subject = $subject;
  157. return $this;
  158. }
  159. /**
  160. * @return string|null
  161. */
  162. public function getDescription(): ?string
  163. {
  164. return $this->description;
  165. }
  166. /**
  167. * @param string $description
  168. */
  169. public function setDescription(string $description)
  170. {
  171. $this->description = $description;
  172. return $this;
  173. }
  174. /**
  175. * @return DateTime
  176. */
  177. public function getCreatedDate(): DateTime
  178. {
  179. return $this->createdDate;
  180. }
  181. /**
  182. * @param DateTime $createdDate
  183. */
  184. public function setCreatedDate(DateTime $createdDate)
  185. {
  186. $this->createdDate = $createdDate;
  187. return $this;
  188. }
  189. /**
  190. * @return DateTime
  191. */
  192. public function getUpdateDate(): DateTime
  193. {
  194. return $this->updateDate;
  195. }
  196. /**
  197. * @param DateTime $updateDate
  198. */
  199. public function setUpdateDate(DateTime $updateDate)
  200. {
  201. $this->updateDate = $updateDate;
  202. return $this;
  203. }
  204. /**
  205. * @param Department $department
  206. *
  207. * @return Ticket
  208. */
  209. public function addDepartment(Department $department)
  210. {
  211. $this->departments[] = $department;
  212. return $this;
  213. }
  214. /**
  215. * @param Department $department
  216. *
  217. * @return Ticket
  218. */
  219. public function removeDepartment(Department $department)
  220. {
  221. if (!$this->departments->contains($department)) {
  222. return;
  223. }
  224. $this->departments->removeElement($department);
  225. return $this;
  226. }
  227. /**
  228. * @return Ticket
  229. */
  230. public function removeDepartments()
  231. {
  232. $this->departments = [];
  233. return $this;
  234. }
  235. /**
  236. * @return ArrayCollection
  237. */
  238. public function getDepartments()
  239. {
  240. return $this->departments;
  241. }
  242. /**
  243. * @return User
  244. */
  245. public function getUser()
  246. {
  247. return $this->user;
  248. }
  249. /**
  250. * @param User $user
  251. */
  252. public function setUser(User $user)
  253. {
  254. $this->user = $user;
  255. return $this;
  256. }
  257. /**
  258. * @return User
  259. */
  260. public function getReplyUser()
  261. {
  262. return $this->replyUser;
  263. }
  264. /**
  265. * @param User $replyUser
  266. */
  267. public function setReplyUser(User $replyUser)
  268. {
  269. $this->replyUser = $replyUser;
  270. return $this;
  271. }
  272. /**
  273. * @param UploadedFile $attachmentFile
  274. */
  275. public function setAttachmentFile(UploadedFile $attachmentFile = null)
  276. {
  277. $this->attachmentFile = $attachmentFile;
  278. return $this;
  279. }
  280. /**
  281. * @return UploadedFile
  282. */
  283. public function getAttachmentFile()
  284. {
  285. return $this->attachmentFile;
  286. }
  287. public function getUploadFilePath()
  288. {
  289. if (!is_dir(self::FILE_FOLDER . $this->id)) {
  290. mkdir(self::FILE_FOLDER . $this->id, 0777, true);
  291. }
  292. return self::FILE_FOLDER . $this->id . '/';
  293. }
  294. public function getAttachmentFilePath()
  295. {
  296. if (!$this->filename) {
  297. return;
  298. }
  299. return $this->getUploadFilePath() . $this->filename;
  300. }
  301. public function uploadFile()
  302. {
  303. $attachmentFile = $this->getAttachmentFile();
  304. if (!$attachmentFile) {
  305. return;
  306. }
  307. $this->removeAttachmentFile();
  308. $nDate = new DateTime();
  309. $filename = 'TICKET_FILE_' . $nDate->format('Y-m-d-His');
  310. $this->filename = $filename . '.' . $attachmentFile->guessExtension();
  311. $attachmentFile->move(
  312. $this->getUploadFilePath(),
  313. $this->filename
  314. );
  315. $this->setAttachmentFile(null);
  316. }
  317. #[ORM\PreUpdate]
  318. #[ORM\PrePersist]
  319. public function lifecycleFileUpload()
  320. {
  321. $this->uploadFile();
  322. }
  323. #[ORM\PostRemove]
  324. public function removeUpload()
  325. {
  326. $this->removeAttachmentFile();
  327. }
  328. #[ORM\PostPersist]
  329. public function onPostPersist()
  330. {
  331. rename(
  332. self::FILE_FOLDER . $this->filename,
  333. $this->getUploadFilePath() . $this->filename
  334. );
  335. }
  336. public function removeAttachmentFile()
  337. {
  338. if ($attachmentFile = $this->getAttachmentFilePath()) {
  339. @unlink($attachmentFile);
  340. }
  341. }
  342. /**
  343. * @param TicketMessage $ticketMessage
  344. *
  345. * @return Ticket
  346. */
  347. public function addTicketMessage(TicketMessage $ticketMessage)
  348. {
  349. $ticketMessage->setTicket($this);
  350. $this->ticketMessages->add($ticketMessage);
  351. return $this;
  352. }
  353. /**
  354. * @param TicketMessage $ticketMessage
  355. *
  356. * @return Ticket
  357. */
  358. public function removeTicketMessage(TicketMessage $ticketMessage)
  359. {
  360. if (!$this->ticketMessages->contains($ticketMessage)) {
  361. return;
  362. }
  363. $this->ticketMessages->removeElement($ticketMessage);
  364. $ticketMessage->setTicket(null);
  365. return $this;
  366. }
  367. /**
  368. * @return ArrayCollection
  369. */
  370. public function getTicketMessages()
  371. {
  372. return $this->ticketMessages;
  373. }
  374. /**
  375. * @return string
  376. */
  377. public function getFilename()
  378. {
  379. return $this->filename;
  380. }
  381. /**
  382. * @return int
  383. */
  384. public function getType(): int
  385. {
  386. return $this->type;
  387. }
  388. /**
  389. * @param int $type
  390. */
  391. public function setType(int $type)
  392. {
  393. $this->type = $type;
  394. return $this;
  395. }
  396. /**
  397. * @return string
  398. */
  399. public function getDepartmentNames()
  400. {
  401. $departmentNames = [];
  402. foreach ($this->getDepartments() as $department) {
  403. $departmentNames[] = $department->getName();
  404. }
  405. return implode(', ', $departmentNames);
  406. }
  407. public function getLastRepliedAt()
  408. {
  409. if ($this->getTicketMessages()->count() === 0) {
  410. return '';
  411. }
  412. $lastMessage = $this->getTicketMessages()->last();
  413. return $lastMessage->getCreatedDate()->format('d/m/Y');
  414. }
  415. /**
  416. * Get the Replies count (number of Ticket Messages).
  417. *
  418. * @return integer
  419. */
  420. public function getReplies()
  421. {
  422. return $this->getTicketMessages()->count();
  423. }
  424. }