src/AdminBundle/Entity/Ticket.php line 13

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