mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-09 23:24:06 -08:00
934afa036f
Shift automatically applies the Laravel coding style - which uses the PSR-2 coding style as a base with some minor additions. You may customize the adopted coding style by adding your own [PHP CS Fixer][1] `.php_cs` config file to your project root. Feel free to use [Shift's Laravel ruleset][2] to help you get started. [1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer [2]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200
93 lines
2.6 KiB
PHP
93 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Models\Import;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class ItemImportRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function authorize()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
'import-type' => 'required',
|
|
];
|
|
}
|
|
|
|
public function import(Import $import)
|
|
{
|
|
ini_set('max_execution_time', env('IMPORT_TIME_LIMIT', 600)); //600 seconds = 10 minutes
|
|
ini_set('memory_limit', env('IMPORT_MEMORY_LIMIT', '500M'));
|
|
|
|
$filename = config('app.private_uploads').'/imports/'.$import->file_path;
|
|
$import->import_type = $this->input('import-type');
|
|
$class = title_case($import->import_type);
|
|
$classString = "App\\Importer\\{$class}Importer";
|
|
$importer = new $classString($filename);
|
|
$import->field_map = request('column-mappings');
|
|
$import->save();
|
|
$fieldMappings = [];
|
|
|
|
if ($import->field_map) {
|
|
foreach ($import->field_map as $field => $fieldValue) {
|
|
$errorMessage = null;
|
|
|
|
if (is_null($fieldValue)) {
|
|
$errorMessage = trans('validation.import_field_empty');
|
|
$this->errorCallback($import, $field, $errorMessage);
|
|
|
|
return $this->errors;
|
|
}
|
|
}
|
|
// We submit as csv field: column, but the importer is happier if we flip it here.
|
|
$fieldMappings = array_change_key_case(array_flip($import->field_map), CASE_LOWER);
|
|
// dd($fieldMappings);
|
|
}
|
|
$importer->setCallbacks([$this, 'log'], [$this, 'progress'], [$this, 'errorCallback'])
|
|
->setUserId(Auth::id())
|
|
->setUpdating($this->has('import-update'))
|
|
->setShouldNotify($this->has('send-welcome'))
|
|
->setUsernameFormat('firstname.lastname')
|
|
->setFieldMappings($fieldMappings);
|
|
// $logFile = storage_path('logs/importer.log');
|
|
// \Log::useFiles($logFile);
|
|
$importer->import();
|
|
|
|
return $this->errors;
|
|
}
|
|
|
|
public function log($string)
|
|
{
|
|
\Log::Info($string);
|
|
}
|
|
|
|
public function progress($count)
|
|
{
|
|
// Open for future
|
|
|
|
}
|
|
|
|
public function errorCallback($item, $field, $errorString)
|
|
{
|
|
$this->errors[$item->name][$field] = $errorString;
|
|
}
|
|
|
|
private $errors;
|
|
}
|