2016-12-26 15:16:42 -08:00
|
|
|
<?php
|
|
|
|
namespace App\Importer;
|
|
|
|
|
|
|
|
use App\Models\CustomField;
|
|
|
|
use App\Models\Setting;
|
|
|
|
use App\Models\User;
|
|
|
|
use ForceUTF8\Encoding;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use League\Csv\Reader;
|
|
|
|
|
|
|
|
abstract class Importer
|
|
|
|
{
|
2017-07-11 20:37:02 -07:00
|
|
|
protected $csv;
|
2016-12-26 15:16:42 -08:00
|
|
|
/**
|
|
|
|
* Id of User performing import
|
|
|
|
* @var
|
|
|
|
*/
|
|
|
|
protected $user_id;
|
|
|
|
/**
|
|
|
|
* Are we updating items in the import
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $updating;
|
2017-07-11 20:37:02 -07:00
|
|
|
/**
|
|
|
|
* Default Map of item fields->csv names
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $defaultFieldMap = [
|
|
|
|
'asset_tag' => 'asset tag',
|
|
|
|
'category' => 'category',
|
|
|
|
'company' => 'company',
|
|
|
|
'item_name' => 'item name',
|
|
|
|
'image' => 'image',
|
|
|
|
'expiration_date' => 'expiration date',
|
|
|
|
'location' => 'location',
|
|
|
|
'notes' => 'notes',
|
|
|
|
'license_email' => 'licensed to email',
|
|
|
|
'license_name' => "licensed to name",
|
|
|
|
'maintained' => 'maintained',
|
|
|
|
'manufacturer' => 'manufacturer',
|
|
|
|
'asset_model' => "model name",
|
|
|
|
'model_number' => 'model number',
|
|
|
|
'order_number' => 'order number',
|
|
|
|
'purchase_cost' => 'purchase cost',
|
|
|
|
'purchase_date' => 'purchase date',
|
|
|
|
'purchase_order' => 'purchase order',
|
|
|
|
'qty' => 'quantity',
|
|
|
|
'reassignable' => 'reassignable',
|
|
|
|
'requestable' => 'requestable',
|
|
|
|
'seats' => 'seats',
|
|
|
|
'serial_number' => 'serial number',
|
|
|
|
'status' => 'status',
|
|
|
|
'supplier' => 'supplier',
|
|
|
|
'termination_date' => 'termination date',
|
|
|
|
'warranty_months' => 'warranty',
|
|
|
|
'name' => 'name',
|
|
|
|
'email' => 'email',
|
|
|
|
'username' => 'username'
|
|
|
|
];
|
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
|
|
|
/**
|
|
|
|
* Map of item fields->csv names
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fieldMap = [];
|
2016-12-26 15:16:42 -08:00
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
protected $logCallback;
|
|
|
|
protected $tempPassword;
|
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
protected $progressCallback;
|
|
|
|
/**
|
|
|
|
* @var null
|
|
|
|
*/
|
|
|
|
protected $usernameFormat;
|
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
protected $errorCallback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ObjectImporter constructor.
|
2017-07-11 20:37:02 -07:00
|
|
|
* @param string $file
|
2016-12-26 15:16:42 -08:00
|
|
|
*/
|
2017-07-11 20:37:02 -07:00
|
|
|
public function __construct($file)
|
2017-01-25 21:29:23 -08:00
|
|
|
{
|
2017-07-11 20:37:02 -07:00
|
|
|
$this->fieldMap = $this->defaultFieldMap;
|
|
|
|
// By default the importer passes a url to the file.
|
|
|
|
// However, for testing we also support passing a string directly
|
|
|
|
if (is_file($file)) {
|
|
|
|
$this->csv = Reader::createFromPath($file);
|
|
|
|
} else {
|
|
|
|
$this->csv = Reader::createFromString($file);
|
|
|
|
}
|
2016-12-26 15:16:42 -08:00
|
|
|
$this->csv->setNewLine('\r\n');
|
|
|
|
if (! ini_get("auto_detect_line_endings")) {
|
|
|
|
ini_set("auto_detect_line_endings", '1');
|
|
|
|
}
|
|
|
|
$this->tempPassword = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 20);
|
|
|
|
}
|
|
|
|
// Cached Values for import lookups
|
|
|
|
protected $customFields;
|
|
|
|
|
|
|
|
public function import()
|
|
|
|
{
|
2017-07-11 20:37:02 -07:00
|
|
|
$headerRow = $this->csv->fetchOne();
|
|
|
|
$results = $this->normalizeInputArray($this->csv->fetchAssoc());
|
2017-01-25 21:29:23 -08:00
|
|
|
$this->customFields = CustomField::All(['name']);
|
2016-12-26 15:16:42 -08:00
|
|
|
DB::transaction(function () use (&$results) {
|
|
|
|
Model::unguard();
|
|
|
|
$resultsCount = sizeof($results);
|
|
|
|
foreach ($results as $row) {
|
|
|
|
$this->handle($row);
|
2017-07-11 20:37:02 -07:00
|
|
|
if ($this->progressCallback) {
|
|
|
|
call_user_func($this->progressCallback, $resultsCount);
|
|
|
|
}
|
2016-12-26 15:16:42 -08:00
|
|
|
|
|
|
|
$this->log('------------- Action Summary ----------------');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract protected function handle($row);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check to see if the given key exists in the array, and trim excess white space before returning it
|
|
|
|
*
|
|
|
|
* @author Daniel Melzter
|
|
|
|
* @since 3.0
|
|
|
|
* @param $array array
|
|
|
|
* @param $key string
|
|
|
|
* @param $default string
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-08-31 11:14:21 -07:00
|
|
|
public function findCsvMatch(array $array, $key, $default = null)
|
2016-12-26 15:16:42 -08:00
|
|
|
{
|
2017-07-11 20:37:02 -07:00
|
|
|
|
2017-01-10 16:19:18 -08:00
|
|
|
$val = $default;
|
2017-07-11 20:37:02 -07:00
|
|
|
|
2017-08-31 11:14:21 -07:00
|
|
|
$key = $this->lookupCustomKey($key);
|
2017-07-11 20:37:02 -07:00
|
|
|
|
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
|
|
|
$this->log("Custom Key: ${key}");
|
|
|
|
if (array_key_exists($key, $array)) {
|
2017-01-25 03:10:18 -08:00
|
|
|
$val = e(Encoding::toUTF8(trim($array[ $key ])));
|
2017-01-10 16:19:18 -08:00
|
|
|
}
|
2017-01-25 21:29:23 -08:00
|
|
|
// $this->log("${key}: ${val}");
|
2017-01-10 16:19:18 -08:00
|
|
|
return $val;
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Looks up A custom key in the custom field map
|
|
|
|
*
|
|
|
|
* @author Daniel Melzter
|
|
|
|
* @since 4.0
|
|
|
|
* @param $key string
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function lookupCustomKey($key)
|
|
|
|
{
|
|
|
|
// dd($this->fieldMap);
|
2017-07-11 20:37:02 -07:00
|
|
|
|
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
|
|
|
if (array_key_exists($key, $this->fieldMap)) {
|
2017-07-11 20:37:02 -07:00
|
|
|
$this->log("Found a match in our custom map: {$key} is " . $this->fieldMap[$key]);
|
|
|
|
return $this->fieldMap[$key];
|
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
|
|
|
}
|
2017-08-31 11:14:21 -07:00
|
|
|
// Otherwise no custom key, return original.
|
|
|
|
return $key;
|
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
|
|
|
}
|
2017-07-11 20:37:02 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $results
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function normalizeInputArray($results)
|
|
|
|
{
|
|
|
|
$newArray = [];
|
|
|
|
foreach ($results as $index => $arrayToNormalize) {
|
|
|
|
$newArray[$index] = array_change_key_case($arrayToNormalize);
|
|
|
|
}
|
|
|
|
return $newArray;
|
|
|
|
}
|
2016-12-26 15:16:42 -08:00
|
|
|
/**
|
|
|
|
* Figure out the fieldname of the custom field
|
|
|
|
*
|
|
|
|
* @author A. Gianotto <snipe@snipe.net>
|
|
|
|
* @since 3.0
|
|
|
|
* @param $array array
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function array_smart_custom_field_fetch(array $array, $key)
|
|
|
|
{
|
|
|
|
$index_name = strtolower($key->name);
|
2017-05-03 12:14:35 -07:00
|
|
|
return array_key_exists($index_name, $array) ? e(trim($array[$index_name])) : false;
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
|
|
|
|
2016-12-29 14:02:18 -08:00
|
|
|
protected function log($string)
|
|
|
|
{
|
2017-07-11 20:37:02 -07:00
|
|
|
if ($this->logCallback) {
|
|
|
|
call_user_func($this->logCallback, $string);
|
|
|
|
}
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
protected function logError($item, $field)
|
2016-12-29 14:02:18 -08:00
|
|
|
{
|
2017-07-11 20:37:02 -07:00
|
|
|
if ($this->errorCallback) {
|
|
|
|
call_user_func($this->errorCallback, $item, $field, $item->getErrors());
|
|
|
|
}
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the user matching given data, or creates a new one if there is no match
|
|
|
|
*
|
|
|
|
* @author Daniel Melzter
|
|
|
|
* @since 3.0
|
|
|
|
* @param $row array
|
|
|
|
* @return User Model w/ matching name
|
|
|
|
* @internal param string $user_username Username extracted from CSV
|
|
|
|
* @internal param string $user_email Email extracted from CSV
|
|
|
|
* @internal param string $first_name
|
|
|
|
* @internal param string $last_name
|
|
|
|
*/
|
|
|
|
protected function createOrFetchUser($row)
|
|
|
|
{
|
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
|
|
|
$user_name = $this->findCsvMatch($row, "name");
|
|
|
|
$user_email = $this->findCsvMatch($row, "email");
|
|
|
|
$user_username = $this->findCsvMatch($row, "username");
|
2016-12-26 15:16:42 -08:00
|
|
|
$first_name = '';
|
|
|
|
$last_name = '';
|
|
|
|
// A number was given instead of a name
|
|
|
|
if (is_numeric($user_name)) {
|
|
|
|
$this->log('User '.$user_name.' is not a name - assume this user already exists');
|
|
|
|
$user_username = '';
|
|
|
|
// No name was given
|
|
|
|
} elseif (empty($user_name)) {
|
2017-07-11 20:37:02 -07:00
|
|
|
|
2016-12-26 15:16:42 -08:00
|
|
|
$this->log('No user data provided - skipping user creation, just adding asset');
|
|
|
|
//$user_username = '';
|
|
|
|
} else {
|
|
|
|
$user_email_array = User::generateFormattedNameFromFullName(Setting::getSettings()->email_format, $user_name);
|
|
|
|
$first_name = $user_email_array['first_name'];
|
|
|
|
$last_name = $user_email_array['last_name'];
|
|
|
|
|
|
|
|
if ($user_email=='') {
|
|
|
|
if (Setting::getSettings()->email_domain) {
|
|
|
|
$user_email = str_slug($user_email_array['username']).'@'.Setting::getSettings()->email_domain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user_username=='') {
|
|
|
|
if ($this->usernameFormat =='email') {
|
|
|
|
$user_username = $user_email;
|
|
|
|
} else {
|
|
|
|
$user_name_array = User::generateFormattedNameFromFullName(Setting::getSettings()->username_format, $user_name);
|
|
|
|
$user_username = $user_name_array['username'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$user = new User;
|
2017-07-11 20:37:02 -07:00
|
|
|
|
2016-12-26 15:16:42 -08:00
|
|
|
if (!empty($user_username)) {
|
2017-07-11 20:37:02 -07:00
|
|
|
|
2016-12-26 15:16:42 -08:00
|
|
|
if ($user = User::MatchEmailOrUsername($user_username, $user_email)
|
|
|
|
->whereNotNull('username')->first()) {
|
|
|
|
$this->log('User '.$user_username.' already exists');
|
|
|
|
} elseif (( $first_name != '') && ($last_name != '') && ($user_username != '')) {
|
|
|
|
$user = new User;
|
|
|
|
$user->first_name = $first_name;
|
|
|
|
$user->last_name = $last_name;
|
|
|
|
$user->username = $user_username;
|
|
|
|
$user->email = $user_email;
|
|
|
|
$user->activated = 1;
|
|
|
|
$user->password = $this->tempPassword;
|
|
|
|
|
|
|
|
if ($user->save()) {
|
|
|
|
$this->log('User '.$first_name.' created');
|
|
|
|
} else {
|
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
|
|
|
$this->logError($user, 'User "' . $first_name . '"');
|
2016-12-26 15:16:42 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $user;
|
|
|
|
}
|
2017-01-10 16:19:18 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Id of User performing import.
|
|
|
|
*
|
|
|
|
* @param mixed $user_id the user id
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setUserId($user_id)
|
|
|
|
{
|
|
|
|
$this->user_id = $user_id;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Are we updating items in the import.
|
|
|
|
*
|
|
|
|
* @param bool $updating the updating
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setUpdating($updating)
|
|
|
|
{
|
|
|
|
$this->updating = $updating;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Defines mappings of csv fields
|
|
|
|
*
|
|
|
|
* @param bool $updating the updating
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setFieldMappings($fields)
|
|
|
|
{
|
|
|
|
// Some initial sanitization.
|
2017-07-11 20:37:02 -07:00
|
|
|
$fields = array_map('strtolower', $fields);
|
|
|
|
$this->fieldMap = array_merge($this->defaultFieldMap, $fields);
|
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
|
|
|
|
2017-07-11 20:37:02 -07:00
|
|
|
// $this->log($this->fieldMap);
|
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
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-01-10 16:19:18 -08:00
|
|
|
/**
|
|
|
|
* Sets the callbacks for the import
|
|
|
|
*
|
|
|
|
* @param callable $logCallback Function to call when we have data to log
|
|
|
|
* @param callable $progressCallback Function to call to display progress
|
|
|
|
* @param callable $errorCallback Function to call when we have errors
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setCallbacks(callable $logCallback, callable $progressCallback, callable $errorCallback)
|
|
|
|
{
|
|
|
|
$this->logCallback = $logCallback;
|
|
|
|
$this->progressCallback = $progressCallback;
|
|
|
|
$this->errorCallback = $errorCallback;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Sets the value of usernameFormat.
|
|
|
|
*
|
|
|
|
* @param string $usernameFormat the username format
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setUsernameFormat($usernameFormat)
|
|
|
|
{
|
|
|
|
$this->usernameFormat = $usernameFormat;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2016-12-29 14:02:18 -08:00
|
|
|
}
|