*/ class LampiranHasilUjiController extends Controller { /** * Display and edit attachment form * * @param int $idMcIpal * @param string $bulan * @return \Illuminate\Http\Response */ public function edit($idMcIpal, $bulan) { // Check authorization if (!Gate::allows('pelaporan.view')) { abort(403); } // Get IPAL and pelaporan data $ipal = Ipal::getOneBy(['id' => $idMcIpal]); $pelaporan = Pelaporan::findOrFail($ipal->idmcpelaporan); // Determine if upload is allowed in current period $blnAwal = (($pelaporan->idrefperiodepelaporan - 1) * 3) + 1; $blnAkhir = $blnAwal + 3; $editable = Pelaporan::checkEditable($ipal->idmcpelaporan, 'al') && Gate::allows('pelaporan.edit'); if (($bulan >= $blnAwal && $bulan <= $blnAkhir) && $editable) { $enableUpload = 1; } else { $enableUpload = 0; } $data = [ 'idmcipal' => $idMcIpal, 'tahun' => $pelaporan->tahun, 'periode' => $pelaporan->periode, 'nama_ipal' => $ipal->nama, 'bulan' => $this->_mcMonth($bulan), 'enableUpload' => $enableUpload ]; return view('pelaporan.ipal.lampiran', $data); } /** * Upload files via dropzone * * @param Request $request * @param int $idMcIpal * @param string $bulan * @return void */ public function uploadFile(Request $request, $idMcIpal, $bulan) { // Check authorization if (!Gate::allows('pelaporan.edit')) { abort(403); } // Get IPAL and pelaporan data $ipal = Ipal::getOneBy(['id' => $idMcIpal]); $pelaporan = Pelaporan::findOrFail($ipal->idmcpelaporan); // Get upload directory $dirUpload = $this->_getDirectory($pelaporan->id, $idMcIpal, 'al', 'A6', $bulan); // Create directory if it doesn't exist if (!Storage::disk('public')->exists($dirUpload)) { Storage::disk('public')->makeDirectory($dirUpload, 0777, true); } // Handle file uploads if ($request->hasFile('file')) { $files = $request->file('file'); foreach ($files as $file) { $name = $file->getClientOriginalName(); $ext = $file->getClientOriginalExtension(); $baseName = pathinfo($name, PATHINFO_FILENAME); $newName = $baseName . '_' . date('Ymd_His') . '.' . $ext; $filePath = $file->storeAs($dirUpload, $newName, 'public'); $fullPath = storage_path('app/public/' . $filePath); // Set file permissions chmod($fullPath, 0777); // Save file data to database $data = [ 'idmcipal' => $idMcIpal, 'tahun' => $pelaporan->tahun, 'idrefperusahaan' => $pelaporan->idrefperusahaan, 'bulan' => $bulan, 'dokumen' => $fullPath, 'tanggalunggah' => Carbon::now()->format('Y-m-d H:i:s') ]; Ipal::saveLampiranHasilUji($data); } } } /** * List all uploaded files for dropzone * * @param Request $request * @return \Illuminate\Http\JsonResponse */ public function listFiles(Request $request) { // Check authorization if (!(Gate::allows('pelaporan.view') || Gate::allows('verifikasi.view'))) { abort(403); } // Get parameters from request $idMcIpal = $request->input('idmcipal'); $bulan = $request->input('bulan'); // Get IPAL and pelaporan data $ipal = Ipal::getOneBy(['id' => $idMcIpal]); $pelaporan = Pelaporan::findOrFail($ipal->idmcpelaporan); $result = []; // Get all attachments $allLampiran = Ipal::getLampiranHasilUji([ 'b.idrefipal' => $ipal->idrefipal, 'a.idrefperusahaan' => $pelaporan->idrefperusahaan, 'a.bulan' => $bulan, 'a.tahun' => $pelaporan->tahun ]); foreach ($allLampiran as $lampiran) { if (File::exists($lampiran->dokumen)) { $pathExplode = explode('/', $lampiran->dokumen); $obj = [ 'name' => end($pathExplode), 'size' => File::size($lampiran->dokumen), 'path' => $lampiran->dokumen ]; $result[] = $obj; } } return response()->json($result); } /** * Delete files from dropzone * * @param Request $request * @return void */ public function deleteFile(Request $request) { // Check authorization if (!Gate::allows('pelaporan.edit')) { abort(403); } // Get parameters from request $idMcIpal = $request->input('idmcipal'); $bulan = $request->input('bulan'); $fileName = $request->input('fileName'); // Get IPAL data $ipal = Ipal::getOneBy(['id' => $idMcIpal]); // Get upload directory $dirUpload = storage_path('app/public/') . $this->_getDirectory($ipal->idmcpelaporan, $idMcIpal, 'al', 'A6', $bulan); // Delete file $fullPath = $dirUpload . basename($fileName); if (File::exists($fullPath)) { File::delete($fullPath); } // Delete from database $filter = [ 'idmcipal' => $idMcIpal, 'bulan' => $bulan, 'dokumen' => $dirUpload . basename($fileName) ]; Ipal::deleteLampiranHasilUji($filter); } /** * Get directory for file storage * * @param int $idMcPelaporan * @param int $idMcIpal * @param string $kodeRefPelaporan * @param string $kodeKomponen * @param string $bulan * @return string */ private function _getDirectory($idMcPelaporan, $idMcIpal, $kodeRefPelaporan, $kodeKomponen, $bulan) { $pelaporan = Pelaporan::findOrFail($idMcPelaporan); $perusahaan = Pelaporan::getPerusahaan($pelaporan->idrefperusahaan); $ipal = Ipal::getOneBy(['id' => $idMcIpal]); $notAllowedChar = [' ', '.', '/', '-', ',', ':']; $noInduk = $perusahaan->nomorinduk; $noIpal = $ipal->nomor; $tahun = $pelaporan->tahun; $periode = strtolower(str_replace($notAllowedChar, '_', $pelaporan->periode)); $dirUpload = 'data_perusahaan/' . $noInduk . '/lampiran' . '/' . $kodeRefPelaporan . '/' . $kodeKomponen . '/' . $tahun . '_' . $periode . '/lampiran_hasiluji' . '/' . $bulan . '/' . $noIpal . '/'; return $dirUpload; } /** * Convert month number to name * * @param int $monthNum * @return string */ private function _mcMonth($monthNum) { $months = [ 1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December' ]; return $months[$monthNum] ?? ''; } }