Ivan - Add CustomResponse model and paths [skip ci]

This commit is contained in:
ivan 2016-10-03 16:44:41 -03:00
parent 8747326fca
commit 84b33ea871
13 changed files with 179 additions and 18 deletions

View File

@ -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();

View File

@ -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();

View File

@ -0,0 +1,39 @@
<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
class AddCustomResponseController extends Controller {
const PATH = '/add-custom-response';
public function validations() {
return [
'permission' => '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();
}
}

View File

@ -0,0 +1,39 @@
<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
class EditCustomResponseController extends Controller {
const PATH = '/edit-custom-response';
public function validations() {
return [
'permission' => '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();
}
}

View File

@ -0,0 +1,20 @@
<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
class GetCustomResponsesController extends Controller {
const PATH = '/get-custom-responses';
public function validations() {
return [
'permission' => 'staff_1',
'requestData' => []
];
}
public function handler() {
$customResponsesList = CustomResponse::getAll();
Response::respondSuccess($customResponsesList->toArray());
}
}

View File

@ -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';
}

View File

@ -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

View File

@ -39,6 +39,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) {
if ($item->id === $dataStore->id) {

View File

@ -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'
]);
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace CustomValidations;
use Respect\Validation\Rules\AbstractRule;
class ValidLanguage extends AbstractRule {
private $languages = [
'en',
'es',
'de',
'cn',
];
public function validate($ticketNumber) {
return in_array($ticketNumber, $this->languages);
}
}

View File

@ -0,0 +1,21 @@
<?php
class CustomResponse extends DataStore {
const TABLE = 'customresponse';
public static function getProps() {
return [
'name',
'language',
'content'
];
}
public function toArray() {
return [
'name' => $this->name,
'language' => $this->language,
'content' => $this->content,
];
}
}

View File

@ -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;

View File

@ -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;
}
}