snipe-it/app/Http/Requests/ItemImportRequest.php
Daniel Meltzer 57374955a8 Importer rework (#3100)
* Step 1 of refactoring importer to use separate classes.

* Port web importer.  Fix an issue with validation where index 0 would be treated as false and cause weird results.

* Farewall, AssetImport.  You've served us well.
2016-12-26 15:16:42 -08:00

84 lines
1.9 KiB
PHP

<?php
namespace App\Http\Requests;
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 [
//
];
}
public function import()
{
$filename = config('app.private_uploads') . '/imports/assets/' . $this->get('filename');
$importerClass = Importer::class;
switch ($this->get('import-type')) {
case "asset":
$importerClass = 'App\Importer\AssetImporter';
break;
case "accessory":
$importerClass = 'App\Importer\AccessoryImporter';
break;
case "component":
die("This is not implemented yet");
$importerClass = ComponentImporter::class;
break;
case "consumable":
$importerClass = 'App\Importer\ConsumableImporter';
break;
}
$importer = new $importerClass(
$filename,
[$this, 'log'],
[$this, 'progress'],
[$this, 'errorCallback'],
false, /*testrun*/
Auth::id(),
$this->has('import-update'),
'firstname.lastname'
);
$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;
}
private $errors;
}