From 84b33ea871af56c7339c85049232f512121e55f6 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 3 Oct 2016 16:44:41 -0300 Subject: [PATCH] Ivan - Add CustomResponse model and paths [skip ci] --- server/controllers/system/init-settings.php | 4 +- server/controllers/ticket.php | 6 +++ .../ticket/add-custom-response.php | 39 +++++++++++++++++++ .../ticket/edit-custom-response.php | 39 +++++++++++++++++++ .../ticket/get-custom-responses.php | 20 ++++++++++ server/data/ERRORS.php | 1 + server/index.php | 1 + server/libs/DataStoreList.php | 11 ++++++ server/libs/validations/dataStoreId.php | 6 ++- server/libs/validations/validLanguage.php | 19 +++++++++ server/models/CustomResponse.php | 21 ++++++++++ server/models/DataStore.php | 11 ++++++ server/models/Department.php | 19 ++------- 13 files changed, 179 insertions(+), 18 deletions(-) create mode 100644 server/controllers/ticket/add-custom-response.php create mode 100644 server/controllers/ticket/edit-custom-response.php create mode 100644 server/controllers/ticket/get-custom-responses.php create mode 100644 server/libs/validations/validLanguage.php create mode 100644 server/models/CustomResponse.php diff --git a/server/controllers/system/init-settings.php b/server/controllers/system/init-settings.php index e4d81831..8b6050de 100644 --- a/server/controllers/system/init-settings.php +++ b/server/controllers/system/init-settings.php @@ -89,8 +89,8 @@ class InitSettingsController extends Controller { 'email' => 'staff@opensupports.com', 'password' => Hashing::hashPassword('staff'), 'profilePic' => 'http://i65.tinypic.com/9bep95.jpg', - 'level' => 1, - 'sharedDepartmentList' => Department::getAllDepartments(), + 'level' => 3, + 'sharedDepartmentList' => Department::getAll(), 'sharedTicketList' => [] ]); $staff->store(); diff --git a/server/controllers/ticket.php b/server/controllers/ticket.php index 726ed42c..5a93e7bc 100644 --- a/server/controllers/ticket.php +++ b/server/controllers/ticket.php @@ -2,6 +2,9 @@ include 'ticket/create.php'; include 'ticket/comment.php'; include 'ticket/get.php'; +include 'ticket/add-custom-response.php'; +include 'ticket/edit-custom-response.php'; +include 'ticket/get-custom-responses.php'; $ticketControllers = new ControllerGroup(); $ticketControllers->setGroupPath('/ticket'); @@ -9,5 +12,8 @@ $ticketControllers->setGroupPath('/ticket'); $ticketControllers->addController(new CreateController); $ticketControllers->addController(new CommentController); $ticketControllers->addController(new TicketGetController); +$ticketControllers->addController(new AddCustomResponseController); +$ticketControllers->addController(new EditCustomResponseController); +$ticketControllers->addController(new GetCustomResponsesController); $ticketControllers->finalize(); \ No newline at end of file diff --git a/server/controllers/ticket/add-custom-response.php b/server/controllers/ticket/add-custom-response.php new file mode 100644 index 00000000..4ba7b3fe --- /dev/null +++ b/server/controllers/ticket/add-custom-response.php @@ -0,0 +1,39 @@ + 'staff_2', + 'requestData' => [ + 'name' => [ + 'validation' => DataValidator::length(5, 100), + 'error' => ERRORS::INVALID_NAME + ], + 'content' => [ + 'validation' => DataValidator::length(20, 500), + 'error' => ERRORS::INVALID_CONTENT + ], + 'language' => [ + 'validation' => DataValidator::validLanguage(), + 'error' => ERRORS::INVALID_LANGUAGE + ] + ] + ]; + } + + public function handler() { + $customResponse = new CustomResponse(); + $customResponse->setProperties([ + 'name' => Controller::request('name'), + 'content' => Controller::request('content'), + 'language' => Controller::request('language') + ]); + $customResponse->store(); + + Response::respondSuccess(); + } +} \ No newline at end of file diff --git a/server/controllers/ticket/edit-custom-response.php b/server/controllers/ticket/edit-custom-response.php new file mode 100644 index 00000000..c030f2de --- /dev/null +++ b/server/controllers/ticket/edit-custom-response.php @@ -0,0 +1,39 @@ + 'staff_2', + 'requestData' => [ + 'id' => [ + 'validation' => DataValidator::dataStoreId('customresponse'), + 'error' => ERRORS::INVALID_NAME + ] + ] + ]; + } + + public function handler() { + $customResponse = CustomResponse::getDataStore(Controller::request('id')); + + if (Controller::request('content')) { + $customResponse->content = Controller::request('content'); + } + + if (Controller::request('language')) { + $customResponse->language = Controller::request('language'); + } + + if (Controller::request('name')) { + $customResponse->name = Controller::request('name'); + } + + $customResponse->store(); + + Response::respondSuccess(); + } +} \ No newline at end of file diff --git a/server/controllers/ticket/get-custom-responses.php b/server/controllers/ticket/get-custom-responses.php new file mode 100644 index 00000000..7c2872cf --- /dev/null +++ b/server/controllers/ticket/get-custom-responses.php @@ -0,0 +1,20 @@ + 'staff_1', + 'requestData' => [] + ]; + } + + public function handler() { + $customResponsesList = CustomResponse::getAll(); + + Response::respondSuccess($customResponsesList->toArray()); + } +} \ No newline at end of file diff --git a/server/data/ERRORS.php b/server/data/ERRORS.php index d173ccfa..e5b33417 100644 --- a/server/data/ERRORS.php +++ b/server/data/ERRORS.php @@ -16,4 +16,5 @@ class ERRORS { const INVALID_OLD_PASSWORD = 'Invalid old password'; const INVALID_CAPTCHA = 'Invalid captcha'; const INVALID_TICKET_EVENT = 'INVALID_TICKET_EVENT'; + const INVALID_LANGUAGE = 'INVALID_LANGUAGE'; } diff --git a/server/index.php b/server/index.php index db77a0df..a9056e1e 100644 --- a/server/index.php +++ b/server/index.php @@ -41,6 +41,7 @@ spl_autoload_register(function ($class) { include_once 'libs/validations/dataStoreId.php'; include_once 'libs/validations/userEmail.php'; include_once 'libs/validations/captcha.php'; +include_once 'libs/validations/validLanguage.php'; include_once 'libs/validations/validTicketNumber.php'; // LOAD CONTROLLERS diff --git a/server/libs/DataStoreList.php b/server/libs/DataStoreList.php index c14eed97..ad209098 100644 --- a/server/libs/DataStoreList.php +++ b/server/libs/DataStoreList.php @@ -38,6 +38,17 @@ class DataStoreList implements IteratorAggregate { return $beanList; } + + public function toArray() { + $array = []; + + foreach($this->list as $item) { + $item->updateBeanProperties(); + $array[] = $item->toArray(); + } + + return $array; + } private function getIndexInListOf($dataStore) { foreach ($this->list as $itemIdInList => $item) { diff --git a/server/libs/validations/dataStoreId.php b/server/libs/validations/dataStoreId.php index 4bd16725..182d6774 100644 --- a/server/libs/validations/dataStoreId.php +++ b/server/libs/validations/dataStoreId.php @@ -28,6 +28,9 @@ class DataStoreId extends AbstractRule { case 'department': $dataStore = \Department::getDataStore($dataStoreId); break; + case 'customresponse': + $dataStore = \CustomResponse::getDataStore($dataStoreId); + break; } return !$dataStore->isNull(); @@ -37,7 +40,8 @@ class DataStoreId extends AbstractRule { return in_array($dataStoreName, [ 'user', 'ticket', - 'department' + 'department', + 'customresponse' ]); } } \ No newline at end of file diff --git a/server/libs/validations/validLanguage.php b/server/libs/validations/validLanguage.php new file mode 100644 index 00000000..3249606e --- /dev/null +++ b/server/libs/validations/validLanguage.php @@ -0,0 +1,19 @@ +languages); + } +} \ No newline at end of file diff --git a/server/models/CustomResponse.php b/server/models/CustomResponse.php new file mode 100644 index 00000000..5bcd8731 --- /dev/null +++ b/server/models/CustomResponse.php @@ -0,0 +1,21 @@ + $this->name, + 'language' => $this->language, + 'content' => $this->content, + ]; + } +} \ No newline at end of file diff --git a/server/models/DataStore.php b/server/models/DataStore.php index c8296b12..614c9b5e 100644 --- a/server/models/DataStore.php +++ b/server/models/DataStore.php @@ -20,6 +20,17 @@ abstract class DataStore { return RedBean::count(static::TABLE); } + public static function getAll() { + $beanList = RedBean::findAll(static::TABLE); + $dataStoreList = new DataStoreList(); + + foreach($beanList as $bean) { + $dataStoreList->add(new static($bean)); + } + + return $dataStoreList; + } + private static function validateProp($propToValidate) { $validProp = false; diff --git a/server/models/Department.php b/server/models/Department.php index f2c93407..10628b65 100644 --- a/server/models/Department.php +++ b/server/models/Department.php @@ -12,24 +12,13 @@ class Department extends DataStore { } public static function getDepartmentNames() { - $departmentsQuantity = RedBean::count(Department::TABLE); + $departmentsList = RedBean::findAll(Department::TABLE); $departmentsNameList = []; - - for ($departmentIndex = 1; $departmentIndex <= $departmentsQuantity; ++$departmentIndex) { - $departmentsNameList[] = Department::getDataStore($departmentIndex)->name; + + foreach($departmentsList as $department) { + $departmentsNameList[] = $department->name; } return $departmentsNameList; } - - public static function getAllDepartments() { - $departmentsQuantity = RedBean::count(Department::TABLE); - $departmentList = new DataStoreList(); - - for ($departmentIndex = 1; $departmentIndex <= $departmentsQuantity; ++$departmentIndex) { - $departmentList->add(Department::getDataStore($departmentIndex)); - } - - return $departmentList; - } } \ No newline at end of file