157 lines
5.2 KiB
PHP
157 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Setup;
|
|
|
|
use App\Http\Concerns\SortsQuery;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\Setup\AssignPpmRequest;
|
|
use App\Http\Requests\Admin\Setup\UpsertPusatMengundiRequest;
|
|
use App\Models\Dun;
|
|
use App\Models\Election;
|
|
use App\Models\Position;
|
|
use App\Models\PusatMengundi;
|
|
use App\Models\User;
|
|
use App\Services\Admin\PpmAssignmentService;
|
|
use App\Services\Admin\PusatOperationalSetupService;
|
|
use App\Services\Admin\PusatQrCodeService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\View\View;
|
|
|
|
class PusatMengundiController extends Controller
|
|
{
|
|
use SortsQuery;
|
|
|
|
public function index(): View
|
|
{
|
|
return view('admin.setup.pusat.index', [
|
|
'pusats' => $this->sortedQuery(
|
|
PusatMengundi::query()->with(['election', 'dun', 'saluranMengundis']),
|
|
['code', 'name'],
|
|
'code'
|
|
)->paginate(15)->withQueryString(),
|
|
]);
|
|
}
|
|
|
|
public function create(): View
|
|
{
|
|
return view('admin.setup.pusat.create', [
|
|
'pusat' => new PusatMengundi,
|
|
'elections' => Election::query()->orderBy('name')->get(),
|
|
'duns' => Dun::query()->orderBy('name')->get(),
|
|
]);
|
|
}
|
|
|
|
public function store(
|
|
UpsertPusatMengundiRequest $request,
|
|
PusatQrCodeService $qrCodeService,
|
|
PusatOperationalSetupService $operationalSetupService,
|
|
): RedirectResponse {
|
|
$pusat = DB::transaction(function () use ($request, $operationalSetupService): PusatMengundi {
|
|
$pusat = PusatMengundi::query()->create([
|
|
...$request->safe()->only([
|
|
'election_id',
|
|
'dun_id',
|
|
'code',
|
|
'name',
|
|
'address',
|
|
]),
|
|
'public_uuid' => (string) Str::uuid(),
|
|
]);
|
|
|
|
$operationalSetupService->configure($pusat, $request->validated());
|
|
|
|
return $pusat;
|
|
});
|
|
|
|
$qrCodeService->generate($pusat);
|
|
|
|
return redirect()->route('admin.setup.pusat.index')
|
|
->with('status', 'Pusat Mengundi telah disimpan dan QR telah dijana.');
|
|
}
|
|
|
|
public function show(PusatMengundi $pusatMengundi): View
|
|
{
|
|
$ppmPosition = Position::query()->where('code', 'PPM')->first();
|
|
|
|
return view('admin.setup.pusat.show', [
|
|
'pusat' => $pusatMengundi->load([
|
|
'election',
|
|
'dun',
|
|
'saluranMengundis',
|
|
'positionQuotas.position',
|
|
]),
|
|
'ppmAssignment' => $ppmPosition
|
|
? $pusatMengundi->staffAssignments()
|
|
->with('user')
|
|
->where('position_id', $ppmPosition->id)
|
|
->where('status', 'active')
|
|
->whereNull('saluran_mengundi_id')
|
|
->first()
|
|
: null,
|
|
'users' => User::query()->orderBy('name')->get(),
|
|
]);
|
|
}
|
|
|
|
public function edit(PusatMengundi $pusatMengundi): View
|
|
{
|
|
return view('admin.setup.pusat.edit', [
|
|
'pusat' => $pusatMengundi,
|
|
'elections' => Election::query()->orderBy('name')->get(),
|
|
'duns' => Dun::query()->orderBy('name')->get(),
|
|
]);
|
|
}
|
|
|
|
public function update(UpsertPusatMengundiRequest $request, PusatMengundi $pusatMengundi): RedirectResponse
|
|
{
|
|
$pusatMengundi->update($request->safe()->only([
|
|
'election_id',
|
|
'dun_id',
|
|
'code',
|
|
'name',
|
|
'address',
|
|
]));
|
|
|
|
return redirect()->route('admin.setup.pusat.show', $pusatMengundi)
|
|
->with('status', 'Pusat Mengundi telah dikemas kini.');
|
|
}
|
|
|
|
public function destroy(PusatMengundi $pusatMengundi): RedirectResponse
|
|
{
|
|
$pusatMengundi->forceDelete();
|
|
|
|
return redirect()->route('admin.setup.pusat.index')
|
|
->with('status', 'Pusat Mengundi telah dipadam.');
|
|
}
|
|
|
|
public function generateQr(PusatMengundi $pusatMengundi, PusatQrCodeService $qrCodeService): RedirectResponse
|
|
{
|
|
$qrCodeService->generate($pusatMengundi);
|
|
|
|
return redirect()->route('admin.setup.pusat.show', $pusatMengundi)
|
|
->with('status', 'QR pendaftaran telah dijana semula.');
|
|
}
|
|
|
|
public function qrSvg(PusatMengundi $pusatMengundi): Response
|
|
{
|
|
abort_unless($pusatMengundi->qr_code_path && Storage::disk('local')->exists($pusatMengundi->qr_code_path), 404);
|
|
|
|
return response(Storage::disk('local')->get($pusatMengundi->qr_code_path), 200, [
|
|
'Content-Type' => 'image/svg+xml',
|
|
]);
|
|
}
|
|
|
|
public function assignPpm(AssignPpmRequest $request, PusatMengundi $pusatMengundi, PpmAssignmentService $assignmentService): RedirectResponse
|
|
{
|
|
$user = User::query()->findOrFail($request->validated('user_id'));
|
|
|
|
$assignmentService->assign($pusatMengundi, $user);
|
|
|
|
return redirect()->route('admin.setup.pusat.show', $pusatMengundi)
|
|
->with('status', 'PPM telah ditetapkan.');
|
|
}
|
|
}
|