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

120 lines
3.7 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;
use Carbon\Carbon;
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;
2019-12-27 10:59:31 +01:00
use Illuminate\Http\Response as LaravelResponse;
use Illuminate\View\View;
use League\Csv\CannotInsertRecord;
2019-12-27 10:59:31 +01:00
/**
* Class IndexController
*/
class IndexController extends Controller
{
/** @var JournalRepositoryInterface */
private $journalRepository;
/**
* IndexController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
app('view')->share('mainTitleIcon', 'fa-life-bouy');
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);
}
);
}
/**
* @throws CannotInsertRecord
2019-12-28 06:43:53 +01:00
* @return LaravelResponse
2019-12-27 10:59:31 +01:00
*/
2019-12-28 06:43:53 +01:00
public function export(): LaravelResponse
2019-12-27 10:59:31 +01:00
{
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:
$firstDate = new Carbon;
$firstDate->subYear();
$journal = $this->journalRepository->firstNull();
if (null !== $journal) {
$firstDate = clone $journal->date;
}
$generator->setStart($firstDate);
$result = $generator->export();
$name = sprintf('%s_transaction_export.csv', date('Y_m_d'));
$quoted = sprintf('"%s"', addcslashes($name, '"\\'));
// headers for CSV file.
/** @var LaravelResponse $response */
$response = response($result['transactions'], 200);
$response
->header('Content-Description', 'File Transfer')
->header('Content-Type', 'text/x-csv')
->header('Content-Disposition', 'attachment; filename=' . $quoted)
//->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', 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');
}
}