fungsi delete program
This commit is contained in:
@@ -9,6 +9,8 @@ use App\Models\Program;
|
||||
use App\Services\AuditLogService;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ProgramController extends Controller
|
||||
@@ -136,13 +138,53 @@ class ProgramController extends Controller
|
||||
{
|
||||
$this->authorize('delete', $program);
|
||||
|
||||
if ($program->attendances()->exists()) {
|
||||
return back()->with('error', 'Program tidak boleh dipadam kerana sudah ada rekod kehadiran.');
|
||||
}
|
||||
// Capture audit data before deletion
|
||||
$auditData = [
|
||||
'program_title' => $program->title,
|
||||
'start_date' => $program->start_date?->toDateString(),
|
||||
'end_date' => $program->end_date?->toDateString(),
|
||||
'program_created_at' => $program->created_at?->toDateTimeString(),
|
||||
'deleted_by' => auth()->user()->name,
|
||||
];
|
||||
|
||||
// Collect file paths before transaction
|
||||
$certFiles = $program->certificates()->pluck('file_path')->filter()->values()->all();
|
||||
$templateFiles = $program->certificateTemplate ? [$program->certificateTemplate->image_path] : [];
|
||||
$qrFiles = $program->qrCode ? [$program->qrCode->qr_image_path] : [];
|
||||
|
||||
$title = $program->title;
|
||||
AuditLogService::log('program.deleted', $program);
|
||||
$program->delete();
|
||||
|
||||
AuditLogService::log('program.deleted', $program, [], $auditData);
|
||||
|
||||
DB::transaction(function () use ($program) {
|
||||
$programId = $program->id;
|
||||
|
||||
DB::table('email_logs')->where('program_id', $programId)->delete();
|
||||
|
||||
// questionnaire_answers has FK to questionnaire_responses
|
||||
DB::table('questionnaire_answers')
|
||||
->whereIn('questionnaire_response_id', function ($q) use ($programId) {
|
||||
$q->select('id')->from('questionnaire_responses')->where('program_id', $programId);
|
||||
})
|
||||
->delete();
|
||||
|
||||
DB::table('questionnaire_responses')->where('program_id', $programId)->delete();
|
||||
DB::table('program_questionnaires')->where('program_id', $programId)->delete();
|
||||
DB::table('certificates')->where('program_id', $programId)->delete();
|
||||
DB::table('attendances')->where('program_id', $programId)->delete();
|
||||
DB::table('program_participants')->where('program_id', $programId)->delete();
|
||||
DB::table('certificate_templates')->where('program_id', $programId)->delete();
|
||||
DB::table('program_qr_codes')->where('program_id', $programId)->delete();
|
||||
|
||||
$program->delete();
|
||||
});
|
||||
|
||||
// Delete physical files after transaction
|
||||
foreach (array_merge($certFiles, $templateFiles, $qrFiles) as $path) {
|
||||
if ($path) {
|
||||
Storage::disk('local')->delete($path);
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()
|
||||
->route('admin.programs.index')
|
||||
|
||||
@@ -28,6 +28,13 @@
|
||||
</button>
|
||||
</form>
|
||||
@endif
|
||||
<form method="POST" action="{{ route('admin.programs.destroy', $program) }}" class="d-inline">
|
||||
@csrf @method('DELETE')
|
||||
<button class="btn btn-sm btn-danger"
|
||||
onclick="return confirm('AMARAN: Padam program "{{ addslashes($program->title) }}"?\n\nSemua data termasuk peserta, kehadiran, sijil dan soal selidik akan dipadam kekal.\n\nTindakan ini TIDAK BOLEH dibatalkan.')">
|
||||
<i class="bi bi-trash me-1"></i> Padam Program
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
|
||||
Reference in New Issue
Block a user