cms/app/Presenters/Admin/OptionFormFieldsPresenter.php

175 lines
9.5 KiB
PHP

<?php
namespace App\Presenters\Admin;
use App\Repositories\MediaFileRepository;
use Arr;
class OptionFormFieldsPresenter
{
public function render($page)
{
$options = config('admin.options.' . $page);
$presenter = app('Html');
$optionRepo = app('Option');
$mediaSelectionFieldPresenter = app(MediaSelectionFieldPresenter::class);
$html = '';
if(!empty($options['fields'])) {
foreach($options['fields'] as $key => $option) {
$type = !empty($option['type']) ? $option['type'] : 'text';
$required = !empty($option['required']) && $option['required'] ? true : false;
$label = !empty($option['label']) ? $option['label'] : $key;
$html .= $presenter->div([
'class' => 'form-group row',
'html' => [
$presenter->div([
'class' => 'col-12 col-md-6',
'html' => $presenter->label([
'for' => 'option-' . $key,
'html' => [
trans('adminOptionLabels.' . $label),
$required ? $presenter->span([
'class' => 'required',
'html' => '*'
]) : ''
]
])
]),
$presenter->div([
'class' => 'col-12 col-md-6',
'html' => function() use ($key, $type, $required, $presenter, $optionRepo, $mediaSelectionFieldPresenter) {
$html = '';
$bastHtmlArgs = [
'name' => $key,
'id' => 'option-' . $key,
];
switch ($type) {
case 'media':
$mediaFileId = $optionRepo->$key;
$html .= $mediaSelectionFieldPresenter->render($mediaFileId, $bastHtmlArgs, ['option-media-field']);
break;
case 'text':
case 'password':
case 'email':
case 'number':
$htmlArgs = array_merge([
'type' => $type,
'value' => $optionRepo->$key,
'class' => 'form-control',
], $bastHtmlArgs);
if($required)
$htmlArgs['required'] = null;
$html .= $presenter->input($htmlArgs);
break;
case 'checkbox':
$htmlArgs = array_merge([
'type' => 'checkbox',
'value' => 1,
], $bastHtmlArgs);
if($optionRepo->$key) {
$htmlArgs['checked'] = 'checked';
}
$html .= $presenter->input($htmlArgs);
break;
case 'textarea':
$htmlArgs = array_merge([
'html' => $optionRepo->$key,
'class' => 'form-control',
], $bastHtmlArgs);
if($required)
$htmlArgs['required'] = null;
if(!empty($option['row']))
$htmlArgs['row'] = $option['row'];
$html .= $presenter->textarea($htmlArgs);
break;
case 'select':
$selectOptions = !empty($option['options']) ? $option['options'] : null;
$htmlArgs = array_merge([
'class' => 'form-control',
], $bastHtmlArgs);
$currentValue = $optionRepo->$key;
if(!empty($selectOptions)) {
$htmlArgs['html'] = function() use ($currentValue, $selectOptions, $presenter) {
$html = '';
foreach ($selectOptions as $value => $label) {
$htmlArgs = [
'value' => $value,
'html' => trans('form.options.' . $label),
];
if($currentValue == $value) {
$htmlArgs['selected'] = 'selected';
}
$html .= $presenter->option($htmlArgs);
}
return $html;
};
}
if($required)
$htmlArgs['required'] = null;
$html .= $presenter->select($htmlArgs);
break;
case 'radio':
$radioOptions = !empty($option['options']) ? $option['options'] : null;
$currentValue = $optionRepo->$key;
$defaultValue = !empty($option['default']) ? $option['default'] : null;
if(!empty($radioOptions)) {
foreach ($radioOptions as $value => $label) {
$html .= $presenter->div([
'class' => 'form-check',
'html' => function() use ($key, $currentValue, $defaultValue, $label, $value, $radioOptions, $presenter) {
$html = '';
$inputId = 'option-' . $key . '-' . $value;
$inputHtmlArgs = [
'type' => 'radio',
'class' => 'form-check-input',
'name' => $key,
'id' => $inputId,
'value' => $value
];
if($currentValue === null) {
if($defaultValue && $defaultValue == $value) {
$inputHtmlArgs['checked'] = 'checked';
} elseif(array_key_first($radioOptions) == $value) {
$inputHtmlArgs['checked'] = 'checked';
}
} elseif($currentValue == $value) {
$inputHtmlArgs['checked'] = 'checked';
}
$html .= $presenter->input($inputHtmlArgs);
$html .= $presenter->label([
'class' => 'form-check-label',
'for' => $inputId,
'html' => $label
]);
return $html;
}
]);
}
}
break;
}
return $html;
}
]),
]
]);
}
}
return $html;
}
}