mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
New export controller.
This commit is contained in:
parent
70ba9a4db5
commit
78f8b1454d
182
app/Http/Controllers/ExportController.php
Normal file
182
app/Http/Controllers/ExportController.php
Normal file
@ -0,0 +1,182 @@
|
||||
<?php
|
||||
/**
|
||||
* ExportController.php
|
||||
* Copyright (C) 2016 Sander Dorigo
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Http\Controllers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Config;
|
||||
use FireflyIII\Export\Processor;
|
||||
use FireflyIII\Http\Requests;
|
||||
use FireflyIII\Http\Requests\ExportFormRequest;
|
||||
use FireflyIII\Models\ExportJob;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
|
||||
use FireflyIII\Repositories\ExportJob\ExportJobRepositoryInterface as EJRI;
|
||||
use Preferences;
|
||||
use Response;
|
||||
use View;
|
||||
|
||||
/**
|
||||
* Class ExportController
|
||||
*
|
||||
* @package FireflyIII\Http\Controllers
|
||||
*/
|
||||
class ExportController extends Controller
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
View::share('mainTitleIcon', 'fa-file-archive-o');
|
||||
View::share('title', trans('firefly.export_data'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ExportJob $job
|
||||
*/
|
||||
public function download(ExportJob $job)
|
||||
{
|
||||
$file = storage_path('export') . DIRECTORY_SEPARATOR . $job->key . '.zip';
|
||||
$date = date('Y-m-d \a\t H-i-s');
|
||||
$name = 'Export job on ' . $date . '.zip';
|
||||
$quoted = sprintf('"%s"', addcslashes($name, '"\\'));
|
||||
|
||||
|
||||
return response(file_get_contents($file), 200)
|
||||
->header('Content-Description', 'File Transfer')
|
||||
->header('Content-Type', 'application/octet-stream')
|
||||
->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', filesize($file));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function getStatus(ExportJob $job)
|
||||
{
|
||||
return Response::json(['status' => trans('firefly.' . $job->status)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ARI $repository
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function index(ARI $repository, EJRI $jobs)
|
||||
{
|
||||
// create new export job.
|
||||
$job = $jobs->create();
|
||||
// delete old ones.
|
||||
$jobs->cleanup();
|
||||
|
||||
// does the user have shared accounts?
|
||||
$accounts = $repository->getAccounts(['Default account', 'Asset account']);
|
||||
$formats = array_keys(Config::get('firefly.export_formats'));
|
||||
$defaultFormat = Preferences::get('export_format', Config::get('firefly.default_export_format'))->data;
|
||||
//$first = session('first')->format('Y-m-d');
|
||||
$first = Carbon::create()->startOfYear()->format('Y-m-d');
|
||||
$today = Carbon::create()->format('Y-m-d');
|
||||
|
||||
return view('export.index', compact('accounts', 'job', 'formats', 'defaultFormat', 'first', 'today'));
|
||||
|
||||
// select date range
|
||||
// format
|
||||
// select accounts
|
||||
// include attachments
|
||||
// include uploaded CSV files (if any)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ExportFormRequest $request
|
||||
* @param ARI $repository
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function postIndex(ExportFormRequest $request, ARI $repository, EJRI $jobs)
|
||||
{
|
||||
set_time_limit(0);
|
||||
$job = $jobs->findByKey($request->get('job'));
|
||||
$settings = [
|
||||
'accounts' => $repository->get($request->get('accounts')),
|
||||
'startDate' => new Carbon($request->get('start_date')),
|
||||
'endDate' => new Carbon($request->get('end_date')),
|
||||
'exportFormat' => $request->get('exportFormat'),
|
||||
'includeAttachments' => intval($request->get('include_attachments')) === 1,
|
||||
'includeConfig' => intval($request->get('include_config')) === 1,
|
||||
'includeOldUploads' => intval($request->get('include_old_uploads')) === 1,
|
||||
'job' => $job,
|
||||
];
|
||||
|
||||
$job->change('export_status_make_exporter');
|
||||
$processor = new Processor($settings);
|
||||
|
||||
/*
|
||||
* Collect journals:
|
||||
*/
|
||||
$job->change('export_status_collecting_journals');
|
||||
$processor->collectJournals();
|
||||
$job->change('export_status_collected_journals');
|
||||
/*
|
||||
* Transform to exportable entries:
|
||||
*/
|
||||
$job->change('export_status_converting_to_export_format');
|
||||
$processor->convertJournals();
|
||||
$job->change('export_status_converted_to_export_format');
|
||||
/*
|
||||
* Transform to (temporary) file:
|
||||
*/
|
||||
$job->change('export_status_creating_journal_file');
|
||||
$processor->exportJournals();
|
||||
$job->change('export_status_created_journal_file');
|
||||
/*
|
||||
* Collect attachments, if applicable.
|
||||
*/
|
||||
if ($settings['includeAttachments']) {
|
||||
$job->change('export_status_collecting_attachments');
|
||||
$processor->collectAttachments();
|
||||
$job->change('export_status_collected_attachments');
|
||||
}
|
||||
|
||||
/*
|
||||
* Collect old uploads
|
||||
*/
|
||||
if ($settings['includeOldUploads']) {
|
||||
$job->change('export_status_collecting_old_uploads');
|
||||
$processor->collectOldUploads();
|
||||
$job->change('export_status_collected_old_uploads');
|
||||
}
|
||||
|
||||
/*
|
||||
* Generate / collect config file.
|
||||
*/
|
||||
if ($settings['includeConfig']) {
|
||||
$job->change('export_status_creating_config_file');
|
||||
$processor->createConfigFile();
|
||||
$job->change('export_status_created_config_file');
|
||||
}
|
||||
|
||||
/*
|
||||
* Create ZIP file:
|
||||
*/
|
||||
$job->change('export_status_creating_zip_file');
|
||||
$processor->createZipFile();
|
||||
$job->change('export_status_created_zip_file');
|
||||
|
||||
$job->change('export_status_finished');
|
||||
|
||||
return Response::json('ok');
|
||||
}
|
||||
}
|
@ -133,6 +133,14 @@ Route::group(
|
||||
Route::post('/currency/update/{currency}', ['uses' => 'CurrencyController@update', 'as' => 'currency.update']);
|
||||
Route::post('/currency/destroy/{currency}', ['uses' => 'CurrencyController@destroy', 'as' => 'currency.destroy']);
|
||||
|
||||
/**
|
||||
* Export Controller
|
||||
*/
|
||||
Route::get('/export', ['uses' => 'ExportController@index', 'as' => 'export.index']);
|
||||
Route::post('/export/submit', ['uses' => 'ExportController@postIndex', 'as' => 'export.export']);
|
||||
Route::get('/export/status/{jobKey}', ['uses' => 'ExportController@getStatus', 'as' => 'export.status']);
|
||||
Route::get('/export/download/{jobKey}', ['uses' => 'ExportController@download', 'as' => 'export.download']);
|
||||
|
||||
|
||||
/**
|
||||
* ALL CHART Controllers
|
||||
|
Loading…
Reference in New Issue
Block a user