<?php
namespace AdminBundle\Entity;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
#[ORM\Table(name: 'ticket')]
#[ORM\Entity(repositoryClass: \AdminBundle\Repository\TicketsRepository::class)]
#[ORM\HasLifecycleCallbacks]
class Ticket extends BaseEntity
{
const STATUS_CLOSED = 0;
const STATUS_NEW = 1;
const STATUS_WAITING = 2;
const STATUS_PROCESSING = 3;
const STATUS_SOLVED = 4;
const STATUS_UNSOLVED = 5;
const TYPE_INTERNAL = 0;
const TYPE_EXTERNAL = 1;
const TICKET_LIST_PERSONAL = 'personal';
const TICKET_LIST_ORGANIZATIONAL = 'organizational';
const TYPES = [
self::TYPE_INTERNAL => 'Internal',
self::TYPE_EXTERNAL => 'External',
];
public static $statusTypes = [
self::STATUS_CLOSED => 'Deleted',
self::STATUS_NEW => 'New ticket',
self::STATUS_WAITING => 'Waiting',
self::STATUS_PROCESSING => 'Processing',
self::STATUS_SOLVED => 'Ticket solved',
self::STATUS_UNSOLVED => 'Ticket unsolved',
];
const FILE_FOLDER = 'upload/tickets/';
/**
* @var integer
*/
#[ORM\Column(name: 'id', type: 'integer', nullable: false)]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
protected $id;
#[ORM\Column(name: 'booking', type: 'string', length: 255, nullable: true)]
// #[ORM\JoinColumn(name: 'booking', referencedColumnName: 'id', nullable: true)]
// #[ORM\ManyToOne(targetEntity: \AdminBundle\Entity\Booking::class, inversedBy: 'tickets')]
protected $booking;
/**
* @var integer
*/
#[ORM\Column(name: 'status', type: 'integer')]
protected $status = self::STATUS_NEW;
/**
* @var string
*/
#[ORM\Column(name: 'subject', type: 'string', length: 255, nullable: false)]
protected $subject;
/**
* @var string
*/
#[ORM\Column(name: 'description', type: 'text', nullable: false)]
protected $description;
/**
* @var DateTime
*/
#[ORM\Column(name: 'created_date', type: 'datetime', nullable: true)]
protected $createdDate;
/**
* @var DateTime
*/
#[ORM\Column(name: 'update_date', type: 'datetime', nullable: true)]
protected $updateDate;
#[ORM\ManyToMany(targetEntity: \AdminBundle\Entity\Department::class, inversedBy: 'tickets')]
private $departments;
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: \AdminBundle\Entity\User::class)]
protected $user;
#[ORM\JoinColumn(name: 'reply_user_id', referencedColumnName: 'id', nullable: true)]
#[ORM\ManyToOne(targetEntity: \AdminBundle\Entity\User::class)]
protected $replyUser;
#[ORM\OneToMany(targetEntity: \AdminBundle\Entity\TicketMessage::class, mappedBy: 'ticket', cascade: ['persist', 'remove'], orphanRemoval: true)]
private $ticketMessages;
/**
* @var string
*/
#[ORM\Column(name: 'filename', type: 'string', length: 255, nullable: true)]
protected $filename;
/**
* @var integer
*/
#[ORM\Column(name: 'type', type: 'integer')]
protected $type;
private $attachmentFile;
public function __construct()
{
$this->type = 0;
$this->createdDate = new DateTime();
$this->departments = new ArrayCollection();
$this->ticketMessages = new ArrayCollection();
}
/**
* @return int
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id)
{
$this->id = $id;
return $this;
}
/**
* @return string
*/
public function getBooking(): ?string
{
return $this->booking;
}
/**
* @param string $booking
*/
public function setBooking($booking)
{
$this->booking = $booking;
return $this;
}
/**
* @return int
*/
public function getStatus(): int
{
return $this->status;
}
/**
* @param int $status
*/
public function setStatus(int $status)
{
$this->status = $status;
return $this;
}
/**
* @return string|null
*/
public function getSubject(): ?string
{
return $this->subject;
}
/**
* @param string $subject
*/
public function setSubject(string $subject)
{
$this->subject = $subject;
return $this;
}
/**
* @return string|null
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* @param string $description
*/
public function setDescription(string $description)
{
$this->description = $description;
return $this;
}
/**
* @return DateTime
*/
public function getCreatedDate(): DateTime
{
return $this->createdDate;
}
/**
* @param DateTime $createdDate
*/
public function setCreatedDate(DateTime $createdDate)
{
$this->createdDate = $createdDate;
return $this;
}
/**
* @return DateTime
*/
public function getUpdateDate(): DateTime
{
return $this->updateDate;
}
/**
* @param DateTime $updateDate
*/
public function setUpdateDate(DateTime $updateDate)
{
$this->updateDate = $updateDate;
return $this;
}
/**
* @param Department $department
*
* @return Ticket
*/
public function addDepartment(Department $department)
{
$this->departments[] = $department;
return $this;
}
/**
* @param Department $department
*
* @return Ticket
*/
public function removeDepartment(Department $department)
{
if (!$this->departments->contains($department)) {
return;
}
$this->departments->removeElement($department);
return $this;
}
/**
* @return Ticket
*/
public function removeDepartments()
{
$this->departments = [];
return $this;
}
/**
* @return ArrayCollection
*/
public function getDepartments()
{
return $this->departments;
}
/**
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
return $this;
}
/**
* @return User
*/
public function getReplyUser()
{
return $this->replyUser;
}
/**
* @param User $replyUser
*/
public function setReplyUser(User $replyUser)
{
$this->replyUser = $replyUser;
return $this;
}
/**
* @param UploadedFile $attachmentFile
*/
public function setAttachmentFile(UploadedFile $attachmentFile = null)
{
$this->attachmentFile = $attachmentFile;
return $this;
}
/**
* @return UploadedFile
*/
public function getAttachmentFile()
{
return $this->attachmentFile;
}
public function getUploadFilePath()
{
if (!is_dir(self::FILE_FOLDER . $this->id)) {
mkdir(self::FILE_FOLDER . $this->id, 0777, true);
}
return self::FILE_FOLDER . $this->id . '/';
}
public function getAttachmentFilePath()
{
if (!$this->filename) {
return;
}
return $this->getUploadFilePath() . $this->filename;
}
public function uploadFile()
{
$attachmentFile = $this->getAttachmentFile();
if (!$attachmentFile) {
return;
}
$this->removeAttachmentFile();
$nDate = new DateTime();
$filename = 'TICKET_FILE_' . $nDate->format('Y-m-d-His');
$this->filename = $filename . '.' . $attachmentFile->guessExtension();
$attachmentFile->move(
$this->getUploadFilePath(),
$this->filename
);
$this->setAttachmentFile(null);
}
#[ORM\PreUpdate]
#[ORM\PrePersist]
public function lifecycleFileUpload()
{
$this->uploadFile();
}
#[ORM\PostRemove]
public function removeUpload()
{
$this->removeAttachmentFile();
}
#[ORM\PostPersist]
public function onPostPersist()
{
rename(
self::FILE_FOLDER . $this->filename,
$this->getUploadFilePath() . $this->filename
);
}
public function removeAttachmentFile()
{
if ($attachmentFile = $this->getAttachmentFilePath()) {
@unlink($attachmentFile);
}
}
/**
* @param TicketMessage $ticketMessage
*
* @return Ticket
*/
public function addTicketMessage(TicketMessage $ticketMessage)
{
$ticketMessage->setTicket($this);
$this->ticketMessages->add($ticketMessage);
return $this;
}
/**
* @param TicketMessage $ticketMessage
*
* @return Ticket
*/
public function removeTicketMessage(TicketMessage $ticketMessage)
{
if (!$this->ticketMessages->contains($ticketMessage)) {
return;
}
$this->ticketMessages->removeElement($ticketMessage);
$ticketMessage->setTicket(null);
return $this;
}
/**
* @return ArrayCollection
*/
public function getTicketMessages()
{
return $this->ticketMessages;
}
/**
* @return string
*/
public function getFilename()
{
return $this->filename;
}
/**
* @return int
*/
public function getType(): int
{
return $this->type;
}
/**
* @param int $type
*/
public function setType(int $type)
{
$this->type = $type;
return $this;
}
/**
* @return string
*/
public function getDepartmentNames()
{
$departmentNames = [];
foreach ($this->getDepartments() as $department) {
$departmentNames[] = $department->getName();
}
return implode(', ', $departmentNames);
}
public function getLastRepliedAt()
{
if ($this->getTicketMessages()->count() === 0) {
return '';
}
$lastMessage = $this->getTicketMessages()->last();
return $lastMessage->getCreatedDate()->format('d/m/Y');
}
/**
* Get the Replies count (number of Ticket Messages).
*
* @return integer
*/
public function getReplies()
{
return $this->getTicketMessages()->count();
}
}