snipe-it/app/Importer/AccessoryImporter.php

64 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Importer;
use App\Helpers\Helper;
use App\Models\Accessory;
class AccessoryImporter extends ItemImporter
{
protected $accessories;
public function __construct($filename)
{
parent::__construct($filename);
$this->accessories = Accessory::all();
}
protected function handle($row)
{
parent::handle($row); // TODO: Change the autogenerated stub
$this->createAccessoryIfNotExists();
}
/**
* Create an accessory if a duplicate does not exist
*
* @author Daniel Melzter
* @since 3.0
*/
public function createAccessoryIfNotExists()
{
$accessoryId = $this->accessories->search(function ($key) {
return strcasecmp($key->name, $this->item['name']) == 0;
});
if ($accessoryId !== false) {
if (!$this->updating) {
$this->log('A matching Accessory ' . $this->item["name"] . ' already exists. ');
return;
}
$this->log('Updating Accessory');
$accessory = $this->accessories[$accessoryId];
$accessory->update($this->sanitizeItemForUpdating($accessory));
if (!$this->testRun) {
$accessory->save();
}
return;
}
$this->log("No Matching Accessory, Creating a new one");
$accessory = new Accessory();
$accessory->fill($this->sanitizeItemForStoring($accessory));
if ($this->testRun) {
$this->log('TEST RUN - Accessory ' . $this->item["name"] . ' not created');
return;
}
if ($accessory->save()) {
$accessory->logCreate('Imported using CSV Importer');
$this->log('Accessory ' . $this->item["name"] . ' was created');
}
$this->jsonError($accessory, 'Accessory');
}
2016-12-29 14:02:18 -08:00
}