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

View File

@ -53,6 +53,7 @@ return [
'csv_config' => 'CSV import configuration',
'specifix' => 'Bank- or file specific fixes',
'csv_import_account' => 'Default import account',
'csv_delimiter' => 'CSV field delimiter',
'attachments[]' => 'Attachments',
'store_new_withdrawal' => 'Store new withdrawal',
'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_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_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?',
// create new stuff:

View File

@ -53,6 +53,7 @@ return [
'csv_config' => 'Configuratiebestand',
'specifix' => 'Bank- or of bestandsspecifieke opties',
'csv_import_account' => 'Standaard rekening voor importeren',
'csv_delimiter' => 'CSV scheidingsteken',
'attachments[]' => 'Bijlagen',
'store_new_withdrawal' => 'Nieuwe uitgave 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_delimiter', { ',': ', (comma)', 'tab': '(tab)', ';': '; (semicolon)'}, 0, {helpText: 'csv_delimiter_help'|_} ) }}
{{ ExpandedForm.multiCheckbox('specifix', specifix) }}