opensupports/server/models/Ticket.php

148 lines
4.0 KiB
PHP
Raw Normal View History

<?php
use RedBeanPHP\Facade as RedBean;
class Ticket extends DataStore {
const TABLE = 'ticket';
public static function getProps() {
return array(
'ticketNumber',
'title',
'content',
'language',
'department',
'file',
'date',
'unread',
'closed',
'priority',
'author',
'owner',
2016-10-21 06:08:06 +02:00
'ownTicketeventList',
'unreadStaff',
'language'
);
}
public static function getTicket($value, $property = 'id') {
return parent::getDataStore($value, $property);
}
public static function getByTicketNumber($value) {
return Ticket::getTicket($value, 'ticketNumber');
}
public function getDefaultProps() {
return array(
'priority' => 'low',
2016-10-21 21:38:08 +02:00
'unread' => false,
'unreadStaff' => true,
'ticketNumber' => $this->generateUniqueTicketNumber()
);
}
public function store() {
parent::store();
}
2016-09-29 19:34:20 +02:00
public function generateUniqueTicketNumber() {
$ticketQuantity = Ticket::count();
$minValue = 100000;
$maxValue = 999999;
if ($ticketQuantity === 0) {
$ticketNumber = Hashing::getRandomTicketNumber($minValue, $maxValue);
} else {
$firstTicketNumber = Ticket::getTicket(1)->ticketNumber;
$gap = 176611; //TODO: USE RANDOM PRIME INSTEAD
$ticketNumber = ($firstTicketNumber - $minValue + $ticketQuantity * $gap) % ($maxValue - $minValue + 1) + $minValue;
}
return $ticketNumber;
}
public function toArray() {
return [
'ticketNumber' => $this->ticketNumber,
'title' => $this->title,
'content' => $this->content,
'department' => [
'id' => $this->department->id,
'name' => $this->department->name
],
'date' => $this->date,
'file' => $this->file,
'language' => $this->language,
'unread' => !!$this->unread,
2016-11-21 03:01:38 +01:00
'unreadStaff' => !!$this->unreadStaff,
'closed' => !!$this->closed,
'priority' => $this->priority,
'author' => $this->authorToArray(),
'owner' => $this->ownerToArray(),
2016-09-29 19:34:20 +02:00
'events' => $this->eventsToArray()
];
}
public function authorToArray() {
$author = $this->author;
if ($author && !$author->isNull()) {
return [
'id' => $author->id,
'name' => $author->name,
'email' => $author->email
];
} else {
return [];
}
}
public function ownerToArray() {
$owner = $this->owner;
if ($owner && !$owner->isNull()) {
return [
'id' => $owner->id,
'name' => $owner->name,
'email' => $owner->email
];
} else {
2016-11-21 03:01:38 +01:00
return null;
}
}
2016-09-29 19:34:20 +02:00
public function eventsToArray() {
$events = [];
foreach ($this->ownTicketeventList as $ticketEvent) {
$event = [
'type' => $ticketEvent->type,
'content'=> $ticketEvent->content,
'author' => [],
'date'=> $ticketEvent->date,
'file'=> $ticketEvent->file
];
2016-09-29 19:34:20 +02:00
$author = $ticketEvent->getAuthor();
if(!$author->isNull()) {
$event['author'] = [
'id'=> $author->id,
'name' => $author->name,
'email' =>$author->email,
2016-11-21 03:01:38 +01:00
'profilePic' => ($author instanceof Staff) ? $author->profilePic : null,
2016-09-29 19:34:20 +02:00
'staff' => $author instanceof Staff
];
}
$events[] = $event;
}
2016-09-29 19:34:20 +02:00
return $events;
}
public function addEvent(Ticketevent $event) {
$this->ownTicketeventList->add($event);
}
}