167 lines
4.7 KiB
PHP
167 lines
4.7 KiB
PHP
<?php
|
|
|
|
class Ticket extends \DB\SQL\Mapper {
|
|
function __construct($db){
|
|
parent::__construct($db, 'tickets');
|
|
}
|
|
|
|
/**
|
|
* Return all tickets in descending order of creation
|
|
*/
|
|
public function findAll(): array
|
|
{
|
|
return $this->db->exec(
|
|
'SELECT *
|
|
FROM tickets
|
|
WHERE recycled = 0
|
|
ORDER BY created_at DESC'
|
|
);
|
|
}
|
|
|
|
public function findById($id): ?Ticket
|
|
{
|
|
$this->load(['id = ?', $id]);
|
|
return $this->dry() ? null : $this;
|
|
}
|
|
|
|
public function createTicket(array $data): int
|
|
{
|
|
$this->reset();
|
|
|
|
$this->title = $data['title'] ?? '';
|
|
$this->description = $data['description'] ?? '';
|
|
$this->priority = $data['priority'] ?? 'Low';
|
|
$this->status = $data['status'] ?? 'New';
|
|
$this->created_by = $data['created_by'] ?? null;
|
|
$this->created_at = $data['created_at'] ?? date('Y-m-d H:i:s');
|
|
$this->updated_at = date('Y-m-d H:i:s');
|
|
|
|
$this->save();
|
|
return (int)$this->id;
|
|
}
|
|
|
|
public function updateTicket(array $data): void
|
|
{
|
|
if(isset($data['title'])){ $this->title = $data['title']; }
|
|
if(isset($data['description'])) { $this->description = $data['description']; }
|
|
if(isset($data['priority'])) { $this->priority = $data['priority']; }
|
|
if(isset($data['status'])) { $this->status = $data['status']; }
|
|
if(isset($data['updated_by'])) { $this->updated_by = $data['updated_by']; }
|
|
$this->created_at = ($data['created_at'] == '' ? date('Y-m-d H:i:s') : $data['created_at']) ?? date('Y-m-d H:i:s');
|
|
$this->updated_at = date('Y-m-d H:i:s');
|
|
$this->save();
|
|
}
|
|
|
|
public function softDelete():void {
|
|
$this->recycled = 1;
|
|
$this->save();
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
public function getParentTickets()
|
|
{
|
|
return $this->db->exec(
|
|
'SELECT p.*
|
|
FROM ticket_relations r
|
|
INNER JOIN tickets p ON r.parent_ticket_id = p.id
|
|
WHERE r.child_ticket_id = ?',
|
|
[$this->id]
|
|
);
|
|
}
|
|
|
|
public function getChildTickets()
|
|
{
|
|
return $this->db->exec(
|
|
'SELECT c.*
|
|
FROM ticket_relations r
|
|
INNER JOIN tickets c ON r.child_ticket_id = c.id
|
|
WHERE r.parent_ticket_id = ?',
|
|
[$this->id]
|
|
);
|
|
}
|
|
|
|
public function addChildTicket(int $childId)
|
|
{
|
|
$this->db->exec(
|
|
'INSERT IGNORE INTO ticket_relations (parent_ticket_id, child_ticket_id)
|
|
VALUES (?, ?)',
|
|
[$this->id, $childId]
|
|
);
|
|
}
|
|
|
|
// meta data
|
|
public function getMeta()
|
|
{
|
|
return $this->db->exec(
|
|
'SELECT id, meta_key, meta_value
|
|
FROM ticket_meta
|
|
WHERE ticket_id = ?',
|
|
[$this->id]
|
|
);
|
|
}
|
|
|
|
public function getMetaAssoc()
|
|
{
|
|
$rows = $this->getMeta();
|
|
$assoc = [];
|
|
foreach($rows as $row){
|
|
$assoc[$row['meta_key']] = $row['meta_value'];
|
|
}
|
|
return $assoc;
|
|
}
|
|
|
|
public function assocExistingMeta($meta_ids, $meta_keys, $meta_values){
|
|
if(is_array($meta_ids) && is_array($meta_keys) && is_array($meta_values)){
|
|
$field_assoc = [];
|
|
foreach($meta_ids as $i => $m_id){
|
|
$key = $meta_keys[$i] ?? '';
|
|
$value = $meta_values[$i] ?? '';
|
|
if(!empty($key) && $value !== ''){
|
|
$field_assoc[$key] = $value;
|
|
}
|
|
}
|
|
return $field_assoc;
|
|
}
|
|
return [];
|
|
}
|
|
|
|
public function assocMetaFromKeyValue($meta_keys, $meta_values)
|
|
{
|
|
if(is_array($meta_keys) && is_array($meta_values)){
|
|
$field_assoc = [];
|
|
foreach($meta_keys as $i => $key){
|
|
$val = $meta_values[$i] ?? '';
|
|
if(!empty($key) && $val != ''){
|
|
$field_assoc[$key] = $val;
|
|
}
|
|
}
|
|
return $field_assoc;
|
|
}
|
|
return [];
|
|
}
|
|
|
|
public function setCustomFields(array $fields)
|
|
{
|
|
$this->db->exec(
|
|
'DELETE FROM ticket_meta WHERE ticket_id = ?', [$this->id]
|
|
);
|
|
|
|
foreach($fields as $key => $value){
|
|
$this->db->exec(
|
|
'INSERT INTO ticket_meta (ticket_id, meta_key, meta_value)
|
|
VALUES (?, ?, ?)',
|
|
[$this->id, $key, $value]
|
|
);
|
|
}
|
|
}
|
|
|
|
} |