2021-06-25 11:31:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import from files
|
|
|
|
*/
|
|
|
|
namespace App\Traits;
|
|
|
|
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
2022-11-04 11:26:08 +00:00
|
|
|
use App\Models\File;
|
|
|
|
|
2021-06-25 11:31:57 +00:00
|
|
|
trait Import
|
|
|
|
{
|
|
|
|
protected Collection $_columns; // Columns in the Import File
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count the lines in a file
|
2022-11-04 11:26:08 +00:00
|
|
|
* Assumes $file is compressed with ZIP
|
2021-06-25 11:31:57 +00:00
|
|
|
*/
|
2024-06-15 05:12:09 +00:00
|
|
|
private function getFileLines(mixed $f): int
|
2021-06-25 11:31:57 +00:00
|
|
|
{
|
|
|
|
$c = 0;
|
|
|
|
|
|
|
|
while (! feof($f)) {
|
|
|
|
fgets($f);
|
|
|
|
$c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $c;
|
|
|
|
}
|
|
|
|
|
2024-06-15 05:12:09 +00:00
|
|
|
private function openFile(string $file): \ZipArchive
|
2022-11-04 11:26:08 +00:00
|
|
|
{
|
|
|
|
$z = new \ZipArchive;
|
|
|
|
|
2023-07-09 12:03:15 +00:00
|
|
|
if ($z->open($file,\ZipArchive::RDONLY) === TRUE) {
|
2024-06-15 05:12:09 +00:00
|
|
|
return $z;
|
2022-11-04 11:26:08 +00:00
|
|
|
|
|
|
|
} else {
|
2024-06-15 04:23:05 +00:00
|
|
|
throw new \Exception(sprintf('%s:! Failed opening ZipArchive [%s] (%s)',self::LOGKEY,$file,$z->getStatusString()));
|
2022-11-04 11:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 11:31:57 +00:00
|
|
|
/**
|
|
|
|
* Return the columns from the file that we'll work with
|
|
|
|
*
|
|
|
|
* @param string $line
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
private function getColumns(string $line): Collection
|
|
|
|
{
|
|
|
|
$this->_columns = collect(explode(',',strtoupper($line)))->filter();
|
|
|
|
return $this->_columns->intersect($this->columns);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the index for the column in the file
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return int|null
|
|
|
|
*/
|
|
|
|
private function getColumnKey(string $key): ?int
|
|
|
|
{
|
|
|
|
return ($x=$this->_columns->search(strtoupper($this->columns->get($key)))) !== FALSE ? $x : NULL;
|
|
|
|
}
|
|
|
|
|
2023-07-09 12:03:15 +00:00
|
|
|
private function getFileFromHost(string $key,mixed $file): string
|
2021-06-25 11:31:57 +00:00
|
|
|
{
|
2023-07-09 12:03:15 +00:00
|
|
|
if ($file instanceof File) {
|
|
|
|
$path = sprintf('import/%s.%d',$key,$file->id);
|
2021-06-25 11:31:57 +00:00
|
|
|
|
2023-09-23 12:01:18 +00:00
|
|
|
Storage::disk(config('fido.local_disk'))->put($path,Storage::get($file->rel_name));
|
2021-06-25 11:31:57 +00:00
|
|
|
|
2023-09-23 12:01:18 +00:00
|
|
|
return Storage::disk(config('fido.local_disk'))->path($path);
|
2023-07-09 12:03:15 +00:00
|
|
|
|
|
|
|
} else {
|
2023-09-23 12:01:18 +00:00
|
|
|
return Storage::disk(config('fido.local_disk'))->path($file);
|
2023-07-09 12:03:15 +00:00
|
|
|
}
|
2021-06-25 11:31:57 +00:00
|
|
|
}
|
|
|
|
}
|