diff --git a/ActiveForm.php b/ActiveForm.php new file mode 100755 index 0000000..da82c3a --- /dev/null +++ b/ActiveForm.php @@ -0,0 +1,107 @@ +"; + } + + /** + * @return void + */ + public function endForm(): void + { + echo ""; + + } + + public function field($class, string $name, array $params = []) + { + if ($class === Select::class){ + $this->fieldObject = SelectBuilder::build($name, $params); + } + elseif ($class === TextInput::class){ + $this->fieldObject = TextInputBuilder::build($name, $params); + } + elseif ($class === TextArea::class){ + $this->fieldObject = TextAreaBuilder::build($name, $params); + } + elseif ($class === Checkbox::class) { + $this->fieldObject = CheckBoxBuilder::build($name, $params); + } + elseif ($class === Button::class) { + $this->fieldObject = ButtonBuilder::build($name, $params); + } + elseif ($class === RadioButton::class){ + $this->fieldObject = RadioButtonBuilder::build($name, $params); + } + elseif ($class === Hidden::class){ + $this->fieldObject = HiddenBuilder::build($name, $params); + } + else { + $this->fieldObject = new $class($name, $params); + } + + return $this; + } + + public function setLabel(string $title): self + { + $this->fieldObject->setLabel($title); + + return $this; + } + + public function setOptions(array $options): self + { + $this->fieldObject->setOptions($options); + + return $this; + } + + public function setTemplate($template): self + { + $this->fieldObject->setTemplate($template); + + return $this; + } + + public function render() + { + $this->fieldObject->create(); + $this->fieldObject->render(); + } + + public function fetch() + { + $this->fieldObject->create(); + return $this->fieldObject->fetch(); + } + +} \ No newline at end of file diff --git a/Form.php b/Form.php new file mode 100755 index 0000000..6c732a3 --- /dev/null +++ b/Form.php @@ -0,0 +1,110 @@ +"; + } + + /** + * @param string $name + * @param array $paramsArray + * @return void + */ + + public function textInput(string $name, array $paramsArray = []): void + { + TextInput::build($name, $paramsArray); + } + + /** + * @param string $name + * @param string $value + * @param array $paramsArray + * @return void + */ + public function checkBox(string $name, string $value, array $paramsArray = []): void + { + Checkbox::build($name, $value, $paramsArray); + + } + + /** + * @param string $title + * @param array $paramsArray + * @return void + */ + public function label(string $title, array $paramsArray = []): void + { + Label::build($title, $paramsArray); + } + + /** + * @param string $name + * @param array $paramsArray + * @return void + */ + public function radio(string $name, array $paramsArray = []): void + { + RadioButton::build($name, $paramsArray); + } + + /** + * @param string $name + * @param string $value + * @param array $paramsArray + * @return void + */ + public function textarea(string $name, string $value = "", array $paramsArray = []): void + { + TextArea::build($name, $value, $paramsArray); + } + + /** + * @param string $name + * @param array $options + * @param $value + * @param array $paramsArray + * @return void + */ + public function select(string $name, array $options = [], $value = null, array $paramsArray = []): void + { + Select::build($name, $options, $value, $paramsArray); + + } + + /** + * @param string $name + * @param string $value + * @param array $paramsArray + * @return void + */ + public function button(string $name, string $value, array $paramsArray = []): void + { + Button::build($name, $value, $paramsArray); + } + + /** + * @return void + */ + public function endForm(): void + { + echo ""; + + } +} diff --git a/JsonForm.php b/JsonForm.php new file mode 100755 index 0000000..6ceb9a0 --- /dev/null +++ b/JsonForm.php @@ -0,0 +1,86 @@ +jsonData = json_decode($json, true); + + + } + + public function beginForm($params = []): string + { + $paramsString = $this->createParams($params); + return "
"; + } + + public function endForm(): string + { + return "
"; + + } + + public function convertHTML(): void + { + $this->html .= $this->beginForm($this->jsonData['meta']); + + foreach ($this->jsonData['data'] as $item) { + + if (isset($item["type"]) and isset($item["name"])) { + + /** + * @var $builder Builder + */ + $builder = JsonInputMapper::getBuilder($item["type"]); + unset($item["type"]); + $name = $item["name"]; + unset($item["name"]); + $label = ''; + if (isset($item['label'])) { + $label = $this->createLabel($item['label']); + } + unset($item['label']); + $input = $builder::build($name, $item); + $input->setLabel($label)->create(); + $this->html .= $input->fetch(); + } + } + + $this->html .= $this->endForm(); + //return $this->fetch($html); + } + + private function createLabel($labelParams) + { + $title = ''; + if(isset($labelParams['title'])){ + $title = $labelParams['title']; + unset($labelParams['title']); + } + return LabelBuilder::build($title, $labelParams); + } + + public function render(): void + { + echo $this->html; + } + + public function fetch(): string + { + return $this->html; + } + +} diff --git a/builders/Builder.php b/builders/Builder.php new file mode 100755 index 0000000..07c3b44 --- /dev/null +++ b/builders/Builder.php @@ -0,0 +1,12 @@ +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/core/cg_view/CgView.php b/core/cg_view/CgView.php new file mode 100644 index 0000000..04d1e9f --- /dev/null +++ b/core/cg_view/CgView.php @@ -0,0 +1,59 @@ +createContent($view, $data); + + echo $content; + } + + public function fetch(string $view, array $data = []): false|string + { + $content = $this->createContent($view, $data); + + return $content; + } + + private function createContent(string $view, array $data = []): false|string + { + ob_start(); + + $cgView = new self(); + $cgView->viewPath = $this->viewPath; + + foreach ($data as $key => $datum){ + ${"$key"} = $datum; + } + + include ($this->viewPath . $view); + + $content = ob_get_contents(); + ob_end_clean (); + + ob_start(); + $file_content = $content; + + if ($this->layout){ + if (file_exists($this->viewPath . $this->layout)){ + include ($this->viewPath . $this->layout); + $file_content = ob_get_contents(); + } + } + ob_end_clean (); + + return $file_content; + } + +} \ No newline at end of file diff --git a/exceptions/CreateFormException.php b/exceptions/CreateFormException.php new file mode 100644 index 0000000..2ca61e9 --- /dev/null +++ b/exceptions/CreateFormException.php @@ -0,0 +1,11 @@ +message = "Create form error: " . $msg; + } +} \ No newline at end of file diff --git a/exceptions/FieldTypeNotFound.php b/exceptions/FieldTypeNotFound.php new file mode 100644 index 0000000..1e0893d --- /dev/null +++ b/exceptions/FieldTypeNotFound.php @@ -0,0 +1,13 @@ +message = "Тип поля $type не найден"; + } + +} \ No newline at end of file diff --git a/exceptions/FormNotFoundException.php b/exceptions/FormNotFoundException.php new file mode 100644 index 0000000..593e9cd --- /dev/null +++ b/exceptions/FormNotFoundException.php @@ -0,0 +1,11 @@ + message = "Форма $formId не найдена"; + } +} \ No newline at end of file diff --git a/exceptions/FormResultNotFoundException.php b/exceptions/FormResultNotFoundException.php new file mode 100644 index 0000000..634f276 --- /dev/null +++ b/exceptions/FormResultNotFoundException.php @@ -0,0 +1,11 @@ + message = "Ответ формы $id не найдена"; + } +} \ No newline at end of file diff --git a/helpers/TranslitorHelper.php b/helpers/TranslitorHelper.php new file mode 100644 index 0000000..26f70c3 --- /dev/null +++ b/helpers/TranslitorHelper.php @@ -0,0 +1,22 @@ +html; + } + + public function fetch(): string + { + return $this->html; + } + + public abstract function create(); + + /** + * @param string|Label $title + * @return $this + */ + public function setLabel(string|Label $title): self + { + $this->hasLabel = true; + $this->label= $title; + + return $this; + } + + protected function createLabel() + { + if($this->hasLabel) { + if(is_string($this->label)){ + $this->labelString = ""; + } + else { + $this->labelString= $this->label->create()->fetch(); + } + } + } + + /** + * @param $template + * @return $this + */ + public function setTemplate($template): self + { + $this->inputTemplate = new $template(); + + return $this; + } + + +} \ No newline at end of file diff --git a/inputs/Button.php b/inputs/Button.php new file mode 100755 index 0000000..d28f9e3 --- /dev/null +++ b/inputs/Button.php @@ -0,0 +1,59 @@ +typeInput = $typeInput; + $this->name = $name; + $this->paramsArray = $paramsArray; + $this->value = $value; + $this->inputTemplate = new SimpleTemplate(); + } + + /** + * @return $this + */ + public function create(): self + { + $paramsString = $this->createParams($this->paramsArray); + $label = ""; + $button = ""; + + $this->createLabel(); + + $this->html = str_replace('{input}', $button, $this->inputTemplate->getInputTemplate()); + $this->html = str_replace('{label}', $this->labelString, $this->html); + + return $this; + } + + /** + * @param string $name + * @param string $value + * @param array $paramsArray + * @return void + */ + public static function build( string $name, string $value, string $typeInput, array $paramsArray = []): void + { + $button = new self($name, $value, $typeInput, $paramsArray); + $button->create()->render(); + } + +} \ No newline at end of file diff --git a/inputs/Checkbox.php b/inputs/Checkbox.php new file mode 100755 index 0000000..b721b22 --- /dev/null +++ b/inputs/Checkbox.php @@ -0,0 +1,60 @@ +name = $name; + $this->value = $value; + $this->paramsArray = $paramsArray; + $this->inputTemplate = new SimpleTemplate(); + } + + /** + * @return $this + */ + public function create(): self + { + $paramsString = $this->createParams($this->paramsArray); + $checkBox = ""; + $label = ""; + + $this->createLabel(); + + $this->html = str_replace('{input}', $checkBox, $this->inputTemplate->getInputTemplate()); + $this->html = str_replace('{label}', $this->labelString, $this->html); + + return $this; + } + + /** + * @param string $name + * @param string $value + * @param array $paramsArray + * @return void + */ + public static function build(string $name, string $value, array $paramsArray): void + { + $checkBox = new self($name, $value, $paramsArray); + $checkBox->create()->render(); + + } +} \ No newline at end of file diff --git a/inputs/Hidden.php b/inputs/Hidden.php new file mode 100644 index 0000000..66ba700 --- /dev/null +++ b/inputs/Hidden.php @@ -0,0 +1,60 @@ +name = $name; + $this->value = $value; + $this->paramsArray = $paramsArray; + $this->inputTemplate = new SimpleTemplate(); + } + + /** + * @return $this + */ + public function create(): self + { + $paramsString = $this->createParams($this->paramsArray); + $hidden = ""; + $label = ""; + + $this->createLabel(); + + $this->html = str_replace('{input}', $hidden, $this->inputTemplate->getInputTemplate()); + $this->html = str_replace('{label}', $this->labelString, $this->html); + + return $this; + } + + /** + * @param string $name + * @param string $value + * @param array $paramsArray + * @return void + */ + public static function build(string $name, string $value, array $paramsArray): void + { + $hidden = new self($name, $value, $paramsArray); + $hidden->create()->render(); + + } +} \ No newline at end of file diff --git a/inputs/Label.php b/inputs/Label.php new file mode 100755 index 0000000..4bb47b2 --- /dev/null +++ b/inputs/Label.php @@ -0,0 +1,58 @@ +title = $title; + $this->paramsArray = $paramsArray; + } + + /** + * @return $this + */ + public function create(): self + { + $paramsString = $this->createParams($this->paramsArray); + $this->html = ""; + + + return $this; + + } + + /** + * @return string + */ + public function getHtml(): string + { + return $this->html; + } + + /** + * @param string $title + * @param array $paramsArray + * @return void + */ + public static function build(string $title, array $paramsArray = []) + { + $label = new self($title, $paramsArray); + $label->create()->render(); + } + +} \ No newline at end of file diff --git a/inputs/RadioButton.php b/inputs/RadioButton.php new file mode 100755 index 0000000..a1a7e6d --- /dev/null +++ b/inputs/RadioButton.php @@ -0,0 +1,63 @@ +name = $name; + $this->paramsArray = $paramsArray; + $this->inputTemplate = new SimpleTemplate(); + } + + /** + * @return $this + */ + public function create(): self + { + $paramsString = $this->createParams($this->paramsArray); + $optionsString = $this->createOption($this->options, $this->value); + $radioButton = "$optionsString"; + $label = ""; + + $this->createLabel(); + + $this->html = str_replace('{input}', $radioButton, $this->inputTemplate->getInputTemplate()); + $this->html = str_replace('{label}', $this->labelString, $this->html); + return $this; + } + + /** + * @param string $name + * @param array $paramsArray + * @return void + */ + public static function build(string $name, array $paramsArray = []): void + { + $radioButton = new self($name, $paramsArray); + $radioButton->create()->render(); + } + + + /** + * @param $options + * @return $this + */ + public function setOptions($options) + { + $this->options = array_merge($options, $this->options); + return $this; + } +} \ No newline at end of file diff --git a/inputs/Select.php b/inputs/Select.php new file mode 100755 index 0000000..d4c3c11 --- /dev/null +++ b/inputs/Select.php @@ -0,0 +1,80 @@ +name = $name; + $this->options = $options; + $this->value = $value; + $this->paramsArray = $paramsArray; + $this->inputTemplate = new SimpleTemplate(); + } + + /** + * @return $this + */ + public function create(): self + { + $paramsString = $this->createParams($this->paramsArray); + $optionsString = $this->createOption($this->options, $this->value); + $label = ""; + $select = ""; + + $this->createLabel(); + + $this->html = str_replace('{input}', $select, $this->inputTemplate->getInputTemplate()); + $this->html = str_replace('{label}', $this->labelString, $this->html); + + return $this; + + } + + /** + * @param string $name + * @param array $options + * @param $value + * @param array $paramsArray + * @return void + */ + public static function build(string $name, array $options = [], $value = null, array $paramsArray = []): void + { + $select = new self($name, $options, $value, $paramsArray); + $select->create()->render(); + } + + /** + * @param $options + * @return $this + */ + public function setOptions($options) + { + $this->options = array_merge($options, $this->options); + + return $this; + } + + +} \ No newline at end of file diff --git a/inputs/TextArea.php b/inputs/TextArea.php new file mode 100755 index 0000000..65b6b3e --- /dev/null +++ b/inputs/TextArea.php @@ -0,0 +1,60 @@ +name = $name; + $this->value = $value; + $this->paramsArray = $paramsArray; + $this->inputTemplate = new SimpleTemplate(); + } + + /** + * @return $this + */ + public function create(): self + { + $paramsString = $this->createParams($this->paramsArray); + $label = ""; + $textarea = ""; + + $this->createLabel(); + + $this->html = str_replace('{input}', $textarea, $this->inputTemplate->getInputTemplate()); + $this->html = str_replace('{label}', $this->labelString, $this->html); + + return $this; + } + + + /** + * @param string $name + * @param string $value + * @param array $paramsArray + * @return void + */ + public static function build(string $name, string $value, array $paramsArray = []): void + { + $textarea = new self($name, $value, $paramsArray); + $textarea->create()->render(); + } + +} \ No newline at end of file diff --git a/inputs/TextInput.php b/inputs/TextInput.php new file mode 100755 index 0000000..5ba4072 --- /dev/null +++ b/inputs/TextInput.php @@ -0,0 +1,58 @@ +name = $name; + $this->paramsArray = $paramsArray; + $this->inputTemplate = new SimpleTemplate(); + } + + /** + * @return self + */ + public function create(): self + { + $paramsString = $this->createParams($this->paramsArray); + $label = ""; + $input = ""; + + $this->createLabel(); + + $this->html = str_replace('{input}', $input, $this->inputTemplate->getInputTemplate()); + $this->html = str_replace('{label}', $this->labelString, $this->html); + + return $this; + } + + /** + * @param string $name + * @param array $paramsArray + * @return void + */ + public static function build(string $name, array $paramsArray = []): void + { + $input = new self($name, $paramsArray); + $input->create()->render(); + + } + +} \ No newline at end of file diff --git a/mappers/JsonInputMapper.php b/mappers/JsonInputMapper.php new file mode 100755 index 0000000..b905b78 --- /dev/null +++ b/mappers/JsonInputMapper.php @@ -0,0 +1,36 @@ + TextInputBuilder::class, + "textArea" => TextAreaBuilder::class, + "radio" => RadioButtonBuilder::class, + "select" => SelectBuilder::class, + "button" => ButtonBuilder::class, + "checkbox" => CheckBoxBuilder::class, + "hidden" => HiddenBuilder::class, + ]; + } + + public static function getBuilder(string $type): string + { + if(isset(self::getBuilders()[$type])){ + return self::getBuilders()[$type]; + } + + return false; + } +} \ No newline at end of file diff --git a/migrations/FormsInputMigration.php b/migrations/FormsInputMigration.php new file mode 100644 index 0000000..53aeab8 --- /dev/null +++ b/migrations/FormsInputMigration.php @@ -0,0 +1,36 @@ +create("form_input", function (Blueprint $table) { + $table->id("id"); + $table->unsignedBigInteger('form_id'); + $table->unsignedBigInteger('input_type_id'); + $table->string('label'); + $table->string('name'); + $table->text('params'); + $table->integer(11)->default(1); + $table->timestamps(); + + $table->index('form_id'); + $table->index('input_type_id'); + $table->foreign('form_id', 'fk_form_input_form')->references('id')->on('form'); + $table->foreign('input_type_id', 'fk_form_input_input_type')->references('id')->on('input_type'); + }); + + } + + public static function down(): void + { + Illuminate\Database\Capsule\Manager::schema()->drop("form_input"); + } + +} \ No newline at end of file diff --git a/migrations/FormsMigration.php b/migrations/FormsMigration.php new file mode 100644 index 0000000..d13ef2c --- /dev/null +++ b/migrations/FormsMigration.php @@ -0,0 +1,32 @@ +create("form", function ($table) { + $table->id('id'); + $table->string('title'); + $table->integer('status'); + $table->text('params')->nullable(); + $table->timestamps(); + }); + + } + public static function get() + { + return DB::forms('form_input')-> get(); + } + + public static function down(): void + { + Illuminate\Database\Capsule\Manager::schema()->drop("form"); + } + +} \ No newline at end of file diff --git a/migrations/FormsResMigration.php b/migrations/FormsResMigration.php new file mode 100644 index 0000000..7421746 --- /dev/null +++ b/migrations/FormsResMigration.php @@ -0,0 +1,28 @@ +create("form_res", function ($table) { + $table->id('id'); + $table->unsignedBigInteger('form_id'); + $table->json('data'); + $table->timestamps(); + + $table->foreign('form_id', 'fk_form_form')->references('id')->on('form'); + }); + + } + + public static function down(): void + { + Illuminate\Database\Capsule\Manager::schema()->drop("form_res"); + } + +} \ No newline at end of file diff --git a/migrations/InputsTypeMigration.php b/migrations/InputsTypeMigration.php new file mode 100644 index 0000000..488e11f --- /dev/null +++ b/migrations/InputsTypeMigration.php @@ -0,0 +1,25 @@ +create("input_type", function ($table) { + $table->id('id'); + $table->string('type'); + $table->string('name'); + $table->integer('status'); + }); + + } + + public static function down(): void + { + Illuminate\Database\Capsule\Manager::schema()->drop("input_type"); + } + +} \ No newline at end of file diff --git a/migrations/InputsValueMigrations.php b/migrations/InputsValueMigrations.php new file mode 100644 index 0000000..c42fd2e --- /dev/null +++ b/migrations/InputsValueMigrations.php @@ -0,0 +1,28 @@ +create("input_value", function ($table) { + $table->id('id'); + $table->unsignedBigInteger('form_input_id'); + $table->string('value'); + $table->timestamps(); + + $table->foreign('form_input_id', 'fk_form_input')->references('id')->on('form_input'); + }); + + } + + public static function down(): void + { + Illuminate\Database\Capsule\Manager::schema()->drop("input_value"); + } + +} \ No newline at end of file diff --git a/migrations/UserMigration.php b/migrations/UserMigration.php new file mode 100755 index 0000000..e70820b --- /dev/null +++ b/migrations/UserMigration.php @@ -0,0 +1,30 @@ +create("user", function ($table) { + $table->increments('id'); + $table->string('name'); + $table->string('email')->unique(); + $table->string('password'); + $table->string('userimage')->nullable(); + $table->string('api_key')->nullable()->unique(); + $table->rememberToken(); + $table->timestamps(); + }); + + } + + public static function down(): void + { + Illuminate\Database\Capsule\Manager::schema()->drop("user"); + } + +} \ No newline at end of file diff --git a/templates/Simple/SimpleTemplate.php b/templates/Simple/SimpleTemplate.php new file mode 100755 index 0000000..b5fccd9 --- /dev/null +++ b/templates/Simple/SimpleTemplate.php @@ -0,0 +1,17 @@ +{label}
{input}"; + } +} \ No newline at end of file diff --git a/templates/Template.php b/templates/Template.php new file mode 100755 index 0000000..90cf2f8 --- /dev/null +++ b/templates/Template.php @@ -0,0 +1,11 @@ +{label}{input}"; + } + public static function getTextAreaTemplate(): string + { + return "
{label}{textarea}
"; + } + public static function getSelectTemplate(): string + { + return "
{label}{select}
"; + } + public static function getRadioTemplate(): string + { + return "
{label}{radio}
"; + } + public static function getCheckBoxTemplate(): string + { + return "
{label}{checkbox}
"; + } + public static function getButtonTemplate(): string + { + return "
{label}{button}
"; + } +} \ No newline at end of file diff --git a/traits/CreateOption.php b/traits/CreateOption.php new file mode 100755 index 0000000..23900ad --- /dev/null +++ b/traits/CreateOption.php @@ -0,0 +1,21 @@ + $title){ + $selected = (int)$value === $val ? "selected" : ""; + $optionsString .= ""; + } + return $optionsString; + } +} \ No newline at end of file diff --git a/traits/CreateParams.php b/traits/CreateParams.php new file mode 100755 index 0000000..ef27304 --- /dev/null +++ b/traits/CreateParams.php @@ -0,0 +1,24 @@ + $param){ + if(is_string($param)){ + $paramsString .= $key . "='" . $param . "'"; + } + } + + return $paramsString; + } + +} \ No newline at end of file diff --git a/widgets/BaseWidget.php b/widgets/BaseWidget.php new file mode 100644 index 0000000..2033aac --- /dev/null +++ b/widgets/BaseWidget.php @@ -0,0 +1,32 @@ +cgView = new CgView(); + $this->cgView->viewPath = VIEW_PATH; + } + + /** + * @return self + */ + public static function create(): BaseWidget + { + return new static(); + } + + /** + * @param array $data + * @return mixed + */ + abstract public function run(array $data = []); +} \ No newline at end of file diff --git a/widgets/FormFieldBlockWidget.php b/widgets/FormFieldBlockWidget.php new file mode 100644 index 0000000..d98c78c --- /dev/null +++ b/widgets/FormFieldBlockWidget.php @@ -0,0 +1,62 @@ +prepare($data['fields']) as $field) { + $this->cgView->render('/admin/field_type/' . $field['view_name'] . ".php", $field); + } + } + + private function prepare(Collection $rawFields): array + { + $fields = []; + + $i = 0; + foreach ($rawFields as $field) { + $params = json_decode($field['params'], true); + $viewName = \itguild\forms\app\models\InputTypeModel::getViewNameByTypeId($field['input_type_id']); + + if (!isset($fields[$field['name']])) { + $fields[$field['name']] = [ + 'name' => $field['name'], + 'form' => new ActiveForm(), + 'view_name' => $viewName, + 'count' => $i, + ]; + } + + if ($field['input_type_id'] == InputTypeModel::RADIO_TYPE) { + if (isset($fields[$field['name']]['value'])) { + $fields[$field['name']]['value'] .= "\n" . $params['value']; + } else { + $fields[$field['name']]['value'] = $params['value']; + } + + } elseif ($field['input_type_id'] == InputTypeModel::SELECT_TYPE) { + $fields[$field['name']]['value'] = implode("\n", InputValueService::getInputValuesById($field['id'])); + } else { + $fields[$field['name']]['value'] = $params['value'] ?? null; + $fields[$field['name']]['placeholder'] = $params['placeholder'] ?? null; + } + $i++; + } + + return $fields; + } + +} \ No newline at end of file