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;
}
}