From 62c349d3d326c17006ca3baeb4627fd36b558b48 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 5 Nov 2016 22:23:48 +0100 Subject: [PATCH] Create units --- Controller.php | 31 ++++++++++++++++++++----------- Models/Unit.php | 4 ++-- Theme/Backend/unit-create.tpl.php | 4 ++-- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/Controller.php b/Controller.php index 9301742..966b167 100644 --- a/Controller.php +++ b/Controller.php @@ -263,29 +263,38 @@ class Controller extends ModuleAbstract implements WebInterface return $view; } - public function apiUnitCreate(RequestAbstract $request, ResponseAbstract $response, $data = null) + private function validateUnitCreate(RequestAbstract $request) : array { $val = []; if ( - ($val['name'] = empty($request->getData('name'))) + ($val['name'] = empty($request->getData('name'))) || ($val['parent'] = ( - $request->getData('parent') !== null - && !is_numeric($request->getData('parent')) - )) - || ($val['status'] = ( - $request->getData('status') === null - || !Status::isValidValue((int) $request->getData('status')) - )) + !empty($request->getData('parent')) + && !is_numeric($request->getData('parent')) + )) + || ($val['status'] = ( + $request->getData('status') === null + || !Status::isValidValue((int) $request->getData('status')) + )) ) { - $response->set('unit_create_validation', new FormValidation($val)); + return $val; + } + + return []; + } + + public function apiUnitCreate(RequestAbstract $request, ResponseAbstract $response, $data = null) + { + if (!empty($val = $this->validateUnitCreate($request))) { + $response->set('unit_create', new FormValidation($val)); return; } $unit = new Unit(); $unit->setName($request->getData('name')); + $unit->setDescription($request->getData('description') ?? ''); $unit->setStatus((int) $request->getData('status')); - $unit->setDescription($request->getData('desc')); UnitMapper::create($unit); diff --git a/Models/Unit.php b/Models/Unit.php index 62ebeb2..a30fd33 100644 --- a/Models/Unit.php +++ b/Models/Unit.php @@ -23,7 +23,7 @@ class Unit implements ArrayableInterface, \JsonSerializable private $name = ''; - private $parent = 0; + private $parent = null; private $description = ''; @@ -44,7 +44,7 @@ class Unit implements ArrayableInterface, \JsonSerializable $this->name = $name; } - public function getParent() : int + public function getParent() { return $this->parent; } diff --git a/Theme/Backend/unit-create.tpl.php b/Theme/Backend/unit-create.tpl.php index 7f9af81..6a827f3 100644 --- a/Theme/Backend/unit-create.tpl.php +++ b/Theme/Backend/unit-create.tpl.php @@ -30,8 +30,8 @@ echo $this->getData('nav')->render(); ?>