Some code improvements.

This commit is contained in:
James Cole
2015-07-06 16:08:36 +02:00
parent 39af9e4414
commit 6e261abb73

View File

@@ -61,7 +61,7 @@ class CsvController extends Controller
if (!$this->wizard->sessionHasValues($fields)) { if (!$this->wizard->sessionHasValues($fields)) {
Session::flash('warning', 'Could not recover upload.'); Session::flash('warning', 'Could not recover upload.');
return Redirect::route('csv.index'); return redirect(route('csv.index'));
} }
$subTitle = trans('firefly.csv_define_column_roles'); $subTitle = trans('firefly.csv_define_column_roles');
@@ -92,6 +92,8 @@ class CsvController extends Controller
* Optional download of mapping. * Optional download of mapping.
* *
* STEP FOUR THREE-A * STEP FOUR THREE-A
*
* @return \Illuminate\Http\RedirectResponse|string
*/ */
public function downloadConfig() public function downloadConfig()
{ {
@@ -99,7 +101,7 @@ class CsvController extends Controller
if (!$this->wizard->sessionHasValues($fields)) { if (!$this->wizard->sessionHasValues($fields)) {
Session::flash('warning', 'Could not recover upload.'); Session::flash('warning', 'Could not recover upload.');
return Redirect::route('csv.index'); return redirect(route('csv.index'));
} }
$data = [ $data = [
'date-format' => Session::get('date-format'), 'date-format' => Session::get('date-format'),
@@ -122,11 +124,12 @@ class CsvController extends Controller
header('Content-disposition: attachment; filename=' . $name); header('Content-disposition: attachment; filename=' . $name);
header('Content-type: application/json'); header('Content-type: application/json');
echo $result; echo $result;
exit;
return '';
} }
/** /**
* @return View * @return \Illuminate\View\View
*/ */
public function downloadConfigPage() public function downloadConfigPage()
{ {
@@ -140,7 +143,7 @@ class CsvController extends Controller
* *
* STEP ONE * STEP ONE
* *
* @return View * @return \Illuminate\View\View
*/ */
public function index() public function index()
{ {
@@ -158,19 +161,6 @@ class CsvController extends Controller
// get values which are yet unsaveable or unmappable: // get values which are yet unsaveable or unmappable:
$unsupported = []; $unsupported = [];
foreach (Config::get('csv.roles') as $role) {
if (!isset($role['converter'])) {
$unsupported[] = trans('firefly.csv_unsupported_value', ['columnRole' => $role['name']]);
}
if ($role['mappable'] === true && !isset($role['mapper'])) {
$unsupported[] = trans('firefly.csv_unsupported_map', ['columnRole' => $role['name']]);
}
if (!isset($role['field'])) {
$unsupported[] = trans('firefly.csv_cannot_store_value', ['columnRole' => $role['name']]);
}
}
sort($unsupported);
// can actually upload? // can actually upload?
$uploadPossible = is_writable(storage_path('upload')); $uploadPossible = is_writable(storage_path('upload'));
@@ -192,7 +182,7 @@ class CsvController extends Controller
if (!$this->wizard->sessionHasValues($fields)) { if (!$this->wizard->sessionHasValues($fields)) {
Session::flash('warning', 'Could not recover upload.'); Session::flash('warning', 'Could not recover upload.');
return Redirect::route('csv.index'); return redirect(route('csv.index'));
} }
@@ -209,14 +199,14 @@ class CsvController extends Controller
if (count($roles) === 0) { if (count($roles) === 0) {
Session::flash('warning', 'Please select some roles.'); Session::flash('warning', 'Please select some roles.');
return Redirect::route('csv.column-roles'); return redirect(route('csv.column-roles'));
} }
/* /*
* Continue with map specification when necessary. * Continue with map specification when necessary.
*/ */
if (count($maps) > 0) { if (count($maps) > 0) {
return Redirect::route('csv.map'); return redirect(route('csv.map'));
} }
/* /*
@@ -224,7 +214,7 @@ class CsvController extends Controller
*/ */
// proceed to download config // proceed to download config
return Redirect::route('csv.download-config-page'); return redirect(route('csv.download-config-page'));
} }
@@ -234,7 +224,7 @@ class CsvController extends Controller
* *
* STEP FIVE. * STEP FIVE.
* *
* @return \Illuminate\Http\RedirectResponse|View * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
* @throws FireflyException * @throws FireflyException
*/ */
public function map() public function map()
@@ -247,7 +237,7 @@ class CsvController extends Controller
if (!$this->wizard->sessionHasValues($fields)) { if (!$this->wizard->sessionHasValues($fields)) {
Session::flash('warning', 'Could not recover upload.'); Session::flash('warning', 'Could not recover upload.');
return Redirect::route('csv.index'); return redirect(route('csv.index'));
} }
/* /*
@@ -284,9 +274,12 @@ class CsvController extends Controller
} }
/** /**
*
* Finally actually process the CSV file. * Finally actually process the CSV file.
* *
* STEP SEVEN * STEP SEVEN
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
*/ */
public function process() public function process()
{ {
@@ -297,7 +290,7 @@ class CsvController extends Controller
if (!$this->wizard->sessionHasValues($fields)) { if (!$this->wizard->sessionHasValues($fields)) {
Session::flash('warning', 'Could not recover upload.'); Session::flash('warning', 'Could not recover upload.');
return Redirect::route('csv.index'); return redirect(route('csv.index'));
} }
Log::debug('Created importer'); Log::debug('Created importer');
@@ -328,6 +321,8 @@ class CsvController extends Controller
* Store the mapping the user has made. This is * Store the mapping the user has made. This is
* *
* STEP SIX * STEP SIX
*
* @return \Illuminate\Http\RedirectResponse
*/ */
public function saveMapping() public function saveMapping()
{ {
@@ -338,7 +333,7 @@ class CsvController extends Controller
if (!$this->wizard->sessionHasValues($fields)) { if (!$this->wizard->sessionHasValues($fields)) {
Session::flash('warning', 'Could not recover upload.'); Session::flash('warning', 'Could not recover upload.');
return Redirect::route('csv.index'); return redirect(route('csv.index'));
} }
// save mapping to session. // save mapping to session.
@@ -346,7 +341,7 @@ class CsvController extends Controller
if (!is_array(Input::get('mapping'))) { if (!is_array(Input::get('mapping'))) {
Session::flash('warning', 'Invalid mapping.'); Session::flash('warning', 'Invalid mapping.');
return Redirect::route('csv.map'); return redirect(route('csv.map'));
} }
foreach (Input::get('mapping') as $index => $data) { foreach (Input::get('mapping') as $index => $data) {
@@ -360,7 +355,7 @@ class CsvController extends Controller
Session::put('csv-mapped', $mapped); Session::put('csv-mapped', $mapped);
// proceed to process. // proceed to process.
return Redirect::route('csv.download-config-page'); return redirect(route('csv.download-config-page'));
} }
@@ -380,46 +375,36 @@ class CsvController extends Controller
if (!$request->hasFile('csv')) { if (!$request->hasFile('csv')) {
Session::flash('warning', 'No file uploaded.'); Session::flash('warning', 'No file uploaded.');
return Redirect::route('csv.index'); return redirect(route('csv.index'));
} }
/* $fullPath = $this->wizard->storeCsvFile($request->file('csv')->getRealPath());
* Store CSV and put in session. $settings = [];
*/ $settings['date-format'] = Input::get('date_format');
$fullPath = $this->wizard->storeCsvFile($request->file('csv')->getRealPath()); $settings['has-headers'] = intval(Input::get('has_headers')) === 1;
$dateFormat = Input::get('date_format'); $settings['map'] = [];
$hasHeaders = intval(Input::get('has_headers')) === 1; $settings['mapped'] = [];
$map = []; $settings['roles'] = [];
$roles = [];
$mapped = [];
/* /*
* Process config file if present. * Process config file if present.
*/ */
if ($request->hasFile('csv_config')) { if ($request->hasFile('csv_config')) {
$data = file_get_contents($request->file('csv_config')->getRealPath()); $data = file_get_contents($request->file('csv_config')->getRealPath());
$json = json_decode($data, true); $json = json_decode($data, true);
if (is_array($json)) {
if (!is_null($json)) { $settings = array_merge($settings, $json);
$dateFormat = isset($json['date-format']) ? $json['date-format'] : $dateFormat;
$hasHeaders = isset($json['has-headers']) ? $json['has-headers'] : $hasHeaders;
$map = isset($json['map']) && is_array($json['map']) ? $json['map'] : [];
$mapped = isset($json['mapped']) && is_array($json['mapped']) ? $json['mapped'] : [];
$roles = isset($json['roles']) && is_array($json['roles']) ? $json['roles'] : [];
} }
} }
$this->data->setCsvFileLocation($fullPath); $this->data->setCsvFileLocation($fullPath);
$this->data->setDateFormat($dateFormat); $this->data->setDateFormat($settings['date-format']);
$this->data->setHasHeaders($hasHeaders); $this->data->setHasHeaders($settings['has-headers']);
$this->data->setMap($map); $this->data->setMap($settings['map']);
$this->data->setMapped($mapped); $this->data->setMapped($settings['mapped']);
$this->data->setRoles($roles); $this->data->setRoles($settings['roles']);
return redirect(route('csv.column-roles'));
return Redirect::route('csv.column-roles');
} }
} }