diff --git a/app/Controllers/Inwardgateregister.php b/app/Controllers/Inwardgateregister.php index 80ab9aca..516b46ac 100755 --- a/app/Controllers/Inwardgateregister.php +++ b/app/Controllers/Inwardgateregister.php @@ -67,8 +67,26 @@ class Inwardgateregister extends BaseController $this->global['pageTitle'] = 'View Inward Gate Register'; - $result = $this->inwardgateregister_model->ViewigrListing($fromDate,$toDate); - $data['IGR'] = $result[0]; + $result = $this->inwardgateregister_model->ViewigrListing($fromDate, $toDate); + $igrList = $result[0]; + + foreach ($igrList as &$igr) { + $igrNo = $igr->IGRNO; + $fileDetails = $this->inwardgateregister_model->getAllIgrFileDetails($igrNo); + $igr->isIgrFilePresent = false; // Initialize the key + + foreach ($fileDetails as $file) { + if (!empty($file->file)) { + $igr->isIgrFilePresent = true; + break; + } + } + } + + // Unset reference to avoid side effects + unset($igr); + + $data['IGR'] = $igrList; $data['IGRDownload'] = $result[1]; $data['userRole'] = $this->session->get('role'); $data['transporter'] = $this->inwardgateregister_model->transporter(); @@ -1672,19 +1690,22 @@ function updateIGRWeight(){ public function downloadFilesAsZip($igrno) { // Get all file details for the given IGR number - $fileDetails = $this->inwardgateregister_model->getAllIgrFileDetails($igrno); - if (empty($fileDetails)) { - echo ''; - redirect('ViewIGR', 'refresh'); - return; - } + $fileDetails = $this->inwardgateregister_model->getAllIgrFileDetails($igrno); // Define the root path for files $rootPath = ROOTPATH . 'public/uploads/Igrfiles/'; - - // Check if root path is valid + + // Check if the directory exists, create if it doesn't if (!is_dir($rootPath)) { - echo ''; + if (!mkdir($rootPath, 0755, true)) { + echo ''; + return; + } + } + + // Ensure the directory is writable + if (!is_writable($rootPath)) { + echo ''; return; } @@ -1693,49 +1714,50 @@ function updateIGRWeight(){ $zipFileName = $igrno . '.zip'; $zipFilePath = $rootPath . $zipFileName; - if ($zip->open($zipFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) { - $filesAdded = false; + // Attempt to open the ZIP file + if ($zip->open($zipFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE) { + echo ''; + return redirect()->to('/ViewIGR'); + } - // Add each file to the ZIP archive - foreach ($fileDetails as $file) { - $filePath = $rootPath . $file->file; + $filesAdded = false; - // Check if file exists - if (!file_exists($filePath)) { - log_message('error', 'File not found: ' . $filePath); - continue; - } + // Add each file to the ZIP archive + foreach ($fileDetails as $file) { + $filePath = $rootPath . $file->file; + // Ensure file exists and is not a directory + if (file_exists($filePath) && is_file($filePath)) { + // Sanitize filename for ZIP $sanitizedFilename = preg_replace('/[^a-zA-Z0-9_\-\.]/', '_', $file->filename); - if (!$zip->addFile($filePath, $sanitizedFilename)) { - log_message('error', 'Failed to add file: ' . $sanitizedFilename); + echo ''; $zip->close(); - redirect('ViewIGR', 'refresh'); - return; + return redirect()->to('/ViewIGR'); } $filesAdded = true; - } - - // Close the ZIP archive - $zip->close(); - - if ($filesAdded) { - return $this->response->download($zipFilePath, null)->setFileName($zipFileName); } else { - if (file_exists($zipFilePath)) { - unlink($zipFilePath); - } - echo ''; - redirect('ViewIGR', 'refresh'); - return; + echo ''; + continue; } + } + + // Close ZIP archive if files were added + if ($filesAdded) { + $zip->close(); // Close ZIP only if files were added + return $this->response->download($zipFilePath, null)->setFileName($zipFileName); } else { - echo ''; - redirect('ViewIGR', 'refresh'); - return; + // Close and remove any empty ZIP file created + $zip->close(); + if (file_exists($zipFilePath)) { + unlink($zipFilePath); + } + echo ''; + return redirect()->to('/ViewIGR'); } } + + } \ No newline at end of file diff --git a/app/Views/viewinwardgateregister.php b/app/Views/viewinwardgateregister.php index d410e5ee..1d5956e2 100755 --- a/app/Views/viewinwardgateregister.php +++ b/app/Views/viewinwardgateregister.php @@ -165,6 +165,12 @@ + getFlashdata('message')): ?> +