85 lines
3.2 KiB
PHP
85 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\CsvController;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Error;
|
|
use Illuminate\Support\Facades\Log;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Csv;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xls;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Ods;
|
|
use RuntimeException;
|
|
|
|
class CsvController extends Controller
|
|
{
|
|
private $csv_data_init;
|
|
|
|
private $csv_data_handling;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->csv_data_init = new CsvDataInit();
|
|
$this->csv_data_handling = new CsvDataHandling();
|
|
$this->third_party_integrations_allow = env('THIRD_PARTY_INTEGRATIONS_ALLOW', false);
|
|
}
|
|
|
|
function create_columns(array $issue, array $issue_time, array $company_agents = [])
|
|
{
|
|
try {
|
|
$array = $this->csv_data_init->initialize_csv_data($issue);
|
|
$array = $this->csv_data_init->handle_labels($array, $issue['labels']);
|
|
$array = $this->csv_data_handling->handle_csv_time($array, $issue, $issue_time, $company_agents);
|
|
return $array;
|
|
} catch (Error $e) {
|
|
Log::error('E-CSV-COLUMNS - ' . $e->getMessage());
|
|
throw new RuntimeException("E-CSV-GEN");
|
|
}
|
|
}
|
|
|
|
private function create_calc_document($reader, $file_name, $is_xls = false)
|
|
{
|
|
// Determine the appropriate writer based on the file type
|
|
$file_extension = $is_xls ? '.xls' : '.csv';
|
|
$writerClass = $is_xls ? Xls::class : Csv::class;
|
|
// Create the writer instance
|
|
$writer = new $writerClass($reader);
|
|
$file_name = $file_name . $file_extension;
|
|
$file_path = storage_path() . '/app/' . $file_name;
|
|
$writer->save($file_path);
|
|
|
|
//Define header information
|
|
header('Content-Description: File Transfer');
|
|
header('Content-Type: application/octet-stream');
|
|
header("Cache-Control: no-cache, must-revalidate");
|
|
header("Expires: 0");
|
|
header('Content-Disposition: attachment; filename="' . basename($file_name) . '"');
|
|
header('Content-Length: ' . filesize($file_path));
|
|
header('Pragma: public');
|
|
|
|
//Terminate from the script
|
|
return ['file_name' => $file_name, 'file_path' => $file_path];
|
|
}
|
|
|
|
function create_csv(string $issues_params_state, string $from_date, string $to_date, array $data)
|
|
{
|
|
try {
|
|
$file_name = $issues_params_state . "_issues_from_" . $from_date . "_to_" . $to_date;
|
|
$reader = new Spreadsheet();
|
|
$spreadsheet = $reader->getActiveSheet();
|
|
$headers = array_keys($data[0]);
|
|
$data_new = array_unshift($data, $headers);
|
|
$spreadsheet->fromArray(
|
|
$data
|
|
);
|
|
// The code is enclosed in a function for eventual support of multiple file types as xls and ods, as of now a csv will be generated
|
|
$csv_file = $this->create_calc_document($reader, $file_name);
|
|
// $ods_file = $this->third_party_integrations_allow ? $this->create_calc_document($reader, $file_name) : null;
|
|
return $csv_file;
|
|
} catch (Error $e) {
|
|
Log::error('E-CSV-GEN - ' . $e->getMessage());
|
|
throw new RuntimeException("E-CSV-GEN");
|
|
}
|
|
}
|
|
}
|