mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-11 05:47:28 -08:00
Improved error checking in locations importer
This commit is contained in:
parent
27231d49ea
commit
cbff66c9db
|
@ -53,27 +53,30 @@ class ImportLocations extends Command
|
||||||
|
|
||||||
// Import parent location names first if they don't exist
|
// Import parent location names first if they don't exist
|
||||||
foreach ($results as $parent_index => $parent_row) {
|
foreach ($results as $parent_index => $parent_row) {
|
||||||
$parent_name = trim($parent_row['Parent Name']);
|
|
||||||
$this->info('- Parent: '.$parent_name.' in row as: '.trim($parent_row['Parent Name']));
|
|
||||||
// First create any parents if they don't exist
|
|
||||||
|
|
||||||
if ($parent_name!='') {
|
if (array_key_exists('Parent Name', $parent_row)) {
|
||||||
|
$parent_name = trim($parent_row['Parent Name']);
|
||||||
|
if (array_key_exists('Name', $parent_row)) {
|
||||||
|
$this->info('- Parent: ' . $parent_name . ' in row as: ' . trim($parent_row['Parent Name']));
|
||||||
|
}
|
||||||
|
|
||||||
// Save parent location name
|
// Save parent location name
|
||||||
// This creates a sort of name-stub that we'll update later on in this script
|
// This creates a sort of name-stub that we'll update later on in this script
|
||||||
$parent_location = Location::firstOrCreate(array('name' => $parent_name));
|
$parent_location = Location::firstOrCreate(array('name' => $parent_name));
|
||||||
$this->info('Parent for '.$parent_row['Name'].' is '.$parent_name.'. Attempting to save '.$parent_name.'.');
|
if (array_key_exists('Name', $parent_row)) {
|
||||||
|
$this->info('Parent for ' . $parent_row['Name'] . ' is ' . $parent_name . '. Attempting to save ' . $parent_name . '.');
|
||||||
|
}
|
||||||
|
|
||||||
// Check if the record was updated or created.
|
// Check if the record was updated or created.
|
||||||
// This is mostly for clearer debugging.
|
// This is mostly for clearer debugging.
|
||||||
if ($parent_location->exists) {
|
if ($parent_location->exists) {
|
||||||
$this->info('- Parent location '.$parent_name.' already exists.');
|
$this->info('- Parent location '.$parent_name.' already exists.');
|
||||||
} else {
|
} else {
|
||||||
$this->info('- Parent location '.$parent_name.' was created.');
|
$this->info('- Parent location '.$parent_name.' was created.');
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$this->info('- No parent location for '.$parent_row['Name'].' provided.');
|
$this->info('- No Parent Name provided, so no parent location will be created.');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -83,25 +86,49 @@ class ImportLocations extends Command
|
||||||
// besides name
|
// besides name
|
||||||
foreach ($results as $index => $row) {
|
foreach ($results as $index => $row) {
|
||||||
|
|
||||||
$parent_name = trim($row['Parent Name']);
|
if (array_key_exists('Parent Name', $row)) {
|
||||||
// Set the location attributes to save
|
$parent_name = trim($row['Parent Name']);
|
||||||
$location = Location::firstOrNew(array('name' => trim($row['Name'])));
|
}
|
||||||
$location->name = trim($row['Name']);
|
|
||||||
$location->currency = trim($row['Currency']);
|
// Set the location attributes to save
|
||||||
$location->address = trim($row['Address 1']);
|
if (array_key_exists('Name', $row)) {
|
||||||
$location->address2 = trim($row['Address 2']);
|
$location = Location::firstOrNew(array('name' => trim($row['Name'])));
|
||||||
$location->city = trim($row['City']);
|
$location->name = trim($row['Name']);
|
||||||
$location->state = trim($row['State']);
|
$this->info('Checking location: '.$location->name);
|
||||||
$location->zip = trim($row['Zip']);
|
} else {
|
||||||
$location->country = trim($row['Country']);
|
$this->error('Location name is required and is missing from at least one row in this dataset. Check your CSV for extra trailing rows and try again.');
|
||||||
$location->ldap_ou = trim($row['OU']);
|
return false;
|
||||||
|
}
|
||||||
|
if (array_key_exists('Currency', $row)) {
|
||||||
|
$location->currency = trim($row['Currency']);
|
||||||
|
}
|
||||||
|
if (array_key_exists('Address 1', $row)) {
|
||||||
|
$location->address = trim($row['Address 1']);
|
||||||
|
}
|
||||||
|
if (array_key_exists('Address 2', $row)) {
|
||||||
|
$location->address2 = trim($row['Address 2']);
|
||||||
|
}
|
||||||
|
if (array_key_exists('City', $row)) {
|
||||||
|
$location->city = trim($row['City']);
|
||||||
|
}
|
||||||
|
if (array_key_exists('State', $row)) {
|
||||||
|
$location->state = trim($row['State']);
|
||||||
|
}
|
||||||
|
if (array_key_exists('Zip', $row)) {
|
||||||
|
$location->zip = trim($row['Zip']);
|
||||||
|
}
|
||||||
|
if (array_key_exists('Country', $row)) {
|
||||||
|
$location->country = trim($row['Country']);
|
||||||
|
}
|
||||||
|
if (array_key_exists('Country', $row)) {
|
||||||
|
$location->ldap_ou = trim($row['OU']);
|
||||||
|
}
|
||||||
|
|
||||||
$this->info('Checking location: '.$location->name);
|
|
||||||
|
|
||||||
// If a parent name is provided, we created it earlier in the script,
|
// If a parent name is provided, we created it earlier in the script,
|
||||||
// so let's grab that ID
|
// so let's grab that ID
|
||||||
if ($parent_name) {
|
if ($parent_name) {
|
||||||
$this->info('-- Searching for Parent Name: '.$parent_name.' - '.$row['Parent Name']);
|
$this->info('-- Searching for Parent Name: '.$parent_name);
|
||||||
$parent = Location::where('name', '=', $parent_name)->first();
|
$parent = Location::where('name', '=', $parent_name)->first();
|
||||||
$location->parent_id = $parent->id;
|
$location->parent_id = $parent->id;
|
||||||
$this->info('Parent: '.$parent_name.' - ID: '.$parent->id);
|
$this->info('Parent: '.$parent_name.' - ID: '.$parent->id);
|
||||||
|
|
Loading…
Reference in a new issue