Merge branch 'models'

This commit is contained in:
tp_dhu 2025-03-23 10:34:13 +00:00
commit 4b45d94ebb
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

@ -2,47 +2,50 @@
<div class="content"> <div class="content">
<h2 class="title">Attachments</h2> <h2 class="title">Attachments</h2>
<div class="block"> <div class="block">
<check if="isset( {{@attachments }})"> <check if="isset( {{@attachments }})">
<table class="table is-fullwidth is-narrow is-striped is-hoverable"> <check if="count({{@attachments}}) > 0">
<thead> <table class="table is-fullwidth is-narrow is-striped is-hoverable">
<tr> <thead>
<th class="th-icon"></th>
<th>File Name</th>
<th>Uploaded By</th>
<th>Created At</th>
<th>Version</th>
</tr>
</thead>
<tbody>
<repeat group="{{ @attachments }}" value="{{ @attach }}">
<tr> <tr>
<td> <th class="th-icon"></th>
<span class="icon"><i class="fas fa-file"></i></span> <th>File Name</th>
</td> <th>Uploaded By</th>
<td><a href="/attachment/{{@attach.id}}/download">{{ @attach.file_name }}</a></td> <th>Created At</th>
<td>{{ @attach.username }}</td> <th>Version</th>
<td>{{ @attach.created_at }}</td>
<td>{{ @attach.version_number }}</td>
</tr> </tr>
</repeat> </thead>
</tbody> <tbody>
</table> <repeat group="{{ @attachments }}" value="{{ @attach }}">
</check> <tr>
<div class="block"> <td>
<h3 class="title">Upload attachment</h3> <span class="icon"><i class="fas fa-file"></i></span>
<form action="/ticket/{{@ticket_id}}/attachments/upload" method="POST" enctype="multipart/form-data"> </td>
<div class="field has-addons"> <td><a href="/attachment/{{@attach.id}}/download">{{ @attach.file_name }}</a></td>
<div class="control has-icons-left"><!-- is-expanded --> <td>{{ @attach.username }}</td>
<input class="input" type="file" name="attachment" required> <td>{{ @attach.created_at }}</td>
<span class="icon is-small is-left"> <td>{{ @attach.version_number }}</td>
<i class="fas fa-file"></i> </tr>
</span> </repeat>
</tbody>
</table>
</check>
</check>
<div class="block">
<h3 class="title">Upload attachment</h3>
<form action="/ticket/{{@PARAMS.id}}/attachments/upload" method="POST" enctype="multipart/form-data">
<div class="field has-addons">
<div class="control has-icons-left"><!-- is-expanded -->
<input class="input" type="file" name="attachment" required>
<span class="icon is-small is-left">
<i class="fas fa-file"></i>
</span>
</div>
<div class="control">
<button class="button" type="submit">Upload</button>
</div>
</div> </div>
<div class="control"> </form>
<button class="button" type="submit">Upload</button> </div>
</div>
</div>
</form>
</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>