Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 141 additions & 12 deletions app/Http/Controllers/DataSpasial/DasborDataSpasialController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@

namespace App\Http\Controllers\DataSpasial;

use App\Services\DasborDataSpasialService;
use App\Services\DataSpasialApiService;
use App\Services\DataSpasialResult;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\View\View;

/**
* Menu Dasbor DataSpasial.
*
* Kerangka menu pada fase fondasi ini. Agregat dan visualisasi penuh
* menjadi lingkup KAB-08 (OpenSID/OpenKab#1304).
*/
class DasborDataSpasialController extends DataSpasialController
{
public function __construct(
protected DasborDataSpasialService $dasbor
) {
parent::__construct($this->dasbor?->dataspasial ?? app(DataSpasialApiService::class));
}

protected function pageTitle(): string
{
return 'Dasbor DataSpasial';
Expand All @@ -27,12 +32,136 @@ protected function viewName(): string

protected function fetch(Request $request): DataSpasialResult
{
return $this->dataspasial->ringkasan($request->only([
'sektor_id',
'periode',
'kode_kabupaten',
'kode_kecamatan',
'kode_desa',
return $this->dasbor->fetchRingkasan($request->only([
'sektor', 'sektor_id', 'periode', 'kode_kabupaten', 'kode_kecamatan', 'kode_desa',
]));
}

public function index(Request $request): View
{
$cacheKey = 'dataspasial:dasbor:filter-meta:v1';
$cached = Cache::get($cacheKey);

if (is_array($cached) && isset($cached['sektorList'])) {
$sektorList = $cached['sektorList'];
$indikatorBySektor = $cached['indikatorBySektor'] ?? [];
} else {
$sektorList = $this->daftarSektor();
$indikatorBySektor = [];
foreach ($sektorList as $s) {
$kode = $s['kode'] ?? null;
if (! $kode) {
continue;
}
$skema = $this->dataspasial->listSkema(['sektor' => $kode]);
if (! $skema->isData() || empty($skema->data)) {
continue;
}
$attrs = $skema->data[0]['attributes'] ?? $skema->data[0] ?? [];
$isian = $attrs['isian'] ?? [];
if (is_array($isian)) {
foreach ($isian as $item) {
if (! is_array($item)) {
continue;
}
$ikode = $item['kode'] ?? null;
if (! $ikode) {
continue;
}
$indikatorBySektor[$kode][] = [
'kode' => (string) $ikode,
'label' => $item['label'] ?? $ikode,
'satuan' => $item['satuan'] ?? null,
];
}
}
}
Cache::put($cacheKey, [
'sektorList' => $sektorList,
'indikatorBySektor' => $indikatorBySektor,
], now()->addMinutes(30));
}

return view($this->viewName(), [
'title' => $this->pageTitle(),
'sektorList' => $sektorList,
'indikatorBySektor' => $indikatorBySektor,
]);
}

public function data(Request $request): JsonResponse
{
$built = $this->dasbor->buildDashboard($request->all());
$ringkasanResult = $this->dasbor->fetchRingkasan($request->only(['sektor', 'sektor_id', 'periode', 'kode_kabupaten', 'kode_kecamatan', 'kode_desa']));
$unavailable = $ringkasanResult->isUnavailable();
$meta = [
'unavailable' => $unavailable,
'empty' => false,
'message' => $unavailable ? $ringkasanResult->userMessage() : null,
'periode' => $request->input('periode'),
'periods' => $built['periods'] ?? [],
'indikator_terpilih' => $built['indikator_terpilih'] ?? null,
];
if ($unavailable) {
return response()->json([
'meta' => $meta,
'indikator' => [],
'kartu' => [],
'grafik' => ['per_kecamatan' => [], 'per_desa' => [], 'per_kategori' => []],
'tabel' => ['rows' => []],
], 200);
}
$rows = $built['tabel']['rows'] ?? [];
$meta['empty'] = count($rows) === 0;

return response()->json([
'meta' => $meta,
'indikator' => $built['indikator'] ?? [],
'kartu' => $built['kartu'] ?? [],
'grafik' => $built['grafik'] ?? ['per_kecamatan' => [], 'per_desa' => [], 'per_kategori' => []],
'tabel' => ['rows' => $rows],
], 200);
}

/**
* Daftar sektor untuk dropdown filter (kosong bila API tidak tersedia).
*
* @return array<int, array<string, string>>
*/
private function daftarSektor(): array
{
$result = $this->dataspasial->listSektor();
$out = [];
if (! $result->isData()) {
return $out;
}

foreach ($result->data as $item) {
$attrs = is_array($item) ? ($item['attributes'] ?? $item) : [];
$kode = $attrs['kode'] ?? null;
if ($kode === null || $kode === '') {
continue;
}
$out[] = [
'kode' => (string) $kode,
'nama' => (string) ($attrs['nama'] ?? $kode),
];
}

return $out;
}

public function ekspor(Request $request): mixed
{
$built = $this->dasbor->buildDashboard($request->all());
$rows = $built['tabel']['rows'] ?? [];
if ($rows === []) {
return response()->json(['success' => false, 'message' => 'Tidak ada data yang cocok dengan saringan.'], 422);
}

return response()
->view('dataspasial.dasbor.ekspor', ['rows' => $rows])
->header('Content-Type', 'application/vnd.ms-excel')
->header('Content-Disposition', 'attachment; filename="dasbor-dataspasial-'.date('Ymd-His').'.xls"');
}
}
50 changes: 47 additions & 3 deletions app/Http/Controllers/DataSpasial/PetaGabunganController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function data(Request $request): JsonResponse
$payload['features'] = $features;
$payload['meta'] = array_merge(
$payload['meta'],
$this->deriveMeta($features, $this->skemaIsian($sektor))
$this->deriveMeta($features, $this->isianUntukFitur($sektor, $features))
);
$payload['meta']['empty'] = count($features) === 0;

Expand All @@ -98,10 +98,11 @@ public function data(Request $request): JsonResponse
private function filters(Request $request): array
{
$filters = [
'sektor' => $this->stringInput($request, 'sektor'),
'sektor' => $this->stringInput($request, 'sektor') ?? $this->stringInput($request, 'sektor_id'),
'kode_kabupaten' => $this->stringInput($request, 'kode_kabupaten'),
'kode_kecamatan' => $this->stringInput($request, 'kode_kecamatan'),
'kode_desa' => $this->stringInput($request, 'kode_desa'),
'kode_desa' => $this->stringInput($request, 'kode_desa') ?? $this->stringInput($request, 'desa'),
'desa' => $this->stringInput($request, 'kode_desa') ?? $this->stringInput($request, 'desa'),
'periode' => $this->stringInput($request, 'periode'),
];

Expand Down Expand Up @@ -183,6 +184,42 @@ private function valueMatches(mixed $actual, mixed $expected): bool
return strcasecmp((string) $actual, (string) $expected) === 0;
}

/**
* Isian untuk popup/filter. Bila sektor tidak dipilih (mis. halaman Peta
* Gabungan awal), ambil isian dari seluruh sektor yang muncul pada fitur
* sehingga lokasi ber-DataSpasial tetap menampilkan informasinya.
*
* @param array<int, array<string, mixed>> $features
* @return array<int, array<string, mixed>>
*/
private function isianUntukFitur(?string $sektor, array $features): array
{
if ($sektor !== null) {
return $this->skemaIsian($sektor);
}

$sektorKode = [];
foreach ($features as $feature) {
$props = $feature['properties'] ?? [];
$kode = $props['sektor_kode'] ?? null;
if (is_array($props['dataspasial'] ?? null) && is_string($kode) && $kode !== '') {
$sektorKode[$kode] = true;
}
}

$merged = [];
foreach (array_slice(array_keys($sektorKode), 0, 10) as $kode) {
foreach ($this->skemaIsian((string) $kode) as $item) {
$key = (string) ($item['kode'] ?? '');
if ($key !== '' && ! isset($merged[$key])) {
$merged[$key] = $item;
}
}
}

return array_values($merged);
}

/**
* Metadata isian dari skema sektor aktif (isian ikut pada respons skema).
*
Expand Down Expand Up @@ -240,9 +277,16 @@ private function deriveMeta(array $features, array $isian): array
$key = (string) ($props['sektor_kode'] ?? 'dataspasial');
$label = (string) ($props['sektor_nama'] ?? $props['sektor_kode'] ?? 'DataSpasial');
$this->addLayer($layers, $key, $label, 'dataspasial');
$kategori = $props['kategori'] ?? $props['simbol'] ?? null;
if ($kategori) {
$this->addLayer($layers, 'kategori:'.(string) $kategori, (string) $kategori, 'kategori');
}
} else {
$key = (string) ($props['kategori'] ?? $props['simbol'] ?? 'lainnya');
$this->addLayer($layers, $key, $key, 'geospasial_lama');
if ($key !== '') {
$this->addLayer($layers, 'kategori:'.$key, $key, 'kategori');
}
}

$period = $dataspasial['periode'] ?? ($props['periode'] ?? null);
Expand Down
1 change: 1 addition & 0 deletions app/Policies/CustomCspPreset.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function configure(Policy $policy): void
'https://unpkg.com/leaflet.markercluster@1.5.3/dist/leaflet.markercluster.js',
'https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js',
'https://cdn.jsdelivr.net/npm/sweetalert2@11',
'https://cdn.jsdelivr.net/npm/apexcharts'
])
->addNonce(Directive::SCRIPT)
->add(Directive::FONT, [
Expand Down
Loading