From 143fa9f324734518f8bf7556b551f1e64e017192 Mon Sep 17 00:00:00 2001 From: kali Date: Thu, 2 May 2024 17:52:03 +0300 Subject: [PATCH] bd 10.0 --- .../14/14bb04555643336484373f73ae8830b9.php | 96 ++++++++ .../17/17b0eaaa5d26fb4fa6894d430c1c27da.php | 98 ++++++++ .../1c/1c1a65045d16ca598e290fed477b7831.php | 97 ++++++++ .../b6/b6a9b94df97efc8dc9336c36ada05909.php | 103 ++++++++ .../ea/ea1278337c261f39a1523ba336d5c786.php | 102 ++++++++ composer.json | 3 +- composer.lock | 48 +++- index.php | 1 + src/app/DTO/FormDTO.php | 21 ++ src/app/controllers/AdminController.php | 136 +++++++++-- src/app/controllers/FormController.php | 15 +- src/app/core/BaseController.php | 7 + src/app/core/CGDTO.php | 33 +++ src/app/models/InputTypeModel.php | 7 + src/app/requests/CreateFormRequest.php | 19 ++ src/app/services/InputTypeService.php | 15 ++ src/core/CgRequest.php | 226 ++++++++++++++++++ src/exceptions/CreateFormException.php | 11 + src/helpers/TranslitorHelper.php | 22 ++ src/table/ListJsonTable.php | 9 +- views/admin/field_type/_button.php | 1 + views/admin/field_type/_radioButton.php | 4 + views/admin/field_type/_select.php | 5 +- views/admin/field_type/_textarea.php | 2 +- views/admin/index.php | 40 +++- 25 files changed, 1083 insertions(+), 38 deletions(-) create mode 100644 compilation_cache/14/14bb04555643336484373f73ae8830b9.php create mode 100644 compilation_cache/17/17b0eaaa5d26fb4fa6894d430c1c27da.php create mode 100644 compilation_cache/1c/1c1a65045d16ca598e290fed477b7831.php create mode 100644 compilation_cache/b6/b6a9b94df97efc8dc9336c36ada05909.php create mode 100644 compilation_cache/ea/ea1278337c261f39a1523ba336d5c786.php create mode 100644 src/app/DTO/FormDTO.php create mode 100644 src/app/core/CGDTO.php create mode 100644 src/app/requests/CreateFormRequest.php create mode 100644 src/app/services/InputTypeService.php create mode 100644 src/core/CgRequest.php create mode 100644 src/exceptions/CreateFormException.php create mode 100644 src/helpers/TranslitorHelper.php diff --git a/compilation_cache/14/14bb04555643336484373f73ae8830b9.php b/compilation_cache/14/14bb04555643336484373f73ae8830b9.php new file mode 100644 index 0000000..d39c722 --- /dev/null +++ b/compilation_cache/14/14bb04555643336484373f73ae8830b9.php @@ -0,0 +1,96 @@ +source = $this->getSourceContext(); + + $this->blocks = [ + 'title' => [$this, 'block_title'], + 'content' => [$this, 'block_content'], + ]; + } + + protected function doGetParent(array $context) + { + // line 1 + return "layouts/simple.html.twig"; + } + + protected function doDisplay(array $context, array $blocks = []) + { + $macros = $this->macros; + $this->parent = $this->loadTemplate("layouts/simple.html.twig", "main/index.html.twig", 1); + $this->parent->display($context, array_merge($this->blocks, $blocks)); + } + + // line 3 + public function block_title($context, array $blocks = []) + { + $macros = $this->macros; + echo twig_escape_filter($this->env, ($context["title"] ?? null), "html", null, true); + } + + // line 5 + public function block_content($context, array $blocks = []) + { + $macros = $this->macros; + // line 6 + echo " "; + echo twig_escape_filter($this->env, $this->env->getFunction('create_form')->getCallable()(), "html", null, true); + echo " + "; + // line 7 + echo twig_escape_filter($this->env, ($context["content"] ?? null), "html", null, true); + echo " +"; + } + + /** + * @codeCoverageIgnore + */ + public function getTemplateName() + { + return "main/index.html.twig"; + } + + /** + * @codeCoverageIgnore + */ + public function isTraitable() + { + return false; + } + + /** + * @codeCoverageIgnore + */ + public function getDebugInfo() + { + return array ( 63 => 7, 58 => 6, 54 => 5, 47 => 3, 36 => 1,); + } + + public function getSourceContext() + { + return new Source("", "main/index.html.twig", "/home/kali/php/untitled/views/main/index.html.twig"); + } +} diff --git a/compilation_cache/17/17b0eaaa5d26fb4fa6894d430c1c27da.php b/compilation_cache/17/17b0eaaa5d26fb4fa6894d430c1c27da.php new file mode 100644 index 0000000..2ba31b1 --- /dev/null +++ b/compilation_cache/17/17b0eaaa5d26fb4fa6894d430c1c27da.php @@ -0,0 +1,98 @@ +source = $this->getSourceContext(); + + $this->blocks = [ + 'title' => [$this, 'block_title'], + 'content' => [$this, 'block_content'], + ]; + } + + protected function doGetParent(array $context) + { + // line 1 + return "layouts/simple.html.twig"; + } + + protected function doDisplay(array $context, array $blocks = []) + { + $macros = $this->macros; + $this->parent = $this->loadTemplate("layouts/simple.html.twig", "form/resultItem.html.twig", 1); + $this->parent->display($context, array_merge($this->blocks, $blocks)); + } + + // line 3 + public function block_title($context, array $blocks = []) + { + $macros = $this->macros; + echo twig_escape_filter($this->env, ($context["title"] ?? null), "html", null, true); + } + + // line 5 + public function block_content($context, array $blocks = []) + { + $macros = $this->macros; + // line 6 + echo "

"; + echo twig_escape_filter($this->env, ($context["title"] ?? null), "html", null, true); + echo "

+ + "; + // line 8 + echo twig_escape_filter($this->env, $this->env->getFunction('create_get_action')->getCallable()(), "html", null, true); + echo " + +"; + } + + /** + * @codeCoverageIgnore + */ + public function getTemplateName() + { + return "form/resultItem.html.twig"; + } + + /** + * @codeCoverageIgnore + */ + public function isTraitable() + { + return false; + } + + /** + * @codeCoverageIgnore + */ + public function getDebugInfo() + { + return array ( 64 => 8, 58 => 6, 54 => 5, 47 => 3, 36 => 1,); + } + + public function getSourceContext() + { + return new Source("", "form/resultItem.html.twig", "/home/kali/php/untitled/views/form/resultItem.html.twig"); + } +} diff --git a/compilation_cache/1c/1c1a65045d16ca598e290fed477b7831.php b/compilation_cache/1c/1c1a65045d16ca598e290fed477b7831.php new file mode 100644 index 0000000..141a7d7 --- /dev/null +++ b/compilation_cache/1c/1c1a65045d16ca598e290fed477b7831.php @@ -0,0 +1,97 @@ +source = $this->getSourceContext(); + + $this->blocks = [ + 'title' => [$this, 'block_title'], + 'content' => [$this, 'block_content'], + ]; + } + + protected function doGetParent(array $context) + { + // line 1 + return "layouts/simple.html.twig"; + } + + protected function doDisplay(array $context, array $blocks = []) + { + $macros = $this->macros; + $this->parent = $this->loadTemplate("layouts/simple.html.twig", "form/form.html.twig", 1); + $this->parent->display($context, array_merge($this->blocks, $blocks)); + } + + // line 3 + public function block_title($context, array $blocks = []) + { + $macros = $this->macros; + echo twig_escape_filter($this->env, ($context["title"] ?? null), "html", null, true); + } + + // line 5 + public function block_content($context, array $blocks = []) + { + $macros = $this->macros; + // line 6 + echo "

"; + echo twig_escape_filter($this->env, ($context["title"] ?? null), "html", null, true); + echo "

+"; + // line 7 + echo twig_escape_filter($this->env, $this->env->getFunction('create_form')->getCallable()(), "html", null, true); + echo " + +"; + } + + /** + * @codeCoverageIgnore + */ + public function getTemplateName() + { + return "form/form.html.twig"; + } + + /** + * @codeCoverageIgnore + */ + public function isTraitable() + { + return false; + } + + /** + * @codeCoverageIgnore + */ + public function getDebugInfo() + { + return array ( 63 => 7, 58 => 6, 54 => 5, 47 => 3, 36 => 1,); + } + + public function getSourceContext() + { + return new Source("", "form/form.html.twig", "/home/kali/php/untitled/views/form/form.html.twig"); + } +} diff --git a/compilation_cache/b6/b6a9b94df97efc8dc9336c36ada05909.php b/compilation_cache/b6/b6a9b94df97efc8dc9336c36ada05909.php new file mode 100644 index 0000000..69844b3 --- /dev/null +++ b/compilation_cache/b6/b6a9b94df97efc8dc9336c36ada05909.php @@ -0,0 +1,103 @@ +source = $this->getSourceContext(); + + $this->parent = false; + + $this->blocks = [ + 'title' => [$this, 'block_title'], + 'content' => [$this, 'block_content'], + ]; + } + + protected function doDisplay(array $context, array $blocks = []) + { + $macros = $this->macros; + // line 1 + echo " + + + + "; + // line 5 + $this->displayBlock('title', $context, $blocks); + echo " + + + + + +
+
+
+ "; + // line 15 + $this->displayBlock('content', $context, $blocks); + // line 16 + echo "
+ +
+ + +
+ + +"; + } + + // line 5 + public function block_title($context, array $blocks = []) + { + $macros = $this->macros; + } + + // line 15 + public function block_content($context, array $blocks = []) + { + $macros = $this->macros; + } + + /** + * @codeCoverageIgnore + */ + public function getTemplateName() + { + return "layouts/simple.html.twig"; + } + + /** + * @codeCoverageIgnore + */ + public function getDebugInfo() + { + return array ( 78 => 15, 72 => 5, 60 => 16, 58 => 15, 45 => 5, 39 => 1,); + } + + public function getSourceContext() + { + return new Source("", "layouts/simple.html.twig", "/home/kali/php/untitled/views/layouts/simple.html.twig"); + } +} diff --git a/compilation_cache/ea/ea1278337c261f39a1523ba336d5c786.php b/compilation_cache/ea/ea1278337c261f39a1523ba336d5c786.php new file mode 100644 index 0000000..3395aae --- /dev/null +++ b/compilation_cache/ea/ea1278337c261f39a1523ba336d5c786.php @@ -0,0 +1,102 @@ +source = $this->getSourceContext(); + + $this->blocks = [ + 'title' => [$this, 'block_title'], + 'content' => [$this, 'block_content'], + ]; + } + + protected function doGetParent(array $context) + { + // line 1 + return "layouts/simple.html.twig"; + } + + protected function doDisplay(array $context, array $blocks = []) + { + $macros = $this->macros; + $this->parent = $this->loadTemplate("layouts/simple.html.twig", "form/result.html.twig", 1); + $this->parent->display($context, array_merge($this->blocks, $blocks)); + } + + // line 3 + public function block_title($context, array $blocks = []) + { + $macros = $this->macros; + echo twig_escape_filter($this->env, ($context["title"] ?? null), "html", null, true); + } + + // line 5 + public function block_content($context, array $blocks = []) + { + $macros = $this->macros; + // line 6 + echo "

"; + echo twig_escape_filter($this->env, ($context["title"] ?? null), "html", null, true); + echo "

+ + "; + // line 8 + echo twig_escape_filter($this->env, $this->env->getFunction('create_table')->getCallable()(), "html", null, true); + echo " + "; + // line 9 + echo twig_escape_filter($this->env, $this->env->getFunction('create_pagination')->getCallable()(), "html", null, true); + echo " + +"; + } + + /** + * @codeCoverageIgnore + */ + public function getTemplateName() + { + return "form/result.html.twig"; + } + + /** + * @codeCoverageIgnore + */ + public function isTraitable() + { + return false; + } + + /** + * @codeCoverageIgnore + */ + public function getDebugInfo() + { + return array ( 68 => 9, 64 => 8, 58 => 6, 54 => 5, 47 => 3, 36 => 1,); + } + + public function getSourceContext() + { + return new Source("", "form/result.html.twig", "/home/kali/php/untitled/views/form/result.html.twig"); + } +} diff --git a/composer.json b/composer.json index 9e6bf03..3bb2d4f 100755 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "twig/twig": "^3.0", "illuminate/database": "^11.0", "vlucas/phpdotenv": "^5.6", - "illuminate/filesystem": "^11.1" + "illuminate/filesystem": "^11.1", + "rakit/validation": "^1.4" } } diff --git a/composer.lock b/composer.lock index 07559e0..31bd86b 100755 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8425ba8fa8c3256af1097d721de637ad", + "content-hash": "4e0167cc51d61bb5064d445b684aefae", "packages": [ { "name": "brick/math", @@ -1150,6 +1150,52 @@ }, "time": "2021-10-29T13:26:27+00:00" }, + { + "name": "rakit/validation", + "version": "v1.4.0", + "source": { + "type": "git", + "url": "https://github.com/rakit/validation.git", + "reference": "ff003a35cdf5030a5f2482299f4c93f344a35b29" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rakit/validation/zipball/ff003a35cdf5030a5f2482299f4c93f344a35b29", + "reference": "ff003a35cdf5030a5f2482299f4c93f344a35b29", + "shasum": "" + }, + "require": { + "ext-mbstring": "*", + "php": ">=7.0" + }, + "require-dev": { + "php-coveralls/php-coveralls": "^2.2", + "phpunit/phpunit": "^6.5", + "squizlabs/php_codesniffer": "^3" + }, + "type": "library", + "autoload": { + "psr-4": { + "Rakit\\Validation\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Muhammad Syifa", + "email": "emsifa@gmail.com" + } + ], + "description": "PHP Laravel like standalone validation library", + "support": { + "issues": "https://github.com/rakit/validation/issues", + "source": "https://github.com/rakit/validation/tree/v1.4.0" + }, + "time": "2020-08-27T05:07:01+00:00" + }, { "name": "symfony/clock", "version": "v7.0.5", diff --git a/index.php b/index.php index f9b9458..28ab764 100755 --- a/index.php +++ b/index.php @@ -34,6 +34,7 @@ $router->get("/admin/create-form", [\itguild\forms\app\controllers\AdminControll $router->post("/admin/save-form", [\itguild\forms\app\controllers\AdminController::class, "saveFormAction"]); $router->get("/admin/form-add-fields", [\itguild\forms\app\controllers\AdminController::class, "getFormAddFieldsAction"]); $router->get("/admin/set-value-select", [\itguild\forms\app\controllers\AdminController::class, "getSelectValue"]); +$router->get("/admin/edit-form/{id}", [\itguild\forms\app\controllers\AdminController::class, "editFormAction"]); $dispatcher = new Phroute\Phroute\Dispatcher($router->getData()); $response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); diff --git a/src/app/DTO/FormDTO.php b/src/app/DTO/FormDTO.php new file mode 100644 index 0000000..fb89d99 --- /dev/null +++ b/src/app/DTO/FormDTO.php @@ -0,0 +1,21 @@ +cgView->layout = "/layouts/admin_layout.php"; - - $this->cgView->render("/admin/index.php", ['title' => "Создать форму", 'form' => new ActiveForm()]); + $this->cgView->render("/admin/index.php", [ + 'title' => "Редактировать форму", + 'form' => new ActiveForm(), + 'dto' => new FormDTO(), + ]); } + + /** + * @throws CreateFormException + */ public function saveFormAction(): void { + $request = new CreateFormRequest(); + if (!$request->validate()) { - $title = $_POST["title"]; - $perPage = $_POST['perPage']; - $inputs = $_POST["InputForm"]; - $inputArray = []; - foreach ($inputs as $input){ - $form = FormInputModel::Create(['form_id' => 42, 'input_type_id' => $input['type'], 'label' => $input['name'], 'name' => 'adsads', 'params' => "adsasd"]); - Debug::prn($form); + throw new CreateFormException("Validation error"); + } + + $title = $request->post('title'); + $perPage = $request->post('perPage', 10); + $inputs = $request->post('InputForm', []); + + $actionView = $request->post("actionView"); + $actionEdit = $request->post("actionEdit"); + $actionDelete = $request->post("actionDelete"); + $actions = [$actionView, $actionEdit, $actionDelete]; + + + $resultArray = ['meta' => ['perpage' => $perPage, 'status' => "1", 'actions' => $actions], 'params' => ['class' => "form-control", 'id' => 'form']]; + + $form = FormModel::Create(['title' => $title, 'status' => FormModel::STATUS_ACTIVE, 'params' => json_encode($resultArray, true)]); + + foreach ($inputs as $input) { + $name = TranslitorHelper::translit($input['name']); + $params = [ + "name" => $name, + "class" => "form-control", + ]; + + if ($input['type'] == InputTypeModel::RADIO_TYPE) { + $radio = explode("\n", $input['radio']); + + foreach ($radio as $key) { + $nameRadio = TranslitorHelper::translit($key); + $paramsRadio = [ + "name" => $name, + "value" => $key, + 'label' => ['title' => $key, 'for' => $key, "class" => 'form-label'] + ]; + $formInput = FormInputModel::Create([ + 'form_id' => $form->id, + 'input_type_id' => $input['type'], + 'label' => $key, + 'name' => $name, + 'params' => json_encode($paramsRadio) + ]); + } + + + } else { + + $formInput = FormInputModel::Create([ + 'form_id' => $form->id, + 'input_type_id' => $input['type'], + 'label' => $name, + 'name' => $name, + 'params' => json_encode($params) + ]); } - $resultArray = ['meta' => ['perpage' => $perPage]]; + if ($input['type'] == InputTypeModel::SELECT_TYPE) { + $options = explode("\n", $input['options']); + foreach ($options as $option) { + InputValueModel::Create(['form_input_id' => $formInput->id, 'value' => $option]); + } + } - Debug::prn(json_encode($resultArray, true)); + } - //if (isset($title) and isset($params)) { - // $form = FormModel::Create(['title' => $title, 'status' => FormModel::STATUS_ACTIVE, 'params' => json_encode($resultArray, true)]); - // echo "Форма сохранена!"; - //}// + $this->redirect("/form-result/$form->id"); } + + public function getFormAddFieldsAction(): void { $this->cgView->render("/admin/_form_add_fields_ajax.php", ['form' => new ActiveForm()]); } + public function getSelectValue(): void { $selectValue = $_GET["value"]; @@ -73,13 +144,44 @@ class AdminController extends BaseController $viewName = $fields[$selectValue] ?? false; - if (!$viewName){ + if (!$viewName) { throw new FieldTypeNotFound($selectValue); } $this->cgView->render("/admin/field_type/$viewName.php", ['form' => new ActiveForm(), 'count' => $countInput]); } + public function editFormAction($id) + { + $form = FormModel::find($id)->toArray(); + $dto = new FormDTO(); + + $formArr = json_decode($form['params'], true); + $fillableRes = []; + $fillableRes['title'] = $form['title']; + foreach ($formArr['meta'] as $key => $value){ + if($key == 'perpage'){ + $fillableRes['perPage'] = (string)$value; + } + else if($key == "actions"){ + foreach ($value as $item){ + $fillableRes[$item] = $item; + } + } + } + $dto->load($fillableRes); + + if (!$form) { + throw new FormNotFoundException($id); + } + + $this->cgView->layout = "/layouts/admin_layout.php"; + $this->cgView->render("/admin/index.php", [ + 'title' => "Редактировать форму", + 'form' => new ActiveForm(), + 'dto' => $dto, + ]); + } diff --git a/src/app/controllers/FormController.php b/src/app/controllers/FormController.php index 1274c66..89298b2 100644 --- a/src/app/controllers/FormController.php +++ b/src/app/controllers/FormController.php @@ -5,6 +5,7 @@ namespace itguild\forms\app\controllers; use itguild\forms\app\core\BaseController; use itguild\forms\app\models\FormModel; use itguild\forms\app\models\FormResModel; +use itguild\forms\app\models\InputTypeModel; use itguild\forms\app\models\InputValueModel; use itguild\forms\debug\Debug; use itguild\forms\exceptions\FormNotFoundException; @@ -91,7 +92,7 @@ class FormController extends BaseController $fillable = $formParams["meta"]["fillable"] ?? null; $actions = $formParams["meta"]["actions"] ?? null; $offset = ($page - 1) * $perPage; - $countItems = FormResModel::where("status", 1)->count(); + $countItems = FormResModel::where("status", 1)->where("form_id", $form->id)->count(); $table = FormResModel::where("form_id", $id)->where("status", FormResModel::STATUS_ACTIVE)->limit(3)->offset($offset)->get(); foreach ($table as $key => $item) { @@ -306,24 +307,24 @@ class FormController extends BaseController $options[$value['id']] = $value['value']; } } - + //Debug::dd($field->params); if ($field->params) { $fieldArr = array_merge($fieldArr, json_decode($field->params, true)); } $fieldArr['name'] = $field->name ?? $fieldArr['name']; - if ($field->input_type_id === 4) { - if ($resArr[$fieldArr['name']] === $fieldArr['value']) { + if ($field->input_type_id === InputTypeModel::RADIO_TYPE) { + if (isset($resArr[$fieldArr['name']]) && $resArr[$fieldArr['name']] === $fieldArr['value']) { $fieldArr['checked'] = "true"; } } - if ($field->input_type_id === 1) { - if ($resArr[$fieldArr['name']] === $fieldArr['value']) { + if ($field->input_type_id === InputTypeModel::CHECKBOX_TYPE) { + if (isset($resArr[$fieldArr['name']]) && $resArr[$fieldArr['name']] === $fieldArr['value']) { $fieldArr['checked'] = "true"; } } - if ($formResult && $field->input_type_id !== 4) { + if ($formResult && $field->input_type_id !== InputTypeModel::RADIO_TYPE) { $fieldArr['value'] = $resArr[$fieldArr['name']] ?? null; } $fieldArr['type'] = $field->inputType['type'] ? $field->inputType['type'] : "textInput"; diff --git a/src/app/core/BaseController.php b/src/app/core/BaseController.php index fbd4f89..c149994 100755 --- a/src/app/core/BaseController.php +++ b/src/app/core/BaseController.php @@ -3,6 +3,7 @@ namespace itguild\forms\app\core; use itguild\forms\core\cg_view\CgView; +use JetBrains\PhpStorm\NoReturn; class BaseController { @@ -21,4 +22,10 @@ class BaseController $this->cgView->viewPath = VIEW_PATH; } + #[NoReturn] protected function redirect(string $url, int $code = 301): void + { + header('Location: ' . $url, true, $code); + exit; + } + } \ No newline at end of file diff --git a/src/app/core/CGDTO.php b/src/app/core/CGDTO.php new file mode 100644 index 0000000..bfc17e3 --- /dev/null +++ b/src/app/core/CGDTO.php @@ -0,0 +1,33 @@ +fillable as $item) { + if (isset($params[$item])) { + + $this->$item = $params[$item]; + } + } + } + + public function get(string $name) + { + if (!empty($this->$name)) + { + return $this->$name; + } + + return null; + } + +} \ No newline at end of file diff --git a/src/app/models/InputTypeModel.php b/src/app/models/InputTypeModel.php index a0a6f31..b08c786 100644 --- a/src/app/models/InputTypeModel.php +++ b/src/app/models/InputTypeModel.php @@ -6,6 +6,13 @@ use Illuminate\Database\Eloquent\Model; class InputTypeModel extends Model { + const SELECT_TYPE = 6; + const INPUT_TEXT_TYPE = 2; + const TEXT_AREA_TYPE = 3; + const BUTTON_TYPE = 5; + const CHECKBOX_TYPE = 1; + const RADIO_TYPE = 4; + protected $table = "input_type"; protected $fillable = [ diff --git a/src/app/requests/CreateFormRequest.php b/src/app/requests/CreateFormRequest.php new file mode 100644 index 0000000..597e982 --- /dev/null +++ b/src/app/requests/CreateFormRequest.php @@ -0,0 +1,19 @@ + 'required|min:5', + 'perPage' => 'numeric', + 'InputForm' => 'required|array', + ]; + } + +} \ No newline at end of file diff --git a/src/app/services/InputTypeService.php b/src/app/services/InputTypeService.php new file mode 100644 index 0000000..a092d09 --- /dev/null +++ b/src/app/services/InputTypeService.php @@ -0,0 +1,15 @@ +pluck('name', 'id')->toArray(); + } + +} \ No newline at end of file diff --git a/src/core/CgRequest.php b/src/core/CgRequest.php new file mode 100644 index 0000000..8b110e2 --- /dev/null +++ b/src/core/CgRequest.php @@ -0,0 +1,226 @@ +headers = $this->getRequestHeaders(); + $this->load(); + } + + + /** + * @return array + */ + public function rules() + { + return []; + } + + /** + * @return array + */ + public function messages() + { + return []; + } + + /** + * Возвращает абсолютный адрес сервера. + * @return string + */ + public function getHost(): string + { + if ($this->host !== null) { + return $this->host; + } + + $http = $this->getIsSecure() ? 'https' : 'http'; + + if ($this->headerExist('Host')) { + $this->host = $http . '://' . $this->getHeader('Host'); + } elseif (isset($_SERVER['SERVER_NAME'])) { + $this->host = $http . '://' . $_SERVER['SERVER_NAME']; + } + + return $this->host; + } + + + /** + * Возвращает true если шифрование https, иначе false. + * @return bool + */ + public function getIsSecure(): bool + { + if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)) { + return true; + } + + return false; + } + + + /** + * Проверяет был ли передан заголовок запроса. + * @return bool + */ + public function headerExist($header): bool + { + return isset($this->headers[$header]); + } + + + /** + * Возвращает заголовок запроса + * @param string $header Заголовок. + * @param mixed $defaultValue Значение если, параметр не передан. + * @return mixed|null + */ + public function getHeader($header, $defaultValue = null) + { + return $this->headers[$header] ?? $defaultValue; + } + + + /** + * Возвращает GET - параметр. + * @param string $param Параметр. + * @param mixed $defaultValue Значение если, параметр не передан. + * @return mixed + */ + public function get($param = null, $defaultValue = null) + { + if (is_null($param)) { + return $_GET; + } + + return $_GET[$param] ?? $defaultValue; + } + + + /** + * Возвращает POST - параметр. + * @param string $param Параметр. + * @param mixed $defaultValue Значение если, параметр не передан. + * @return mixed + */ + public function post($param = null, $defaultValue = null) + { + if (is_null($param)) { + return $_POST; + } + + return $_POST[$param] ?? $defaultValue; + } + + + /** + * Был ли POST - запрос. + * @return bool + */ + public function isPost(): bool + { + return ($_SERVER['REQUEST_METHOD'] === 'POST'); + } + + /** + * Был ли GET - запрос. + * @return bool + */ + public function isGet(): bool + { + return ($_SERVER['REQUEST_METHOD'] === 'GET'); + } + + /** + * Загружаем свойсва + */ + public function load(): void + { + if (!empty($_REQUEST)) { + foreach ($_REQUEST as $key => $item) { + $this->{$key} = $item; + $this->data[$key] = $item; + } + } + } + + /** + * @return bool + */ + public function validate() + { + if (!empty($this->data)) { + $valid = new Validator(); + $validation = $valid->make($this->data, $this->rules()); + $validation->setMessages($this->messages()); + $validation->validate(); + if ($validation->fails()) { + $this->errors = $validation->errors(); + return false; + } + } + + return true; + } + + /** + * @return array + */ + public function getMessagesArray() + { + $msgs = []; + if($this->errors){ + foreach ($this->errors->toArray() as $item){ + $msgs[] = array_values($item)[0]; + } + } + + return $msgs; + } + + /** + * @return array + */ + protected function getRequestHeaders() + { + $headers = array(); + foreach ($_SERVER as $key => $value) { + if (substr($key, 0, 5) <> 'HTTP_') { + continue; + } + $header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5))))); + $headers[$header] = $value; + } + + return $headers; + } +} diff --git a/src/exceptions/CreateFormException.php b/src/exceptions/CreateFormException.php new file mode 100644 index 0000000..6952c3d --- /dev/null +++ b/src/exceptions/CreateFormException.php @@ -0,0 +1,11 @@ +message = "Create form error: " . $msg; + } +} \ No newline at end of file diff --git a/src/helpers/TranslitorHelper.php b/src/helpers/TranslitorHelper.php new file mode 100644 index 0000000..dc495ad --- /dev/null +++ b/src/helpers/TranslitorHelper.php @@ -0,0 +1,22 @@ +data['meta']['fillable'])) { + if (isset($this->data['meta']['fillable'])){ + if (in_array($column, $this->data['meta']['fillable'])) { + return true; + } + } + else{ return true; } diff --git a/views/admin/field_type/_button.php b/views/admin/field_type/_button.php index 124fda4..67d86a1 100644 --- a/views/admin/field_type/_button.php +++ b/views/admin/field_type/_button.php @@ -3,6 +3,7 @@ /** * * @var \itguild\forms\ActiveForm $form + * @var integer $count * */ diff --git a/views/admin/field_type/_radioButton.php b/views/admin/field_type/_radioButton.php index 1469f24..d0007bd 100644 --- a/views/admin/field_type/_radioButton.php +++ b/views/admin/field_type/_radioButton.php @@ -3,6 +3,7 @@ /** * * @var \itguild\forms\ActiveForm $form + * @var integer $count * */ @@ -11,5 +12,8 @@ use itguild\forms\inputs\TextInput; $form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class' => "form-control", 'placeholder' => "Наименование"])->setLabel("Наименование (например \"Телефон\")")->render(); +$form->field(\itguild\forms\inputs\TextArea::class, name: "InputForm[$count][radio]", params: ['class' => "form-control"]) + ->setLabel("Введите радио кнопки, каждую на новой строке") + ->render(); $form->field(Checkbox::class, name: "InputForm[$count][required]", params:['value' => '1'])->setLabel("Обязательное")->render(); $form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '4'])->render(); \ No newline at end of file diff --git a/views/admin/field_type/_select.php b/views/admin/field_type/_select.php index b84d475..9ee4533 100644 --- a/views/admin/field_type/_select.php +++ b/views/admin/field_type/_select.php @@ -3,6 +3,7 @@ /** * * @var \itguild\forms\ActiveForm $form + * @var integer $count * */ @@ -12,9 +13,9 @@ use itguild\forms\inputs\TextInput; $form->field(TextInput::class, name: "InputForm[$count][name]", params: ['class' => "form-control", - 'placeholder' => "Наименование"])->setLabel("Наименование (например \"Телефон\")")->render(); + 'placeholder' => "Наименование"])->setLabel("Наименование (например \"Список городов\")")->render(); $form->field(TextArea::class, name: "InputForm[$count][options]", params: ['class' => "form-control"]) - ->setLabel("Опции выпадающего списка") + ->setLabel("Опции выпадающего списка, каждый с новой строки") ->render(); $form->field(Checkbox::class, name: "InputForm[$count][required]", params:['value' => '1'])->setLabel("Обязательное")->render(); $form->field(\itguild\forms\inputs\Hidden::class, name: "InputForm[$count][type]", params: ['value' => '6'])->render(); \ No newline at end of file diff --git a/views/admin/field_type/_textarea.php b/views/admin/field_type/_textarea.php index 3bb1cd8..bd21cc6 100644 --- a/views/admin/field_type/_textarea.php +++ b/views/admin/field_type/_textarea.php @@ -3,9 +3,9 @@ /** * * @var \itguild\forms\ActiveForm $form + * @var integer $count * */ - use itguild\forms\inputs\Checkbox; use itguild\forms\inputs\TextInput; diff --git a/views/admin/index.php b/views/admin/index.php index a59512d..f5a3add 100644 --- a/views/admin/index.php +++ b/views/admin/index.php @@ -1,41 +1,67 @@ pluck('name', 'id')->toArray(); ?> beginForm("/admin/save-form"); ?> field(TextInput::class, name: "title", params: [ 'class' => "form-control", - 'placeholder' => 'Название формы' + 'placeholder' => 'Название формы', + 'value' => $dto->get('title'), + ]) ->setLabel("Название формы") ->render(); $form->field(TextInput::class, name: "perPage", params: [ 'class' => "form-control", - 'placeholder' => 'Perpage' + 'placeholder' => 'Perpage', + 'value' => $dto->get('perPage'), ]) ->setLabel("Количество выводимых записей на страницу") ->render(); +$form->field(\itguild\forms\inputs\Checkbox::class, name: "actionView", params: [ + 'value' => 'view', + 'checked' => $dto->get('view') + +]) + ->setLabel("Разрешить просмотр") + ->render(); + +$form->field(\itguild\forms\inputs\Checkbox::class, name: "actionEdit", params: [ + 'value' => 'edit', + 'checked' => $dto->get('edit') +]) + ->setLabel("Разрешить редактирование") + ->render(); + +$form->field(\itguild\forms\inputs\Checkbox::class, name: "actionDelete", params: [ + 'value' => 'delete', + 'checked' => $dto->get('delete') +]) + ->setLabel("Разрешить удаление") + ->render(); + $form->field(\itguild\forms\inputs\Select::class, name: "inputType", params: [ 'class' => "form-control-sm", 'id' => "selectID", 'placeholder' => 'Параметры', - 'options' => $types + 'options' => \itguild\forms\app\services\InputTypeService::getInputTypes() ]) ->setLabel("Добавить поле") ->render(); ?> - - +