Working on import

This commit is contained in:
Hillel Coren 2017-04-02 18:10:24 +03:00
parent 7a13d93082
commit 6e4d3536dc
3 changed files with 40 additions and 14 deletions

View File

@ -22,6 +22,10 @@ class ImportController extends BaseController
public function doImport(Request $request) public function doImport(Request $request)
{ {
if (! Auth::user()->confirmed) {
return redirect('/settings/' . ACCOUNT_IMPORT_EXPORT)->withError(trans('texts.confirm_account_to_import'));
}
$source = Input::get('source'); $source = Input::get('source');
$files = []; $files = [];
$timestamp = time(); $timestamp = time();

View File

@ -148,8 +148,9 @@ class ImportService
public function importJSON($file, $includeData, $includeSettings) public function importJSON($file, $includeData, $includeSettings)
{ {
$this->initMaps(); $this->initMaps();
$fileName = storage_path() . '/import/' . $file;
$file = file_get_contents(storage_path() . '/import/' . $file); $this->checkForFile($fileName);
$file = file_get_contents($fileName);
$json = json_decode($file, true); $json = json_decode($file, true);
$json = $this->removeIdFields($json); $json = $this->removeIdFields($json);
$transformer = new BaseTransformer($this->maps); $transformer = new BaseTransformer($this->maps);
@ -166,7 +167,7 @@ class ImportService
$settings[$field] = $value; $settings[$field] = $value;
} }
} }
//dd($settings);
$account = Auth::user()->account; $account = Auth::user()->account;
$account->fill($settings); $account->fill($settings);
$account->save(); $account->save();
@ -228,6 +229,8 @@ class ImportService
} }
} }
unlink($fileName);
return $this->results; return $this->results;
} }
@ -284,9 +287,10 @@ class ImportService
// Convert the data // Convert the data
$row_list = []; $row_list = [];
$file = storage_path() . '/import/' . $file; $fileName = storage_path() . '/import/' . $file;
$this->checkForFile($fileName);
Excel::load($file, function ($reader) use ($source, $entityType, &$row_list, &$results) { Excel::load($fileName, function ($reader) use ($source, $entityType, &$row_list, &$results) {
$this->checkData($entityType, count($reader->all())); $this->checkData($entityType, count($reader->all()));
$reader->each(function ($row) use ($source, $entityType, &$row_list, &$results) { $reader->each(function ($row) use ($source, $entityType, &$row_list, &$results) {
@ -317,6 +321,8 @@ class ImportService
} }
} }
unlink($fileName);
return $results; return $results;
} }
@ -568,8 +574,6 @@ class ImportService
} }
} }
//Session::put("{$entityType}-data", $csv->data);
$data = [ $data = [
'entityType' => $entityType, 'entityType' => $entityType,
'data' => $data, 'data' => $data,
@ -582,12 +586,16 @@ class ImportService
return $data; return $data;
} }
private function getCsvData($filename) private function getCsvData($fileName)
{ {
require_once app_path().'/Includes/parsecsv.lib.php'; require_once app_path().'/Includes/parsecsv.lib.php';
$fileName = storage_path() . '/import/' . $fileName;
$this->checkForFile($fileName);
$csv = new parseCSV(); $csv = new parseCSV();
$csv->heading = false; $csv->heading = false;
$csv->auto(storage_path() . '/import/' . $filename); $csv->auto($fileName);
$data = $csv->data; $data = $csv->data;
if (count($data) > 0) { if (count($data) > 0) {
@ -678,9 +686,8 @@ class ImportService
]; ];
$source = IMPORT_CSV; $source = IMPORT_CSV;
//$data = Session::get("{$entityType}-data"); $fileName = sprintf('%s_%s_%s.csv', Auth::user()->account_id, $timestamp, $entityType);
$filename = sprintf('%s_%s_%s.csv', Auth::user()->account_id, $timestamp, $entityType); $data = $this->getCsvData($fileName);
$data = $this->getCsvData($filename);
$this->checkData($entityType, count($data)); $this->checkData($entityType, count($data));
$this->initMaps(); $this->initMaps();
@ -719,7 +726,7 @@ class ImportService
} }
} }
//Session::forget("{$entityType}-data"); unlink($fileName);
return $results; return $results;
} }
@ -934,4 +941,19 @@ class ImportService
return $message; return $message;
} }
private function checkForFile($fileName)
{
$counter = 0;
while (! file_exists($fileName)) {
$counter++;
if ($counter > 60) {
throw new Exception('File not found: ' . $fileName);
}
sleep(2);
}
return true;
}
} }

View File

@ -2461,7 +2461,7 @@ $LANG = array(
'reply_to_email_help' => 'Specify the reply-to address for client emails.', 'reply_to_email_help' => 'Specify the reply-to address for client emails.',
'bcc_email_help' => 'Privately include this address with client emails.', 'bcc_email_help' => 'Privately include this address with client emails.',
'import_complete' => 'Your import has successfully completed.', 'import_complete' => 'Your import has successfully completed.',
'confirm_account_to_import' => 'Please confirm your account to import data.',
); );