mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-29 18:20:01 +00:00
Initial code base for new CSV import.
This commit is contained in:
@@ -2,7 +2,14 @@
|
||||
|
||||
namespace FireflyIII\Http\Controllers;
|
||||
|
||||
use Crypt;
|
||||
use FireflyIII\Http\Requests;
|
||||
use FireflyIII\Http\Requests\ImportUploadRequest;
|
||||
use FireflyIII\Import\Importer\ImporterInterface;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use SplFileObject;
|
||||
use Storage;
|
||||
use View;
|
||||
|
||||
/**
|
||||
@@ -22,6 +29,26 @@ class ImportController extends Controller
|
||||
View::share('title', trans('firefly.import_data'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*
|
||||
* @return View
|
||||
*/
|
||||
public function configure(ImportJob $job)
|
||||
{
|
||||
// create proper importer (depends on job)
|
||||
$type = $job->file_type;
|
||||
/** @var ImporterInterface $importer */
|
||||
$importer = app('FireflyIII\Import\Importer\\' . ucfirst($type) . 'Importer');
|
||||
$importer->setJob($job);
|
||||
$importer->configure();
|
||||
$data = $importer->getConfigurationData();
|
||||
|
||||
return view('import.' . $type . '.configure', compact('data', 'job'));
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
@@ -38,4 +65,27 @@ class ImportController extends Controller
|
||||
|
||||
return view('import.index', compact('subTitle', 'subTitleIcon', 'importFileTypes', 'defaultImportType'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportUploadRequest $request
|
||||
* @param ImportJobRepositoryInterface $repository
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function upload(ImportUploadRequest $request, ImportJobRepositoryInterface $repository)
|
||||
{
|
||||
// create import job:
|
||||
$type = $request->get('import_file_type');
|
||||
$job = $repository->create($type);
|
||||
$upload = $request->files->get('import_file');
|
||||
$newName = $job->key . '.upload';
|
||||
$uploaded = new SplFileObject($upload->getRealPath());
|
||||
$content = $uploaded->fread($uploaded->getSize());
|
||||
$contentEncrypted = Crypt::encrypt($content);
|
||||
$disk = Storage::disk('upload');
|
||||
$disk->put($newName, $contentEncrypted);
|
||||
|
||||
return redirect(route('import.configure', [$job->key]));
|
||||
|
||||
}
|
||||
}
|
||||
|
46
app/Http/Requests/ImportUploadRequest.php
Normal file
46
app/Http/Requests/ImportUploadRequest.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* ImportUploadRequest.php
|
||||
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace FireflyIII\Http\Requests;
|
||||
|
||||
use Auth;
|
||||
|
||||
/**
|
||||
* Class ImportUploadRequest
|
||||
*
|
||||
*
|
||||
* @package FireflyIII\Http\Requests
|
||||
*/
|
||||
class ImportUploadRequest extends Request
|
||||
{
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
// Only allow logged in users
|
||||
return Auth::check();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
$types = array_keys(config('firefly.import_formats'));
|
||||
|
||||
return [
|
||||
'import_file' => 'required|file',
|
||||
'import_file_type' => 'required|in:' . join(',', $types),
|
||||
];
|
||||
|
||||
}
|
||||
}
|
@@ -223,8 +223,9 @@ Route::group(
|
||||
/**
|
||||
* IMPORT CONTROLLER
|
||||
*/
|
||||
Route::get('/import', ['uses' => 'ImportController@index', 'as' => 'import.index']);
|
||||
Route::post('/import/upload', ['uses' => 'ImportController@upload', 'as' => 'import.upload']);
|
||||
Route::get('/import', ['uses' => 'ImportController@index','as' => 'import.index']);
|
||||
Route::post('/import/upload', ['uses' => 'ImportController@upload','as' => 'import.upload']);
|
||||
Route::get('/import/configure/{importJob}', ['uses' => 'ImportController@configure','as' => 'import.configure']);
|
||||
|
||||
/**
|
||||
* Help Controller
|
||||
|
81
app/Import/Importer/CsvImporter.php
Normal file
81
app/Import/Importer/CsvImporter.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* CsvImporter.php
|
||||
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace FireflyIII\Import\Importer;
|
||||
|
||||
|
||||
use ExpandedForm;
|
||||
use FireflyIII\Import\Role\Map;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
|
||||
/**
|
||||
* Class CsvImporter
|
||||
*
|
||||
* @package FireflyIII\Import\Importer
|
||||
*/
|
||||
class CsvImporter implements ImporterInterface
|
||||
{
|
||||
/** @var ImportJob */
|
||||
public $job;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function configure(): bool
|
||||
{
|
||||
// need to do nothing, for now.
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getConfigurationData(): array
|
||||
{
|
||||
$crud = app('FireflyIII\Crud\Account\AccountCrudInterface');
|
||||
$accounts = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
|
||||
$delimiters = [
|
||||
',' => trans('form.csv_comma'),
|
||||
';' => trans('form.csv_semicolon'),
|
||||
'tab' => trans('form.csv_tab'),
|
||||
];
|
||||
|
||||
$data = [
|
||||
'accounts' => ExpandedForm::makeSelectList($accounts),
|
||||
'specifix' => [],
|
||||
'delimiters' => $delimiters,
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*/
|
||||
public function setJob(ImportJob $job)
|
||||
{
|
||||
$this->job = $job;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Map thing used to allow the user to
|
||||
* define roles for each entry.
|
||||
*
|
||||
* @return Map
|
||||
*/
|
||||
public function prepareRoles(): Map
|
||||
{
|
||||
return 'do not work';
|
||||
exit;
|
||||
}
|
||||
}
|
52
app/Import/Importer/ImporterInterface.php
Normal file
52
app/Import/Importer/ImporterInterface.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* ImporterInterface.php
|
||||
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace FireflyIII\Import\Importer;
|
||||
|
||||
use FireflyIII\Import\Role\Map;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
|
||||
/**
|
||||
* Interface ImporterInterface
|
||||
*
|
||||
* @package FireflyIII\Import\Importer
|
||||
*/
|
||||
interface ImporterInterface
|
||||
{
|
||||
/**
|
||||
* After uploading, and after setJob(), prepare anything that is
|
||||
* necessary for the configure() line.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function configure(): bool;
|
||||
|
||||
/**
|
||||
* Returns any data necessary to do the configuration.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getConfigurationData(): array;
|
||||
|
||||
/**
|
||||
* Returns a Map thing used to allow the user to
|
||||
* define roles for each entry.
|
||||
*
|
||||
* @return Map
|
||||
*/
|
||||
public function prepareRoles(): Map;
|
||||
|
||||
/**
|
||||
* @param ImportJob $job
|
||||
*
|
||||
*/
|
||||
public function setJob(ImportJob $job);
|
||||
}
|
18
app/Import/Role/Map.php
Normal file
18
app/Import/Role/Map.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* Map.php
|
||||
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace FireflyIII\Import\Role;
|
||||
|
||||
|
||||
class Map
|
||||
{
|
||||
|
||||
}
|
18
app/Import/Role/Role.php
Normal file
18
app/Import/Role/Role.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* Role.php
|
||||
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace FireflyIII\Import\Role;
|
||||
|
||||
|
||||
class Role
|
||||
{
|
||||
|
||||
}
|
75
app/Models/ImportJob.php
Normal file
75
app/Models/ImportJob.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* ImportJob.php
|
||||
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace FireflyIII\Models;
|
||||
|
||||
use Auth;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
/**
|
||||
* Class ImportJob
|
||||
*
|
||||
* @package FireflyIII\Models
|
||||
* @property integer $id
|
||||
* @property \Carbon\Carbon $created_at
|
||||
* @property \Carbon\Carbon $updated_at
|
||||
* @property integer $user_id
|
||||
* @property string $key
|
||||
* @property string $file_type
|
||||
* @property string $status
|
||||
* @property-read \FireflyIII\User $user
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\ImportJob whereId($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\ImportJob whereCreatedAt($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\ImportJob whereUpdatedAt($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\ImportJob whereUserId($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\ImportJob whereKey($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\ImportJob whereFileType($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\ImportJob whereStatus($value)
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
class ImportJob extends Model
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*
|
||||
* @return mixed
|
||||
* @throws NotFoundHttpException
|
||||
*/
|
||||
public static function routeBinder($value)
|
||||
{
|
||||
if (Auth::check()) {
|
||||
$model = self::where('key', $value)->where('user_id', Auth::user()->id)->first();
|
||||
if (!is_null($model)) {
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
throw new NotFoundHttpException;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $status
|
||||
*/
|
||||
public function change($status)
|
||||
{
|
||||
$this->status = $status;
|
||||
$this->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo('FireflyIII\User');
|
||||
}
|
||||
}
|
@@ -31,6 +31,27 @@ class ExportJobServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->exportJob();
|
||||
$this->importJob();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function exportJob()
|
||||
{
|
||||
|
||||
$this->app->bind(
|
||||
'FireflyIII\Repositories\ExportJob\ExportJobRepositoryInterface',
|
||||
function (Application $app, array $arguments) {
|
||||
@@ -46,13 +67,20 @@ class ExportJobServiceProvider extends ServiceProvider
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
private function importJob()
|
||||
{
|
||||
//
|
||||
$this->app->bind(
|
||||
'FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface',
|
||||
function (Application $app, array $arguments) {
|
||||
if (!isset($arguments[0]) && $app->auth->check()) {
|
||||
return app('FireflyIII\Repositories\ImportJob\ImportJobRepository', [$app->auth->user()]);
|
||||
}
|
||||
if (!isset($arguments[0]) && !$app->auth->check()) {
|
||||
throw new FireflyException('There is no user present.');
|
||||
}
|
||||
|
||||
return app('FireflyIII\Repositories\ImportJob\ImportJobRepository', $arguments);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
81
app/Repositories/ImportJob/ImportJobRepository.php
Normal file
81
app/Repositories/ImportJob/ImportJobRepository.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* ImportJobRepository.php
|
||||
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace FireflyIII\Repositories\ImportJob;
|
||||
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Class ImportJobRepository
|
||||
*
|
||||
* @package FireflyIII\Repositories\ImportJob
|
||||
*/
|
||||
class ImportJobRepository implements ImportJobRepositoryInterface
|
||||
{
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* ExportJobRepository constructor.
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
public function __construct(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fileType
|
||||
*
|
||||
* @return ImportJob
|
||||
*/
|
||||
public function create(string $fileType): ImportJob
|
||||
{
|
||||
$count = 0;
|
||||
while ($count < 30) {
|
||||
$key = Str::random(12);
|
||||
$existing = $this->findByKey($key);
|
||||
if (is_null($existing->id)) {
|
||||
$importJob = new ImportJob;
|
||||
$importJob->user()->associate($this->user);
|
||||
$importJob->file_type = $fileType;
|
||||
$importJob->key = Str::random(12);
|
||||
$importJob->status = 'import_status_never_started';
|
||||
$importJob->save();
|
||||
|
||||
// breaks the loop:
|
||||
return $importJob;
|
||||
}
|
||||
$count++;
|
||||
|
||||
}
|
||||
|
||||
return new ImportJob;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @return ImportJob
|
||||
*/
|
||||
public function findByKey(string $key): ImportJob
|
||||
{
|
||||
$result = $this->user->importJobs()->where('key', $key)->first();
|
||||
if (is_null($result)) {
|
||||
return new ImportJob;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
36
app/Repositories/ImportJob/ImportJobRepositoryInterface.php
Normal file
36
app/Repositories/ImportJob/ImportJobRepositoryInterface.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/**
|
||||
* ImportJobRepositoryInterface.php
|
||||
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace FireflyIII\Repositories\ImportJob;
|
||||
|
||||
use FireflyIII\Models\ImportJob;
|
||||
|
||||
/**
|
||||
* Interface ImportJobRepositoryInterface
|
||||
*
|
||||
* @package FireflyIII\Repositories\ImportJob
|
||||
*/
|
||||
interface ImportJobRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* @param string $fileType
|
||||
*
|
||||
* @return ImportJob
|
||||
*/
|
||||
public function create(string $fileType): ImportJob;
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @return ImportJob
|
||||
*/
|
||||
public function findByKey(string $key): ImportJob;
|
||||
}
|
@@ -54,6 +54,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\User whereBlocked($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\User whereBlockedCode($value)
|
||||
* @mixin \Eloquent
|
||||
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\ImportJob[] $importjobs
|
||||
*/
|
||||
class User extends Authenticatable
|
||||
{
|
||||
@@ -145,6 +146,14 @@ class User extends Authenticatable
|
||||
return $this->hasMany('FireflyIII\Models\ExportJob');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany
|
||||
*/
|
||||
public function importjobs(): HasMany
|
||||
{
|
||||
return $this->hasMany('FireflyIII\Models\ImportJob');
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user has a role by its name.
|
||||
*
|
||||
|
Reference in New Issue
Block a user