2016-12-26 15:16:42 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
2017-01-25 21:29:23 -08:00
|
|
|
use App\Models\Import;
|
2016-12-26 15:16:42 -08:00
|
|
|
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 [
|
|
|
|
//
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:29:23 -08:00
|
|
|
public function import(Import $import)
|
2016-12-26 15:16:42 -08:00
|
|
|
{
|
2017-01-25 21:29:23 -08:00
|
|
|
$filename = config('app.private_uploads') . '/imports/' . $import->file_path;
|
2017-01-05 15:45:12 -08:00
|
|
|
$class = title_case($this->input('import-type'));
|
|
|
|
$classString = "App\\Importer\\{$class}Importer";
|
2017-01-10 16:19:18 -08:00
|
|
|
$importer = new $classString($filename);
|
|
|
|
$importer->setCallbacks([$this, 'log'], [$this, 'progress'], [$this, 'errorCallback'])
|
|
|
|
->setUserId(Auth::id())
|
|
|
|
->setUpdating($this->has('import-update'))
|
|
|
|
->setUsernameFormat('firstname.lastname');
|
2016-12-26 15:16:42 -08:00
|
|
|
|
|
|
|
$importer->import();
|
|
|
|
return $this->errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function log($string)
|
|
|
|
{
|
|
|
|
return; // FUTURE IMPLEMENTATION
|
|
|
|
}
|
|
|
|
|
|
|
|
public function progress($count)
|
|
|
|
{
|
|
|
|
// Open for future
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
public function errorCallback($item, $field, $errorString)
|
|
|
|
{
|
|
|
|
$this->errors[$item->name][$field] = $errorString;
|
2017-01-25 21:29:23 -08:00
|
|
|
// $this->errors[$item->name] = $errorString;
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private $errors;
|
|
|
|
}
|