[Operator] Perbaikan Halaman Pegawai dan Export Excel
This commit is contained in:
@@ -57,11 +57,8 @@ class EmployeeListController extends Controller
|
||||
'columns' => [
|
||||
['key' => 'nip', 'label' => 'NIP'],
|
||||
['key' => 'nama', 'label' => 'Nama'],
|
||||
['key' => 'kelamin', 'label' => 'L/P'],
|
||||
['key' => 'nidn', 'label' => 'NIDN'],
|
||||
['key' => 'nuptk', 'label' => 'NUPTK'],
|
||||
['key' => 'telepon', 'label' => 'No. Telp.'],
|
||||
['key' => 'email', 'label' => 'Email'],
|
||||
['key' => 'status', 'label' => 'Status', 'badge' => true],
|
||||
],
|
||||
'perPage' => EmployeeDirectory::PER_PAGE,
|
||||
@@ -218,11 +215,8 @@ class EmployeeListController extends Controller
|
||||
'id' => $p['id'],
|
||||
'nip' => $dash($p['nip']),
|
||||
'nama' => $p['nama'],
|
||||
'kelamin' => $dash($p['kelamin']),
|
||||
'nidn' => $dash($p['nidn']),
|
||||
'nuptk' => '–',
|
||||
'telepon' => $dash($p['telepon']),
|
||||
'email' => $dash($p['email']),
|
||||
'status' => $p['status']['label'],
|
||||
'tone' => $p['status']['tone'] === 'neutral' ? null : $p['status']['tone'],
|
||||
])->all(),
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Operator;
|
||||
|
||||
use App\Exports\ReportExport;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Legacy\Account;
|
||||
use App\Support\Legacy\AcademicOverview;
|
||||
@@ -15,11 +16,14 @@ use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Maatwebsite\Excel\Excel as ExcelWriter;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
/**
|
||||
* Menu Laporan operator untuk akun DB lama: formulir parameter yang menjalankan laporan otomatis lewat JSON
|
||||
* (tabel berhalaman), unduh CSV, dan halaman cetak. Hanya baca. Sumber laporan (ReportSource) hanya mengenal
|
||||
* (tabel berhalaman), unduh CSV (atau Excel lewat maatwebsite/excel bila definisi laporan memuat 'export' => 'xlsx'), dan halaman cetak. Hanya baca. Sumber laporan (ReportSource) hanya mengenal
|
||||
* limit, jadi halaman N diambil dengan limit N × per halaman lalu dipotong (dibatasi CAP baris pertama).
|
||||
*/
|
||||
class LaporanController extends Controller
|
||||
@@ -34,6 +38,8 @@ class LaporanController extends Controller
|
||||
|
||||
private const int DOWNLOAD = 50000;
|
||||
|
||||
private const string XLSX_MIME = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
||||
|
||||
public function __construct(private readonly AcademicOverview $overview) {}
|
||||
|
||||
public function __invoke(Request $request, string $slug): View
|
||||
@@ -52,6 +58,7 @@ class LaporanController extends Controller
|
||||
'slug' => $slug,
|
||||
'definition' => $definition,
|
||||
'values' => $values,
|
||||
'downloadLabel' => self::downloadLabel($definition),
|
||||
'config' => [
|
||||
'url' => route('operator.laporan.data', $slug),
|
||||
'downloadUrl' => route('operator.laporan.unduh', $slug),
|
||||
@@ -74,18 +81,7 @@ class LaporanController extends Controller
|
||||
}
|
||||
|
||||
$list = (bool) ($definition['list'] ?? true);
|
||||
$raw = $request->input('q');
|
||||
$q = is_string($raw) ? mb_substr(trim($raw), 0, 100) : '';
|
||||
$filterQ = null;
|
||||
if ($list && $q !== '') {
|
||||
if ($definition['search'] ?? false) {
|
||||
// Sumber mengenal kata cari: diteruskan sebagai parameter q.
|
||||
$values['q'] = $q;
|
||||
} elseif (! self::hasParam($definition, 'q')) {
|
||||
// Sumber tidak mengenal kata cari: hasil disaring di sini atas semua kolom (sampai batas CAP baris).
|
||||
$filterQ = $q;
|
||||
}
|
||||
}
|
||||
$filterQ = $this->searchTerm($request, $definition, $values);
|
||||
$per = in_array($request->integer('per'), self::PER_PAGE, true) ? $request->integer('per') : self::PER_PAGE[1];
|
||||
|
||||
try {
|
||||
@@ -95,6 +91,52 @@ class LaporanController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Kata cari dari kotak cari daftar. Sumber yang mengenal kata cari menerimanya sebagai parameter q ($values diisi);
|
||||
* selain itu kata cari dikembalikan untuk saringan generik atas semua kolom hasil. Null bila tidak ada yang perlu
|
||||
* disaring di sini (tanpa kata cari, bukan daftar, atau laporan punya parameter q sendiri).
|
||||
*
|
||||
* @param array<string, mixed> $definition
|
||||
* @param array<string, mixed> $values
|
||||
*/
|
||||
private function searchTerm(Request $request, array $definition, array &$values): ?string
|
||||
{
|
||||
$raw = $request->input('q');
|
||||
$q = is_string($raw) ? mb_substr(trim($raw), 0, 100) : '';
|
||||
if ($q === '' || ! ($definition['list'] ?? true)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($definition['search'] ?? false) {
|
||||
$values['q'] = $q;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return self::hasParam($definition, 'q') ? null : $q;
|
||||
}
|
||||
|
||||
/**
|
||||
* Baris yang salah satu kolomnya memuat kata cari (tanpa membedakan huruf besar/kecil).
|
||||
*
|
||||
* @param list<array<string, mixed>> $rows
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
private static function matching(array $rows, string $q): array
|
||||
{
|
||||
$needle = mb_strtolower($q);
|
||||
|
||||
return array_values(array_filter($rows, function (array $row) use ($needle): bool {
|
||||
foreach ($row as $value) {
|
||||
if (is_scalar($value) && str_contains(mb_strtolower((string) $value), $needle)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}));
|
||||
}
|
||||
|
||||
/** Laporan punya parameter bernama {name}? */
|
||||
private static function hasParam(array $definition, string $name): bool
|
||||
{
|
||||
@@ -115,16 +157,7 @@ class LaporanController extends Controller
|
||||
if ($filterQ !== null) {
|
||||
// Kata cari generik: ambil sampai batas CAP lalu saring baris yang salah satu kolomnya memuat kata itu.
|
||||
$result = $source->run($slug, $values, $context, self::CAP + 1);
|
||||
$needle = mb_strtolower($filterQ);
|
||||
$rows = array_values(array_filter($result->rows, function (array $row) use ($needle): bool {
|
||||
foreach ($row as $value) {
|
||||
if (is_scalar($value) && str_contains(mb_strtolower((string) $value), $needle)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}));
|
||||
$rows = self::matching($result->rows, $filterQ);
|
||||
$result = new ReportResult($result->columns, $rows, count($rows), $result->summary, $result->note);
|
||||
} else {
|
||||
// +1 agar terlihat bila masih ada baris sesudah halaman ini saat sumber tidak mengisi total
|
||||
@@ -216,34 +249,56 @@ class LaporanController extends Controller
|
||||
// Kotak cari: ke sumber bila mengenal q, selain itu saringan generik atas kolom hasil; disembunyikan hanya bila laporan
|
||||
// punya parameter lain bernama q.
|
||||
'searchable' => (bool) ($definition['search'] ?? false) || ! self::hasParam($definition, 'q'),
|
||||
'barLinks' => [['label' => 'Unduh (CSV)', 'href' => route('operator.laporan.unduh', $slug), 'icon' => 'arrow-down-tray', 'withFilters' => true]],
|
||||
'barLinks' => [['label' => self::downloadLabel($definition), 'href' => route('operator.laporan.unduh', $slug), 'icon' => 'arrow-down-tray', 'withFilters' => true]],
|
||||
'data' => $data,
|
||||
]);
|
||||
}
|
||||
|
||||
public function download(Request $request, string $slug): StreamedResponse|RedirectResponse
|
||||
/** Format unduhan laporan: 'xlsx' bila definisinya meminta ('export' => 'xlsx'), selain itu CSV. */
|
||||
private static function exportFormat(array $definition): string
|
||||
{
|
||||
[$source, $context, , $definition] = $this->prepare($request, $slug);
|
||||
return ($definition['export'] ?? 'csv') === 'xlsx' ? 'xlsx' : 'csv';
|
||||
}
|
||||
|
||||
/** Label tombol unduh menurut format. */
|
||||
private static function downloadLabel(array $definition): string
|
||||
{
|
||||
return self::exportFormat($definition) === 'xlsx' ? 'Unduh (Excel)' : 'Unduh (CSV)';
|
||||
}
|
||||
|
||||
public function download(Request $request, string $slug): StreamedResponse|BinaryFileResponse|RedirectResponse
|
||||
{
|
||||
[$source, $context, $page, $definition] = $this->prepare($request, $slug);
|
||||
[$values, $errors] = $this->parse($request, $definition['params'], true);
|
||||
if ($errors !== []) {
|
||||
return redirect()->route('operator.halaman', [$slug] + $this->queryString($request));
|
||||
}
|
||||
|
||||
// Kata cari di kotak cari ikut berlaku, agar unduhan sama dengan yang tampil di tabel.
|
||||
$filterQ = $this->searchTerm($request, $definition, $values);
|
||||
|
||||
try {
|
||||
$result = $source->run($slug, $values, $context, self::DOWNLOAD);
|
||||
} catch (PortalException $e) {
|
||||
return redirect()->route('operator.halaman', [$slug] + $this->queryString($request));
|
||||
}
|
||||
|
||||
return response()->streamDownload(function () use ($result): void {
|
||||
$rows = $filterQ === null ? $result->rows : self::matching($result->rows, $filterQ);
|
||||
$name = $slug.'-'.now()->format('Ymd-His');
|
||||
|
||||
if (self::exportFormat($definition) === 'xlsx') {
|
||||
return Excel::download(new ReportExport($page['title'], $result->columns, $rows), $name.'.xlsx', ExcelWriter::XLSX, ['Content-Type' => self::XLSX_MIME]);
|
||||
}
|
||||
|
||||
return response()->streamDownload(function () use ($result, $rows): void {
|
||||
$out = fopen('php://output', 'w');
|
||||
fwrite($out, "\xEF\xBB\xBF"); // BOM agar Excel membaca UTF-8
|
||||
fputcsv($out, array_column($result->columns, 'label'), ',', '"', '\\');
|
||||
foreach ($result->rows as $row) {
|
||||
foreach ($rows as $row) {
|
||||
fputcsv($out, array_map(fn (array $c): mixed => $row[$c['key']] ?? '', $result->columns), ',', '"', '\\');
|
||||
}
|
||||
fclose($out);
|
||||
}, $slug.'-'.now()->format('Ymd-His').'.csv', ['Content-Type' => 'text/csv; charset=UTF-8']);
|
||||
}, $name.'.csv', ['Content-Type' => 'text/csv; charset=UTF-8']);
|
||||
}
|
||||
|
||||
public function print(Request $request, string $slug): View|RedirectResponse
|
||||
@@ -264,6 +319,7 @@ class LaporanController extends Controller
|
||||
'title' => $page['title'],
|
||||
'result' => $result,
|
||||
'kop' => (bool) ($values['kop'] ?? true),
|
||||
'downloadLabel' => self::downloadLabel($definition),
|
||||
'parameters' => $this->describe($definition['params'], $values),
|
||||
'operator' => $request->user()->username,
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user