Fixed upload form, made a new form element, added some processing.

This commit is contained in:
James Cole 2015-07-03 11:52:51 +02:00
parent 86011d4ea2
commit 16374bce9b
14 changed files with 2533 additions and 78 deletions

View File

@ -7,6 +7,12 @@
*/
namespace FireflyIII\Http\Controllers;
use Illuminate\Http\Request;
use Input;
use League\Csv\Reader;
use Redirect;
use Session;
use View;
/**
@ -23,13 +29,64 @@ class CsvController extends Controller
public function __construct()
{
parent::__construct();
View::share('title', 'CSV');
View::share('title', trans('firefly.csv'));
View::share('mainTitleIcon', 'fa-file-text-o');
}
/**
* @return View
*/
public function index()
{
return view('csv.index');
$subTitle = trans('firefly.csv_import');
// can actually upload?
$uploadPossible = !is_writable(storage_path('upload'));
$path = storage_path('upload');
return view('csv.index', compact('subTitle', 'uploadPossible', 'path'));
}
/**
*
*/
public function upload(Request $request)
{
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:
if ($hasHeaders) {
// first row = headers.
$headers = $data->current();
} else {
$count = count($data->current());
$headers = [];
for ($i = 1; $i <= $count; $i++) {
$headers[] = trans('firefly.csv_row') . ' #' . $i;
}
}
// example data is always the second row:
$data->next();
$example = $data->current();
var_dump($headers);
var_dump($example);
// store file somewhere temporary?
exit;
}
}

View File

@ -224,6 +224,7 @@ Route::group(
* CSV controller
*/
Route::get('/csv', ['uses' => 'CsvController@index', 'as' => 'csv.index']);
Route::post('/csv/upload', ['uses' => 'CsvController@upload', 'as' => 'csv.upload']);
/**
* Currency Controller

View File

@ -347,6 +347,24 @@ class ExpandedForm
return $html;
}
/**
* @param $name
* @param null $value
* @param array $options
*
* @return string
*/
public function file($name, array $options = [])
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$html = View::make('form.file', compact('classes', 'name', 'label', 'options'))->render();
return $html;
}
/**
* @param $name
* @param null $value

View File

@ -28,7 +28,8 @@
"illuminate/html": "~5.0",
"league/commonmark": "0.7.*",
"rcrowe/twigbridge": "0.7.x@dev",
"zizaco/entrust": "dev-laravel-5"
"zizaco/entrust": "dev-laravel-5",
"league/csv": "^7.1"
},
"require-dev": {
"barryvdh/laravel-debugbar": "@stable",

2415
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -145,12 +145,12 @@ return [
'ExpandedForm' => [
'is_safe' => [
'date', 'text', 'select', 'balance', 'optionsList', 'checkbox', 'amount', 'tags', 'integer', 'textarea', 'location',
'multiRadio'
'multiRadio','file'
]
],
'Form' => [
'is_safe' => [
'input', 'select', 'checkbox', 'model', 'open', 'radio', 'textarea'
'input', 'select', 'checkbox', 'model', 'open', 'radio', 'textarea','file'
]
],
],

View File

@ -21,6 +21,13 @@ return [
// csv import:
'csv_import' => 'Import CSV file',
'csv' => 'CSV',
'csv_index_text' => 'Here be explanation.',
'csv_upload_form' => 'Upload form',
'upload_csv_file' => 'Upload CSV file',
'csv_header_help' => 'Check this when bla bla',
'csv_row' => 'row',
'upload_not_writeable' => 'Cannot write to the path mentioned here. Cannot upload',
// create new stuff:
'create_new_withdrawal' => 'Create new withdrawal',

View File

@ -45,6 +45,8 @@ return [
'under' => 'Under',
'symbol' => 'Symbol',
'code' => 'Code',
'csv' => 'CSV file',
'has_headers' => 'Headers',
'store_new_withdrawal' => 'Store new withdrawal',
'store_new_deposit' => 'Store new deposit',

View File

@ -20,7 +20,14 @@ return [
'search_results_for' => 'Zoekresultaten voor ":query"',
// csv import:
'csv_import' => 'Import CSV file',
'csv_import' => 'Importeer CSV-bestand',
'csv' => 'CSV',
'csv_index_text' => 'Hier komt uitleg.',
'csv_upload_form' => 'Upload formulier',
'upload_csv_file' => 'Upload CSV-bestand',
'csv_header_help' => 'Check dit als bla bla',
'csv_row' => 'rij',
'upload_not_writeable' => 'Cannot write to the path mentioned here. Cannot upload',
// create new stuff:
'create_new_withdrawal' => 'Nieuwe uitgave',

View File

@ -45,6 +45,8 @@ return [
'under' => 'Onder',
'symbol' => 'Symbool',
'code' => 'Code',
'csv' => 'CSV-bestand',
'has_headers' => 'Eerste rij zijn kolomnamen',
'store_new_withdrawal' => 'Nieuwe uitgave opslaan',
'store_new_deposit' => 'Nieuwe inkomsten opslaan',

View File

@ -7,6 +7,81 @@
{% block content %}
Bla bla.
<div class="row">
<div class="col-lg-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'csv'|_ }}</h3>
<!-- ACTIONS MENU -->
<div class="box-tools pull-right">
<button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
</div>
</div>
<div class="box-body">
{{ 'csv_index_text'|_ }}
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'csv_upload_form'|_ }}</h3>
<!-- ACTIONS MENU -->
<div class="box-tools pull-right">
<button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
</div>
</div>
<div class="box-body">
<form class="form-horizontal" action="{{ route('csv.upload') }}" method="post" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
{{ ExpandedForm.checkbox('has_headers',false,null,{helpText: 'csv_header_help'|_}) }}
{{ ExpandedForm.file('csv') }}
{% if uploadPossible %}
<div class="form-group" id="csv_holder">
<div class="col-sm-4">
&nbsp;
</div>
<div class="col-sm-8">
<button type="submit" class="btn btn-success">
{{ 'upload_csv_file'|_ }}
</button>
</div>
</div>
{% else %}
<div class="form-group" id="csv_holder">
<div class="col-sm-4">
&nbsp;
</div>
<div class="col-sm-8">
<pre>{{ path }}</pre>
<p class="text-danger">
{{ 'upload_not_writeable'|_ }}
</p>
</div>
</div>
{% endif %}
</form>
</div>
</div>
</div>
</div>
{% endblock %}

View File

@ -7,6 +7,7 @@
{{ Form.checkbox(name, value, options.checked, options) }}
</label>
</div>
{% include 'form/help.twig' %}
{% include 'form/feedback.twig' %}
</div>
</div>

View File

@ -0,0 +1,9 @@
<div class="{{ classes }}" id="{{ name }}_holder">
<label for="{{ options.id }}" class="col-sm-4 control-label">{{ label }}</label>
<div class="col-sm-8">
{{ Form.file(name, options) }}
{% include 'form/help.twig' %}
{% include 'form/feedback.twig' %}
</div>
</div>

2
storage/upload/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*
!.gitignore