Implemented option to choose field delimiter in CSV import

This commit is contained in:
Robert Horlings
2016-01-15 15:24:07 +01:00
parent 8e0e9734a5
commit 61a703e605
7 changed files with 87 additions and 24 deletions

View File

@@ -1,5 +1,4 @@
<?php <?php
namespace FireflyIII\Helpers\Csv; namespace FireflyIII\Helpers\Csv;
use Crypt; use Crypt;
@@ -17,19 +16,27 @@ class Data
/** @var string */ /** @var string */
protected $csvFileContent; protected $csvFileContent;
/** @var string */
protected $delimiter;
/** @var string */ /** @var string */
protected $csvFileLocation; protected $csvFileLocation;
/** @var string */ /** @var string */
protected $dateFormat; protected $dateFormat;
/** @var bool */ /** @var bool */
protected $hasHeaders; protected $hasHeaders;
/** @var array */ /** @var array */
protected $map = []; protected $map = [];
/** @var array */ /** @var array */
protected $mapped = []; protected $mapped = [];
/** @var Reader */ /** @var Reader */
protected $reader; protected $reader;
/** @var array */ /** @var array */
protected $roles = []; protected $roles = [];
@@ -40,7 +47,6 @@ class Data
protected $importAccount = 0; protected $importAccount = 0;
/** /**
*
*/ */
public function __construct() public function __construct()
{ {
@@ -52,12 +58,13 @@ class Data
$this->sessionMapped(); $this->sessionMapped();
$this->sessionSpecifix(); $this->sessionSpecifix();
$this->sessionImportAccount(); $this->sessionImportAccount();
$this->sessionDelimiter();
} }
protected function sessionHasHeaders() protected function sessionHasHeaders()
{ {
if (Session::has('csv-has-headers')) { if (Session::has('csv-has-headers')) {
$this->hasHeaders = (bool)Session::get('csv-has-headers'); $this->hasHeaders = (bool) Session::get('csv-has-headers');
} }
} }
@@ -71,46 +78,54 @@ class Data
protected function sessionDateFormat() protected function sessionDateFormat()
{ {
if (Session::has('csv-date-format')) { if (Session::has('csv-date-format')) {
$this->dateFormat = (string)Session::get('csv-date-format'); $this->dateFormat = (string) Session::get('csv-date-format');
} }
} }
protected function sessionCsvFileLocation() protected function sessionCsvFileLocation()
{ {
if (Session::has('csv-file')) { if (Session::has('csv-file')) {
$this->csvFileLocation = (string)Session::get('csv-file'); $this->csvFileLocation = (string) Session::get('csv-file');
} }
} }
protected function sessionMap() protected function sessionMap()
{ {
if (Session::has('csv-map')) { if (Session::has('csv-map')) {
$this->map = (array)Session::get('csv-map'); $this->map = (array) Session::get('csv-map');
} }
} }
protected function sessionRoles() protected function sessionRoles()
{ {
if (Session::has('csv-roles')) { if (Session::has('csv-roles')) {
$this->roles = (array)Session::get('csv-roles'); $this->roles = (array) Session::get('csv-roles');
} }
} }
protected function sessionMapped() protected function sessionMapped()
{ {
if (Session::has('csv-mapped')) { if (Session::has('csv-mapped')) {
$this->mapped = (array)Session::get('csv-mapped'); $this->mapped = (array) Session::get('csv-mapped');
} }
} }
protected function sessionSpecifix() protected function sessionSpecifix()
{ {
if (Session::has('csv-specifix')) { if (Session::has('csv-specifix')) {
$this->specifix = (array)Session::get('csv-specifix'); $this->specifix = (array) Session::get('csv-specifix');
}
}
protected function sessionDelimiter()
{
if (Session::has('csv-delimiter')) {
$this->delimiter = Session::get('csv-delimiter');
} }
} }
/** /**
*
* @return string * @return string
*/ */
public function getDateFormat() public function getDateFormat()
@@ -119,7 +134,8 @@ class Data
} }
/** /**
* @param mixed $dateFormat *
* @param mixed $dateFormat
*/ */
public function setDateFormat($dateFormat) public function setDateFormat($dateFormat)
{ {
@@ -128,7 +144,8 @@ class Data
} }
/** /**
* @param int $importAccount *
* @param int $importAccount
*/ */
public function setImportAccount($importAccount) public function setImportAccount($importAccount)
{ {
@@ -137,6 +154,7 @@ class Data
} }
/** /**
*
* @return bool * @return bool
*/ */
public function hasHeaders() public function hasHeaders()
@@ -145,7 +163,8 @@ class Data
} }
/** /**
* @param bool $hasHeaders *
* @param bool $hasHeaders
*/ */
public function setHasHeaders($hasHeaders) public function setHasHeaders($hasHeaders)
{ {
@@ -154,6 +173,7 @@ class Data
} }
/** /**
*
* @return array * @return array
*/ */
public function getMap() public function getMap()
@@ -162,7 +182,8 @@ class Data
} }
/** /**
* @param array $map *
* @param array $map
*/ */
public function setMap(array $map) public function setMap(array $map)
{ {
@@ -171,6 +192,7 @@ class Data
} }
/** /**
*
* @return array * @return array
*/ */
public function getMapped() public function getMapped()
@@ -179,7 +201,8 @@ class Data
} }
/** /**
* @param array $mapped *
* @param array $mapped
*/ */
public function setMapped(array $mapped) public function setMapped(array $mapped)
{ {
@@ -188,31 +211,33 @@ class Data
} }
/** /**
*
* @return Reader * @return Reader
*/ */
public function getReader() public function getReader()
{ {
if (strlen($this->csvFileContent) === 0) { if (strlen($this->csvFileContent) === 0) {
$this->loadCsvFile(); $this->loadCsvFile();
} }
if (is_null($this->reader)) { if (is_null($this->reader)) {
$this->reader = Reader::createFromString($this->getCsvFileContent()); $this->reader = Reader::createFromString($this->getCsvFileContent());
$this->reader->setDelimiter($this->delimiter);
} }
return $this->reader; return $this->reader;
} }
protected function loadCsvFile() protected function loadCsvFile()
{ {
$file = $this->getCsvFileLocation(); $file = $this->getCsvFileLocation();
$content = file_get_contents($file); $content = file_get_contents($file);
$contentDecrypted = Crypt::decrypt($content); $contentDecrypted = Crypt::decrypt($content);
$this->setCsvFileContent($contentDecrypted); $this->setCsvFileContent($contentDecrypted);
} }
/** /**
*
* @return string * @return string
*/ */
public function getCsvFileLocation() public function getCsvFileLocation()
@@ -221,7 +246,8 @@ class Data
} }
/** /**
* @param string $csvFileLocation *
* @param string $csvFileLocation
*/ */
public function setCsvFileLocation($csvFileLocation) public function setCsvFileLocation($csvFileLocation)
{ {
@@ -230,6 +256,7 @@ class Data
} }
/** /**
*
* @return string * @return string
*/ */
public function getCsvFileContent() public function getCsvFileContent()
@@ -238,7 +265,8 @@ class Data
} }
/** /**
* @param string $csvFileContent *
* @param string $csvFileContent
*/ */
public function setCsvFileContent($csvFileContent) public function setCsvFileContent($csvFileContent)
{ {
@@ -246,6 +274,7 @@ class Data
} }
/** /**
*
* @return array * @return array
*/ */
public function getRoles() public function getRoles()
@@ -254,7 +283,8 @@ class Data
} }
/** /**
* @param array $roles *
* @param array $roles
*/ */
public function setRoles(array $roles) public function setRoles(array $roles)
{ {
@@ -263,6 +293,7 @@ class Data
} }
/** /**
*
* @return array * @return array
*/ */
public function getSpecifix() public function getSpecifix()
@@ -271,7 +302,8 @@ class Data
} }
/** /**
* @param array $specifix *
* @param array $specifix
*/ */
public function setSpecifix($specifix) public function setSpecifix($specifix)
{ {
@@ -279,5 +311,22 @@ class Data
$this->specifix = $specifix; $this->specifix = $specifix;
} }
/**
*
* @return string
*/
public function getDelimiter()
{
return $this->delimiter;
}
/**
*
* @param string $delimiter
*/
public function setDelimiter($delimiter)
{
Session::put('csv-delimiter', $delimiter);
$this->delimiter = $delimiter;
}
} }

View File

@@ -385,6 +385,13 @@ class CsvController extends Controller
$settings['has-headers'] = intval(Input::get('has_headers')) === 1; $settings['has-headers'] = intval(Input::get('has_headers')) === 1;
$settings['specifix'] = Input::get('specifix'); $settings['specifix'] = Input::get('specifix');
$settings['import-account'] = intval(Input::get('csv_import_account')); $settings['import-account'] = intval(Input::get('csv_import_account'));
$settings['delimiter'] = Input::get('csv_delimiter', ',');
// A tab character cannot be used itself as option value in HTML
// See http://stackoverflow.com/questions/6064135/valid-characters-in-option-value
if( $settings[ 'delimiter' ] == 'tab' )
$settings[ 'delimiter' ] = "\t";
$settings['map'] = []; $settings['map'] = [];
$settings['mapped'] = []; $settings['mapped'] = [];
$settings['roles'] = []; $settings['roles'] = [];
@@ -405,7 +412,8 @@ class CsvController extends Controller
$this->data->setRoles($settings['roles']); $this->data->setRoles($settings['roles']);
$this->data->setSpecifix($settings['specifix']); $this->data->setSpecifix($settings['specifix']);
$this->data->setImportAccount($settings['import-account']); $this->data->setImportAccount($settings['import-account']);
$this->data->setDelimiter($settings['delimiter']);
return redirect(route('csv.column-roles')); return redirect(route('csv.column-roles'));
} }

View File

@@ -282,6 +282,7 @@ return [
'csv_specifix_RabobankDescription' => 'Select this when you\'re importing Rabobank CSV export files.', 'csv_specifix_RabobankDescription' => 'Select this when you\'re importing Rabobank CSV export files.',
'csv_specifix_Dummy' => 'Checking this has no effect whatsoever.', 'csv_specifix_Dummy' => 'Checking this has no effect whatsoever.',
'csv_import_account_help' => 'If your CSV file does NOT contain information about your asset account(s), use this dropdown to select to which account the transactions in the CSV belong to.', 'csv_import_account_help' => 'If your CSV file does NOT contain information about your asset account(s), use this dropdown to select to which account the transactions in the CSV belong to.',
'csv_delimiter_help' => 'Choose the field delimiter that is used in your input file. If not sure, comma is the safest option.',
'csv_date_parse_error' => 'Could not parse a valid date from ":value", using the format ":format". Are you sure your CSV is correct?', 'csv_date_parse_error' => 'Could not parse a valid date from ":value", using the format ":format". Are you sure your CSV is correct?',
// create new stuff: // create new stuff:

View File

@@ -53,6 +53,7 @@ return [
'csv_config' => 'CSV import configuration', 'csv_config' => 'CSV import configuration',
'specifix' => 'Bank- or file specific fixes', 'specifix' => 'Bank- or file specific fixes',
'csv_import_account' => 'Default import account', 'csv_import_account' => 'Default import account',
'csv_delimiter' => 'CSV field delimiter',
'attachments[]' => 'Attachments', 'attachments[]' => 'Attachments',
'store_new_withdrawal' => 'Store new withdrawal', 'store_new_withdrawal' => 'Store new withdrawal',
'store_new_deposit' => 'Store new deposit', 'store_new_deposit' => 'Store new deposit',

View File

@@ -282,6 +282,7 @@ return [
'csv_specifix_RabobankDescription' => 'Vink dit aan als je Rabobank bestanden importeert.', 'csv_specifix_RabobankDescription' => 'Vink dit aan als je Rabobank bestanden importeert.',
'csv_specifix_Dummy' => 'Dit vinkje doet niks (dummy).', 'csv_specifix_Dummy' => 'Dit vinkje doet niks (dummy).',
'csv_import_account_help' => 'Als jouw CSV bestand geen referenties bevat naar jouw rekening(en), geef dan hier aan om welke rekening het gaat.', 'csv_import_account_help' => 'Als jouw CSV bestand geen referenties bevat naar jouw rekening(en), geef dan hier aan om welke rekening het gaat.',
'csv_delimiter_help' => 'Kies het veld scheidingsteken dat in het invoerbestand is gebruikt. Bij twijfel is de komma de veiligste optie.',
'csv_date_parse_error' => 'Firefly kan van ":value" geen datum maken, gegeven het formaat ":format". Weet je zeker dat je CSV goed is?', 'csv_date_parse_error' => 'Firefly kan van ":value" geen datum maken, gegeven het formaat ":format". Weet je zeker dat je CSV goed is?',
// create new stuff: // create new stuff:

View File

@@ -53,6 +53,7 @@ return [
'csv_config' => 'Configuratiebestand', 'csv_config' => 'Configuratiebestand',
'specifix' => 'Bank- or of bestandsspecifieke opties', 'specifix' => 'Bank- or of bestandsspecifieke opties',
'csv_import_account' => 'Standaard rekening voor importeren', 'csv_import_account' => 'Standaard rekening voor importeren',
'csv_delimiter' => 'CSV scheidingsteken',
'attachments[]' => 'Bijlagen', 'attachments[]' => 'Bijlagen',
'store_new_withdrawal' => 'Nieuwe uitgave opslaan', 'store_new_withdrawal' => 'Nieuwe uitgave opslaan',
'store_new_deposit' => 'Nieuwe inkomsten opslaan', 'store_new_deposit' => 'Nieuwe inkomsten opslaan',

View File

@@ -63,6 +63,8 @@
{{ ExpandedForm.select('csv_import_account', accounts, 0, {helpText: 'csv_import_account_help'|_} ) }} {{ ExpandedForm.select('csv_import_account', accounts, 0, {helpText: 'csv_import_account_help'|_} ) }}
{{ ExpandedForm.select('csv_delimiter', { ',': ', (comma)', 'tab': '(tab)', ';': '; (semicolon)'}, 0, {helpText: 'csv_delimiter_help'|_} ) }}
{{ ExpandedForm.multiCheckbox('specifix', specifix) }} {{ ExpandedForm.multiCheckbox('specifix', specifix) }}