Penambahan Filter Jenjang Pendidikan untuk menu P2 dan Simulasi Jabatan
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
use App\Models\Verifikator\View\Pegawai;
|
||||
|
||||
class JenjangPendidikanHelper
|
||||
{
|
||||
/**
|
||||
* Nilai sentinel untuk kategori "belum diisi" (pendidikan_terakhir NULL/kosong).
|
||||
* Tanpa ini, pegawai yang datanya kosong tidak akan pernah muncul di filter
|
||||
* manapun sehingga jumlah per kategori bisa lebih kecil dari total keseluruhan.
|
||||
*/
|
||||
public const KOSONG = '__KOSONG__';
|
||||
|
||||
public const KOSONG_LABEL = '(Belum Diisi)';
|
||||
|
||||
/**
|
||||
* Daftar jenjang pendidikan unik (sudah dinormalisasi) untuk dropdown filter.
|
||||
*/
|
||||
public static function options(): array
|
||||
{
|
||||
$raw = Pegawai::withoutGlobalScopes()
|
||||
->whereNotNull('pendidikan_terakhir')
|
||||
->distinct()
|
||||
->pluck('pendidikan_terakhir');
|
||||
|
||||
$options = $raw->map(fn ($value) => self::normalize($value))
|
||||
->unique()
|
||||
->sort()
|
||||
->values()
|
||||
->all();
|
||||
|
||||
$options[] = self::KOSONG;
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/** Angka diploma -> angka romawi setara, dipakai untuk gabungkan "DIII" ke "D3", dst. */
|
||||
private const DIPLOMA_ROMAN = ['2' => 'ii', '3' => 'iii', '4' => 'iv'];
|
||||
|
||||
/**
|
||||
* Gabungkan varian penulisan (mis. "S-1 Adm Publik", "S-1" -> "S1";
|
||||
* "SMA / sederajat", "SMA/SMK" -> "SMA"; "DIII" -> "D3";
|
||||
* "Profesi", "Sp-1" -> "S1") jadi satu kategori.
|
||||
*/
|
||||
public static function normalize(string $value): string
|
||||
{
|
||||
$trimmed = trim($value);
|
||||
|
||||
if (preg_match('/^(Profesi|Sp-?1)\b/i', $trimmed)) {
|
||||
return 'S1';
|
||||
}
|
||||
|
||||
if (preg_match('/^S-?([123])\b/i', $trimmed, $matches)) {
|
||||
return 'S'.$matches[1];
|
||||
}
|
||||
|
||||
if (preg_match('/^SMA\b/i', $trimmed)) {
|
||||
return 'SMA';
|
||||
}
|
||||
|
||||
if (preg_match('/^D-?(2|II|3|III|4|IV)\b/i', $trimmed, $matches)) {
|
||||
$digit = array_search(strtolower($matches[1]), self::DIPLOMA_ROMAN, true) ?: $matches[1];
|
||||
|
||||
return 'D'.$digit;
|
||||
}
|
||||
|
||||
return $trimmed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Terapkan filter jenjang pendidikan ke query builder, mencocokkan seluruh
|
||||
* varian penulisan yang tergabung dalam satu kategori (S1/S2/S3/SMA/D2/D3/D4).
|
||||
*/
|
||||
public static function applyQueryFilter($query, string $column, string $jenjangPendidikan): void
|
||||
{
|
||||
if ($jenjangPendidikan === self::KOSONG) {
|
||||
$query->whereNull($column);
|
||||
} elseif ($jenjangPendidikan === 'S1') {
|
||||
$query->whereRaw("{$column} ~* ?", ['^(s-?1|profesi|sp-?1)']);
|
||||
} elseif (preg_match('/^S([23])$/', $jenjangPendidikan, $matches)) {
|
||||
$query->whereRaw("{$column} ~* ?", ['^s-?'.$matches[1]]);
|
||||
} elseif ($jenjangPendidikan === 'SMA') {
|
||||
$query->whereRaw("{$column} ~* ?", ['^sma']);
|
||||
} elseif (preg_match('/^D([234])$/', $jenjangPendidikan, $matches)) {
|
||||
$roman = self::DIPLOMA_ROMAN[$matches[1]];
|
||||
$query->whereRaw("{$column} ~* ?", ['^d-?('.$matches[1].'|'.$roman.')\y']);
|
||||
} else {
|
||||
$query->where($column, $jenjangPendidikan);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Terapkan filter jenjang pendidikan ke collection di memori (dataset yang
|
||||
* sudah di-cache), dengan nilai per baris yang sudah dinormalisasi lebih dulu.
|
||||
*/
|
||||
public static function applyCollectionFilter(\Illuminate\Support\Collection $rows, string $column, string $jenjangPendidikan): \Illuminate\Support\Collection
|
||||
{
|
||||
$target = $jenjangPendidikan === self::KOSONG ? '' : $jenjangPendidikan;
|
||||
|
||||
return $rows->where($column, $target);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user