diff --git a/Admin/Routes/Web/Api.php b/Admin/Routes/Web/Api.php new file mode 100644 index 0000000..ca73176 --- /dev/null +++ b/Admin/Routes/Web/Api.php @@ -0,0 +1,12 @@ + [ + [ + 'dest' => '\Modules\Draw\Controller:apiDrawCreate', + 'verb' => RouteVerb::SET, + ], + ], +]; diff --git a/Controller.js b/Controller.js index 0c85129..9b750ea 100644 --- a/Controller.js +++ b/Controller.js @@ -1,5 +1,8 @@ (function (jsOMS) { "use strict"; + + /** @namespace jsOMS.Uri.UriFactory */ + jsOMS.Autoloader.defineNamespace('jsOMS.Modules.Draw'); jsOMS.Modules.Draw = function (app) { this.app = app; diff --git a/Controller.php b/Controller.php index e0e8678..515fe54 100644 --- a/Controller.php +++ b/Controller.php @@ -92,7 +92,9 @@ class Controller extends ModuleAbstract implements WebInterface { /** @var Head $head */ $head = $response->get('Content')->getData('head'); - $head->addAsset(AssetType::JS, $request->getUri()->getBase() . 'Modules/Draw/ModuleDraw.js'); + $head->addAsset(AssetType::JSLATE, $request->getUri()->getBase() . 'Modules/Draw/Controller.js'); + $head->addAsset(AssetType::JSLATE, $request->getUri()->getBase() . 'Modules/Draw/Models/DrawType.enum.js'); + $head->addAsset(AssetType::JSLATE, $request->getUri()->getBase() . 'Modules/Draw/Models/Editor.js'); } /** @@ -133,4 +135,77 @@ class Controller extends ModuleAbstract implements WebInterface return $view; } + private function validateDrawCreate(RequestAbstract $request) : array + { + $val = []; + if ( + ($val['title'] = empty($request->getData('title'))) + || ($val['image'] = empty($request->getData('plain'))) + || ($val['status'] = ( + $request->getData('status') === null + || !NewsStatus::isValidValue((int) $request->getData('status')) + )) + ) { + return $val; + } + + return []; + } + + /** + * @param RequestAbstract $request Request + * @param ResponseAbstract $response Response + * @param mixed $data Generic data + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function apiDrawCreate(RequestAbstract $request, ResponseAbstract $response, $data = null) + { + if (!empty($val = $this->validateDrawCreate($request))) { + $response->set('draw_create', new FormValidation($val)); + + return; + } + + $path = MediaController::createMediaPath(); + $extension = 'png'; + $filename = ''; + $rnd = ''; + + // todo: implement limit since this could get exploited + do { + $filename = sha1($request->getData('image') . $rnd); + $filename .= '.' . $extension; + + $rnd = mt_rand(); + } while (file_exists($path . '/' . $filename)); + + $fullPath = $path . '/' . $filename; + + $this->createLocalFile($fullPath, $request->getData('image')); + + $status = [ + 'path' => $path, + 'filename' => $filename, + 'size' => File::size($fullPath), + 'extension' => $extension, + ]; + + $media = MediaController::createDbEntries($status, $request->getAccount()); + $draw = Draw::fromMedia(end($media)); + + DrawMapper::create($draw); + + $response->set('image', $draw->jsonSerialize()); + } + + private function createLocalFile(string $outputPath, string $raw) : bool + { + $imageData = ImageUtils::decodeBase64Image($raw); + File::put($outputPath, $imageData); + + return true; + } + } diff --git a/Models/DrawType.enum.js b/Models/DrawType.enum.js index 5b3b4e4..976d28d 100644 --- a/Models/DrawType.enum.js +++ b/Models/DrawType.enum.js @@ -10,6 +10,9 @@ (function (jsOMS) { "use strict"; + + /** @namespace jsOMS.Uri.UriFactory */ + jsOMS.Autoloader.defineNamespace('jsOMS.Modules.Draw'); jsOMS.Modules.Draw.DrawTypeEnum = Object.freeze({ DRAW: 0, diff --git a/Models/Editor.js b/Models/Editor.js index fb5e8f9..ef555a2 100644 --- a/Models/Editor.js +++ b/Models/Editor.js @@ -1,6 +1,9 @@ (function (jsOMS) { "use strict"; + + /** @namespace jsOMS.Uri.UriFactory */ + jsOMS.Autoloader.defineNamespace('jsOMS.Modules.Draw'); jsOMS.Modules.Draw.Editor = function (editor) { diff --git a/Theme/Backend/draw-create.tpl.php b/Theme/Backend/draw-create.tpl.php index 86ae76f..49fc5c8 100644 --- a/Theme/Backend/draw-create.tpl.php +++ b/Theme/Backend/draw-create.tpl.php @@ -21,8 +21,8 @@ echo $this->getData('nav')->render(); ?>
-
- + +
@@ -63,7 +63,7 @@ echo $this->getData('nav')->render(); ?>
- +
diff --git a/Theme/backend/draw-create.tpl.php b/Theme/backend/draw-create.tpl.php index 86ae76f..49fc5c8 100644 --- a/Theme/backend/draw-create.tpl.php +++ b/Theme/backend/draw-create.tpl.php @@ -21,8 +21,8 @@ echo $this->getData('nav')->render(); ?>
-
- + +
@@ -63,7 +63,7 @@ echo $this->getData('nav')->render(); ?>
- +