1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/ImportController.php
2015-12-15 22:25:12 +02:00

82 lines
2.4 KiB
PHP

<?php namespace app\Http\Controllers;
use Utils;
use View;
use Exception;
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()
{
$source = Input::get('source');
$files = [];
$skipped = [];
foreach (ImportService::$entityTypes as $entityType) {
if (Input::file("{$entityType}_file")) {
$files[$entityType] = Input::file("{$entityType}_file")->getRealPath();
if ($source === IMPORT_CSV) {
Session::forget("{$entityType}-data");
}
}
}
try {
if ($source === IMPORT_CSV) {
$data = $this->importService->mapCSV($files);
return View::make('accounts.import_map', ['data' => $data]);
} else {
$skipped = $this->importService->import($source, $files);
return $this->showResult($skipped);
}
} catch (Exception $exception) {
Utils::logError($exception);
Session::flash('error', $exception->getMessage());
return Redirect::to('/settings/' . ACCOUNT_IMPORT_EXPORT);
}
}
public function doImportCSV()
{
$map = Input::get('map');
$headers = Input::get('headers');
$skipped = [];
try {
$skipped = $this->importService->importCSV($map, $headers);
return $this->showResult($skipped);
} catch (Exception $exception) {
Utils::logError($exception);
Session::flash('error', $exception->getMessage());
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'));
}
return Redirect::to('/settings/' . ACCOUNT_IMPORT_EXPORT);
}
}