Files
firefly-iii/app/Http/Controllers/Export/IndexController.php

121 lines
3.9 KiB
PHP
Raw Normal View History

2019-12-27 10:59:31 +01:00
<?php
2020-06-30 19:05:35 +02:00
2019-12-27 10:59:31 +01:00
/**
* IndexController.php
2020-06-30 19:05:35 +02:00
* Copyright (c) 2020 james@firefly-iii.org
2019-12-27 10:59:31 +01:00
*
* 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/>.
*/
2020-06-30 19:05:35 +02:00
declare(strict_types=1);
2019-12-27 10:59:31 +01:00
namespace FireflyIII\Http\Controllers\Export;
2021-03-28 11:46:23 +02:00
2021-09-18 10:20:19 +02:00
use FireflyIII\Exceptions\FireflyException;
2019-12-27 10:59:31 +01:00
use FireflyIII\Http\Controllers\Controller;
2019-12-28 06:43:53 +01:00
use FireflyIII\Http\Middleware\IsDemoUser;
2019-12-27 10:59:31 +01:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2019-12-27 21:31:25 +01:00
use FireflyIII\Support\Export\ExportDataGenerator;
use Illuminate\Contracts\View\Factory;
2024-01-30 18:14:56 +01:00
use Illuminate\Http\RedirectResponse;
2019-12-27 10:59:31 +01:00
use Illuminate\Http\Response as LaravelResponse;
use Illuminate\View\View;
2019-12-27 10:59:31 +01:00
/**
* Class IndexController
*/
class IndexController extends Controller
{
2020-10-01 16:52:01 +02:00
private JournalRepositoryInterface $journalRepository;
2019-12-27 10:59:31 +01:00
/**
* IndexController constructor.
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
app('view')->share('mainTitleIcon', 'fa-life-bouy');
2022-12-29 19:41:57 +01:00
app('view')->share('title', (string)trans('firefly.export_data_title'));
2019-12-27 10:59:31 +01:00
$this->journalRepository = app(JournalRepositoryInterface::class);
2019-12-28 06:43:53 +01:00
$this->middleware(IsDemoUser::class)->except(['index']);
2019-12-27 10:59:31 +01:00
return $next($request);
}
);
}
/**
2021-09-18 10:20:19 +02:00
* @throws FireflyException
2019-12-27 10:59:31 +01:00
*/
2024-01-30 18:14:56 +01:00
public function export(): LaravelResponse|RedirectResponse
2019-12-27 10:59:31 +01:00
{
if(auth()->user()->hasRole('demo')) {
session()->flash('info', (string) trans('firefly.demo_user_export'));
return redirect(route('export.index'));
}
2019-12-27 21:31:25 +01:00
/** @var ExportDataGenerator $generator */
$generator = app(ExportDataGenerator::class);
$generator->setUser(auth()->user());
$generator->setExportTransactions(true);
2019-12-27 10:59:31 +01:00
// get first transaction in DB:
2020-09-11 07:12:33 +02:00
$firstDate = today(config('app.timezone'));
2019-12-27 10:59:31 +01:00
$firstDate->subYear();
$journal = $this->journalRepository->firstNull();
2019-12-27 10:59:31 +01:00
if (null !== $journal) {
$firstDate = clone $journal->date;
}
$generator->setStart($firstDate);
$result = $generator->export();
2019-12-27 10:59:31 +01:00
$name = sprintf('%s_transaction_export.csv', date('Y_m_d'));
$quoted = sprintf('"%s"', addcslashes($name, '"\\'));
2023-12-20 19:35:52 +01:00
2019-12-27 10:59:31 +01:00
// headers for CSV file.
/** @var LaravelResponse $response */
$response = response($result['transactions']);
2019-12-27 10:59:31 +01:00
$response
->header('Content-Description', 'File Transfer')
->header('Content-Type', 'text/x-csv')
2023-12-20 19:35:52 +01:00
->header('Content-Disposition', 'attachment; filename='.$quoted)
// ->header('Content-Transfer-Encoding', 'binary')
2019-12-27 10:59:31 +01:00
->header('Connection', 'Keep-Alive')
->header('Expires', '0')
->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
->header('Pragma', 'public')
2023-12-20 19:35:52 +01:00
->header('Content-Length', (string)strlen($result['transactions']))
;
2019-12-27 10:59:31 +01:00
// return CSV file made from 'transactions' array.
2019-12-28 06:43:53 +01:00
return $response;
2019-12-27 10:59:31 +01:00
}
/**
* @return Factory|View
2019-12-27 10:59:31 +01:00
*/
public function index()
{
return view('export.index');
2019-12-27 10:59:31 +01:00
}
}