LayoutForm::class
];
protected static function booted()
{
static::creating(function ($model) {
$max = self::where('tabel_id', $model->tabel_id)->max('urutan');
$model->urutan = ($max ?? 0) + 1;
});
static::addGlobalScope('order', function (Builder $builder) {
$builder->orderBy('urutan');
});
}
public function tabel(): BelongsTo
{
return $this->belongsTo(TabelBasisData::class, 'tabel_id');
}
public function form(): HasMany
{
return $this->hasMany(FormBasisData::class, 'parent_id')
->with('form')
->orderBy('urutan');
}
public function kolom(): BelongsTo
{
return $this->belongsTo(KolomBasisData::class, 'kolom_id');
}
public function renderHtml(string $state, $errors = null, $optionsFK = null, $values = null): string
{
return match ($this->tipe) {
LayoutForm::COLUMN => '
'
.$this->childHtml($state, $errors, $optionsFK, $values).'
',
LayoutForm::VALUE => $this->value($state, $errors, $optionsFK, $values),
LayoutForm::SECTION => ''.$this->section_label.'
',
LayoutForm::ROW => ''.$this->childHtml($state, $errors, $optionsFK, $values).'
',
LayoutForm::HR_LINE => '
',
default => ''
};
}
private function childHtml(string $state, $errors = null, $optionsFK = null, $values = null): string
{
$html = '';
foreach ($this->form as $form) {
$html .= $form->renderHtml($state, $errors, $optionsFK, $values);
}
return $html;
}
private function value(string $state, $errors = null, $optionsFK = null, $values = null): string
{
$html = '';
if ($this->kolom) {
$k = $this->kolom;
$value = $values ? $values->where('kolom_id', $k->id)->first() : null;
$oldValue = old($k->kolom);
$html .= '';
if($k->tipe == 'varchar') {
$html .= '';
}
elseif($k->tipe == 'text') {
$html .= '';
}
elseif($k->tipe == 'integer') {
$html .= '';
}
elseif($k->tipe == 'float') {
$html .= '';
}
elseif($k->tipe == 'date') {
$html .= '';
}
elseif($k->tipe == 'datetime'){
$html .= '';
}
elseif($k->tipe == 'boolean') {
if ($state == 'preview' || $state == 'view') {
$html .= '';
} elseif ($state == 'add') {
$html .= '';
} elseif ($state == 'edit') {
$html .= '';
}
}
elseif($k->tipe == 'foreign_key') {
if ($state == 'preview' || $state == 'view') {
$html .= '';
} elseif ($state == 'add') {
$html .= '';
} elseif ($state == 'edit') {
$html .= '';
}
}
elseif($k->tipe == 'enum') {
if ($state == 'preview' || $state == 'view') {
$html .= '';
} elseif ($state == 'add') {
$html .= '';
} elseif ($state == 'edit') {
$html .= '';
}
}
else {
$html .= '';
}
$errorClass = $errors && $errors->has($k->kolom)
? 'is-invalid'
: '';
if ($state == 'preview') {
$html = Str::of($html)
->replace('kolom_value', '')
->replace('kolom_name', $k->kolom)
->replace('kolom_error', '')
->replace('kolom_required', '');
} elseif ($state == 'add') {
$html = Str::of($html)
->replace('kolom_value', e($oldValue))
->replace('kolom_name', $k->kolom)
->replace('kolom_error', $errorClass)
->replace('kolom_required', '')
->replace('readonly', '');
} elseif ($state == 'edit') {
$html = Str::of($html)
->replace('kolom_value', e($oldValue ?? ($value ? ($value['value_'.$k->tipe]) : null)))
->replace('kolom_name', $k->kolom)
->replace('kolom_error', $errorClass)
->replace('kolom_required', '')
->replace('readonly', '');
} elseif ($state == 'view') {
$html = Str::of($html)
->replace('kolom_name', $k->kolom)
->replace('kolom_error', '')
->replace('kolom_required', '');
if ($k->tipe == 'boolean') {
$html = Str::of($html)
->replace('kolom_value', e($value ? ($value['value_'.$k->tipe] == 1 ? 'Ya' : 'Tidak') : null));
} elseif($k->tipe == 'foreign_key') {
$html = Str::of($html)
->replace('kolom_value', e($value ? $value['value_relasi_label'] : null));
} elseif($k->tipe == 'enum') {
$html = Str::of($html)
->replace('kolom_value', e($value ? $value['value_enum_label'] : null));
} else {
$html = Str::of($html)
->replace('kolom_value', e($value ? $value['value_'.$k->tipe] : null));
}
}
}
return $html;
}
}