You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
5.2 KiB

6 years ago
<?php
/**
* Created by Error202
* Date: 02.08.2018
*/
namespace common\modules\forms\widgets;
class FormViewField
{
public function text(array $settings): string
{
$options = [];
$options[] = isset($item['placeholder']) ? "'placeholder' => '{$settings['placeholder']}'" : "";
$options[] = isset($item['value']) ? "'value' => '{$settings['value']}'" : "";
$options[] = isset($item['maxlength']) ? "'maxlength' => true" : "";
$options = implode(',', array_filter($options));
$description = isset($item['description']) ? $settings['description'] : '';
$field = "<?= \$form->field(\$model, '{$settings['name']}')->textInput([{$options}])->hint('{$description}') ?>";
return $field;
}
public function textArea(array $settings): string
{
$options = [];
$options[] = isset($item['rows']) ? "'rows' => {$settings['rows']}" : "";
$options[] = isset($item['placeholder']) ? "'placeholder' => '{$settings['placeholder']}'" : "";
$options[] = isset($item['value']) ? "'value' => '{$settings['value']}'" : "";
$options = implode(',', array_filter($options));
$description = isset($item['description']) ? $settings['description'] : '';
$field = "<?= \$form->field(\$model, '{$settings['name']}')->textarea([{$options}])->hint('{$description}') ?>";
return $field;
}
public function header(array $settings): string
{
$field = "<{$settings['subtype']}>{$settings['label']}</{$settings['subtype']}>";
return $field;
}
public function paragraph(array $settings): string
{
$field = "<{$settings['subtype']}>{$settings['label']}</{$settings['subtype']}>";
return $field;
}
public function hidden(array $settings): string
{
$options = [];
$options[] = isset($item['value']) ? "'value' => '{$settings['value']}'" : "";
$options = implode(',', array_filter($options));
$field = "<?= \$form->field(\$model, '{$settings['name']}')->hiddenInput([{$options}])->label(false) ?>";
return $field;
}
public function radioGroup(array $settings): string
{
$values = [];
$selected = [];
foreach ($settings['values'] as $value) {
$values[] = "'{$value['value']}' => '{$value['label']}'";
if (isset($value['selected']) && $value['selected'] == true) {
$selected[] = "'{$value['value']}'";
}
}
$values = implode(',', $values);
$selected = implode(',', $selected);
if ($selected) {
$fields[] = "<?php \$model->{$settings['name']} = [{$selected}] ?>";
}
$options = [];
$options = implode(',', array_filter($options));
$description = isset($settings['description']) ? $settings['description'] : '';
$field = "<?= \$form->field(\$model, '{$settings['name']}')
->radioList([
{$values}
], [{$options}])->hint('{$description}'); ?>";
return $field;
}
public function checkboxGroup(array $settings): string
{
$values = [];
$selected = [];
foreach ($settings['values'] as $value) {
$values[] = "'{$value['value']}' => '{$value['label']}'";
if (isset($value['selected']) && $value['selected'] == true) {
$selected[] = "'{$value['value']}'";
}
}
$values = implode(',', $values);
$selected = implode(',', $selected);
if ($selected) {
$fields[] = "<?php \$model->{$settings['name']} = [{$selected}] ?>";
}
$options = [];
$options = implode(',', array_filter($options));
$description = isset($settings['description']) ? $settings['description'] : '';
$field = "<?= \$form->field(\$model, '{$settings['name']}')
->checkboxList([
{$values}
], [{$options}])->hint('{$description}'); ?>";
return $field;
}
public function select(array $settings): string
{
$values = [];
$selected = [];
foreach ($settings['values'] as $value) {
$values[] = "'{$value['value']}' => '{$value['label']}'";
if (isset($value['selected']) && $value['selected'] == true) {
$selected[] = "'{$value['value']}'";
}
}
$values = implode(',', $values);
$selected = implode(',', $selected);
$options = [];
$options[] = $selected ? "'value' => [{$selected}]" : "";
$options[] = isset($settings['placeholder']) ? "'prompt' => '{$settings['placeholder']}'" : "";
$options[] = isset($settings['multiple']) && $settings['multiple'] == true ? "'multiple' => 'multiple'" : "";
$options = implode(',', array_filter($options));
$description = isset($settings['description']) ? $settings['description'] : '';
$field = "<?= \$form->field(\$model, '{$settings['name']}')->dropDownList([{$values}], [{$options}])->hint('{$description}') ?>";
return $field;
}
public function button(array $settings): string
{
$style = isset($settings['style']) ? $settings['style'] : 'default';
if ($settings['subtype'] == 'submit') {
$field = "<div class=\"form-group\"><?= Html::submitButton('{$settings['label']}', ['class' => 'btn btn-{$style}']) ?></div>";
return $field;
}
elseif ($settings['subtype'] == 'button') {
$field = "<div class=\"form-group\"><?= Html::button('{$settings['label']}', ['class' => 'btn btn-{$style}']) ?></div>";
return $field;
}
elseif ($settings['subtype'] == 'reset') {
$field = "<div class=\"form-group\"><?= Html::resetButton('{$settings['label']}', ['class' => 'btn btn-{$style}']) ?></div>";
return $field;
}
return '';
}
}