view igr download issue solved -vadivel
This commit is contained in:
parent
1779f3ab06
commit
fd5690f745
@ -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,26 +1690,22 @@ function updateIGRWeight(){
|
||||
public function downloadFilesAsZip($igrno)
|
||||
{
|
||||
// Get all file details for the given IGR number
|
||||
$fileDetails = $this->inwardgateregister_model->getAllIgrFileDetails($igrno);
|
||||
|
||||
dd($fileDetails);
|
||||
|
||||
foreach($fileDetails as $file){
|
||||
if (empty($file->file)) {
|
||||
|
||||
$this->session->setFlashdata('message', 'There are no files.');
|
||||
|
||||
return redirect()->to('/ViewIGR');
|
||||
}
|
||||
}
|
||||
|
||||
$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 '<script>alert("Invalid file directory.");</script>';
|
||||
if (!mkdir($rootPath, 0755, true)) {
|
||||
echo '<script>alert("Failed to create the directory.");</script>';
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the directory is writable
|
||||
if (!is_writable($rootPath)) {
|
||||
echo '<script>alert("Directory is not writable.");</script>';
|
||||
return;
|
||||
}
|
||||
|
||||
@ -1700,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 '<script>alert("Failed to create ZIP file.");</script>';
|
||||
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 '<script>alert("Failed to add file: ' . $file->filename . '");</script>';
|
||||
$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 '<script>alert("No files were added to the ZIP.");</script>';
|
||||
redirect('ViewIGR', 'refresh');
|
||||
return;
|
||||
echo '<script> alert("File not found or is a directory: ' . $filePath . '");</script>';
|
||||
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 '<script>alert("Failed to create ZIP file");</script>';
|
||||
redirect('ViewIGR', 'refresh');
|
||||
return;
|
||||
// Close and remove any empty ZIP file created
|
||||
$zip->close();
|
||||
if (file_exists($zipFilePath)) {
|
||||
unlink($zipFilePath);
|
||||
}
|
||||
echo '<script>alert("No files were added to the ZIP.");</script>';
|
||||
return redirect()->to('/ViewIGR');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -251,21 +251,7 @@
|
||||
<td><?php echo $record->TransporterName ?></td>
|
||||
<td><?php echo $record->IGRStatus == 'ST074' ? 'Draft' : 'Completed'; ?></td>
|
||||
<td>
|
||||
<?php if(($record->IGRNO)){ ?>
|
||||
<a target="_blank" href="<?php echo base_url('download-files/' . $record->IGRNO); ?>">
|
||||
<i class="fa fa-download" style="text-align: center;"></i>
|
||||
</a>
|
||||
|
||||
<?php } ?>
|
||||
|
||||
<?php if (!empty($record->FilePath)) { ?>
|
||||
<!-- <a target="_blank" data-toggle="modal" data-target="#Fileshow" data-id="<%=index%>" title="Files" data-igrno="<?php echo $record->IGRNO ?>" data-pono="<?php echo $record->PONO ?>"><i class="fa fa-clone" style="text-align: center;"></i></a> -->
|
||||
<?php } else if (!empty($record->file)) { ?>
|
||||
<!-- <a target="_blank" data-toggle="modal" data-target="#Fileshow" data-id="<%=index%>" title="Files" data-igrno="<?php echo $record->IGRNO ?>" data-pono="<?php echo $record->PONO ?>"><i class="fa fa-clone" style="text-align: center;"></i></a> -->
|
||||
<?php } else { ?>
|
||||
<!-- <a target="_blank" data-toggle="modal" data-target="#Fileshow" data-id="<%=index%>" title="Files" data-igrno="<?php echo $record->IGRNO ?>" data-pono="<?php echo $record->PONO ?>"><i class="fa fa-clone" style="text-align: center;"></i></a> -->
|
||||
<?php } ?>
|
||||
|
||||
|
||||
<a target="_blank" data-toggle="modal" data-target="#Historyshow" data-id="<%=index%>" title="IGR History" data-igrno="<?php echo $record->IGRNO ?>" data-pono="<?php echo $record->PONO ?>">
|
||||
<i class="fa fa-history" style="text-align: center;"></i>
|
||||
</a>
|
||||
@ -273,7 +259,13 @@
|
||||
|
||||
|
||||
<!-- <a class="a_tag_for_mrir" href="<?php echo base_url().'MRIRcontroller/igrdatavalues?IGRNO='.$record->IGRNO; ?>" target="_blank" data-id="<%=index%>" title="Generate MRIR"><i class="fa fa-external-link" style="text-align: center;"></i></a> -->
|
||||
</td>
|
||||
<?php if(($record->isIgrFilePresent)){ ?>
|
||||
<a target="_blank" href="<?php echo base_url('download-files/' . $record->IGRNO); ?>">
|
||||
<i class="fa fa-download" style="text-align: center;"></i>
|
||||
</a>
|
||||
|
||||
<?php } ?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user