2016-02-04 10:16:16 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CsvExporter.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-02-04 10:16:16 -06:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-04 10:16:16 -06:00
|
|
|
*/
|
|
|
|
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-05-20 05:27:31 -05:00
|
|
|
|
2016-02-04 10:16:16 -06:00
|
|
|
namespace FireflyIII\Export\Exporter;
|
|
|
|
|
2016-04-26 05:39:29 -05:00
|
|
|
use FireflyIII\Export\Entry\Entry;
|
2016-02-04 10:16:16 -06:00
|
|
|
use League\Csv\Writer;
|
|
|
|
use SplFileObject;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CsvExporter
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Export\Exporter
|
|
|
|
*/
|
|
|
|
class CsvExporter extends BasicExporter implements ExporterInterface
|
|
|
|
{
|
|
|
|
/** @var string */
|
|
|
|
private $fileName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CsvExporter constructor.
|
|
|
|
*/
|
2017-02-05 08:58:42 -06:00
|
|
|
public function __construct()
|
2016-02-04 10:16:16 -06:00
|
|
|
{
|
2017-02-05 08:58:42 -06:00
|
|
|
parent::__construct();
|
2016-02-04 10:16:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-04-06 09:37:28 -05:00
|
|
|
public function getFileName(): string
|
2016-02-04 10:16:16 -06:00
|
|
|
{
|
|
|
|
return $this->fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-06 09:37:28 -05:00
|
|
|
* @return bool
|
2016-02-04 10:16:16 -06:00
|
|
|
*/
|
2016-04-06 09:37:28 -05:00
|
|
|
public function run(): bool
|
2016-02-04 10:16:16 -06:00
|
|
|
{
|
|
|
|
// create temporary file:
|
|
|
|
$this->tempFile();
|
|
|
|
|
2016-02-23 00:27:29 -06:00
|
|
|
// necessary for CSV writer:
|
|
|
|
$fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->fileName;
|
2016-05-18 00:01:27 -05:00
|
|
|
$writer = Writer::createFromPath(new SplFileObject($fullPath, 'a+'), 'w');
|
|
|
|
$rows = [];
|
2016-02-23 00:27:29 -06:00
|
|
|
|
2016-10-23 02:44:14 -05:00
|
|
|
// get field names for header row:
|
|
|
|
$first = $this->getEntries()->first();
|
2017-08-18 07:45:42 -05:00
|
|
|
$headers = [];
|
|
|
|
if (!is_null($first)) {
|
|
|
|
$headers = array_keys(get_object_vars($first));
|
|
|
|
}
|
|
|
|
|
|
|
|
$rows[] = $headers;
|
2016-05-17 08:19:07 -05:00
|
|
|
|
2016-02-04 10:16:16 -06:00
|
|
|
/** @var Entry $entry */
|
|
|
|
foreach ($this->getEntries() as $entry) {
|
2016-10-23 02:44:14 -05:00
|
|
|
$line = [];
|
|
|
|
foreach ($headers as $header) {
|
|
|
|
$line[] = $entry->$header;
|
|
|
|
}
|
|
|
|
$rows[] = $line;
|
2016-02-04 10:16:16 -06:00
|
|
|
}
|
|
|
|
$writer->insertAll($rows);
|
2016-04-25 11:43:09 -05:00
|
|
|
|
2016-04-06 09:37:28 -05:00
|
|
|
return true;
|
2016-02-04 10:16:16 -06:00
|
|
|
}
|
|
|
|
|
2016-05-17 08:19:07 -05:00
|
|
|
|
2016-02-04 10:16:16 -06:00
|
|
|
private function tempFile()
|
|
|
|
{
|
2016-02-23 00:27:29 -06:00
|
|
|
$this->fileName = $this->job->key . '-records.csv';
|
2016-02-04 10:16:16 -06:00
|
|
|
}
|
2016-02-10 09:01:18 -06:00
|
|
|
}
|