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;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Models\Asset;
|
2017-01-25 21:29:23 -08:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Import;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Artisan;
|
2020-01-17 16:12:51 -08:00
|
|
|
use Illuminate\Support\Facades\Request;
|
2017-01-25 21:29:23 -08:00
|
|
|
use Illuminate\Support\Facades\Session;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
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;
|
|
|
|
|
|
|
|
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')) {
|
2020-01-17 16:12:51 -08:00
|
|
|
$files = Request::file('files');
|
2017-01-25 21:29:23 -08:00
|
|
|
$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.
|
|
|
|
*
|
2018-08-01 18:01:16 -07:00
|
|
|
* @param int $import_id
|
2017-01-25 21:29:23 -08:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function process(ItemImportRequest $request, $import_id)
|
|
|
|
{
|
2019-03-18 11:58:08 -07:00
|
|
|
$this->authorize('import');
|
2020-05-11 22:57:55 -07:00
|
|
|
|
2017-09-25 15:00:23 -07:00
|
|
|
// Run a backup immediately before processing
|
2020-05-11 22:57:55 -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.
|
|
|
|
*
|
2018-08-01 18:01:16 -07:00
|
|
|
* @param int $import_id
|
2017-01-25 21:29:23 -08:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function destroy($import_id)
|
|
|
|
{
|
|
|
|
$this->authorize('create', Asset::class);
|
2018-08-01 20:49:55 -07:00
|
|
|
|
|
|
|
if ($import = Import::find($import_id)) {
|
|
|
|
try {
|
|
|
|
// Try to delete the file
|
2018-09-29 21:33:52 -07:00
|
|
|
Storage::delete('imports/'.$import->file_path);
|
2018-08-01 20:49:55 -07:00
|
|
|
$import->delete();
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/hardware/message.import.file_delete_success')));
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// If the file delete didn't work, remove it from the database anyway and return a warning
|
|
|
|
$import->delete();
|
2018-08-02 09:53:54 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('warning', null, trans('admin/hardware/message.import.file_not_deleted_warning')));
|
2018-08-01 20:49:55 -07:00
|
|
|
}
|
2017-01-25 21:29:23 -08:00
|
|
|
}
|
2018-08-01 18:01:16 -07:00
|
|
|
|
2017-01-25 21:29:23 -08:00
|
|
|
}
|
|
|
|
}
|