author = $author; } else if($author instanceof Staff) { $this->authorStaff = $author; } } public function getAuthor() { if($this->author && !$this->author->isNull()) { return $this->author; } else { return $this->authorStaff; } } public function getDefaultProps() { return array( 'priority' => 'low', 'unread' => false, 'unreadStaff' => true, 'ticketNumber' => $this->generateUniqueTicketNumber() ); } public function store() { parent::store(); } public function delete() { parent::delete(); } public function generateUniqueTicketNumber() { $linearCongruentialGenerator = new LinearCongruentialGenerator(); if (Ticket::count() === 0) { $ticketNumber = Setting::getSetting('ticket-first-number')->value; } else { $lastTicketId = Ticket::findOne(' ORDER BY id DESC')->id; $linearCongruentialGenerator->setGap(Setting::getSetting('ticket-gap')->value); $linearCongruentialGenerator->setFirst(Setting::getSetting('ticket-first-number')->value); $ticketNumber = $linearCongruentialGenerator->generate($lastTicketId + 1); } return $ticketNumber; } public function toArray($minimized = false) { return [ 'ticketNumber' => $this->ticketNumber, 'title' => $this->title, 'content' => $minimized ? strip_tags($this->content) : $this->content, 'department' => [ 'id' => $this->department->id, 'name' => $this->department->name ], 'date' => $this->date, 'file' => $this->file, 'language' => $this->language, 'unread' => !!$this->unread, 'unreadStaff' => !!$this->unreadStaff, 'closed' => !!$this->closed, 'priority' => $this->priority, 'author' => $this->authorToArray(), 'owner' => $this->ownerToArray(), 'events' => $minimized ? [] : $this->eventsToArray(), 'tags' => $this->sharedTagList->toArray(true), 'edited' => $this->editedContent ]; } public function authorToArray() { $author = $this->getAuthor(); if ($author && !$author->isNull()) { return [ 'id' => $author->id, 'name' => $author->name, 'staff' => $author instanceof Staff, 'profilePic' => ($author instanceof Staff) ? $author->profilePic : null, 'email' => $author->email, 'customfields' => $author->xownCustomfieldvalueList ? $author->xownCustomfieldvalueList->toArray() : [], ]; } else { return [ 'id' => NULL, 'staff' => false, 'name' => $this->authorName, 'email' => $this->authorEmail ]; } } public function ownerToArray() { $owner = $this->owner; if ($owner && !$owner->isNull()) { return [ 'id' => $owner->id, 'name' => $owner->name, 'email' => $owner->email ]; } else { return null; } } public function eventsToArray() { $events = []; foreach ($this->ownTicketeventList as $ticketEvent) { $event = [ 'type' => $ticketEvent->type, 'content'=> $ticketEvent->content, 'author' => [], 'date'=> $ticketEvent->date, 'file'=> $ticketEvent->file, 'private'=> $ticketEvent->private, 'edited' => $ticketEvent->editedContent, 'id' => $ticketEvent->id ]; $author = $ticketEvent->getAuthor(); if($author && !$author->isNull()) { $event['author'] = [ 'id'=> $author->id, 'name' => $author->name, 'email' =>$author->email, 'profilePic' => ($author instanceof Staff) ? $author->profilePic : null, 'staff' => $author instanceof Staff ]; } if(!Controller::isStaffLogged() && $ticketEvent->private) { continue; } $events[] = $event; } return $events; } public function addEvent(Ticketevent $event) { $this->ownTicketeventList->add($event); } public function isAuthor($user) { $ticketAuthor = $this->authorToArray(); if(is_string($user)) return $user == $ticketAuthor['email']; if(!($user instanceof DataStore) || $user->isNull()) return false; return $user->id == $ticketAuthor['id'] && ($user instanceof Staff) == $ticketAuthor['staff']; } public function isOwner($user) { return !$user->isNull() && $this->owner && $user->id == $this->owner->id && ($user instanceof Staff); } }