From 2a64202ea346fdc7599b545dbbf81654554b9743 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Tue, 28 Sep 2021 22:18:11 -0500 Subject: [PATCH] Add ticket message attachment as resources --- public/main/inc/lib/TicketManager.php | 104 +++++++++--------- src/CoreBundle/Entity/Ticket.php | 2 +- .../Entity/TicketMessageAttachment.php | 48 +++++++- src/CoreBundle/Framework/Container.php | 7 ++ .../TicketMessageAttachmentRepository.php | 18 +++ src/CoreBundle/Tool/User.php | 2 + 6 files changed, 129 insertions(+), 52 deletions(-) create mode 100644 src/CoreBundle/Repository/TicketMessageAttachmentRepository.php diff --git a/public/main/inc/lib/TicketManager.php b/public/main/inc/lib/TicketManager.php index 2a497f6bfa..a46d0dfff2 100644 --- a/public/main/inc/lib/TicketManager.php +++ b/public/main/inc/lib/TicketManager.php @@ -3,12 +3,14 @@ /* For licensing terms, see /license.txt */ use Chamilo\CoreBundle\Entity\Ticket; +use Chamilo\CoreBundle\Entity\TicketMessage; use Chamilo\CoreBundle\Entity\TicketMessageAttachment; use Chamilo\CoreBundle\Entity\TicketPriority; use Chamilo\CoreBundle\Entity\TicketProject; use Chamilo\CoreBundle\Entity\TicketStatus; use Chamilo\CoreBundle\Framework\Container; use Chamilo\CourseBundle\Entity\CLp; +use Symfony\Component\HttpFoundation\File\UploadedFile; /** * Class TicketManager. @@ -716,64 +718,66 @@ class TicketManager /** * Attachment files when a message is sent. * - * @param $file_attach - * @param $ticketId - * @param $message_id - * - * @return bool + * @throws \Doctrine\ORM\ORMException + * @throws \Doctrine\ORM\OptimisticLockException + * @throws \Doctrine\ORM\TransactionRequiredException */ public static function saveMessageAttachmentFile( - $file_attach, + $fileAttach, $ticketId, - $message_id - ) { - throw new Exception('Implement saveMessageAttachmentFile'); - $now = api_get_utc_datetime(); - $userId = api_get_user_id(); - $ticketId = (int) $ticketId; + $messageId + ): bool + { + if (!is_array($fileAttach) || UPLOAD_ERR_OK != $fileAttach['error']) { + return false; + } + + $em = Database::getManager(); - $new_file_name = add_ext_on_mime( - stripslashes($file_attach['name']), - $file_attach['type'] + $ticket = $em->find(Ticket::class, $ticketId); + $message = $em->find(TicketMessage::class, $messageId); + + $newFileName = add_ext_on_mime( + stripslashes($fileAttach['name']), + $fileAttach['type'] ); - $table_support_message_attachments = Database::get_main_table(TABLE_TICKET_MESSAGE_ATTACHMENTS); - if (!filter_extension($new_file_name)) { - echo Display::return_message( - get_lang('File upload failed: this file extension or file type is prohibited'), - 'error' + + $fileName = $fileAttach['name']; + + if (!filter_extension($newFileName)) { + Display::addFlash( + Display::return_message( + get_lang('File upload failed: this file extension or file type is prohibited'), + 'error' + ) ); - } else { - throw new Exception('@todo file upload ticket_attachment'); - //$result = api_upload_file('ticket_attachment', $file_attach, $ticketId); - if ($result) { - $safe_file_name = Database::escape_string($new_file_name); - $safe_new_file_name = Database::escape_string($result['path_to_save']); - $sql = "INSERT INTO $table_support_message_attachments ( - filename, - path, - ticket_id, - message_id, - size, - sys_insert_user_id, - sys_insert_datetime, - sys_lastedit_user_id, - sys_lastedit_datetime - ) VALUES ( - '$safe_file_name', - '$safe_new_file_name', - '$ticketId', - '$message_id', - '".$file_attach['size']."', - '$userId', - '$now', - '$userId', - '$now' - )"; - Database::query($sql); - return true; - } + return false; } + + $currentUser = api_get_user_entity(); + + $repo = Container::getTicketMessageAttachmentRepository(); + $attachment = (new TicketMessageAttachment()) + ->setFilename($fileName) + ->setPath(uniqid('ticket_message', true)) + ->setMessage($message) + ->setSize((int) $fileAttach['size']) + ->setTicket($ticket) + ->setInsertUserId($currentUser->getId()) + ->setInsertDateTime(api_get_utc_datetime(null, false, true)) + ->addUserLink($currentUser) + ->setParent($currentUser) + ; + + $em->persist($attachment); + $em->flush(); + + $file = new UploadedFile($fileAttach['tmp_name'], $fileAttach['name'], $fileAttach['type'], $fileAttach['error']); + + $repo->addFile($attachment, $file); + + return true; } /** diff --git a/src/CoreBundle/Entity/Ticket.php b/src/CoreBundle/Entity/Ticket.php index 624a9d93f5..169bdb91a6 100644 --- a/src/CoreBundle/Entity/Ticket.php +++ b/src/CoreBundle/Entity/Ticket.php @@ -49,7 +49,7 @@ class Ticket * @ORM\ManyToOne(targetEntity="TicketCategory") * @ORM\JoinColumn(name="category_id", referencedColumnName="id") */ - protected TicketProject $category; + protected TicketCategory $category; /** * @ORM\ManyToOne(targetEntity="TicketPriority") diff --git a/src/CoreBundle/Entity/TicketMessageAttachment.php b/src/CoreBundle/Entity/TicketMessageAttachment.php index cadcbe963a..fe7641b2b0 100644 --- a/src/CoreBundle/Entity/TicketMessageAttachment.php +++ b/src/CoreBundle/Entity/TicketMessageAttachment.php @@ -13,7 +13,7 @@ use Doctrine\ORM\Mapping as ORM; * @ORM\Table(name="ticket_message_attachments") * @ORM\Entity */ -class TicketMessageAttachment +class TicketMessageAttachment extends AbstractResource implements ResourceInterface { /** * @ORM\Column(name="id", type="integer") @@ -69,6 +69,11 @@ class TicketMessageAttachment */ protected ?DateTime $lastEditDateTime = null; + public function __toString(): string + { + return $this->getFilename(); + } + /** * @return int */ @@ -136,4 +141,45 @@ class TicketMessageAttachment return $this; } + + public function getTicket(): Ticket + { + return $this->ticket; + } + + public function setTicket(Ticket $ticket): self + { + $this->ticket = $ticket; + + return $this; + } + + public function getResourceName(): string + { + return $this->getFilename(); + } + + public function setResourceName(string $name) + { + return $this->setFilename($name); + } + + public function getResourceIdentifier(): int + { + return $this->getId(); + } + + public function setInsertUserId(int $insertUserId): self + { + $this->insertUserId = $insertUserId; + + return $this; + } + + public function setInsertDateTime(DateTime $insertDateTime): self + { + $this->insertDateTime = $insertDateTime; + + return $this; + } } diff --git a/src/CoreBundle/Framework/Container.php b/src/CoreBundle/Framework/Container.php index a24a6bca4a..ed1dff657b 100644 --- a/src/CoreBundle/Framework/Container.php +++ b/src/CoreBundle/Framework/Container.php @@ -8,6 +8,7 @@ namespace Chamilo\CoreBundle\Framework; use Chamilo\CoreBundle\Component\Editor\CkEditor\CkEditor; use Chamilo\CoreBundle\Component\Editor\Editor; +use Chamilo\CoreBundle\Entity\TicketMessageAttachment; use Chamilo\CoreBundle\Repository\AssetRepository; use Chamilo\CoreBundle\Repository\CareerRepository; use Chamilo\CoreBundle\Repository\CourseCategoryRepository; @@ -28,6 +29,7 @@ use Chamilo\CoreBundle\Repository\SessionRepository; use Chamilo\CoreBundle\Repository\SkillRepository; use Chamilo\CoreBundle\Repository\SysAnnouncementRepository; use Chamilo\CoreBundle\Repository\TagRepository; +use Chamilo\CoreBundle\Repository\TicketMessageAttachmentRepository; use Chamilo\CoreBundle\Repository\TrackExerciseRepository; use Chamilo\CoreBundle\Serializer\UserToJsonNormalizer; use Chamilo\CoreBundle\Settings\SettingsManager; @@ -280,6 +282,11 @@ class Container return self::$container->get(CAnnouncementAttachmentRepository::class); } + public static function getTicketMessageAttachmentRepository(): TicketMessageAttachmentRepository + { + return self::$container->get(TicketMessageAttachmentRepository::class); + } + public static function getCourseRepository(): CourseRepository { return self::$container->get(CourseRepository::class); diff --git a/src/CoreBundle/Repository/TicketMessageAttachmentRepository.php b/src/CoreBundle/Repository/TicketMessageAttachmentRepository.php new file mode 100644 index 0000000000..d802c64358 --- /dev/null +++ b/src/CoreBundle/Repository/TicketMessageAttachmentRepository.php @@ -0,0 +1,18 @@ + PersonalFile::class, 'message_attachments' => MessageAttachment::class, + 'ticket_message_attachments' => TicketMessageAttachment::class, ]; } }