Honor the “update” flag
Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
parent
345662c5c2
commit
fd5cb32dd9
1 changed files with 27 additions and 16 deletions
|
@ -6,7 +6,7 @@ use App\Models\Location;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When we are importing users via an Asset/etc import, we use createOrFetchUser() in
|
* When we are importing users via an Asset/etc import, we use createOrFetchUser() in
|
||||||
* App\Importer.php. [ALG]
|
* Importer\Importer.php. [ALG]
|
||||||
*
|
*
|
||||||
* Class LocationImporter
|
* Class LocationImporter
|
||||||
*/
|
*/
|
||||||
|
@ -36,6 +36,22 @@ class LocationImporter extends ItemImporter
|
||||||
public function createLocationIfNotExists(array $row)
|
public function createLocationIfNotExists(array $row)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
$editingLocation = false;
|
||||||
|
$location = Location::where('name', '=', $this->findCsvMatch($row, 'name'))->first();
|
||||||
|
|
||||||
|
if ($location) {
|
||||||
|
if (! $this->updating) {
|
||||||
|
$this->log('A matching Location '.$this->item['name'].' already exists');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->log('Updating Location');
|
||||||
|
$editingLocation = true;
|
||||||
|
} else {
|
||||||
|
$this->log('No Matching Location, Create a new one');
|
||||||
|
$location = new Location;
|
||||||
|
}
|
||||||
|
|
||||||
// Pull the records from the CSV to determine their values
|
// Pull the records from the CSV to determine their values
|
||||||
$this->item['name'] = $this->findCsvMatch($row, 'name');
|
$this->item['name'] = $this->findCsvMatch($row, 'name');
|
||||||
$this->item['address'] = $this->findCsvMatch($row, 'address');
|
$this->item['address'] = $this->findCsvMatch($row, 'address');
|
||||||
|
@ -54,31 +70,26 @@ class LocationImporter extends ItemImporter
|
||||||
$this->item['parent_id'] = $this->createOrFetchLocation($this->findCsvMatch($row, 'parent_location'));
|
$this->item['parent_id'] = $this->createOrFetchLocation($this->findCsvMatch($row, 'parent_location'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!empty($this->item['manager'])) {
|
||||||
if ($manager = $this->createOrFetchUser($row, 'manager')) {
|
if ($manager = $this->createOrFetchUser($row, 'manager')) {
|
||||||
$this->item['manager_id'] = $manager->id;
|
$this->item['manager_id'] = $manager->id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
\Log::debug('Item array is: ');
|
||||||
|
|
||||||
\Log::debug(print_r($this->item, true));
|
\Log::debug(print_r($this->item, true));
|
||||||
$location = Location::where('name', '=', $this->findCsvMatch($row, 'name'))->first();
|
|
||||||
|
|
||||||
|
|
||||||
// Location exists
|
if ($editingLocation) {
|
||||||
if (($location) && ($location->count() > 0)) {
|
\Log::debug('Updating existing location');
|
||||||
\Log::debug('A matching Location '.$this->item['name'].' already exists.');
|
$location->update($this->sanitizeItemForUpdating($location));
|
||||||
$this->log('Updating Location from CSV import');
|
|
||||||
$location->update($this->sanitizeItemForStoring($location));
|
|
||||||
// Location does not exist
|
|
||||||
} else {
|
} else {
|
||||||
$this->log('No matching location ('.$this->item['name'].'), creating one');
|
\Log::debug('Creating location');
|
||||||
$location = new Location();
|
|
||||||
$location->fill($this->sanitizeItemForStoring($location));
|
$location->fill($this->sanitizeItemForStoring($location));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($location->save()) {
|
if ($location->save()) {
|
||||||
$this->log('Location '.$location->name.' created from CSV import');
|
$this->log('Location '.$location->name.' created or updated from CSV import');
|
||||||
return $location;
|
return $location;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Reference in a new issue