forms/widgets/FormFieldBlockWidget.php
2024-05-28 17:10:06 +03:00

62 lines
2.0 KiB
PHP

<?php
/**
* @var \itguild\forms\form\ActiveForm $form
* @var \itguild\forms\app\DTO\FormDTO $dto
* @var \itguild\forms\form\core\cg_view\CgView $cgView
*/
namespace itguild\forms\form\widgets;
use Illuminate\Database\Eloquent\Collection;
use itguild\forms\app\models\InputTypeModel;
use itguild\forms\app\services\InputValueService;
use itguild\forms\form\ActiveForm;
class FormFieldBlockWidget extends BaseWidget
{
public function run(array $data = []): void
{
foreach ($this->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;
}
}