From b74ab18a6d8a6b5dc71a224fa91cfbc493fa0736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 16 Jul 2023 22:59:46 +0200 Subject: [PATCH] Added possibility to define alternative names on data structures This can be used to find elements, based on the data returned by info providers --- .../Base/AbstractStructuralDBElement.php | 30 +++++++++++++++++++ src/Form/AdminPages/BaseEntityAdminForm.php | 16 ++++++++++ .../StructuralDBElementRepository.php | 13 ++++---- .../DTOtoEntityConverter.php | 2 ++ templates/admin/base_company_admin.html.twig | 1 + templates/admin/category_admin.html.twig | 2 ++ templates/admin/footprint_admin.html.twig | 4 +++ .../admin/measurement_unit_admin.html.twig | 1 + templates/admin/storelocation_admin.html.twig | 2 ++ templates/admin/supplier_admin.html.twig | 1 + translations/messages.en.xlf | 12 ++++++++ 11 files changed, 79 insertions(+), 5 deletions(-) diff --git a/src/Entity/Base/AbstractStructuralDBElement.php b/src/Entity/Base/AbstractStructuralDBElement.php index ac201c0d..5c4103d8 100644 --- a/src/Entity/Base/AbstractStructuralDBElement.php +++ b/src/Entity/Base/AbstractStructuralDBElement.php @@ -419,4 +419,34 @@ abstract class AbstractStructuralDBElement extends AttachmentContainingDBElement return $this; } + + /** + * Returns a comma separated list of alternative names. + * @return string|null + */ + public function getAlternativeNames(): ?string + { + if ($this->alternative_names === null) { + return null; + } + + //Remove trailing comma + return rtrim($this->alternative_names, ','); + } + + /** + * Sets a comma separated list of alternative names. + * @return $this + */ + public function setAlternativeNames(?string $new_value): self + { + //Add a trailing comma, if not already there (makes it easier to find in the database) + if (is_string($new_value) && substr($new_value, -1) !== ',') { + $new_value .= ','; + } + + $this->alternative_names = $new_value; + + return $this; + } } diff --git a/src/Form/AdminPages/BaseEntityAdminForm.php b/src/Form/AdminPages/BaseEntityAdminForm.php index 268ea630..19af4de8 100644 --- a/src/Form/AdminPages/BaseEntityAdminForm.php +++ b/src/Form/AdminPages/BaseEntityAdminForm.php @@ -22,6 +22,9 @@ declare(strict_types=1); namespace App\Form\AdminPages; +use App\Entity\PriceInformations\Currency; +use App\Entity\ProjectSystem\Project; +use App\Entity\UserSystem\Group; use Symfony\Bundle\SecurityBundle\Security; use App\Entity\Base\AbstractNamedDBElement; use App\Entity\Base\AbstractStructuralDBElement; @@ -111,6 +114,19 @@ class BaseEntityAdminForm extends AbstractType ); } + if ($entity instanceof AbstractStructuralDBElement && !($entity instanceof Group || $entity instanceof Project || $entity instanceof Currency)) { + $builder->add('alternative_names', TextType::class, [ + 'required' => false, + 'label' => 'entity.edit.alternative_names.label', + 'help' => 'entity.edit.alternative_names.help', + 'empty_data' => null, + 'attr' => [ + 'class' => 'tagsinput', + 'data-controller' => 'elements--tagsinput', + ] + ]); + } + $this->additionalFormElements($builder, $options, $entity); //Attachment section diff --git a/src/Repository/StructuralDBElementRepository.php b/src/Repository/StructuralDBElementRepository.php index 529cce79..24087cfa 100644 --- a/src/Repository/StructuralDBElementRepository.php +++ b/src/Repository/StructuralDBElementRepository.php @@ -209,17 +209,17 @@ class StructuralDBElementRepository extends NamedDBElementRepository return $result[0]; } - /*//If we have no result, try to find the element by additional names + //If we have no result, try to find the element by alternative names $qb = $this->createQueryBuilder('e'); //Use lowercase conversion to be case-insensitive - $qb->where($qb->expr()->like('LOWER(e.additional_names)', 'LOWER(:name)')); - $qb->setParameter('name', '%'.$name.'%'); + $qb->where($qb->expr()->like('LOWER(e.alternative_names)', 'LOWER(:name)')); + $qb->setParameter('name', '%'.$name.',%'); $result = $qb->getQuery()->getResult(); - if (count($result) === 1) { + if (count($result) >= 1) { return $result[0]; - }*/ + } //If we find nothing, return null return null; @@ -247,6 +247,9 @@ class StructuralDBElementRepository extends NamedDBElementRepository $entity = new $class; $entity->setName($name); + //Set the found name to the alternative names, so the entity can be easily renamed later + $entity->setAlternativeNames($name); + $this->setNewEntityToCache($entity); } diff --git a/src/Services/InfoProviderSystem/DTOtoEntityConverter.php b/src/Services/InfoProviderSystem/DTOtoEntityConverter.php index 0c7a639d..a12628ac 100644 --- a/src/Services/InfoProviderSystem/DTOtoEntityConverter.php +++ b/src/Services/InfoProviderSystem/DTOtoEntityConverter.php @@ -265,6 +265,7 @@ final class DTOtoEntityConverter //If the entity was newly created, set the file filter if ($tmp->getId() === null) { $tmp->setFiletypeFilter('application/pdf'); + $tmp->setAlternativeNames(self::TYPE_DATASHEETS_NAME); } return $tmp; @@ -282,6 +283,7 @@ final class DTOtoEntityConverter //If the entity was newly created, set the file filter if ($tmp->getId() === null) { $tmp->setFiletypeFilter('image/*'); + $tmp->setAlternativeNames(self::TYPE_DATASHEETS_NAME); } return $tmp; diff --git a/templates/admin/base_company_admin.html.twig b/templates/admin/base_company_admin.html.twig index 5b5a72c5..2da5e02a 100644 --- a/templates/admin/base_company_admin.html.twig +++ b/templates/admin/base_company_admin.html.twig @@ -17,6 +17,7 @@ {% block additional_panes %}
+ {{ form_row(form.alternative_names) }} {{ form_row(form.comment) }}
{% endblock %} diff --git a/templates/admin/category_admin.html.twig b/templates/admin/category_admin.html.twig index c1542206..4ba0248f 100644 --- a/templates/admin/category_admin.html.twig +++ b/templates/admin/category_admin.html.twig @@ -26,6 +26,8 @@
+ {{ form_row(form.alternative_names) }} +
{{ form_row(form.partname_regex) }} {{ form_row(form.partname_hint) }}
diff --git a/templates/admin/footprint_admin.html.twig b/templates/admin/footprint_admin.html.twig index 04acaa39..e4ed7713 100644 --- a/templates/admin/footprint_admin.html.twig +++ b/templates/admin/footprint_admin.html.twig @@ -15,4 +15,8 @@ {% block new_title %} {% trans %}footprint.new{% endtrans %} +{% endblock %} + +{% block additional_controls %} + {{ form_row(form.alternative_names) }} {% endblock %} \ No newline at end of file diff --git a/templates/admin/measurement_unit_admin.html.twig b/templates/admin/measurement_unit_admin.html.twig index f498fb38..31748509 100644 --- a/templates/admin/measurement_unit_admin.html.twig +++ b/templates/admin/measurement_unit_admin.html.twig @@ -16,5 +16,6 @@ {{ form_row(form.unit) }} {{ form_row(form.is_integer) }} {{ form_row(form.use_si_prefix)}} + {{ form_row(form.alternative_names) }} {% endblock %} diff --git a/templates/admin/storelocation_admin.html.twig b/templates/admin/storelocation_admin.html.twig index 4741c02d..c93339dc 100644 --- a/templates/admin/storelocation_admin.html.twig +++ b/templates/admin/storelocation_admin.html.twig @@ -21,6 +21,8 @@ {% block additional_panes %}
+ {{ form_row(form.alternative_names) }} + {{ form_row(form.storage_type) }} {{ form_row(form.is_full) }} {{ form_row(form.limit_to_existing_parts) }} diff --git a/templates/admin/supplier_admin.html.twig b/templates/admin/supplier_admin.html.twig index 9c0fcb47..ce38a5ca 100644 --- a/templates/admin/supplier_admin.html.twig +++ b/templates/admin/supplier_admin.html.twig @@ -6,6 +6,7 @@ {% block additional_panes %}
+ {{ form_row(form.alternative_names) }} {{ form_row(form.default_currency) }} {{ form_row(form.shipping_costs) }} {{ form_row(form.comment) }} diff --git a/translations/messages.en.xlf b/translations/messages.en.xlf index 7415c546..20680fb7 100644 --- a/translations/messages.en.xlf +++ b/translations/messages.en.xlf @@ -11561,5 +11561,17 @@ Please note, that you can not impersonate a disabled user. If you try you will g Create parts from info provider + + + entity.edit.alternative_names.label + Alternative names + + + + + entity.edit.alternative_names.help + The alternative names given here, are used to find this element based on the results of the information providers. + +