mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-18 18:40:12 +00:00
New export functionality.
This commit is contained in:
55
app/Export/Exporter/BasicExporter.php
Normal file
55
app/Export/Exporter/BasicExporter.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* BasicExporter.php
|
||||
* Copyright (C) 2016 Sander Dorigo
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Export\Exporter;
|
||||
|
||||
|
||||
use FireflyIII\Models\ExportJob;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Class BasicExporter
|
||||
*
|
||||
* @package FireflyIII\Export\Exporter
|
||||
*/
|
||||
class BasicExporter
|
||||
{
|
||||
private $entries;
|
||||
|
||||
/** @var ExportJob */
|
||||
protected $job;
|
||||
|
||||
/**
|
||||
* BasicExporter constructor.
|
||||
*/
|
||||
public function __construct(ExportJob $job)
|
||||
{
|
||||
$this->entries = new Collection;
|
||||
$this->job = $job;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getEntries()
|
||||
{
|
||||
return $this->entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $entries
|
||||
*/
|
||||
public function setEntries(Collection $entries)
|
||||
{
|
||||
$this->entries = $entries;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
80
app/Export/Exporter/CsvExporter.php
Normal file
80
app/Export/Exporter/CsvExporter.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* CsvExporter.php
|
||||
* Copyright (C) 2016 Sander Dorigo
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Export\Exporter;
|
||||
|
||||
use FireflyIII\Export\Entry;
|
||||
use FireflyIII\Models\ExportJob;
|
||||
use League\Csv\Writer;
|
||||
use SplFileObject;
|
||||
|
||||
/**
|
||||
* Class CsvExporter
|
||||
*
|
||||
* @package FireflyIII\Export\Exporter
|
||||
*/
|
||||
class CsvExporter extends BasicExporter implements ExporterInterface
|
||||
{
|
||||
/** @var string */
|
||||
private $fileName;
|
||||
|
||||
/** @var resource */
|
||||
private $handler;
|
||||
|
||||
/**
|
||||
* CsvExporter constructor.
|
||||
*/
|
||||
public function __construct(ExportJob $job)
|
||||
{
|
||||
parent::__construct($job);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFileName()
|
||||
{
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
// create temporary file:
|
||||
$this->tempFile();
|
||||
|
||||
// create CSV writer:
|
||||
$writer = Writer::createFromPath(new SplFileObject($this->fileName, 'a+'), 'w');
|
||||
//the $writer object open mode will be 'w'!!
|
||||
|
||||
// all rows:
|
||||
$rows = [];
|
||||
|
||||
// add header:
|
||||
$first = $this->getEntries()->first();
|
||||
$rows[] = array_keys(get_object_vars($first));
|
||||
|
||||
// then the rest:
|
||||
/** @var Entry $entry */
|
||||
foreach ($this->getEntries() as $entry) {
|
||||
$rows[] = array_values(get_object_vars($entry));
|
||||
|
||||
}
|
||||
$writer->insertAll($rows);
|
||||
}
|
||||
|
||||
private function tempFile()
|
||||
{
|
||||
$fileName = $this->job->key . '-records.csv';
|
||||
$this->fileName = storage_path('export') . DIRECTORY_SEPARATOR . $fileName;
|
||||
$this->handler = fopen($this->fileName, 'w');
|
||||
}
|
||||
}
|
41
app/Export/Exporter/ExporterInterface.php
Normal file
41
app/Export/Exporter/ExporterInterface.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* ExporterInterface.php
|
||||
* Copyright (C) 2016 Sander Dorigo
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Export\Exporter;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Interface ExporterInterface
|
||||
*
|
||||
* @package FireflyIII\Export\Exporter
|
||||
*/
|
||||
interface ExporterInterface
|
||||
{
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getEntries();
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function run();
|
||||
|
||||
/**
|
||||
* @param Collection $entries
|
||||
*
|
||||
*/
|
||||
public function setEntries(Collection $entries);
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFileName();
|
||||
|
||||
}
|
Reference in New Issue
Block a user