2023-04-16 07:46:36 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Importer;
|
|
|
|
|
|
|
|
use App\Models\Location;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When we are importing users via an Asset/etc import, we use createOrFetchUser() in
|
2023-04-18 16:34:47 -07:00
|
|
|
* Importer\Importer.php. [ALG]
|
2023-04-16 07:46:36 -07:00
|
|
|
*
|
2023-04-18 13:26:23 -07:00
|
|
|
* Class LocationImporter
|
2023-04-16 07:46:36 -07:00
|
|
|
*/
|
|
|
|
class LocationImporter extends ItemImporter
|
|
|
|
{
|
|
|
|
protected $locations;
|
|
|
|
|
|
|
|
public function __construct($filename)
|
|
|
|
{
|
|
|
|
parent::__construct($filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function handle($row)
|
|
|
|
{
|
|
|
|
parent::handle($row);
|
|
|
|
$this->createLocationIfNotExists($row);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a location if a duplicate does not exist.
|
|
|
|
* @todo Investigate how this should interact with Importer::createLocationIfNotExists
|
|
|
|
*
|
|
|
|
* @author A. Gianotto
|
|
|
|
* @since 6.1.0
|
|
|
|
* @param array $row
|
|
|
|
*/
|
|
|
|
public function createLocationIfNotExists(array $row)
|
|
|
|
{
|
|
|
|
|
2023-04-18 16:34:47 -07:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2023-04-16 07:46:36 -07:00
|
|
|
// Pull the records from the CSV to determine their values
|
|
|
|
$this->item['name'] = $this->findCsvMatch($row, 'name');
|
|
|
|
$this->item['address'] = $this->findCsvMatch($row, 'address');
|
|
|
|
$this->item['address2'] = $this->findCsvMatch($row, 'address2');
|
|
|
|
$this->item['city'] = $this->findCsvMatch($row, 'city');
|
|
|
|
$this->item['state'] = $this->findCsvMatch($row, 'state');
|
|
|
|
$this->item['country'] = $this->findCsvMatch($row, 'country');
|
|
|
|
$this->item['zip'] = $this->findCsvMatch($row, 'zip');
|
|
|
|
$this->item['currency'] = $this->findCsvMatch($row, 'currency');
|
|
|
|
$this->item['ldap_ou'] = $this->findCsvMatch($row, 'ldap_ou');
|
|
|
|
$this->item['manager'] = $this->findCsvMatch($row, 'manager');
|
|
|
|
$this->item['manager_username'] = $this->findCsvMatch($row, 'manager_username');
|
|
|
|
$this->item['user_id'] = \Auth::user()->id;
|
|
|
|
|
|
|
|
if ($this->findCsvMatch($row, 'parent_location')) {
|
|
|
|
$this->item['parent_id'] = $this->createOrFetchLocation($this->findCsvMatch($row, 'parent_location'));
|
|
|
|
}
|
|
|
|
|
2023-04-18 16:34:47 -07:00
|
|
|
if (!empty($this->item['manager'])) {
|
|
|
|
if ($manager = $this->createOrFetchUser($row, 'manager')) {
|
|
|
|
$this->item['manager_id'] = $manager->id;
|
|
|
|
}
|
2023-04-16 07:46:36 -07:00
|
|
|
}
|
|
|
|
|
2023-04-18 16:34:47 -07:00
|
|
|
\Log::debug('Item array is: ');
|
2023-04-16 07:46:36 -07:00
|
|
|
\Log::debug(print_r($this->item, true));
|
|
|
|
|
|
|
|
|
2023-04-18 16:34:47 -07:00
|
|
|
if ($editingLocation) {
|
|
|
|
\Log::debug('Updating existing location');
|
|
|
|
$location->update($this->sanitizeItemForUpdating($location));
|
2023-04-16 07:46:36 -07:00
|
|
|
} else {
|
2023-04-18 16:34:47 -07:00
|
|
|
\Log::debug('Creating location');
|
2023-04-16 07:46:36 -07:00
|
|
|
$location->fill($this->sanitizeItemForStoring($location));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($location->save()) {
|
2023-04-18 16:34:47 -07:00
|
|
|
$this->log('Location '.$location->name.' created or updated from CSV import');
|
2023-04-16 07:46:36 -07:00
|
|
|
return $location;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
\Log::debug($location->getErrors());
|
|
|
|
return $location->errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|