mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Fix #1262
This commit is contained in:
parent
e06361d5d7
commit
082b5ba895
@ -28,8 +28,8 @@ use FireflyIII\Import\Specifics\SpecificInterface;
|
|||||||
use FireflyIII\Models\ImportJob;
|
use FireflyIII\Models\ImportJob;
|
||||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Iterator;
|
|
||||||
use League\Csv\Reader;
|
use League\Csv\Reader;
|
||||||
|
use League\Csv\Statement;
|
||||||
use Log;
|
use Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -200,29 +200,35 @@ class CsvProcessor implements FileProcessorInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Iterator
|
* @return array
|
||||||
*
|
*
|
||||||
* @throws \League\Csv\Exception
|
* @throws \League\Csv\Exception
|
||||||
* @throws \League\Csv\Exception
|
* @throws \League\Csv\Exception
|
||||||
*/
|
*/
|
||||||
private function getImportArray(): Iterator
|
private function getImportArray(): array
|
||||||
{
|
{
|
||||||
$content = $this->repository->uploadFileContents($this->job);
|
$content = $this->repository->uploadFileContents($this->job);
|
||||||
$config = $this->getConfig();
|
$config = $this->getConfig();
|
||||||
$reader = Reader::createFromString($content);
|
$reader = Reader::createFromString($content);
|
||||||
$delimiter = $config['delimiter'] ?? ',';
|
$delimiter = $config['delimiter'] ?? ',';
|
||||||
$hasHeaders = isset($config['has-headers']) ? $config['has-headers'] : false;
|
$hasHeaders = isset($config['has-headers']) ? $config['has-headers'] : false;
|
||||||
|
$offset = 0;
|
||||||
if ('tab' === $delimiter) {
|
if ('tab' === $delimiter) {
|
||||||
$delimiter = "\t"; // @codeCoverageIgnore
|
$delimiter = "\t"; // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
$reader->setDelimiter($delimiter);
|
$reader->setDelimiter($delimiter);
|
||||||
if ($hasHeaders) {
|
if ($hasHeaders) {
|
||||||
$reader->setHeaderOffset(0); // @codeCoverageIgnore
|
$offset = 1;
|
||||||
|
}
|
||||||
|
$stmt = (new Statement)->offset($offset);
|
||||||
|
$records = $stmt->process($reader);
|
||||||
|
$return = [];
|
||||||
|
foreach ($records as $record) {
|
||||||
|
$return[] = $record;
|
||||||
}
|
}
|
||||||
$results = $reader->getRecords();
|
|
||||||
Log::debug('Created a CSV reader.');
|
Log::debug('Created a CSV reader.');
|
||||||
|
|
||||||
return $results;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -58,19 +58,22 @@ class Roles implements ConfigurationInterface
|
|||||||
$content = $this->repository->uploadFileContents($this->job);
|
$content = $this->repository->uploadFileContents($this->job);
|
||||||
$config = $this->getConfig();
|
$config = $this->getConfig();
|
||||||
$headers = [];
|
$headers = [];
|
||||||
|
$offset = 0;
|
||||||
|
|
||||||
// create CSV reader.
|
// create CSV reader.
|
||||||
$reader = Reader::createFromString($content);
|
$reader = Reader::createFromString($content);
|
||||||
$reader->setDelimiter($config['delimiter']);
|
$reader->setDelimiter($config['delimiter']);
|
||||||
|
|
||||||
|
// CSV headers. Ignore reader. Simply get the first row.
|
||||||
if ($config['has-headers']) {
|
if ($config['has-headers']) {
|
||||||
// get headers:
|
$offset = 1;
|
||||||
$reader->setHeaderOffset(0);
|
$stmt = (new Statement)->limit(1)->offset(0);
|
||||||
$headers = $reader->getHeader();
|
$records = $stmt->process($reader);
|
||||||
|
$headers = $records->fetchOne(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// example rows:
|
// example rows:
|
||||||
$stmt = (new Statement)->limit(intval(config('csv.example_rows', 5)))->offset(0);
|
$stmt = (new Statement)->limit(intval(config('csv.example_rows', 5)))->offset($offset);
|
||||||
// set data:
|
// set data:
|
||||||
$roles = $this->getRoles();
|
$roles = $this->getRoles();
|
||||||
asort($roles);
|
asort($roles);
|
||||||
|
Loading…
Reference in New Issue
Block a user