From a02194d2533e9b41b48d29153378c7b158bc08fc Mon Sep 17 00:00:00 2001 From: tp_dhu Date: Sun, 23 Mar 2025 10:30:36 +0000 Subject: [PATCH] added model for controller to allow ticket to fetch commentss and attachments without JS --- app/controllers/TicketController.php | 22 +++++++- app/models/Attachment.php | 21 ++++++++ app/models/Comment.php | 19 +++++++ app/models/Ticket.php | 22 ++++++++ ui/views/attachment/index.html | 81 ++++++++++++++-------------- ui/views/comments/view.html | 27 +++++----- ui/views/ticket/index.html | 6 +-- ui/views/ticket/view.html | 4 ++ 8 files changed, 146 insertions(+), 56 deletions(-) create mode 100644 app/models/Attachment.php create mode 100644 app/models/Comment.php create mode 100644 app/models/Ticket.php diff --git a/app/controllers/TicketController.php b/app/controllers/TicketController.php index fa48f03..3374152 100644 --- a/app/controllers/TicketController.php +++ b/app/controllers/TicketController.php @@ -37,7 +37,7 @@ class TicketController implements CRUD { $this->get_custom_fields($f3, $db, $ticket_id); // render - $f3->set('js', 'ticket_view.js'); + // $f3->set('js', 'ticket_view.js'); $f3->set('content', '../ui/views/ticket/view.html'); echo \Template::instance()->render('../ui/templates/layout.html'); @@ -216,6 +216,7 @@ class TicketController implements CRUD { protected function get_ticket_check_edit_permission($f3){ + $db = $f3->get('DB'); $ticket_id = $f3->get('PARAMS.id'); @@ -244,6 +245,25 @@ class TicketController implements CRUD { } protected function get_ticket($f3, $db, $ticket_id){ + + // new + $db = $f3->get('DB'); + $ticket_id = $f3->get('PARAMS.id'); + + $ticketModel = new Ticket($db); + $ticket = $ticketModel->findById($ticket_id); + + if(!$ticket->dry()){ + $f3->set('ticket', $ticket); + $f3->set('attachments', $ticket->attachments()); + $f3->set('comments', $ticket->comments()); + } else { + $f3->error(404, "Ticket not found!"); + } + return; + + // old: + $result = $db->exec( 'SELECT t.*, u.username as created_by_name FROM tickets t diff --git a/app/models/Attachment.php b/app/models/Attachment.php new file mode 100644 index 0000000..0d3a6d2 --- /dev/null +++ b/app/models/Attachment.php @@ -0,0 +1,21 @@ +db->exec( + 'SELECT a.*, u.username + FROM attachments a + LEFT JOIN users u ON u.id = a.uploaded_by + WHERE a.ticket_id = ? + ORDER BY a.created_at DESC', + [$ticket_id] + ); + } + +} \ No newline at end of file diff --git a/app/models/Comment.php b/app/models/Comment.php new file mode 100644 index 0000000..f344d80 --- /dev/null +++ b/app/models/Comment.php @@ -0,0 +1,19 @@ +db->exec( + 'SELECT c.*, u.username AS author_name + FROM ticket_comments c + LEFT JOIN users u ON c.created_by = u.id + WHERE c.ticket_id = ? + ORDER BY c.created_at DESC', + [$ticket_id] + ); + } + } \ No newline at end of file diff --git a/app/models/Ticket.php b/app/models/Ticket.php new file mode 100644 index 0000000..40ea567 --- /dev/null +++ b/app/models/Ticket.php @@ -0,0 +1,22 @@ +load(['id = ?', $id]); + return $this; + } + + public function attachments(){ + $attachment = new Attachment($this->db); + return $attachment->findWithUserByTicketId($this->id); + } + + public function comments(){ + $comment = new Comment($this->db); + return $comment->findWithUserByTicketId($this->id); + } +} \ No newline at end of file diff --git a/ui/views/attachment/index.html b/ui/views/attachment/index.html index b2a1236..750d657 100644 --- a/ui/views/attachment/index.html +++ b/ui/views/attachment/index.html @@ -2,47 +2,50 @@

Attachments

- - - - - - - - - - - - - + + +
File NameUploaded ByCreated AtVersion
+ - - - - - + + + + + - - -
- - {{ @attach.file_name }}{{ @attach.username }}{{ @attach.created_at }}{{ @attach.version_number }}File NameUploaded ByCreated AtVersion
-
-
-

Upload attachment

-
-
-
- - - - + + + + + + + + {{ @attach.file_name }} + {{ @attach.username }} + {{ @attach.created_at }} + {{ @attach.version_number }} + + + + + + +
+

Upload attachment

+ +
+
+ + + + +
+
+ +
-
- -
-
- + +
-
+
\ No newline at end of file diff --git a/ui/views/comments/view.html b/ui/views/comments/view.html index 3aad8d7..63d7146 100644 --- a/ui/views/comments/view.html +++ b/ui/views/comments/view.html @@ -1,18 +1,6 @@ +

Comments

-
-
-
- -
- -
-
-
- -
-
-
@@ -38,4 +26,17 @@
+
+
+
+ +
+ +
+
+
+ +
+
+
\ No newline at end of file diff --git a/ui/views/ticket/index.html b/ui/views/ticket/index.html index 9b079ff..0e91d23 100644 --- a/ui/views/ticket/index.html +++ b/ui/views/ticket/index.html @@ -11,7 +11,7 @@ - + @@ -21,8 +21,8 @@ - - + + diff --git a/ui/views/ticket/view.html b/ui/views/ticket/view.html index 9e1ad7e..c886be2 100644 --- a/ui/views/ticket/view.html +++ b/ui/views/ticket/view.html @@ -76,9 +76,13 @@
+ + + \ No newline at end of file
idtitle statusprioritycreated_at
{{@ticket.id}}{{@ticket.title}}{{@ticket.id}}{{@ticket.title}} {{@ticket.status}} {{@ticket.priority}} {{@ticket.created_at}}