mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 17:39:06 +02:00
Allow to import category, footprint and manufacturer by giving a string in the CSV file
This commit is contained in:
parent
85ae862381
commit
61e2dde400
12 changed files with 173 additions and 35 deletions
|
@ -92,7 +92,7 @@ abstract class AbstractStructuralDBElement extends AttachmentContainingDBElement
|
||||||
/**
|
/**
|
||||||
* @var AbstractStructuralDBElement
|
* @var AbstractStructuralDBElement
|
||||||
* @NoneOfItsChildren()
|
* @NoneOfItsChildren()
|
||||||
* @Groups({"include_parents"})
|
* @Groups({"include_parents", "import"})
|
||||||
*/
|
*/
|
||||||
protected $parent = null;
|
protected $parent = null;
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,7 @@ trait BasicPropertyTrait
|
||||||
* @ORM\JoinColumn(name="id_category", referencedColumnName="id", nullable=false)
|
* @ORM\JoinColumn(name="id_category", referencedColumnName="id", nullable=false)
|
||||||
* @Selectable()
|
* @Selectable()
|
||||||
* @Assert\NotNull(message="validator.select_valid_category")
|
* @Assert\NotNull(message="validator.select_valid_category")
|
||||||
* @Groups({"simple", "extended", "full"})
|
* @Groups({"simple", "extended", "full", "import"})
|
||||||
*/
|
*/
|
||||||
protected ?Category $category = null;
|
protected ?Category $category = null;
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ trait BasicPropertyTrait
|
||||||
* @ORM\ManyToOne(targetEntity="Footprint")
|
* @ORM\ManyToOne(targetEntity="Footprint")
|
||||||
* @ORM\JoinColumn(name="id_footprint", referencedColumnName="id")
|
* @ORM\JoinColumn(name="id_footprint", referencedColumnName="id")
|
||||||
* @Selectable()
|
* @Selectable()
|
||||||
* @Groups({"simple", "extended", "full"})
|
* @Groups({"simple", "extended", "full", "import"})
|
||||||
*/
|
*/
|
||||||
protected ?Footprint $footprint = null;
|
protected ?Footprint $footprint = null;
|
||||||
|
|
||||||
|
|
|
@ -56,7 +56,7 @@ trait InstockTrait
|
||||||
* @var ?MeasurementUnit the unit in which the part's amount is measured
|
* @var ?MeasurementUnit the unit in which the part's amount is measured
|
||||||
* @ORM\ManyToOne(targetEntity="MeasurementUnit")
|
* @ORM\ManyToOne(targetEntity="MeasurementUnit")
|
||||||
* @ORM\JoinColumn(name="id_part_unit", referencedColumnName="id", nullable=true)
|
* @ORM\JoinColumn(name="id_part_unit", referencedColumnName="id", nullable=true)
|
||||||
* @Groups({"extended", "full"})
|
* @Groups({"extended", "full", "import"})
|
||||||
*/
|
*/
|
||||||
protected ?MeasurementUnit $partUnit = null;
|
protected ?MeasurementUnit $partUnit = null;
|
||||||
|
|
||||||
|
|
|
@ -39,7 +39,7 @@ trait ManufacturerTrait
|
||||||
* @ORM\ManyToOne(targetEntity="Manufacturer")
|
* @ORM\ManyToOne(targetEntity="Manufacturer")
|
||||||
* @ORM\JoinColumn(name="id_manufacturer", referencedColumnName="id")
|
* @ORM\JoinColumn(name="id_manufacturer", referencedColumnName="id")
|
||||||
* @Selectable()
|
* @Selectable()
|
||||||
* @Groups({"simple","extended", "full"})
|
* @Groups({"simple","extended", "full", "import"})
|
||||||
*/
|
*/
|
||||||
protected ?Manufacturer $manufacturer = null;
|
protected ?Manufacturer $manufacturer = null;
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,12 @@ use RecursiveIteratorIterator;
|
||||||
|
|
||||||
class StructuralDBElementRepository extends NamedDBElementRepository
|
class StructuralDBElementRepository extends NamedDBElementRepository
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var array An array containing all new entities created by getNewEntityByPath.
|
||||||
|
* This is used to prevent creating multiple entities for the same path.
|
||||||
|
*/
|
||||||
|
private array $new_entity_cache = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds all nodes without a parent node. They are our root nodes.
|
* Finds all nodes without a parent node. They are our root nodes.
|
||||||
*
|
*
|
||||||
|
@ -91,7 +97,7 @@ class StructuralDBElementRepository extends NamedDBElementRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a structure of AbsstractStructuralDBElements from a path separated by $separator, which splits the various levels.
|
* Creates a structure of AbstractStructuralDBElements from a path separated by $separator, which splits the various levels.
|
||||||
* This function will try to use existing elements, if they are already in the database. If not, they will be created.
|
* This function will try to use existing elements, if they are already in the database. If not, they will be created.
|
||||||
* An array of the created elements will be returned, with the last element being the deepest element.
|
* An array of the created elements will be returned, with the last element being the deepest element.
|
||||||
* @param string $path
|
* @param string $path
|
||||||
|
@ -108,14 +114,67 @@ class StructuralDBElementRepository extends NamedDBElementRepository
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//See if we already have an element with this name and parent
|
//Use the cache to prevent creating multiple entities for the same path
|
||||||
$entity = $this->findOneBy(['name' => $name, 'parent' => $parent]);
|
$entity = $this->getNewEntityFromCache($name, $parent);
|
||||||
|
|
||||||
|
//See if we already have an element with this name and parent in the database
|
||||||
|
if (!$entity) {
|
||||||
|
$entity = $this->findOneBy(['name' => $name, 'parent' => $parent]);
|
||||||
|
}
|
||||||
if (null === $entity) {
|
if (null === $entity) {
|
||||||
$class = $this->getClassName();
|
$class = $this->getClassName();
|
||||||
/** @var AbstractStructuralDBElement $entity */
|
/** @var AbstractStructuralDBElement $entity */
|
||||||
$entity = new $class;
|
$entity = new $class;
|
||||||
$entity->setName($name);
|
$entity->setName($name);
|
||||||
$entity->setParent($parent);
|
$entity->setParent($parent);
|
||||||
|
|
||||||
|
$this->setNewEntityToCache($entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result[] = $entity;
|
||||||
|
$parent = $entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getNewEntityFromCache(string $name, ?AbstractStructuralDBElement $parent): ?AbstractStructuralDBElement
|
||||||
|
{
|
||||||
|
$key = $parent ? $parent->getFullPath('%->%').'%->%'.$name : $name;
|
||||||
|
if (isset($this->new_entity_cache[$key])) {
|
||||||
|
return $this->new_entity_cache[$key];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setNewEntityToCache(AbstractStructuralDBElement $entity): void
|
||||||
|
{
|
||||||
|
$key = $entity->getFullPath('%->%');
|
||||||
|
$this->new_entity_cache[$key] = $entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an element of AbstractStructuralDBElements queried from a path separated by $separator, which splits the various levels.
|
||||||
|
* An array of the created elements will be returned, with the last element being the deepest element.
|
||||||
|
* If no element was found, an empty array will be returned.
|
||||||
|
* @param string $path
|
||||||
|
* @param string $separator
|
||||||
|
* @return AbstractStructuralDBElement[]
|
||||||
|
*/
|
||||||
|
public function getEntityByPath(string $path, string $separator = '->'): array
|
||||||
|
{
|
||||||
|
$parent = null;
|
||||||
|
$result = [];
|
||||||
|
foreach (explode($separator, $path) as $name) {
|
||||||
|
$name = trim($name);
|
||||||
|
if ('' === $name) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//See if we already have an element with this name and parent
|
||||||
|
$entity = $this->findOneBy(['name' => $name, 'parent' => $parent]);
|
||||||
|
if (null === $entity) {
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$result[] = $entity;
|
$result[] = $entity;
|
||||||
|
|
63
src/Serializer/StructuralElementFromNameDenormalizer.php
Normal file
63
src/Serializer/StructuralElementFromNameDenormalizer.php
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published
|
||||||
|
* by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Serializer;
|
||||||
|
|
||||||
|
use App\Entity\Base\AbstractStructuralDBElement;
|
||||||
|
use App\Form\Type\StructuralEntityType;
|
||||||
|
use App\Repository\StructuralDBElementRepository;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
|
||||||
|
|
||||||
|
class StructuralElementFromNameDenormalizer implements ContextAwareDenormalizerInterface
|
||||||
|
{
|
||||||
|
private EntityManagerInterface $em;
|
||||||
|
|
||||||
|
public function __construct(EntityManagerInterface $em)
|
||||||
|
{
|
||||||
|
$this->em = $em;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supportsDenormalization($data, string $type, string $format = null, array $context = [])
|
||||||
|
{
|
||||||
|
return is_string($data) && is_subclass_of($type, AbstractStructuralDBElement::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function denormalize($data, string $type, string $format = null, array $context = [])
|
||||||
|
{
|
||||||
|
//Retrieve the repository for the given type
|
||||||
|
/** @var StructuralDBElementRepository $repo */
|
||||||
|
$repo = $this->em->getRepository($type);
|
||||||
|
|
||||||
|
$path_delimiter = $context['path_delimiter'] ?? '->';
|
||||||
|
|
||||||
|
if ($context['create_unknown_datastructures'] ?? false) {
|
||||||
|
$elements = $repo->getNewEntityFromPath($data, $path_delimiter);
|
||||||
|
//Persist all new elements
|
||||||
|
foreach ($elements as $element) {
|
||||||
|
$this->em->persist($element);
|
||||||
|
}
|
||||||
|
return end($elements);
|
||||||
|
}
|
||||||
|
|
||||||
|
$elements = $repo->getEntityByPath($data, $path_delimiter);
|
||||||
|
return end($elements);
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,6 +21,7 @@
|
||||||
namespace App\Serializer;
|
namespace App\Serializer;
|
||||||
|
|
||||||
use App\Entity\Base\AbstractStructuralDBElement;
|
use App\Entity\Base\AbstractStructuralDBElement;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
|
||||||
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
|
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
|
||||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
||||||
|
@ -48,7 +49,7 @@ class StructuralElementNormalizer implements ContextAwareNormalizerInterface
|
||||||
$data = $this->normalizer->normalize($object, $format, $context);
|
$data = $this->normalizer->normalize($object, $format, $context);
|
||||||
|
|
||||||
//Remove type field for CSV export
|
//Remove type field for CSV export
|
||||||
if ($format == 'csv') {
|
if ($format === 'csv') {
|
||||||
unset($data['type']);
|
unset($data['type']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -160,6 +160,8 @@ class EntityImporter
|
||||||
[
|
[
|
||||||
'groups' => $groups,
|
'groups' => $groups,
|
||||||
'csv_delimiter' => $options['csv_delimiter'],
|
'csv_delimiter' => $options['csv_delimiter'],
|
||||||
|
'create_unknown_datastructures' => $options['create_unknown_datastructures'],
|
||||||
|
'path_delimiter' => $options['path_delimiter'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
//Ensure we have an array of entity elements.
|
//Ensure we have an array of entity elements.
|
||||||
|
@ -215,7 +217,9 @@ class EntityImporter
|
||||||
'preserve_children' => true,
|
'preserve_children' => true,
|
||||||
'parent' => null, //The parent element to which the imported elements should be added
|
'parent' => null, //The parent element to which the imported elements should be added
|
||||||
'abort_on_validation_error' => true,
|
'abort_on_validation_error' => true,
|
||||||
'part_category' => null
|
'part_category' => null,
|
||||||
|
'create_unknown_datastructures' => true, //If true, unknown datastructures (categories, footprints, etc.) will be created on the fly
|
||||||
|
'path_delimiter' => '->', //The delimiter used to separate the path elements in the name of a structural element
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$resolver->setAllowedValues('format', ['csv', 'json', 'xml', 'yaml']);
|
$resolver->setAllowedValues('format', ['csv', 'json', 'xml', 'yaml']);
|
||||||
|
|
|
@ -12,7 +12,13 @@
|
||||||
<h4><i class="fa-solid fa-exclamation-triangle fa-fw"></i> {% trans %}parts.import.errors.title{% endtrans %}</h4>
|
<h4><i class="fa-solid fa-exclamation-triangle fa-fw"></i> {% trans %}parts.import.errors.title{% endtrans %}</h4>
|
||||||
<ul>
|
<ul>
|
||||||
{% for name, error in import_errors %}
|
{% for name, error in import_errors %}
|
||||||
<li><b>{{ name }}:</b> {{ error.violations }}</li>
|
<li>
|
||||||
|
<b>{{ name }}: </b>
|
||||||
|
{% for violation in error.violations %}
|
||||||
|
{% dump(violation) %}
|
||||||
|
<i>{{ violation.propertyPath }}</i>: {{ violation.message|trans(violation.parameters, 'validators') }}<br>
|
||||||
|
{% endfor %}
|
||||||
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
@ -20,15 +26,19 @@
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block card_content %}
|
{% block card_content %}
|
||||||
{{ form(import_form) }}
|
{{ form(import_form) }}
|
||||||
|
|
||||||
{% if imported_entities %}
|
{% if imported_entities %}
|
||||||
<hr>
|
<hr>
|
||||||
<h4>{% trans %}parts.import.errors.imported_entities{% endtrans %} ({{ imported_entities | length }}):</h4>
|
<h4>{% trans %}parts.import.errors.imported_entities{% endtrans %} ({{ imported_entities | length }}):</h4>
|
||||||
<ul>
|
<ul>
|
||||||
{% for entity in imported_entities %}
|
{% for entity in imported_entities %}
|
||||||
{# @var \App\Entity\Parts\Part entity #}
|
{# @var \App\Entity\Parts\Part entity #}
|
||||||
|
{% if entity.id %}
|
||||||
<li><a href="{{ entity_url(entity) }}">{{ entity.name }}</a> (ID: {{ entity.iD }})</li>
|
<li><a href="{{ entity_url(entity) }}">{{ entity.name }}</a> (ID: {{ entity.iD }})</li>
|
||||||
{% endfor %}
|
{% else %}
|
||||||
|
<li>{{ entity.name }}</li>
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -6673,16 +6673,6 @@ Wenn Sie dies fehlerhafterweise gemacht haben oder ein Computer nicht mehr vertr
|
||||||
<target>Ein Kindelement kann nicht das übergeordnete Element sein!</target>
|
<target>Ein Kindelement kann nicht das übergeordnete Element sein!</target>
|
||||||
</segment>
|
</segment>
|
||||||
</unit>
|
</unit>
|
||||||
<unit id="0IF0VIF" name="validator.isSelectable">
|
|
||||||
<notes>
|
|
||||||
<note priority="1">obsolete</note>
|
|
||||||
<note category="state" priority="1">obsolete</note>
|
|
||||||
</notes>
|
|
||||||
<segment>
|
|
||||||
<source>validator.isSelectable</source>
|
|
||||||
<target>Das Element muss auswählbar sein!</target>
|
|
||||||
</segment>
|
|
||||||
</unit>
|
|
||||||
<unit id="nd207H6" name="validator.part_lot.location_full.no_increasment">
|
<unit id="nd207H6" name="validator.part_lot.location_full.no_increasment">
|
||||||
<notes>
|
<notes>
|
||||||
<note priority="1">obsolete</note>
|
<note priority="1">obsolete</note>
|
||||||
|
|
|
@ -6674,16 +6674,7 @@ If you have done this incorrectly or if a computer is no longer trusted, you can
|
||||||
<target>The parent can not be one of the children of itself.</target>
|
<target>The parent can not be one of the children of itself.</target>
|
||||||
</segment>
|
</segment>
|
||||||
</unit>
|
</unit>
|
||||||
<unit id="0IF0VIF" name="validator.isSelectable">
|
|
||||||
<notes>
|
|
||||||
<note priority="1">obsolete</note>
|
|
||||||
<note category="state" priority="1">obsolete</note>
|
|
||||||
</notes>
|
|
||||||
<segment>
|
|
||||||
<source>validator.isSelectable</source>
|
|
||||||
<target>The element must be selectable.</target>
|
|
||||||
</segment>
|
|
||||||
</unit>
|
|
||||||
<unit id="nd207H6" name="validator.part_lot.location_full.no_increasment">
|
<unit id="nd207H6" name="validator.part_lot.location_full.no_increasment">
|
||||||
<notes>
|
<notes>
|
||||||
<note priority="1">obsolete</note>
|
<note priority="1">obsolete</note>
|
||||||
|
|
|
@ -305,5 +305,25 @@
|
||||||
<target>Wählen Sie einen Wert, oder laden Sie eine Datei hoch, um dessen Dateiname automatisch als Namen für diesen Anhang zu nutzen.</target>
|
<target>Wählen Sie einen Wert, oder laden Sie eine Datei hoch, um dessen Dateiname automatisch als Namen für diesen Anhang zu nutzen.</target>
|
||||||
</segment>
|
</segment>
|
||||||
</unit>
|
</unit>
|
||||||
|
<unit id="0IF0VIF" name="validator.isSelectable">
|
||||||
|
<notes>
|
||||||
|
<note priority="1">obsolete</note>
|
||||||
|
<note category="state" priority="1">obsolete</note>
|
||||||
|
</notes>
|
||||||
|
<segment>
|
||||||
|
<source>validator.isSelectable</source>
|
||||||
|
<target>Das Element muss auswählbar sein!</target>
|
||||||
|
</segment>
|
||||||
|
</unit>
|
||||||
|
<unit id="0IF0VIF" name="validator.isSelectable">
|
||||||
|
<notes>
|
||||||
|
<note priority="1">obsolete</note>
|
||||||
|
<note category="state" priority="1">obsolete</note>
|
||||||
|
</notes>
|
||||||
|
<segment>
|
||||||
|
<source>validator.isSelectable</source>
|
||||||
|
<target>The element must be selectable.</target>
|
||||||
|
</segment>
|
||||||
|
</unit>
|
||||||
</file>
|
</file>
|
||||||
</xliff>
|
</xliff>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue