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