form tabel

This commit is contained in:
2026-05-15 15:31:19 +07:00
parent 34d09d03bb
commit e0ab4bdace
24 changed files with 1853 additions and 182 deletions
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace App\Enums;
enum LayoutForm: string
{
case ROW = 'form-row';
case COLUMN = 'form-column';
case VALUE = 'form-value';
case SECTION = 'form-section';
case HR_LINE = 'form-hr-line';
public function label(): string
{
return match($this) {
self::ROW => 'Row',
self::COLUMN => 'Column',
self::VALUE => 'Value',
self::SECTION => 'Section',
self::HR_LINE => 'HR Line',
};
}
public static function options(): array
{
return collect(self::cases())->map(fn ($case) => [
'value' => $case->value,
'label' => $case->label(),
])->toArray();
}
}
@@ -0,0 +1,295 @@
<?php
namespace App\Http\Controllers\Web\Ekosistem;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use App\Models\Ekosistem\TabelBasisData;
use App\Models\Ekosistem\FormBasisData;
use App\Enums\LayoutForm;
class FormBasisDataController extends Controller
{
public function index(Request $request, string $tabelId): View
{
Gate::authorize('super_admin');
$tabel = TabelBasisData::find($tabelId);
if (!$tabel) {
return response()->json([
'status' => 'error',
'message' => 'Data tabel tidak ditemukan.'
], 404);
}
$listEnum = LayoutForm::options();
$tab = $request->tab ?? 'form';
$tabel->state = 'preview';
return view('ekosistem.form-basis-data.index', compact('tabel', 'listEnum', 'tab'));
}
public function create()
{
//
}
public function store(Request $request, string $tabelId): JsonResponse
{
Gate::authorize('super_admin');
$tabel = TabelBasisData::find($tabelId);
if (!$tabel) {
return response()->json([
'status' => 'error',
'message' => 'Data tabel tidak ditemukan.'
], 404);
}
$validated = $request->validate([
'parent_id' => ['nullable', 'exists:ekosistem_basis_data_form,id'],
'tipe' => ['required', Rule::enum(LayoutForm::class)],
'column_width' => [
Rule::requiredIf($request->tipe === LayoutForm::COLUMN->value),
'nullable', 'integer', 'min:1', 'max:12'],
'kolom_id' => [
Rule::requiredIf($request->tipe === LayoutForm::VALUE->value),
'nullable', 'exists:ekosistem_basis_data_kolom,id'],
'section_label' => [
Rule::requiredIf($request->tipe === LayoutForm::SECTION->value),
'nullable', 'string'],
]);
FormBasisData::create([...$validated,
'tabel_id' => $tabelId,
'created_by' => auth()->user()->id,
'updated_by' => auth()->user()->id
]);
return response()->json([
'status' => 'success',
'message' => 'Form basis data berhasil ditambahkan.'
], 201);
}
public function show(string $tabelId, string $id): JsonResponse
{
Gate::authorize('super_admin');
$form = FormBasisData::where('tabel_id', $tabelId)->find($id);
if (!$form) {
return response()->json([
'status' => 'error',
'message' => 'Data formulir basis data tidak ditemukan.'
], 404);
}
return response()->json([
'status' => 'success',
'data' => $form
], 200);
}
public function edit(string $id)
{
//
}
public function update(Request $request, string $tabelId, string $id)
{
Gate::authorize('super_admin');
$form = FormBasisData::where('tabel_id', $tabelId)->find($id);
if (!$form) {
return response()->json([
'status' => 'error',
'message' => 'Data formulir basis data tidak ditemukan.'
], 404);
}
$validated = $request->validate([
'parent_id' => ['nullable', 'exists:ekosistem_basis_data_form,id'],
'tipe' => ['required', Rule::enum(LayoutForm::class)],
'column_width' => [
Rule::requiredIf($request->tipe === LayoutForm::COLUMN->value),
'nullable', 'integer', 'min:1', 'max:12'],
'kolom_id' => [
Rule::requiredIf($request->tipe === LayoutForm::VALUE->value),
'nullable', 'exists:ekosistem_basis_data_kolom,id'],
'section_label' => [
Rule::requiredIf($request->tipe === LayoutForm::SECTION->value),
'nullable', 'string'],
]);
$form->update([...$validated,
'updated_by' => auth()->user()->id
]);
return response()->json([
'status' => 'success',
'message' => 'Form basis data berhasil diubah.'
]);
}
public function destroy(string $tabelId, string $id): JsonResponse
{
Gate::authorize('super_admin');
$form = FormBasisData::where('tabel_id', $tabelId)->find($id);
if (!$form) {
return response()->json([
'status' => 'error',
'message' => 'Data form basis data tidak ditemukan.'
], 404);
}
if ($form->form->count() > 0) {
return response()->json([
'status' => 'error',
'message' => 'Tidak dapat menghapus formulir basis data yang memiliki formulir.'
], 400);
}
$form->update([
'deleted_by' => auth()->user()->id
]);
$form->delete();
return response()->json([
'status' => 'success',
'message' => 'Form basis data berhasil dihapus.'
], 200);
}
public function generate(Request $request, string $tabelId): RedirectResponse
{
Gate::authorize('super_admin');
$tabel = TabelBasisData::find($tabelId);
if (!$tabel) {
return redirect()->route('tabel.formulir.index', $tabelId)->with('error', 'Tabel basis data tidak ditemukan.');
}
DB::transaction(function () use ($tabel) {
FormBasisData::where('tabel_id', $tabel->id)->delete();
FormBasisData::create([
'tabel_id' => $tabel->id,
'tipe' => LayoutForm::SECTION,
'section_label' => $tabel->label,
'created_by' => auth()->user()->id,
'updated_by' => auth()->user()->id
]);
FormBasisData::create([
'tabel_id' => $tabel->id,
'tipe' => LayoutForm::HR_LINE,
'created_by' => auth()->user()->id,
'updated_by' => auth()->user()->id
]);
$chunks = $tabel->kolom->chunk(4);
foreach ($chunks as $chunk) {
$row = FormBasisData::create([
'tabel_id' => $tabel->id,
'tipe' => LayoutForm::ROW,
'created_by' => auth()->user()->id,
'updated_by' => auth()->user()->id
]);
foreach ($chunk as $kolom) {
$column = FormBasisData::create([
'tabel_id' => $tabel->id,
'tipe' => LayoutForm::COLUMN,
'column_width' => 3,
'parent_id' => $row->id,
'created_by' => auth()->user()->id,
'updated_by' => auth()->user()->id
]);
FormBasisData::create([
'tabel_id' => $tabel->id,
'tipe' => LayoutForm::VALUE,
'parent_id' => $column->id,
'kolom_id' => $kolom->id,
'created_by' => auth()->user()->id,
'updated_by' => auth()->user()->id
]);
}
}
});
return redirect()->route('tabel.formulir.index', $tabelId)->with('success', 'Formulir berhasil dibuat.');
}
public function moveUp(Request $request, string $tabelId, string $id): JsonResponse
{
Gate::authorize('super_admin');
$current = FormBasisData::where('tabel_id', $tabelId)->find($id);
if (!$current) {
return response()->json([
'status' => 'error',
'message' => 'Data formulir tidak ditemukan.'
], 404);
}
$prev = FormBasisData::where('tabel_id', $tabelId)
->where('urutan', '<', $current->urutan)
->where('parent_id', $current->parent_id)
->orderBy('urutan', 'desc')
->first();
if ($prev) {
// swap
[$current->urutan, $prev->urutan] = [$prev->urutan, $current->urutan];
$current->save();
$prev->save();
}
return response()->json([
'status' => 'success',
'message' => 'Formulir basis data berhasil diperbarui.'
], 200);
}
public function moveDown(Request $request, string $tabelId, string $id): JsonResponse
{
Gate::authorize('super_admin');
$current = FormBasisData::where('tabel_id', $tabelId)->find($id);
if (!$current) {
return response()->json([
'status' => 'error',
'message' => 'Data formulir tidak ditemukan.'
], 404);
}
$next = FormBasisData::where('tabel_id', $tabelId)
->where('urutan', '>', $current->urutan)
->where('parent_id', $current->parent_id)
->orderBy('urutan', 'asc')
->first();
if ($next) {
// swap
[$current->urutan, $next->urutan] = [$next->urutan, $current->urutan];
$current->save();
$next->save();
}
return response()->json([
'status' => 'success',
'message' => 'Formulir basis data berhasil diperbarui.'
], 200);
}
}
@@ -188,7 +188,7 @@ class KolomBasisDataController extends Controller
], 404);
}
$prev = KolomBasisData::where('tabel_id', $current->tabel_id)
$prev = KolomBasisData::where('tabel_id', $tabelId)
->where('urutan', '<', $current->urutan)
->orderBy('urutan', 'desc')
->first();
@@ -218,7 +218,7 @@ class KolomBasisDataController extends Controller
], 404);
}
$next = KolomBasisData::where('tabel_id', $current->tabel_id)
$next = KolomBasisData::where('tabel_id', $tabelId)
->where('urutan', '>', $current->urutan)
->orderBy('urutan', 'asc')
->first();
@@ -8,6 +8,10 @@ use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use App\Models\Ekosistem\TabelBasisData;
use App\Models\Ekosistem\KolomBasisData;
@@ -199,8 +203,7 @@ class ValueBasisDataController extends Controller
}
}
$list_relasi = collect($list_relasi);
// dd($kolom->where('tipe', 'enum')->first()?->enum_opsi);
return view('ekosistem.value-basis-data.add', compact('tabel', 'kolom', 'program', 'list_relasi'));
}
+236
View File
@@ -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;
}
}
+20
View File
@@ -7,10 +7,14 @@ use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Casts\Attribute;
use App\Models\Ekosistem\KolomBasisData;
use App\Models\Ekosistem\GroupBasisData;
use App\Models\Ekosistem\RingkasanBasisData;
use App\Models\Ekosistem\FormBasisData;
class TabelBasisData extends Model
{
@@ -53,4 +57,20 @@ class TabelBasisData extends Model
{
return $this->hasMany(RingkasanBasisData::class, 'tabel_id')->orderBy('urutan');
}
public function form(): HasMany
{
return $this->hasMany(FormBasisData::class, 'tabel_id')
->whereNull('parent_id')
->orderBy('urutan');
}
public function renderHtml(string $state, $errors = null, $optionsFK = null, $values = null): string
{
$html = '';
foreach ($this->form as $form) {
$html .= $form->renderHtml($state, $errors, $optionsFK, $values);
}
return $html;
}
}