2016-02-04 17:16:16 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CsvExporter.php
|
2017-10-21 08:40:00 +02:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-02-04 17:16:16 +01:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02: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
|
2017-12-17 14:41:58 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-04 17:16:16 +01:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2016-05-20 12:27:31 +02:00
|
|
|
|
2016-02-04 17:16:16 +01:00
|
|
|
namespace FireflyIII\Export\Exporter;
|
|
|
|
|
2016-04-26 12:39:29 +02:00
|
|
|
use FireflyIII\Export\Entry\Entry;
|
2016-02-04 17:16:16 +01:00
|
|
|
use League\Csv\Writer;
|
2017-12-26 08:32:00 +01:00
|
|
|
use Storage;
|
2016-02-04 17:16:16 +01:00
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class CsvExporter.
|
2016-02-04 17:16:16 +01:00
|
|
|
*/
|
|
|
|
class CsvExporter extends BasicExporter implements ExporterInterface
|
|
|
|
{
|
2017-11-15 12:25:49 +01:00
|
|
|
/** @var string */
|
2016-02-04 17:16:16 +01:00
|
|
|
private $fileName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CsvExporter constructor.
|
|
|
|
*/
|
2017-02-05 15:58:42 +01:00
|
|
|
public function __construct()
|
2016-02-04 17:16:16 +01:00
|
|
|
{
|
2017-02-05 15:58:42 +01:00
|
|
|
parent::__construct();
|
2016-02-04 17:16:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-04-06 16:37:28 +02:00
|
|
|
public function getFileName(): string
|
2016-02-04 17:16:16 +01:00
|
|
|
{
|
|
|
|
return $this->fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-06 16:37:28 +02:00
|
|
|
* @return bool
|
2017-12-22 18:32:43 +01:00
|
|
|
*
|
2017-12-17 14:30:53 +01:00
|
|
|
* @throws \TypeError
|
2016-02-04 17:16:16 +01:00
|
|
|
*/
|
2016-04-06 16:37:28 +02:00
|
|
|
public function run(): bool
|
2016-02-04 17:16:16 +01:00
|
|
|
{
|
|
|
|
// create temporary file:
|
|
|
|
$this->tempFile();
|
|
|
|
|
2016-02-23 07:27:29 +01:00
|
|
|
// necessary for CSV writer:
|
|
|
|
$fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->fileName;
|
2017-12-26 08:32:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
//we create the CSV into memory
|
|
|
|
$writer = Writer::createFromPath($fullPath);
|
2017-12-29 09:05:35 +01:00
|
|
|
$rows = [];
|
2016-02-23 07:27:29 +01:00
|
|
|
|
2016-10-23 09:44:14 +02:00
|
|
|
// get field names for header row:
|
|
|
|
$first = $this->getEntries()->first();
|
2017-08-18 14:45:42 +02:00
|
|
|
$headers = [];
|
2017-11-15 12:25:49 +01:00
|
|
|
if (null !== $first) {
|
2017-08-18 14:45:42 +02:00
|
|
|
$headers = array_keys(get_object_vars($first));
|
|
|
|
}
|
|
|
|
|
|
|
|
$rows[] = $headers;
|
2016-05-17 15:19:07 +02:00
|
|
|
|
2016-02-04 17:16:16 +01:00
|
|
|
/** @var Entry $entry */
|
|
|
|
foreach ($this->getEntries() as $entry) {
|
2016-10-23 09:44:14 +02:00
|
|
|
$line = [];
|
|
|
|
foreach ($headers as $header) {
|
|
|
|
$line[] = $entry->$header;
|
|
|
|
}
|
|
|
|
$rows[] = $line;
|
2016-02-04 17:16:16 +01:00
|
|
|
}
|
|
|
|
$writer->insertAll($rows);
|
2016-04-25 18:43:09 +02:00
|
|
|
|
2016-04-06 16:37:28 +02:00
|
|
|
return true;
|
2016-02-04 17:16:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function tempFile()
|
|
|
|
{
|
2016-02-23 07:27:29 +01:00
|
|
|
$this->fileName = $this->job->key . '-records.csv';
|
2017-12-26 08:32:00 +01:00
|
|
|
// touch file in export directory:
|
|
|
|
$disk = Storage::disk('export');
|
2017-12-29 09:05:35 +01:00
|
|
|
$disk->put($this->fileName, '');
|
2016-02-04 17:16:16 +01:00
|
|
|
}
|
2016-02-10 16:01:18 +01:00
|
|
|
}
|