Files
firefly-iii/app/Api/V1/Controllers/Data/Export/ExportController.php
T

210 lines
5.9 KiB
PHP
Raw Normal View History

2021-03-04 06:28:16 +01:00
<?php
2021-08-10 19:31:55 +02:00
2021-03-04 06:28:16 +01:00
/*
2021-08-10 19:31:55 +02:00
* ExportController.php
2021-03-04 06:28:16 +01:00
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-08-10 19:31:55 +02:00
declare(strict_types=1);
2021-03-04 06:28:16 +01:00
namespace FireflyIII\Api\V1\Controllers\Data\Export;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\Data\Export\ExportRequest;
2025-08-15 20:06:28 +02:00
use FireflyIII\Enums\UserRoleEnum;
2021-09-18 05:58:22 +02:00
use FireflyIII\Exceptions\FireflyException;
2021-03-04 06:28:16 +01:00
use FireflyIII\Support\Export\ExportDataGenerator;
2026-01-23 15:09:50 +01:00
use Illuminate\Http\Request;
2021-03-04 06:28:16 +01:00
use Illuminate\Http\Response as LaravelResponse;
2026-08-15 21:13:19 +02:00
use Override;
2025-06-09 07:06:42 +02:00
use Safe\Exceptions\DatetimeException;
2025-10-05 13:03:51 +02:00
use function Safe\date;
2021-03-04 06:28:16 +01:00
/**
* Class ExportController
*/
2026-03-03 20:33:02 +01:00
final class ExportController extends Controller
2021-03-04 06:28:16 +01:00
{
private ExportDataGenerator $exporter;
2026-08-14 17:25:41 +02:00
#[Override]
2025-08-15 20:06:28 +02:00
protected array $acceptedRoles = [UserRoleEnum::READ_ONLY];
2021-03-04 06:28:16 +01:00
/**
* ExportController constructor.
*/
public function __construct()
{
parent::__construct();
2026-01-23 15:09:50 +01:00
$this->middleware(function (Request $request, $next) {
$this->validateUserGroup($request);
$this->exporter = app(ExportDataGenerator::class);
$this->exporter->setUserGroup($this->userGroup);
$this->exporter->setUser($this->user);
return $next($request);
});
2021-03-04 06:28:16 +01:00
}
/**
2025-10-05 12:57:58 +02:00
* @throws DatetimeException
* @throws FireflyException
2025-10-05 13:03:51 +02:00
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
2021-03-04 06:28:16 +01:00
*/
public function accounts(ExportRequest $request): LaravelResponse
{
$this->exporter->setExportAccounts(true);
return $this->returnExport('accounts');
}
/**
2025-10-05 12:57:58 +02:00
* @throws DatetimeException
* @throws FireflyException
2025-10-05 13:03:51 +02:00
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
2021-03-04 06:28:16 +01:00
*/
public function bills(ExportRequest $request): LaravelResponse
{
$this->exporter->setExportBills(true);
return $this->returnExport('bills');
}
/**
2025-10-05 12:57:58 +02:00
* @throws DatetimeException
* @throws FireflyException
2025-10-05 13:03:51 +02:00
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
2021-03-04 06:28:16 +01:00
*/
public function budgets(ExportRequest $request): LaravelResponse
{
$this->exporter->setExportBudgets(true);
return $this->returnExport('budgets');
}
/**
2025-10-05 12:57:58 +02:00
* @throws DatetimeException
* @throws FireflyException
2025-10-05 13:03:51 +02:00
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
2021-03-04 06:28:16 +01:00
*/
public function categories(ExportRequest $request): LaravelResponse
{
$this->exporter->setExportCategories(true);
return $this->returnExport('categories');
}
/**
2025-10-05 12:57:58 +02:00
* @throws DatetimeException
* @throws FireflyException
2025-10-05 13:03:51 +02:00
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
2021-03-04 06:28:16 +01:00
*/
public function piggyBanks(ExportRequest $request): LaravelResponse
{
$this->exporter->setExportPiggies(true);
return $this->returnExport('piggies');
}
/**
2025-10-05 12:57:58 +02:00
* @throws DatetimeException
* @throws FireflyException
2025-10-05 13:03:51 +02:00
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
2021-03-04 06:28:16 +01:00
*/
public function recurring(ExportRequest $request): LaravelResponse
{
$this->exporter->setExportRecurring(true);
return $this->returnExport('recurrences');
}
/**
2025-10-05 12:57:58 +02:00
* @throws DatetimeException
* @throws FireflyException
2025-10-05 13:03:51 +02:00
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
2021-03-04 06:28:16 +01:00
*/
public function rules(ExportRequest $request): LaravelResponse
{
$this->exporter->setExportRules(true);
return $this->returnExport('rules');
}
/**
2025-10-05 12:57:58 +02:00
* @throws DatetimeException
* @throws FireflyException
2025-10-05 13:03:51 +02:00
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
2021-03-04 06:28:16 +01:00
*/
public function tags(ExportRequest $request): LaravelResponse
{
$this->exporter->setExportTags(true);
return $this->returnExport('tags');
}
/**
2025-10-05 12:57:58 +02:00
* @throws DatetimeException
2021-09-18 05:58:22 +02:00
* @throws FireflyException
2021-03-04 06:28:16 +01:00
*/
public function transactions(ExportRequest $request): LaravelResponse
{
$params = $request->getAll();
$this->exporter->setStart($params['start']);
$this->exporter->setEnd($params['end']);
$this->exporter->setAccounts($params['accounts']);
$this->exporter->setExportTransactions(true);
return $this->returnExport('transactions');
}
2026-02-06 13:55:17 +01:00
/**
* @throws FireflyException
* @throws DatetimeException
*/
private function returnExport(string $key): LaravelResponse
{
$date = date('Y-m-d-H-i-s');
$fileName = sprintf('%s-export-%s.csv', $date, $key);
$data = $this->exporter->export();
/** @var LaravelResponse $response */
$response = response($data[$key]);
$response
->header('Content-Description', 'File Transfer')
->header('Content-Type', 'application/octet-stream')
->header('Content-Disposition', 'attachment; filename='.$fileName)
->header('Content-Transfer-Encoding', 'binary')
->header('Connection', 'Keep-Alive')
->header('Expires', '0')
->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
->header('Pragma', 'public')
->header('Content-Length', (string) strlen((string) $data[$key]))
;
return $response;
}
2021-03-28 11:39:26 +02:00
}