2017-01-25 21:29:23 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use App\Helpers\Helper;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Requests\ItemImportRequest;
|
|
|
|
use App\Http\Transformers\ImportsTransformer;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Import;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Input;
|
|
|
|
use Illuminate\Support\Facades\Session;
|
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
|
|
|
use League\Csv\Reader;
|
2017-01-25 21:29:23 -08:00
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
2017-09-25 15:00:23 -07:00
|
|
|
use Artisan;
|
2018-03-22 14:17:44 -07:00
|
|
|
use App\Models\Asset;
|
2017-01-25 21:29:23 -08:00
|
|
|
|
|
|
|
class ImportController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2019-03-18 11:58:08 -07:00
|
|
|
$this->authorize('import');
|
2017-01-25 21:29:23 -08:00
|
|
|
$imports = Import::latest()->get();
|
|
|
|
return (new ImportsTransformer)->transformImports($imports);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
|
|
|
* Process and store a CSV upload file.
|
2017-01-25 21:29:23 -08:00
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store()
|
|
|
|
{
|
2019-03-18 11:58:08 -07:00
|
|
|
$this->authorize('import');
|
|
|
|
if (!config('app.lock_passwords')) {
|
2017-01-25 21:29:23 -08:00
|
|
|
$files = Input::file('files');
|
|
|
|
$path = config('app.private_uploads').'/imports';
|
|
|
|
$results = [];
|
|
|
|
$import = new Import;
|
|
|
|
foreach ($files as $file) {
|
|
|
|
if (!in_array($file->getMimeType(), array(
|
|
|
|
'application/vnd.ms-excel',
|
|
|
|
'text/csv',
|
|
|
|
'text/plain',
|
|
|
|
'text/comma-separated-values',
|
|
|
|
'text/tsv'))) {
|
|
|
|
$results['error']='File type must be CSV';
|
2017-05-03 12:14:35 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $results['error']), 500);
|
2017-01-25 21:29:23 -08:00
|
|
|
}
|
|
|
|
|
2018-03-22 19:16:15 -07:00
|
|
|
//TODO: is there a lighter way to do this?
|
|
|
|
if (! ini_get("auto_detect_line_endings")) {
|
|
|
|
ini_set("auto_detect_line_endings", '1');
|
|
|
|
}
|
|
|
|
$reader = Reader::createFromFileObject($file->openFile('r')); //file pointer leak?
|
|
|
|
$import->header_row = $reader->fetchOne(0);
|
|
|
|
|
|
|
|
//duplicate headers check
|
|
|
|
$duplicate_headers = [];
|
|
|
|
|
|
|
|
for($i = 0; $i<count($import->header_row); $i++) {
|
|
|
|
$header = $import->header_row[$i];
|
|
|
|
if(in_array($header, $import->header_row)) {
|
|
|
|
$found_at = array_search($header, $import->header_row);
|
|
|
|
if($i > $found_at) {
|
|
|
|
//avoid reporting duplicates twice, e.g. "1 is same as 17! 17 is same as 1!!!"
|
|
|
|
//as well as "1 is same as 1!!!" (which is always true)
|
|
|
|
//has to be > because otherwise the first result of array_search will always be $i itself(!)
|
2018-03-22 19:40:12 -07:00
|
|
|
array_push($duplicate_headers,"Duplicate header '$header' detected, first at column: ".($found_at+1).", repeats at column: ".($i+1));
|
2018-03-22 19:16:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(count($duplicate_headers) > 0) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error',null, implode("; ",$duplicate_headers)), 500); //should this be '4xx'?
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the first row to display via ajax as the user picks fields
|
|
|
|
$import->first_row = $reader->fetchOne(1);
|
|
|
|
|
2017-01-25 21:29:23 -08:00
|
|
|
$date = date('Y-m-d-his');
|
2017-07-14 02:38:13 -07:00
|
|
|
$fixed_filename = str_slug($file->getClientOriginalName());
|
2017-01-25 21:29:23 -08:00
|
|
|
try {
|
|
|
|
$file->move($path, $date.'-'.$fixed_filename);
|
|
|
|
} catch (FileException $exception) {
|
|
|
|
$results['error']=trans('admin/hardware/message.upload.error');
|
|
|
|
if (config('app.debug')) {
|
|
|
|
$results['error'].= ' ' . $exception->getMessage();
|
|
|
|
}
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $results['error']), 500);
|
|
|
|
}
|
|
|
|
$file_name = date('Y-m-d-his').'-'.$fixed_filename;
|
|
|
|
$import->file_path = $file_name;
|
|
|
|
$import->filesize = filesize($path.'/'.$file_name);
|
|
|
|
$import->save();
|
|
|
|
$results[] = $import;
|
|
|
|
}
|
|
|
|
$results = (new ImportsTransformer)->transformImports($results);
|
|
|
|
return [
|
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
|
|
|
'files' => $results,
|
2017-01-25 21:29:23 -08:00
|
|
|
];
|
|
|
|
}
|
2017-02-22 22:49:06 -08:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.feature_disabled')), 500);
|
2017-01-25 21:29:23 -08:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Processes the specified Import.
|
|
|
|
*
|
|
|
|
* @param \App\Import $import
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function process(ItemImportRequest $request, $import_id)
|
|
|
|
{
|
2019-03-18 11:58:08 -07:00
|
|
|
$this->authorize('import');
|
2017-09-25 15:00:23 -07:00
|
|
|
// Run a backup immediately before processing
|
2020-05-11 20:41:10 -07:00
|
|
|
|
|
|
|
if ($request->has('run-backup')) {
|
|
|
|
\Log::debug('Backup manually requested via importer');
|
|
|
|
Artisan::call('backup:run');
|
|
|
|
} else {
|
|
|
|
\Log::debug('NO BACKUP requested via importer');
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:29:23 -08:00
|
|
|
$errors = $request->import(Import::find($import_id));
|
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
|
|
|
$redirectTo = "hardware.index";
|
2017-01-25 21:29:23 -08:00
|
|
|
switch ($request->get('import-type')) {
|
|
|
|
case "asset":
|
|
|
|
$redirectTo = "hardware.index";
|
|
|
|
break;
|
|
|
|
case "accessory":
|
|
|
|
$redirectTo = "accessories.index";
|
|
|
|
break;
|
|
|
|
case "consumable":
|
|
|
|
$redirectTo = "consumables.index";
|
|
|
|
break;
|
|
|
|
case "component":
|
|
|
|
$redirectTo = "components.index";
|
|
|
|
break;
|
|
|
|
case "license":
|
|
|
|
$redirectTo = "licenses.index";
|
|
|
|
break;
|
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
|
|
|
case "user":
|
|
|
|
$redirectTo = "users.index";
|
|
|
|
break;
|
2017-01-25 21:29:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($errors) { //Failure
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('import-errors', null, $errors), 500);
|
|
|
|
}
|
|
|
|
//Flash message before the redirect
|
|
|
|
Session::flash('success', trans('admin/hardware/message.import.success'));
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', null, ['redirect_url' => route($redirectTo)]));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param \App\Import $import
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function destroy($import_id)
|
|
|
|
{
|
2019-03-18 11:58:08 -07:00
|
|
|
$this->authorize('import');
|
2017-01-25 21:29:23 -08:00
|
|
|
$import = Import::find($import_id);
|
|
|
|
try {
|
|
|
|
unlink(config('app.private_uploads').'/imports/'.$import->file_path);
|
|
|
|
$import->delete();
|
2017-05-03 12:14:35 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/hardware/message.import.file_delete_success')));
|
2017-01-25 21:29:23 -08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.import.file_delete_error')), 500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|