2016-06-17 21:28:38 +02:00
|
|
|
<?php
|
2016-07-04 20:57:00 +02:00
|
|
|
use Respect\Validation\Validator as DataValidator;
|
2016-08-04 05:59:04 +02:00
|
|
|
DataValidator::with('CustomValidations', true);
|
2016-07-04 20:57:00 +02:00
|
|
|
|
2016-06-17 21:28:38 +02:00
|
|
|
class CreateController extends Controller {
|
|
|
|
const PATH = '/create';
|
2017-02-08 19:09:15 +01:00
|
|
|
const METHOD = 'POST';
|
2016-06-17 21:28:38 +02:00
|
|
|
|
2016-06-30 23:03:17 +02:00
|
|
|
private $title;
|
2016-06-17 22:44:04 +02:00
|
|
|
private $content;
|
|
|
|
private $departmentId;
|
|
|
|
private $language;
|
2016-09-04 02:46:08 +02:00
|
|
|
private $ticketNumber;
|
2017-01-16 20:07:53 +01:00
|
|
|
private $email;
|
|
|
|
private $name;
|
2016-06-17 22:44:04 +02:00
|
|
|
|
2016-07-04 20:57:00 +02:00
|
|
|
public function validations() {
|
2017-01-16 20:07:53 +01:00
|
|
|
$validations = [
|
2016-08-01 05:34:55 +02:00
|
|
|
'permission' => 'user',
|
2016-07-04 20:57:00 +02:00
|
|
|
'requestData' => [
|
|
|
|
'title' => [
|
2016-11-04 23:10:32 +01:00
|
|
|
'validation' => DataValidator::length(10, 200),
|
2016-07-04 20:57:00 +02:00
|
|
|
'error' => ERRORS::INVALID_TITLE
|
|
|
|
],
|
|
|
|
'content' => [
|
2016-12-21 21:07:34 +01:00
|
|
|
'validation' => DataValidator::length(10, 5000),
|
2016-07-04 20:57:00 +02:00
|
|
|
'error' => ERRORS::INVALID_CONTENT
|
2016-08-04 05:59:04 +02:00
|
|
|
],
|
|
|
|
'departmentId' => [
|
|
|
|
'validation' => DataValidator::dataStoreId('department'),
|
|
|
|
'error' => ERRORS::INVALID_DEPARTMENT
|
2016-12-21 05:09:35 +01:00
|
|
|
],
|
|
|
|
'language' => [
|
|
|
|
'validation' => DataValidator::in(Language::getSupportedLanguages()),
|
|
|
|
'error' => ERRORS::INVALID_LANGUAGE
|
2016-07-04 21:08:16 +02:00
|
|
|
]
|
2016-07-04 20:57:00 +02:00
|
|
|
]
|
|
|
|
];
|
2017-01-16 20:07:53 +01:00
|
|
|
|
|
|
|
if(!Controller::isUserSystemEnabled()) {
|
|
|
|
$validations['permission'] = 'any';
|
|
|
|
$validations['requestData']['captcha'] = [
|
|
|
|
'validation' => DataValidator::captcha(),
|
|
|
|
'error' => ERRORS::INVALID_CAPTCHA
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $validations;
|
2016-07-04 20:57:00 +02:00
|
|
|
}
|
2016-06-17 22:44:04 +02:00
|
|
|
|
2016-07-04 20:57:00 +02:00
|
|
|
public function handler() {
|
2016-06-17 22:44:04 +02:00
|
|
|
$this->title = Controller::request('title');
|
|
|
|
$this->content = Controller::request('content');
|
|
|
|
$this->departmentId = Controller::request('departmentId');
|
|
|
|
$this->language = Controller::request('language');
|
2017-01-16 20:07:53 +01:00
|
|
|
$this->email = Controller::request('email');
|
|
|
|
$this->name = Controller::request('name');
|
2016-09-04 02:46:08 +02:00
|
|
|
|
|
|
|
$this->storeTicket();
|
|
|
|
|
2017-01-05 20:26:43 +01:00
|
|
|
Log::createLog('CREATE_TICKET', $this->ticketNumber);
|
2016-09-04 02:46:08 +02:00
|
|
|
Response::respondSuccess([
|
|
|
|
'ticketNumber' => $this->ticketNumber
|
|
|
|
]);
|
2016-06-17 22:44:04 +02:00
|
|
|
}
|
|
|
|
|
2016-08-04 06:37:23 +02:00
|
|
|
private function storeTicket() {
|
2016-08-04 05:59:04 +02:00
|
|
|
$department = Department::getDataStore($this->departmentId);
|
|
|
|
$author = Controller::getLoggedUser();
|
|
|
|
|
2017-01-16 22:04:26 +01:00
|
|
|
$fileUploader = $this->uploadFile();
|
|
|
|
|
2016-06-17 22:16:10 +02:00
|
|
|
$ticket = new Ticket();
|
|
|
|
$ticket->setProperties(array(
|
2016-06-17 22:44:04 +02:00
|
|
|
'title' => $this->title,
|
|
|
|
'content' => $this->content,
|
|
|
|
'language' => $this->language,
|
2016-08-04 05:59:04 +02:00
|
|
|
'author' => $author,
|
|
|
|
'department' => $department,
|
2017-01-16 22:04:26 +01:00
|
|
|
'file' => ($fileUploader instanceof FileUploader) ? $fileUploader->getFileName() : null,
|
2016-08-01 05:34:55 +02:00
|
|
|
'date' => Date::getCurrentDate(),
|
2016-06-17 22:16:10 +02:00
|
|
|
'unread' => false,
|
2016-10-21 21:12:17 +02:00
|
|
|
'unreadStaff' => true,
|
2016-08-04 05:59:04 +02:00
|
|
|
'closed' => false,
|
2017-01-16 20:07:53 +01:00
|
|
|
'authorName' => $this->name,
|
|
|
|
'authorEmail' => $this->email
|
2016-06-17 22:16:10 +02:00
|
|
|
));
|
2016-08-04 05:59:04 +02:00
|
|
|
|
2017-01-16 20:07:53 +01:00
|
|
|
if(Controller::isUserSystemEnabled()) {
|
|
|
|
$author->sharedTicketList->add($ticket);
|
|
|
|
$author->tickets++;
|
|
|
|
|
|
|
|
$author->store();
|
|
|
|
}
|
2016-11-30 07:33:46 +01:00
|
|
|
|
2016-06-17 22:16:10 +02:00
|
|
|
$ticket->store();
|
2016-09-04 02:46:08 +02:00
|
|
|
|
|
|
|
$this->ticketNumber = $ticket->ticketNumber;
|
2016-06-17 21:28:38 +02:00
|
|
|
}
|
2016-07-21 01:39:36 +02:00
|
|
|
}
|