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

64 lines
1.8 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-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-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-18 18:16:23 +01:00
$result = $this->importService->import($source, $files);
Session::flash('message', trans('texts.imported_file') . ' - ' . $result);
}
2015-11-24 20:45:38 +01:00
} catch (Exception $exception) {
Session::flash('error', $exception->getMessage());
2015-11-18 18:16:23 +01:00
}
2015-11-24 20:45:38 +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-24 20:45:38 +01:00
//try {
$count = $this->importService->importCSV($map, $headers);
2015-11-18 18:16:23 +01:00
$message = Utils::pluralize('created_client', $count);
Session::flash('message', $message);
2015-11-24 20:45:38 +01:00
//} catch (Exception $exception) {
// Session::flash('error', $exception->getMessage());
//}
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
}
}