Update Pendidikan Pelatihan
This commit is contained in:
@@ -5,20 +5,159 @@ namespace App\Http\Controllers\Web\Produktivitas;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
use App\Models\Pertanian\Petani;
|
||||
use App\Models\Pertanian\SurveyPelatihan;
|
||||
use App\Models\Pertanian\SurveySertifikasi;
|
||||
|
||||
class PendidikanPelatihanController extends Controller
|
||||
{
|
||||
public function index($petaniId, Request $request)
|
||||
private function authorizeAccess(): void
|
||||
{
|
||||
if (!Gate::any(['super_admin', 'admin', 'fasilitator'])) {
|
||||
abort(403);
|
||||
}
|
||||
}
|
||||
|
||||
public function index(Petani $petani)
|
||||
{
|
||||
$this->authorizeAccess();
|
||||
|
||||
$title = 'Petani - Pendidikan dan Pelatihan';
|
||||
$petani = Petani::findOrFail($petaniId);
|
||||
|
||||
return view('produktivitas.petani.pendidikan-pelatihan.add', compact('title', 'petani'));
|
||||
|
||||
$petani->load([
|
||||
'surveyPelatihan',
|
||||
'surveySertifikasi',
|
||||
]);
|
||||
|
||||
return view('produktivitas.petani.pendidikan-pelatihan.index', compact(
|
||||
'title',
|
||||
'petani'
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public function storePelatihan(Request $request, Petani $petani)
|
||||
{
|
||||
$this->authorizeAccess();
|
||||
|
||||
$validatedData = $request->validate([
|
||||
'riwayat_pelatihan' => ['nullable', 'array'],
|
||||
|
||||
'riwayat_pelatihan.*.jenis_pelatihan' => ['nullable', 'string', 'max:255'],
|
||||
'riwayat_pelatihan.*.penyelenggara' => ['nullable', 'string', 'max:255'],
|
||||
'riwayat_pelatihan.*.tahun' => ['nullable', 'integer', 'min:1900', 'max:' . date('Y')],
|
||||
], [
|
||||
'riwayat_pelatihan.*.tahun.integer' => 'Tahun pelatihan harus berupa angka.',
|
||||
'riwayat_pelatihan.*.tahun.min' => 'Tahun pelatihan tidak valid.',
|
||||
'riwayat_pelatihan.*.tahun.max' => 'Tahun pelatihan tidak boleh melebihi tahun saat ini.',
|
||||
], [
|
||||
'riwayat_pelatihan.*.jenis_pelatihan' => 'jenis pelatihan',
|
||||
'riwayat_pelatihan.*.penyelenggara' => 'penyelenggara',
|
||||
'riwayat_pelatihan.*.tahun' => 'tahun pelatihan',
|
||||
]);
|
||||
|
||||
foreach ($validatedData['riwayat_pelatihan'] ?? [] as $item) {
|
||||
if (
|
||||
empty($item['jenis_pelatihan']) &&
|
||||
empty($item['penyelenggara']) &&
|
||||
empty($item['tahun'])
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$petani->surveyPelatihan()->create([
|
||||
'uid' => 'SPL-' . strtoupper(Str::random(10)),
|
||||
'petani_id' => $petani->id,
|
||||
'jenis_pelatihan' => $item['jenis_pelatihan'] ?? null,
|
||||
'penyelenggara' => $item['penyelenggara'] ?? null,
|
||||
'tahun_pelaksanaan' => $item['tahun'] ?? null,
|
||||
]);
|
||||
}
|
||||
|
||||
return back()->with('success', 'Riwayat pelatihan berhasil disimpan.');
|
||||
}
|
||||
|
||||
public function destroyPelatihan(Petani $petani, SurveyPelatihan $surveyPelatihan)
|
||||
{
|
||||
$this->authorizeAccess();
|
||||
|
||||
abort_unless($surveyPelatihan->petani_id === $petani->id, 404);
|
||||
|
||||
$surveyPelatihan->delete();
|
||||
|
||||
return back()->with('success', 'Riwayat pelatihan berhasil dihapus.');
|
||||
}
|
||||
|
||||
public function storeSertifikasi(Request $request, Petani $petani)
|
||||
{
|
||||
$this->authorizeAccess();
|
||||
|
||||
$sertifikasi = $request->input('sertifikasi', []);
|
||||
|
||||
foreach ($sertifikasi as $index => $item) {
|
||||
$status = $item['status_berlaku_sertifikat'] ?? 0;
|
||||
|
||||
if (is_array($status)) {
|
||||
$status = in_array('1', $status, true) ? 1 : 0;
|
||||
} else {
|
||||
$status = $status == 1 ? 1 : 0;
|
||||
}
|
||||
$sertifikasi[$index]['status_berlaku_sertifikat'] = $status;
|
||||
}
|
||||
|
||||
$request->merge([
|
||||
'sertifikasi' => $sertifikasi,
|
||||
]);
|
||||
|
||||
$validatedData = $request->validate([
|
||||
'sertifikasi' => ['nullable', 'array'],
|
||||
|
||||
'sertifikasi.*.jenis_sertifikasi' => ['nullable', 'string', 'max:255'],
|
||||
'sertifikasi.*.lembaga_penerbit' => ['nullable', 'string', 'max:255'],
|
||||
'sertifikasi.*.tahun' => ['nullable', 'integer', 'min:1900', 'max:' . date('Y')],
|
||||
'sertifikasi.*.status_berlaku_sertifikat' => ['nullable', 'boolean'],
|
||||
], [
|
||||
'sertifikasi.*.tahun.integer' => 'Tahun sertifikasi harus berupa angka.',
|
||||
'sertifikasi.*.tahun.min' => 'Tahun sertifikasi tidak valid.',
|
||||
'sertifikasi.*.tahun.max' => 'Tahun sertifikasi tidak boleh melebihi tahun saat ini.',
|
||||
], [
|
||||
'sertifikasi.*.jenis_sertifikasi' => 'jenis sertifikasi',
|
||||
'sertifikasi.*.lembaga_penerbit' => 'lembaga penerbit',
|
||||
'sertifikasi.*.tahun' => 'tahun sertifikasi',
|
||||
'sertifikasi.*.status_berlaku_sertifikat' => 'status sertifikat',
|
||||
]);
|
||||
|
||||
foreach ($validatedData['sertifikasi'] ?? [] as $item) {
|
||||
if (
|
||||
empty($item['jenis_sertifikasi']) &&
|
||||
empty($item['lembaga_penerbit']) &&
|
||||
empty($item['tahun'])
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$petani->surveySertifikasi()->create([
|
||||
'uid' => 'SPS-' . strtoupper(Str::random(10)),
|
||||
'petani_id' => $petani->id,
|
||||
'jenis_sertifikasi' => $item['jenis_sertifikasi'] ?? null,
|
||||
'lembaga_penerbit' => $item['lembaga_penerbit'] ?? null,
|
||||
'tahun_pelaksanaan' => $item['tahun'] ?? null,
|
||||
'is_aktif' => $item['status_berlaku_sertifikat'] ?? 0,
|
||||
]);
|
||||
}
|
||||
|
||||
return back()->with('success', 'Sertifikasi berhasil disimpan.');
|
||||
}
|
||||
|
||||
public function destroySertifikasi(Petani $petani, SurveySertifikasi $surveySertifikasi)
|
||||
{
|
||||
$this->authorizeAccess();
|
||||
|
||||
abort_unless($surveySertifikasi->petani_id === $petani->id, 404);
|
||||
|
||||
$surveySertifikasi->delete();
|
||||
|
||||
return back()->with('success', 'Sertifikasi berhasil dihapus.');
|
||||
}
|
||||
}
|
||||
@@ -108,6 +108,16 @@ class Petani extends Model
|
||||
return $this->hasOne(surveyPanen::class, 'petani_id')->latestOfMany();
|
||||
}
|
||||
|
||||
public function surveyPelatihan(): HasMany
|
||||
{
|
||||
return $this->hasMany(surveyPelatihan::class, 'petani_id');
|
||||
}
|
||||
|
||||
public function surveySertifikasi(): HasMany
|
||||
{
|
||||
return $this->hasMany(surveySertifikasi::class, 'petani_id');
|
||||
}
|
||||
|
||||
public function surveyProduktivitas(): HasOne
|
||||
{
|
||||
return $this->hasOne(surveyProduktivitas::class, 'petani_id')->latestOfMany();
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Pertanian;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
use App\Models\Pertanian\Petani;
|
||||
|
||||
class SurveyPelatihan extends Model
|
||||
{
|
||||
use HasFactory, SoftDeletes;
|
||||
|
||||
protected $table = 'survey_pelatihan';
|
||||
|
||||
protected $fillable = [
|
||||
'uid',
|
||||
'petani_id',
|
||||
'jenis_pelatihan',
|
||||
'penyelenggara',
|
||||
'tahun_pelaksanaan',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
'deleted_by',
|
||||
];
|
||||
|
||||
public function petani(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Petani::class, 'petani_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Pertanian;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class SurveySertifikasi extends Model
|
||||
{
|
||||
use HasFactory, SoftDeletes;
|
||||
|
||||
protected $table = 'survey_sertifikasi';
|
||||
|
||||
protected $fillable = [
|
||||
'uid',
|
||||
'petani_id',
|
||||
'jenis_sertifikasi',
|
||||
'lembaga_penerbit',
|
||||
'tahun_pelaksanaan',
|
||||
'is_aktif',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
'deleted_by',
|
||||
];
|
||||
|
||||
public function petani()
|
||||
{
|
||||
return $this->belongsTo(Petani::class, 'petani_id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user