form tabel
This commit is contained in:
@@ -0,0 +1,236 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Ekosistem;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
use App\Models\Ekosistem\TabelBasisData;
|
||||
use App\Enums\LayoutForm;
|
||||
|
||||
class FormBasisData extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $table = 'ekosistem_basis_data_form';
|
||||
|
||||
protected $fillable = [
|
||||
'tabel_id',
|
||||
'parent_id',
|
||||
'tipe',
|
||||
'column_width',
|
||||
'kolom_id',
|
||||
'section_label',
|
||||
'urutan',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
'deleted_by',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'tipe' => 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 => '<div class="col-'.($this->column_width ?? 3).' mb-4 form-control-validation">'
|
||||
.$this->childHtml($state, $errors, $optionsFK, $values).'</div>',
|
||||
LayoutForm::VALUE => $this->value($state, $errors, $optionsFK, $values),
|
||||
LayoutForm::SECTION => '<h6 class="mb-3">'.$this->section_label.'</h6>',
|
||||
LayoutForm::ROW => '<div class="row">'.$this->childHtml($state, $errors, $optionsFK, $values).'</div>',
|
||||
LayoutForm::HR_LINE => '<hr/>',
|
||||
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 .= '<label class="form-label" for="value-data-kolom_name">kolom_required'.$this->kolom->label;
|
||||
$html .= '</label>';
|
||||
|
||||
if($k->tipe == 'varchar') {
|
||||
$html .= '<input type="text" class="form-control kolom_error" id="value-data-kolom_name" readonly
|
||||
name="kolom_name" value="kolom_value" />';
|
||||
}
|
||||
elseif($k->tipe == 'text') {
|
||||
$html .= '<textarea class="form-control kolom_error" id="value-data-kolom_name" readonly
|
||||
name="kolom_name">kolom_value</textarea>';
|
||||
}
|
||||
elseif($k->tipe == 'integer') {
|
||||
$html .= '<input type="number" class="form-control" id="value-data-kolom_name" readonly
|
||||
name="kolom_name" step="1" value="kolom_value" />';
|
||||
}
|
||||
elseif($k->tipe == 'float') {
|
||||
$html .= '<input type="number" class="form-control kolom_error" id="add-data-kolom_name" readonly
|
||||
name="kolom_name" value="kolom_value" step="any" />';
|
||||
}
|
||||
elseif($k->tipe == 'date') {
|
||||
$html .= '<input type="date" class="form-control kolom_error" id="value-data-kolom_name" readonly
|
||||
name="kolom_name" value="kolom_value" />';
|
||||
}
|
||||
elseif($k->tipe == 'datetime'){
|
||||
$html .= '<input type="datetime-local" class="form-control kolom_error" id="value-data-kolom_name" readonly
|
||||
name="kolom_name" value="kolom_value" />';
|
||||
}
|
||||
elseif($k->tipe == 'boolean') {
|
||||
if ($state == 'preview' || $state == 'view') {
|
||||
$html .= '<input type="text" class="form-control kolom_error" id="value-data-kolom_name" readonly
|
||||
name="kolom_name" value="kolom_value" />';
|
||||
} elseif ($state == 'add') {
|
||||
$html .= '<select class="form-select tipe-boolean kolom_error id="value-data-kolom_name" name="kolom_name">
|
||||
<option value="1" '. (old($k->kolom) == 1 ? 'selected' : '') .'>Ya</option>
|
||||
<option value="0" '. (old($k->kolom) == 0 ? 'selected' : '') .'>Tidak</option>
|
||||
</select>';
|
||||
} elseif ($state == 'edit') {
|
||||
$html .= '<select class="form-select tipe-boolean kolom_error id="value-data-kolom_name" name="kolom_name">
|
||||
<option value="1" '. ((old($k->kolom) ?? (($value ? $value['value_'.$k->tipe] : null) ?? null) == 1) ? 'selected' : '') .'>Ya</option>
|
||||
<option value="0" '. ((old($k->kolom) ?? (($value ? $value['value_'.$k->tipe] : null) ?? null) == 0) ? 'selected' : '') .'>Tidak</option>
|
||||
</select>';
|
||||
}
|
||||
}
|
||||
elseif($k->tipe == 'foreign_key') {
|
||||
if ($state == 'preview' || $state == 'view') {
|
||||
$html .= '<input type="text" class="form-control kolom_error" id="value-data-kolom_name" readonly
|
||||
name="kolom_name" value="kolom_value" />';
|
||||
} elseif ($state == 'add') {
|
||||
$html .= '<select class="form-select tipe-boolean kolom_error id="value-data-kolom_name" name="kolom_name">';
|
||||
if ($optionsFK && $optionsFK->where('kolom_id', $k->id)->first() != null) {
|
||||
foreach ($optionsFK->where('kolom_id', $k->id)->first()['options'] as $option) {
|
||||
$html .= '<option value="'.$option->value.'" '.(old($k->kolom) == $option->value ? 'selected' : '').'>'.$option->label.'</option>';
|
||||
}
|
||||
}
|
||||
$html .= '</select>';
|
||||
} elseif ($state == 'edit') {
|
||||
$html .= '<select class="form-select tipe-boolean kolom_error id="value-data-kolom_name" name="kolom_name">';
|
||||
if ($optionsFK && $optionsFK->where('kolom_id', $k->id)->first() != null) {
|
||||
foreach ($optionsFK->where('kolom_id', $k->id)->first()['options'] as $option) {
|
||||
$html .= '<option value="'.$option->value.'" '.(old($k->kolom) ?? ($value ? $value['value_relasi'] : null) == $option->value ? 'selected' : '').'>'.$option->label.'</option>';
|
||||
}
|
||||
}
|
||||
$html .= '</select>';
|
||||
}
|
||||
}
|
||||
elseif($k->tipe == 'enum') {
|
||||
if ($state == 'preview' || $state == 'view') {
|
||||
$html .= '<input type="text" class="form-control kolom_error" id="value-data-kolom_name" readonly
|
||||
name="kolom_name" value="kolom_value" />';
|
||||
} elseif ($state == 'add') {
|
||||
$html .= '<select class="form-select tipe-boolean kolom_error id="value-data-kolom_name" name="kolom_name">';
|
||||
foreach($k->enum_opsi as $opsi) {
|
||||
$html .= '<option value="' .$opsi['value']. '" ' .(old($k->kolom) == $opsi['value'] ? 'selected' : ''). '>' .$opsi['label']. '</option>';
|
||||
}
|
||||
$html .= '</select>';
|
||||
} elseif ($state == 'edit') {
|
||||
$html .= '<select class="form-select tipe-boolean kolom_error id="value-data-kolom_name" name="kolom_name">';
|
||||
foreach($k->enum_opsi as $opsi) {
|
||||
$html .= '<option value="' .$opsi['value']. '" ' .(old($k->kolom) ?? ($value ? $value['value_'.$k->tipe] : null) == $opsi['value'] ? 'selected' : ''). '>' .$opsi['label']. '</option>';
|
||||
}
|
||||
$html .= '</select>';
|
||||
}
|
||||
}
|
||||
else {
|
||||
$html .= '<input type="text" class="form-control kolom_error" id="value-data-kolom_name" readonly
|
||||
name="kolom_name" value="kolom_value" />';
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user