added model for controller to allow ticket to fetch commentss and attachments without JS

This commit is contained in:
tp_dhu 2025-03-23 10:30:36 +00:00
parent 7872049a1c
commit a02194d253
8 changed files with 146 additions and 56 deletions

View File

@ -37,7 +37,7 @@ class TicketController implements CRUD {
$this->get_custom_fields($f3, $db, $ticket_id); $this->get_custom_fields($f3, $db, $ticket_id);
// render // render
$f3->set('js', 'ticket_view.js'); // $f3->set('js', 'ticket_view.js');
$f3->set('content', '../ui/views/ticket/view.html'); $f3->set('content', '../ui/views/ticket/view.html');
echo \Template::instance()->render('../ui/templates/layout.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){ protected function get_ticket_check_edit_permission($f3){
$db = $f3->get('DB'); $db = $f3->get('DB');
$ticket_id = $f3->get('PARAMS.id'); $ticket_id = $f3->get('PARAMS.id');
@ -244,6 +245,25 @@ class TicketController implements CRUD {
} }
protected function get_ticket($f3, $db, $ticket_id){ 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( $result = $db->exec(
'SELECT t.*, u.username as created_by_name 'SELECT t.*, u.username as created_by_name
FROM tickets t FROM tickets t

21
app/models/Attachment.php Normal file
View File

@ -0,0 +1,21 @@
<?php
class Attachment extends \DB\SQL\Mapper {
function __construct($db)
{
parent::__construct($db, 'attachments');
}
public function findWithUserByTicketId($ticket_id){
return $this->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]
);
}
}

19
app/models/Comment.php Normal file
View File

@ -0,0 +1,19 @@
<?php
class Comment extends \DB\SQL\Mapper {
function __construct($db)
{
parent::__construct($db, 'ticket_comments');
}
public function findWithUserByTicketId($ticket_id){
return $this->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]
);
}
}

22
app/models/Ticket.php Normal file
View File

@ -0,0 +1,22 @@
<?php
class Ticket extends \DB\SQL\Mapper {
function __construct($db){
parent::__construct($db, 'tickets');
}
public function findById($id){
$this->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);
}
}

View File

@ -3,6 +3,7 @@
<h2 class="title">Attachments</h2> <h2 class="title">Attachments</h2>
<div class="block"> <div class="block">
<check if="isset( {{@attachments }})"> <check if="isset( {{@attachments }})">
<check if="count({{@attachments}}) > 0">
<table class="table is-fullwidth is-narrow is-striped is-hoverable"> <table class="table is-fullwidth is-narrow is-striped is-hoverable">
<thead> <thead>
<tr> <tr>
@ -28,9 +29,10 @@
</tbody> </tbody>
</table> </table>
</check> </check>
</check>
<div class="block"> <div class="block">
<h3 class="title">Upload attachment</h3> <h3 class="title">Upload attachment</h3>
<form action="/ticket/{{@ticket_id}}/attachments/upload" method="POST" enctype="multipart/form-data"> <form action="/ticket/{{@PARAMS.id}}/attachments/upload" method="POST" enctype="multipart/form-data">
<div class="field has-addons"> <div class="field has-addons">
<div class="control has-icons-left"><!-- is-expanded --> <div class="control has-icons-left"><!-- is-expanded -->
<input class="input" type="file" name="attachment" required> <input class="input" type="file" name="attachment" required>
@ -46,3 +48,4 @@
</div> </div>
</div> </div>
</div> </div>
</div>

View File

@ -1,18 +1,6 @@
<hr>
<div class="box" id="comments"> <div class="box" id="comments">
<h2 class="title">Comments</h2> <h2 class="title">Comments</h2>
<div class="block">
<form action="/ticket/{{@PARAMS.id}}/comment" method="POST">
<div class="field">
<label class="label">Add comment:</label>
<div class="control">
<textarea class="textarea" name="comment" rows="4" cols="50"></textarea>
</div>
</div>
<div class="field">
<button class="button is-primary" type="submit">Submit Comment</button>
</div>
</form>
</div>
<check if="{{ !empty(@comments) }}"> <check if="{{ !empty(@comments) }}">
<div class="list"> <div class="list">
<repeat group="{{ @comments }}" value="{{ @comment}}"> <repeat group="{{ @comments }}" value="{{ @comment}}">
@ -38,4 +26,17 @@
</repeat> </repeat>
</div> </div>
</check> </check>
<div class="block">
<form action="/ticket/{{@PARAMS.id}}/comment" method="POST">
<div class="field">
<label class="label">Add comment:</label>
<div class="control">
<textarea class="textarea" name="comment" rows="4" cols="50"></textarea>
</div>
</div>
<div class="field is-clearfix">
<button class="button is-primary is-pulled-right" type="submit">Submit Comment</button>
</div>
</form>
</div>
</div> </div>

View File

@ -11,7 +11,7 @@
<table class="table is-fullwidth is-bordered"> <table class="table is-fullwidth is-bordered">
<thead> <thead>
<tr> <tr class="has-background-warning">
<th>id</th><th>title</th> <th>id</th><th>title</th>
<th>status</th><th>priority</th><th>created_at</th> <th>status</th><th>priority</th><th>created_at</th>
<th></th> <th></th>
@ -21,8 +21,8 @@
<tbody> <tbody>
<repeat group="{{@tickets}}" value="{{@ticket}}"> <repeat group="{{@tickets}}" value="{{@ticket}}">
<tr> <tr>
<td><a href="/ticket/{{@ticket.id}}">{{@ticket.id}}</a></td> <td>{{@ticket.id}}</td>
<td>{{@ticket.title}}</td> <td><a href="/ticket/{{@ticket.id}}">{{@ticket.title}}</a></td>
<td>{{@ticket.status}}</td> <td>{{@ticket.status}}</td>
<td>{{@ticket.priority}}</td> <td>{{@ticket.priority}}</td>
<td>{{@ticket.created_at}}</td> <td>{{@ticket.created_at}}</td>

View File

@ -76,9 +76,13 @@
<hr> <hr>
<include href="../ui/views/attachment/index.html">
<include href="../ui/views/comments/view.html">
<!--
<div class="block" id="attachments"></div> <div class="block" id="attachments"></div>
<div class="block" id="comments"></div> <div class="block" id="comments"></div>
-->
</div> </div>