[Operator] Perbaikan Laman Portal -> Konsultasi

This commit is contained in:
Ariyutta
2026-11-11 09:28:34 +07:00
parent 52d49cdb41
commit d6425a601d
19 changed files with 1134 additions and 301 deletions
+6 -128
View File
@@ -2,10 +2,10 @@
namespace App\Support\Legacy;
use App\Support\Legacy\Concerns\BiodataFields;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
/**
@@ -16,6 +16,8 @@ use Illuminate\Validation\ValidationException;
*/
final class StudentBiodata
{
use BiodataFields;
/** Kode pilihan tanpa tabel referensi di DB; arti kode diambil dari komentar kolom t_mahasiswa. */
private const array MARITAL = [1 => 'Belum kawin', 2 => 'Kawin'];
@@ -185,21 +187,7 @@ final class StudentBiodata
$row = $this->row($prodi, $id) ?? throw new PortalException('Mahasiswa tidak ditemukan.');
$lookups = $this->lookups();
$changes = [];
foreach ($section['fields'] as $field) {
$name = $field['name'];
if (! array_key_exists($name, $input)) {
continue;
}
$new = $this->normalize($field, $input[$name], $lookups);
if ($field['name'] === 'nama' && $new === null) {
throw new PortalException('Nama wajib diisi.');
}
if ($new !== $this->normalize($field, $row->{$name}, $lookups)) {
// Kolom wajib di DB asli (NOT NULL) tidak pernah diisi null.
$changes[$name] = $new ?? (($field['type'] ?? '') === 'select' ? 0 : null);
}
}
$changes = $this->changes($section['fields'], $row, $input, $lookups);
if ($changes !== []) {
$this->db()->table('t_mahasiswa')->where('idmahasiswa', $id)->update($changes + [
@@ -225,46 +213,6 @@ final class StudentBiodata
->first(array_merge(array_map(fn (string $c): string => 'm.'.$c, $columns), ['m.idpa', 'm.jalur_masuk', 'm.idkurikulum', 'm.stataktif', 'm.tgl_lulus']));
}
/**
* Nilai isian (untuk formulir) dan teks tampil satu bagian.
*
* @param array<string, mixed> $section
* @param array<string, array<int, string>> $lookups
* @return array{fields: list<array<string, mixed>>, values: array<string, mixed>, display: array<string, string>}
*/
private function present(array $section, object $row, array $lookups): array
{
$fields = [];
$values = [];
$display = [];
foreach ($section['fields'] as $field) {
$name = $field['name'];
$value = $this->normalize($field, $row->{$name}, $lookups);
if (($field['type'] ?? '') === 'select') {
$options = $lookups[$field['lookup']];
$current = (int) ($value ?? 0);
// Kode yang tidak ada di tabel referensi tetap tampil dan bisa dipertahankan.
$options += isset($options[$current]) ? [] : [$current => 'Kode '.$current];
$field['options'] = $options;
$values[$name] = $current;
$display[$name] = $current === 0 ? '–' : $options[$current];
} else {
$values[$name] = $value ?? '';
$display[$name] = match (true) {
$value === null => '–',
($field['type'] ?? '') === 'date' => Carbon::parse($value)->locale('id')->isoFormat('D MMMM YYYY'),
default => (string) $value,
};
}
$fields[] = $field;
}
return ['fields' => $fields, 'values' => $values, 'display' => $display];
}
/**
* Data akademik yang hanya dibaca: jalur masuk, kurikulum, dosen PA, tanggal lulus.
*
@@ -285,34 +233,7 @@ final class StudentBiodata
}
/**
* Nilai kolom dalam bentuk baku agar dua nilai bisa dibandingkan: teks dipangkas (kosong = null), tanggal Y-m-d,
* angka integer, pilihan integer (kosong = 0 pada pilihan yang punya kode 0).
*
* @param array<string, mixed> $field
* @param array<string, array<int, string>> $lookups
*/
private function normalize(array $field, mixed $value, array $lookups): string|int|null
{
$type = $field['type'] ?? 'text';
$text = is_scalar($value) ? trim((string) $value) : '';
return match ($type) {
'select' => (function () use ($text, $field, $lookups): ?int {
if ($text === '') {
return isset($lookups[$field['lookup']][0]) ? 0 : null;
}
return (int) $text;
})(),
'number' => $text === '' ? null : (int) $text,
'date' => $text === '' ? null : substr($text, 0, 10),
default => $text === '' ? null : $text,
};
}
/**
* Periksa isian satu bagian. Isian yang nilainya sama dengan yang tersimpan tidak diperiksa bentuknya (data lama
* boleh tidak seragam) dan tidak ikut dikembalikan; kolom wajib selalu diperiksa.
* Periksa isian satu bagian (hanya isian yang berubah diperiksa bentuknya; lihat BiodataFields::validateFields).
*
* @param list<int> $prodi
* @param array<string, mixed> $input
@@ -323,50 +244,7 @@ final class StudentBiodata
public function validated(array $prodi, int $id, string $key, array $input): array
{
$row = $this->row($prodi, $id) ?? throw new PortalException('Mahasiswa tidak ditemukan.');
$lookups = $this->lookups();
$rules = $this->rules($key);
foreach (self::sections()[$key]['fields'] as $field) {
$name = $field['name'];
if (! ($field['required'] ?? false) && array_key_exists($name, $input)
&& $this->normalize($field, $input[$name], $lookups) === $this->normalize($field, $row->{$name}, $lookups)) {
unset($rules[$name]);
}
}
return Validator::make($input, $rules, [], $this->attributes($key))->validate();
}
/** @return array<string, list<string>> */
private function rules(string $key): array
{
$lookups = $this->lookups();
$rules = [];
foreach (self::sections()[$key]['fields'] as $field) {
$type = $field['type'] ?? 'text';
$base = ($field['required'] ?? false) ? ['required'] : ['nullable'];
$rules[$field['name']] = array_merge($base, match (true) {
$type === 'select' => ['integer', 'in:'.implode(',', array_keys($lookups[$field['lookup']]))],
$type === 'number' => ['integer', 'min:'.($field['min'] ?? 0), 'max:'.($field['max'] ?? 99)],
$type === 'date' => ['date_format:Y-m-d', 'before_or_equal:today'],
$type === 'email' => ['string', 'email', 'max:'.$field['max']],
default => array_values(array_filter([
'string',
'max:'.$field['max'],
($field['alnum'] ?? false) ? 'regex:/^[0-9A-Za-z]+$/' : null,
($field['digits'] ?? false) ? 'regex:/^[0-9]{4}$/' : null,
])),
});
}
return $rules;
}
/** @return array<string, string> */
private function attributes(string $key): array
{
return collect(self::sections()[$key]['fields'])->mapWithKeys(fn (array $f): array => [$f['name'] => mb_strtolower($f['label'])])->all();
return $this->validateFields(self::sections()[$key]['fields'], $row, $input);
}
}