222 lines
6.7 KiB
PHP
222 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Web\Program;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
use App\Models\Program\Framework;
|
|
use App\Models\Program\Transformasi;
|
|
use App\Models\Program\Aktivitas;
|
|
use App\Models\Program\Target;
|
|
|
|
class TargetController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
//
|
|
}
|
|
|
|
private function validated(Request $request) {
|
|
return $request->validate([
|
|
'variabel' => ['required','string','max:255'],
|
|
'tipe' => ['required','string','max:255', Rule::in(['integer', 'float', 'boolean']),],
|
|
'target_integer' => [
|
|
Rule::requiredIf($request->tipe === 'integer'),
|
|
'nullable',
|
|
'integer'
|
|
],
|
|
'target_float' => [
|
|
Rule::requiredIf($request->tipe === 'float'),
|
|
'nullable',
|
|
'numeric'
|
|
],
|
|
'target_boolean' => [
|
|
Rule::requiredIf($request->tipe === 'boolean'),
|
|
'nullable',
|
|
'boolean'
|
|
],
|
|
'baseline_integer' => [
|
|
Rule::requiredIf($request->tipe === 'integer'),
|
|
'nullable',
|
|
'integer'
|
|
],
|
|
'baseline_float' => [
|
|
Rule::requiredIf($request->tipe === 'float'),
|
|
'nullable',
|
|
'numeric'
|
|
],
|
|
'baseline_boolean' => [
|
|
Rule::requiredIf($request->tipe === 'boolean'),
|
|
'nullable',
|
|
'boolean'
|
|
],
|
|
'realisasi_integer' => [
|
|
'nullable',
|
|
'integer'
|
|
],
|
|
'realisasi_float' => [
|
|
'nullable',
|
|
'numeric'
|
|
],
|
|
'realisasi_boolean' => [
|
|
'nullable',
|
|
'boolean'
|
|
],
|
|
'tipe_realisasi' => [
|
|
'nullable',
|
|
'string',
|
|
Rule::in(['manual', 'referensi'])
|
|
],
|
|
'value_query' => [
|
|
Rule::requiredIf($request->tipe_realisasi === 'referensi'),
|
|
'nullable',
|
|
'string'
|
|
]
|
|
]);
|
|
}
|
|
|
|
public function create(string $frameworkId, string $transformasiId, string $aktivitasId)
|
|
{
|
|
|
|
}
|
|
|
|
public function store(Request $request, string $frameworkId, string $transformasiId, string $aktivitasId): JsonResponse
|
|
{
|
|
if (!Gate::any(['super_admin', 'admin'])) {
|
|
abort(403);
|
|
}
|
|
|
|
$aktivitas = Aktivitas::whereHas('transformasi', function ($query) use ($frameworkId) {
|
|
$query->where('framework_id', $frameworkId);
|
|
})->where('transformasi_id', $transformasiId)->find($aktivitasId);
|
|
if (!$aktivitas) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'message' => 'Data aktivitas tidak ditemukan.'
|
|
], 404);
|
|
}
|
|
|
|
$validated = $this->validated($request);
|
|
Target::create([...$validated,
|
|
'aktivitas_id' => $aktivitasId,
|
|
'created_by' => auth()->id(),
|
|
'updated_by' => auth()->id(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'message' => 'Target berhasil ditambahkan.'
|
|
], 201);
|
|
}
|
|
|
|
public function show(string $frameworkId, string $transformasiId, string $aktivitasId, string $id)
|
|
{
|
|
if (!Gate::any(['super_admin', 'admin'])) {
|
|
abort(403);
|
|
}
|
|
|
|
$target = Target::with('realisasi', 'latestRealisasi')
|
|
->whereHas('aktivitas.transformasi', function ($query) use ($frameworkId) {
|
|
$query->where('framework_id', $frameworkId);
|
|
})
|
|
->whereHas('aktivitas', function ($query) use ($transformasiId) {
|
|
$query->where('transformasi_id', $transformasiId);
|
|
})
|
|
->where('aktivitas_id', $aktivitasId)->find($id);
|
|
|
|
if (!$target) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'message' => 'Data target tidak ditemukan.'
|
|
], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'data' => $target
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, string $frameworkId, string $transformasiId, string $aktivitasId, string $id)
|
|
{
|
|
if (!Gate::any(['super_admin', 'admin'])) {
|
|
abort(403);
|
|
}
|
|
|
|
$target = Target::whereHas('aktivitas.transformasi', function ($query) use ($frameworkId) {
|
|
$query->where('framework_id', $frameworkId);
|
|
})
|
|
->whereHas('aktivitas', function ($query) use ($transformasiId) {
|
|
$query->where('transformasi_id', $transformasiId);
|
|
})
|
|
->where('aktivitas_id', $aktivitasId)->find($id);
|
|
|
|
if (!$target) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'message' => 'Data target tidak ditemukan.'
|
|
], 404);
|
|
}
|
|
|
|
$validated = $this->validated($request);
|
|
$target->update([...$validated,
|
|
'updated_by' => auth()->id(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'message' => 'Target berhasil diperbarui.'
|
|
], 200);
|
|
}
|
|
|
|
public function destroy(string $frameworkId, string $transformasiId, string $aktivitasId, string $id)
|
|
{
|
|
if (!Gate::any(['super_admin', 'admin'])) {
|
|
abort(403);
|
|
}
|
|
|
|
$target = Target::whereHas('aktivitas.transformasi', function ($query) use ($frameworkId) {
|
|
$query->where('framework_id', $frameworkId);
|
|
})
|
|
->whereHas('aktivitas', function ($query) use ($transformasiId) {
|
|
$query->where('transformasi_id', $transformasiId);
|
|
})
|
|
->where('aktivitas_id', $aktivitasId)->find($id);
|
|
|
|
if (!$target) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'message' => 'Data target tidak ditemukan.'
|
|
], 404);
|
|
}
|
|
|
|
$target->update([
|
|
'deleted_by' => auth()->id(),
|
|
]);
|
|
$target->delete();
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'message' => 'Target berhasil dihapus.'
|
|
], 200);
|
|
}
|
|
}
|