2015-11-18 15:40:50 +01:00
|
|
|
<?php namespace app\Http\Controllers;
|
|
|
|
|
2015-11-18 18:16:23 +01:00
|
|
|
use Utils;
|
|
|
|
use View;
|
2015-11-18 17:08:37 +01:00
|
|
|
use Exception;
|
2015-11-18 15:40:50 +01:00
|
|
|
use Input;
|
|
|
|
use Session;
|
|
|
|
use Redirect;
|
|
|
|
use App\Services\ImportService;
|
|
|
|
use App\Http\Controllers\BaseController;
|
|
|
|
|
|
|
|
class ImportController extends BaseController
|
|
|
|
{
|
|
|
|
public function __construct(ImportService $importService)
|
|
|
|
{
|
2016-03-02 14:36:42 +01:00
|
|
|
//parent::__construct();
|
2015-11-18 15:40:50 +01:00
|
|
|
|
|
|
|
$this->importService = $importService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function doImport()
|
|
|
|
{
|
2015-11-18 18:16:23 +01:00
|
|
|
$source = Input::get('source');
|
2015-11-24 20:45:38 +01:00
|
|
|
$files = [];
|
2015-11-18 18:16:23 +01:00
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
foreach (ImportService::$entityTypes as $entityType) {
|
|
|
|
if (Input::file("{$entityType}_file")) {
|
|
|
|
$files[$entityType] = Input::file("{$entityType}_file")->getRealPath();
|
2015-12-15 21:25:12 +01:00
|
|
|
if ($source === IMPORT_CSV) {
|
|
|
|
Session::forget("{$entityType}-data");
|
|
|
|
}
|
2015-11-18 15:40:50 +01:00
|
|
|
}
|
2015-11-24 20:45:38 +01:00
|
|
|
}
|
2015-11-18 18:16:23 +01:00
|
|
|
|
2015-11-24 20:45:38 +01:00
|
|
|
try {
|
|
|
|
if ($source === IMPORT_CSV) {
|
|
|
|
$data = $this->importService->mapCSV($files);
|
|
|
|
return View::make('accounts.import_map', ['data' => $data]);
|
|
|
|
} else {
|
2015-12-21 20:57:55 +01:00
|
|
|
$results = $this->importService->import($source, $files);
|
|
|
|
return $this->showResult($results);
|
2015-11-18 18:16:23 +01:00
|
|
|
}
|
2015-11-24 20:45:38 +01:00
|
|
|
} catch (Exception $exception) {
|
2015-12-08 11:10:20 +01:00
|
|
|
Utils::logError($exception);
|
2015-11-24 20:45:38 +01:00
|
|
|
Session::flash('error', $exception->getMessage());
|
2015-12-15 21:25:12 +01:00
|
|
|
return Redirect::to('/settings/' . ACCOUNT_IMPORT_EXPORT);
|
2015-11-18 18:16:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function doImportCSV()
|
|
|
|
{
|
|
|
|
$map = Input::get('map');
|
2015-11-24 20:45:38 +01:00
|
|
|
$headers = Input::get('headers');
|
2015-11-18 18:16:23 +01:00
|
|
|
|
2015-11-25 10:35:24 +01:00
|
|
|
try {
|
2015-12-21 20:57:55 +01:00
|
|
|
$results = $this->importService->importCSV($map, $headers);
|
|
|
|
return $this->showResult($results);
|
2015-11-25 10:35:24 +01:00
|
|
|
} catch (Exception $exception) {
|
2015-12-08 11:10:20 +01:00
|
|
|
Utils::logError($exception);
|
2015-11-25 10:35:24 +01:00
|
|
|
Session::flash('error', $exception->getMessage());
|
2015-12-15 21:25:12 +01:00
|
|
|
return Redirect::to('/settings/' . ACCOUNT_IMPORT_EXPORT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-21 20:57:55 +01:00
|
|
|
private function showResult($results)
|
2015-12-15 21:25:12 +01:00
|
|
|
{
|
2015-12-21 20:57:55 +01:00
|
|
|
$message = '';
|
|
|
|
$skipped = [];
|
|
|
|
|
|
|
|
foreach ($results as $entityType => $entityResults) {
|
|
|
|
if ($count = count($entityResults[RESULT_SUCCESS])) {
|
|
|
|
$message .= trans("texts.created_{$entityType}s", ['count' => $count]) . '<br/>';
|
|
|
|
}
|
|
|
|
if (count($entityResults[RESULT_FAILURE])) {
|
|
|
|
$skipped = array_merge($skipped, $entityResults[RESULT_FAILURE]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 21:25:12 +01:00
|
|
|
if (count($skipped)) {
|
2015-12-21 20:57:55 +01:00
|
|
|
$message .= '<p/>' . trans('texts.failed_to_import') . '<br/>';
|
2015-12-15 21:25:12 +01:00
|
|
|
foreach ($skipped as $skip) {
|
2015-12-21 20:57:55 +01:00
|
|
|
$message .= json_encode($skip) . '<br/>';
|
2015-12-15 21:25:12 +01:00
|
|
|
}
|
2015-12-21 20:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($message) {
|
2015-12-15 21:25:12 +01:00
|
|
|
Session::flash('warning', $message);
|
2015-11-25 10:35:24 +01:00
|
|
|
}
|
2015-11-18 15:40:50 +01:00
|
|
|
|
2015-11-18 18:16:23 +01:00
|
|
|
return Redirect::to('/settings/' . ACCOUNT_IMPORT_EXPORT);
|
2015-11-18 15:40:50 +01:00
|
|
|
}
|
|
|
|
}
|