firefly-iii/app/Http/Controllers/ExportController.php

202 lines
6.4 KiB
PHP
Raw Normal View History

2016-02-04 10:13:42 -06:00
<?php
/**
* ExportController.php
2016-04-01 09:44:46 -05:00
* Copyright (C) 2016 thegrumpydictator@gmail.com
2016-02-04 10:13:42 -06:00
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
2016-02-04 10:13:42 -06:00
*/
declare(strict_types = 1);
2016-02-04 10:13:42 -06:00
namespace FireflyIII\Http\Controllers;
use Carbon\Carbon;
2016-02-06 08:01:26 -06:00
use ExpandedForm;
2016-05-20 04:02:07 -05:00
use FireflyIII\Crud\Account\AccountCrudInterface;
2016-02-25 08:04:17 -06:00
use FireflyIII\Exceptions\FireflyException;
2016-02-04 10:13:42 -06:00
use FireflyIII\Export\Processor;
use FireflyIII\Http\Requests\ExportFormRequest;
2016-05-20 04:02:07 -05:00
use FireflyIII\Models\AccountType;
2016-02-04 10:13:42 -06:00
use FireflyIII\Models\ExportJob;
use FireflyIII\Repositories\ExportJob\ExportJobRepositoryInterface as EJRI;
use Preferences;
use Response;
2016-02-25 08:04:17 -06:00
use Storage;
2016-02-04 10:13:42 -06:00
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
2016-02-25 08:04:17 -06:00
*
* @return mixed
* @throws FireflyException
2016-02-04 10:13:42 -06:00
*/
public function download(ExportJob $job)
{
2016-02-25 08:04:17 -06:00
$disk = Storage::disk('export');
$file = $job->key . '.zip';
2016-02-04 10:13:42 -06:00
$date = date('Y-m-d \a\t H-i-s');
$name = 'Export job on ' . $date . '.zip';
$quoted = sprintf('"%s"', addcslashes($name, '"\\'));
2016-02-25 08:04:17 -06:00
if (!$disk->exists($file)) {
throw new FireflyException('Against all expectations, zip file "' . $file . '" does not exist.');
}
2016-02-04 10:22:09 -06:00
$job->change('export_downloaded');
2016-02-04 10:13:42 -06:00
2016-02-25 08:04:17 -06:00
return response($disk->get($file), 200)
2016-02-04 10:13:42 -06:00
->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')
2016-02-25 08:06:01 -06:00
->header('Content-Length', $disk->size($file));
2016-02-04 10:13:42 -06:00
}
/**
2016-02-05 08:41:40 -06:00
* @param ExportJob $job
*
2016-02-04 10:13:42 -06:00
* @return \Illuminate\Http\JsonResponse
*/
public function getStatus(ExportJob $job)
{
return Response::json(['status' => trans('firefly.' . $job->status)]);
}
/**
2016-05-20 04:02:07 -05:00
* @param AccountCrudInterface $crud
* @param EJRI $jobs
2016-02-04 10:13:42 -06:00
*
* @return View
2016-02-04 10:13:42 -06:00
*/
2016-05-20 04:02:07 -05:00
public function index(AccountCrudInterface $crud, EJRI $jobs)
2016-02-04 10:13:42 -06:00
{
// create new export job.
$job = $jobs->create();
// delete old ones.
$jobs->cleanup();
// does the user have shared accounts?
2016-05-20 04:02:07 -05:00
$accounts = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
2016-02-06 08:01:26 -06:00
$accountList = ExpandedForm::makeSelectList($accounts);
$checked = array_keys($accountList);
2016-04-26 14:40:15 -05:00
$formats = array_keys(config('firefly.export_formats'));
$defaultFormat = Preferences::get('export_format', config('firefly.default_export_format'))->data;
2016-02-23 00:31:01 -06:00
$first = session('first')->format('Y-m-d');
2016-02-04 10:57:36 -06:00
$today = Carbon::create()->format('Y-m-d');
2016-02-04 10:13:42 -06:00
2016-02-06 08:01:26 -06:00
return view('export.index', compact('job', 'checked', 'accountList', 'formats', 'defaultFormat', 'first', 'today'));
2016-02-04 10:13:42 -06:00
}
/**
2016-10-05 09:09:37 -05:00
* @param ExportFormRequest $request
* @param AccountCrudInterface $crud
2016-02-04 10:13:42 -06:00
*
2016-10-05 09:09:37 -05:00
* @param EJRI $jobs
2016-02-05 08:41:40 -06:00
*
2016-02-04 10:13:42 -06:00
* @return string
2016-02-05 08:41:40 -06:00
* @throws \FireflyIII\Exceptions\FireflyException
2016-02-04 10:13:42 -06:00
*/
2016-10-05 09:09:37 -05:00
public function postIndex(ExportFormRequest $request, AccountCrudInterface $crud, EJRI $jobs)
2016-02-04 10:13:42 -06:00
{
set_time_limit(0);
$job = $jobs->findByKey($request->get('job'));
$settings = [
2016-10-05 09:09:37 -05:00
'accounts' => $crud->getAccountsById($request->get('accounts')),
'startDate' => new Carbon($request->get('export_start_range')),
'endDate' => new Carbon($request->get('export_end_range')),
2016-02-04 10:13:42 -06:00
'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');
}
}