invoiceninja/app/Http/Controllers/ImportController.php

115 lines
3.4 KiB
PHP
Raw Normal View History

2017-01-30 21:40:43 +02:00
<?php
2015-11-18 16:40:50 +02:00
2017-01-30 21:40:43 +02:00
namespace App\Http\Controllers;
use App\Services\ImportService;
2015-11-18 18:08:37 +02:00
use Exception;
2015-11-18 16:40:50 +02:00
use Input;
use Redirect;
2017-01-30 21:40:43 +02:00
use Session;
use Utils;
use View;
2015-11-18 16:40:50 +02:00
class ImportController extends BaseController
{
public function __construct(ImportService $importService)
{
2016-03-02 15:36:42 +02:00
//parent::__construct();
2015-11-18 16:40:50 +02:00
$this->importService = $importService;
}
public function doImport()
{
2015-11-18 19:16:23 +02:00
$source = Input::get('source');
2015-11-24 21:45:38 +02:00
$files = [];
2015-11-18 19:16:23 +02:00
2015-11-24 21:45:38 +02:00
foreach (ImportService::$entityTypes as $entityType) {
if (Input::file("{$entityType}_file")) {
$files[$entityType] = Input::file("{$entityType}_file")->getRealPath();
2015-12-15 22:25:12 +02:00
if ($source === IMPORT_CSV) {
Session::forget("{$entityType}-data");
}
2015-11-18 16:40:50 +02:00
}
2015-11-24 21:45:38 +02:00
}
2015-11-18 19:16:23 +02:00
2017-01-30 18:05:31 +02:00
if (! count($files)) {
2016-09-25 21:13:14 +03:00
Session::flash('error', trans('texts.select_file'));
2017-01-30 21:40:43 +02:00
2016-09-25 21:13:14 +03:00
return Redirect::to('/settings/' . ACCOUNT_IMPORT_EXPORT);
}
2015-11-24 21:45:38 +02:00
try {
if ($source === IMPORT_CSV) {
$data = $this->importService->mapCSV($files);
2017-01-30 21:40:43 +02:00
2015-11-24 21:45:38 +02:00
return View::make('accounts.import_map', ['data' => $data]);
2016-06-02 22:03:59 +03:00
} elseif ($source === IMPORT_JSON) {
2017-03-22 11:50:55 +02:00
$includeData = filter_var(Input::get('data'), FILTER_VALIDATE_BOOLEAN);
$includeSettings = filter_var(Input::get('settings'), FILTER_VALIDATE_BOOLEAN);
$results = $this->importService->importJSON($files[IMPORT_JSON], $includeData, $includeSettings);
2017-01-30 21:40:43 +02:00
2017-03-22 11:50:55 +02:00
return $this->showResult($results, $includeSettings);
2015-11-24 21:45:38 +02:00
} else {
2016-06-02 22:03:59 +03:00
$results = $this->importService->importFiles($source, $files);
2017-01-30 21:40:43 +02:00
2015-12-21 21:57:55 +02:00
return $this->showResult($results);
2015-11-18 19:16:23 +02:00
}
2015-11-24 21:45:38 +02:00
} catch (Exception $exception) {
2015-12-08 12:10:20 +02:00
Utils::logError($exception);
2015-11-24 21:45:38 +02:00
Session::flash('error', $exception->getMessage());
2017-01-30 21:40:43 +02:00
2015-12-15 22:25:12 +02:00
return Redirect::to('/settings/' . ACCOUNT_IMPORT_EXPORT);
2015-11-18 19:16:23 +02:00
}
}
public function doImportCSV()
{
$map = Input::get('map');
2015-11-24 21:45:38 +02:00
$headers = Input::get('headers');
2015-11-18 19:16:23 +02:00
2015-11-25 11:35:24 +02:00
try {
2015-12-21 21:57:55 +02:00
$results = $this->importService->importCSV($map, $headers);
2017-01-30 21:40:43 +02:00
2015-12-21 21:57:55 +02:00
return $this->showResult($results);
2015-11-25 11:35:24 +02:00
} catch (Exception $exception) {
2015-12-08 12:10:20 +02:00
Utils::logError($exception);
2015-11-25 11:35:24 +02:00
Session::flash('error', $exception->getMessage());
2017-01-30 21:40:43 +02:00
2015-12-15 22:25:12 +02:00
return Redirect::to('/settings/' . ACCOUNT_IMPORT_EXPORT);
}
}
2017-03-22 11:50:55 +02:00
private function showResult($results, $includeSettings = false)
2015-12-15 22:25:12 +02:00
{
2015-12-21 21:57:55 +02:00
$message = '';
$skipped = [];
2017-03-22 11:50:55 +02:00
if ($includeSettings) {
$message = trans('texts.imported_settings') . '<br/>';
}
2015-12-21 21:57:55 +02:00
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 22:25:12 +02:00
if (count($skipped)) {
2015-12-21 21:57:55 +02:00
$message .= '<p/>' . trans('texts.failed_to_import') . '<br/>';
2015-12-15 22:25:12 +02:00
foreach ($skipped as $skip) {
2015-12-21 21:57:55 +02:00
$message .= json_encode($skip) . '<br/>';
2015-12-15 22:25:12 +02:00
}
2015-12-21 21:57:55 +02:00
}
if ($message) {
2015-12-15 22:25:12 +02:00
Session::flash('warning', $message);
2015-11-25 11:35:24 +02:00
}
2015-11-18 16:40:50 +02:00
2015-11-18 19:16:23 +02:00
return Redirect::to('/settings/' . ACCOUNT_IMPORT_EXPORT);
2015-11-18 16:40:50 +02:00
}
}