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') ->orderBy('urutan'); } public function kolom(): BelongsTo { return $this->belongsTo(KolomBasisData::class, 'kolom_id'); } public function renderHtml(string $state, $errors = null, $optionsFK = null, $values = null, $framework = 'bootstrap'): string { if ($framework == 'tailwind') { return match ($this->tipe) { LayoutForm::COLUMN => '
' .$this->childHtml($state, $errors, $optionsFK, $values, $framework).'
', LayoutForm::VALUE => $this->value($state, $errors, $optionsFK, $values, $framework), LayoutForm::SECTION => '

'.$this->section_label.'

', LayoutForm::ROW => '
'.$this->childHtml($state, $errors, $optionsFK, $values, $framework).'
', LayoutForm::HR_LINE => '
', default => '' }; } else { return match ($this->tipe) { LayoutForm::COLUMN => '
' .$this->childHtml($state, $errors, $optionsFK, $values, $framework).'
', LayoutForm::VALUE => $this->value($state, $errors, $optionsFK, $values, $framework), LayoutForm::SECTION => '
'.$this->section_label.'
', LayoutForm::ROW => '
'.$this->childHtml($state, $errors, $optionsFK, $values, $framework).'
', LayoutForm::HR_LINE => '
', default => '' }; } } private function childHtml(string $state, $errors = null, $optionsFK = null, $values = null, $framework = 'bootstrap'): string { $html = ''; foreach ($this->form as $form) { $html .= $form->renderHtml($state, $errors, $optionsFK, $values, $framework); } return $html; } private function value(string $state, $errors = null, $optionsFK = null, $values = null, $framework = 'bootstrap'): string { $html = ''; if ($this->kolom) { $k = $this->kolom; $value = $values ? $values->where('kolom_id', $k->id)->first() : null; $oldValue = old($k->kolom); if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } if($k->tipe == 'varchar') { if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } } elseif($k->tipe == 'text') { if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } } elseif($k->tipe == 'integer') { if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } } elseif($k->tipe == 'float') { if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } } elseif($k->tipe == 'date') { if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } } elseif($k->tipe == 'datetime'){ if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } } elseif($k->tipe == 'boolean') { if ($state == 'preview' || $state == 'view') { if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } } elseif ($state == 'add') { if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } } elseif ($state == 'edit') { if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } } } elseif($k->tipe == 'foreign_key') { if ($state == 'preview' || $state == 'view') { if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } } elseif ($state == 'add') { if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } } elseif ($state == 'edit') { if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } } } elseif($k->tipe == 'enum') { if ($state == 'preview' || $state == 'view') { if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } } elseif ($state == 'add') { if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } } elseif ($state == 'edit') { if ($framework == 'tailwind') { $html .= ''; } else { $html .= ''; } } } else { if ($framework == 'tailwind') { $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)); } } if ($errors && $errors->has($k->kolom)) { if ($framework == 'tailwind') { $html .= '

'.$errors->first($k->kolom).'

'; } else { $html .= '
'.$errors->first($k->kolom).'
'; } } } return $html; } }