clrghouz/app/Traits/Import.php

81 lines
1.7 KiB
PHP
Raw Normal View History

2021-06-25 11:31:57 +00:00
<?php
/**
* Import from files
*/
namespace App\Traits;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
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
* Assumes $file is compressed with ZIP
2021-06-25 11:31:57 +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;
}
private function openFile(string $file): \ZipArchive
{
$z = new \ZipArchive;
if ($z->open($file,\ZipArchive::RDONLY) === TRUE) {
return $z;
} else {
2024-06-15 04:23:05 +00:00
throw new \Exception(sprintf('%s:! Failed opening ZipArchive [%s] (%s)',self::LOGKEY,$file,$z->getStatusString()));
}
}
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;
}
private function getFileFromHost(string $key,mixed $file): string
2021-06-25 11:31:57 +00:00
{
if ($file instanceof File) {
$path = sprintf('import/%s.%d',$key,$file->id);
2021-06-25 11:31:57 +00:00
Storage::disk(config('fido.local_disk'))->put($path,Storage::get($file->rel_name));
2021-06-25 11:31:57 +00:00
return Storage::disk(config('fido.local_disk'))->path($path);
} else {
return Storage::disk(config('fido.local_disk'))->path($file);
}
2021-06-25 11:31:57 +00:00
}
}