2015-07-03 03:45:00 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by PhpStorm.
|
|
|
|
* User: sander
|
|
|
|
* Date: 03/07/15
|
|
|
|
* Time: 10:37
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers;
|
2015-07-03 04:52:51 -05:00
|
|
|
|
2015-07-03 05:22:20 -05:00
|
|
|
use Auth;
|
|
|
|
use Crypt;
|
2015-07-03 04:52:51 -05:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Input;
|
|
|
|
use League\Csv\Reader;
|
|
|
|
use Redirect;
|
|
|
|
use Session;
|
2015-07-03 03:45:00 -05:00
|
|
|
use View;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CsvController
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Controllers
|
|
|
|
*/
|
|
|
|
class CsvController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
2015-07-03 04:52:51 -05:00
|
|
|
View::share('title', trans('firefly.csv'));
|
2015-07-03 03:45:00 -05:00
|
|
|
View::share('mainTitleIcon', 'fa-file-text-o');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-03 04:52:51 -05:00
|
|
|
/**
|
|
|
|
* @return View
|
|
|
|
*/
|
2015-07-03 03:45:00 -05:00
|
|
|
public function index()
|
|
|
|
{
|
2015-07-03 04:52:51 -05:00
|
|
|
$subTitle = trans('firefly.csv_import');
|
|
|
|
|
|
|
|
// can actually upload?
|
2015-07-03 05:22:20 -05:00
|
|
|
$uploadPossible = is_writable(storage_path('upload'));
|
2015-07-03 04:52:51 -05:00
|
|
|
$path = storage_path('upload');
|
|
|
|
|
|
|
|
|
|
|
|
return view('csv.index', compact('subTitle', 'uploadPossible', 'path'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function upload(Request $request)
|
|
|
|
{
|
2015-07-03 05:22:20 -05:00
|
|
|
// possible column roles:
|
|
|
|
$roles = [
|
|
|
|
'(ignore this column)',
|
|
|
|
'Asset account name',
|
|
|
|
'Expense or revenue account name',
|
|
|
|
'Amount',
|
|
|
|
'Date',
|
|
|
|
'Currency',
|
|
|
|
'Description',
|
|
|
|
'Category',
|
|
|
|
'Budget',
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
2015-07-03 04:52:51 -05:00
|
|
|
if (!$request->hasFile('csv')) {
|
|
|
|
Session::flash('warning', 'No file uploaded.');
|
|
|
|
|
|
|
|
|
|
|
|
return Redirect::route('csv.index');
|
|
|
|
}
|
|
|
|
$hasHeaders = intval(Input::get('has_headers')) === 1;
|
|
|
|
$reader = Reader::createFromPath($request->file('csv')->getRealPath());
|
|
|
|
$data = $reader->query();
|
|
|
|
$data->next(); // go to first row:
|
|
|
|
|
2015-07-03 05:22:20 -05:00
|
|
|
$count = count($data->current());
|
|
|
|
$headers = [];
|
|
|
|
for ($i = 1; $i <= $count; $i++) {
|
|
|
|
$headers[] = trans('firefly.csv_row') . ' #' . $i;
|
|
|
|
}
|
|
|
|
if ($hasHeaders) {
|
2015-07-03 04:52:51 -05:00
|
|
|
$headers = $data->current();
|
|
|
|
}
|
|
|
|
|
|
|
|
// example data is always the second row:
|
|
|
|
$data->next();
|
|
|
|
$example = $data->current();
|
2015-07-03 05:22:20 -05:00
|
|
|
// store file somewhere temporary (encrypted)?
|
|
|
|
$time = str_replace(' ', '-', microtime());
|
|
|
|
$fileName = 'csv-upload-' . Auth::user()->id . '-' . $time . '.csv.encrypted';
|
|
|
|
$fullPath = storage_path('upload') . DIRECTORY_SEPARATOR . $fileName;
|
|
|
|
$content = file_get_contents($request->file('csv')->getRealPath());
|
|
|
|
$content = Crypt::encrypt($content);
|
|
|
|
file_put_contents($fullPath, $content);
|
|
|
|
Session::put('latestCSVUpload', $fullPath);
|
|
|
|
|
|
|
|
$subTitle = trans('firefly.csv_process');
|
|
|
|
|
|
|
|
return view('csv.upload', compact('headers', 'example', 'roles', 'subTitle'));
|
2015-07-03 04:52:51 -05:00
|
|
|
|
2015-07-03 03:45:00 -05:00
|
|
|
}
|
|
|
|
}
|