1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00
invoiceninja/app/Http/Controllers/ImportController.php

82 lines
2.4 KiB
PHP
Raw Normal View History

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)
{
parent::__construct();
$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-25 10:35:24 +01:00
$skipped = [];
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-11-25 10:35:24 +01:00
$skipped = $this->importService->import($source, $files);
2015-12-15 21:25:12 +01:00
return $this->showResult($skipped);
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-25 10:35:24 +01:00
$skipped = [];
2015-11-18 18:16:23 +01:00
2015-11-25 10:35:24 +01:00
try {
$skipped = $this->importService->importCSV($map, $headers);
2015-12-15 21:25:12 +01:00
return $this->showResult($skipped);
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);
}
}
private function showResult($skipped)
{
if (count($skipped)) {
$message = trans('texts.failed_to_import');
foreach ($skipped as $skip) {
$message .= '<br/>' . json_encode($skip);
}
Session::flash('warning', $message);
} else {
Session::flash('message', trans('texts.imported_file'));
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
}
}