mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-26 19:58:53 +02:00
Fixed multi-part action selectors.
This commit is contained in:
parent
b8da4c62d0
commit
7ff1584eb9
8 changed files with 266 additions and 190 deletions
|
@ -18,6 +18,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import DatatablesController from "./datatables_controller.js";
|
import DatatablesController from "./datatables_controller.js";
|
||||||
|
import TomSelect from "tom-select";
|
||||||
|
|
||||||
import * as bootbox from "bootbox";
|
import * as bootbox from "bootbox";
|
||||||
|
|
||||||
|
@ -63,22 +64,22 @@ export default class extends DatatablesController {
|
||||||
{
|
{
|
||||||
//Clear options
|
//Clear options
|
||||||
select_element.innerHTML = null;
|
select_element.innerHTML = null;
|
||||||
$(select_element).selectpicker('destroy');
|
//$(select_element).selectpicker('destroy');
|
||||||
|
|
||||||
for(let i=0; i<json.length; i++) {
|
//Retrieve the select controller instance
|
||||||
let json_opt = json[i];
|
const select_controller = this.application.getControllerForElementAndIdentifier(select_element, 'elements--structural-entity-select');
|
||||||
let opt = document.createElement('option');
|
/** @var {TomSelect} tom_select */
|
||||||
opt.value = json_opt.value;
|
const tom_select = select_controller.getTomSelect();
|
||||||
opt.innerHTML = json_opt.text;
|
|
||||||
|
|
||||||
if(json_opt['data-subtext']) {
|
tom_select.clear();
|
||||||
opt.dataset.subtext = json_opt['data-subtext'];
|
tom_select.clearOptions();
|
||||||
}
|
|
||||||
|
|
||||||
select_element.appendChild(opt);
|
tom_select.addOptions(json, false);
|
||||||
}
|
|
||||||
|
|
||||||
$(select_element).selectpicker('show');
|
select_element.parentElement.classList.remove('d-none');
|
||||||
|
select_element.parentElement.classList.add('d-inline-block');
|
||||||
|
|
||||||
|
//$(select_element).selectpicker('show');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,7 +100,9 @@ export default class extends DatatablesController {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
$(select_target).selectpicker('hide');
|
//$(select_target).selectpicker('hide');
|
||||||
|
select_element.parentElement.classList.remove('d-inline-block');
|
||||||
|
select_target.parentElement.classList.add('d-none');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,10 @@ export default class extends Controller {
|
||||||
_emptyMessage;
|
_emptyMessage;
|
||||||
|
|
||||||
connect() {
|
connect() {
|
||||||
|
this._init();
|
||||||
|
}
|
||||||
|
|
||||||
|
_init() {
|
||||||
this._emptyMessage = this.element.getAttribute("data-empty-message") ?? "";
|
this._emptyMessage = this.element.getAttribute("data-empty-message") ?? "";
|
||||||
if (this._emptyMessage === "" && this.element.hasAttribute('title')) {
|
if (this._emptyMessage === "" && this.element.hasAttribute('title')) {
|
||||||
this._emptyMessage = this.element.getAttribute('title');
|
this._emptyMessage = this.element.getAttribute('title');
|
||||||
|
@ -50,6 +53,10 @@ export default class extends Controller {
|
||||||
this._tomSelect = new TomSelect(this.element, settings);
|
this._tomSelect = new TomSelect(this.element, settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getTomSelect() {
|
||||||
|
return this._tomSelect;
|
||||||
|
}
|
||||||
|
|
||||||
renderItem(data, escape) {
|
renderItem(data, escape) {
|
||||||
//The empty option is rendered muted
|
//The empty option is rendered muted
|
||||||
if (data.value === "") {
|
if (data.value === "") {
|
||||||
|
|
|
@ -63,6 +63,10 @@ export default class extends Controller {
|
||||||
this._tomSelect = new TomSelect(this.element, settings);
|
this._tomSelect = new TomSelect(this.element, settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getTomSelect() {
|
||||||
|
return this._tomSelect;
|
||||||
|
}
|
||||||
|
|
||||||
renderItem(data, escape) {
|
renderItem(data, escape) {
|
||||||
//Render empty option as full row
|
//Render empty option as full row
|
||||||
if (data.value === "") {
|
if (data.value === "") {
|
||||||
|
|
|
@ -29,6 +29,7 @@ use App\Entity\Parts\Footprint;
|
||||||
use App\Entity\Parts\Manufacturer;
|
use App\Entity\Parts\Manufacturer;
|
||||||
use App\Entity\Parts\MeasurementUnit;
|
use App\Entity\Parts\MeasurementUnit;
|
||||||
use App\Entity\ProjectSystem\Project;
|
use App\Entity\ProjectSystem\Project;
|
||||||
|
use App\Form\Type\Helper\StructuralEntityChoiceHelper;
|
||||||
use App\Services\Trees\NodesListBuilder;
|
use App\Services\Trees\NodesListBuilder;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
@ -45,11 +46,13 @@ class SelectAPIController extends AbstractController
|
||||||
{
|
{
|
||||||
private NodesListBuilder $nodesListBuilder;
|
private NodesListBuilder $nodesListBuilder;
|
||||||
private TranslatorInterface $translator;
|
private TranslatorInterface $translator;
|
||||||
|
private StructuralEntityChoiceHelper $choiceHelper;
|
||||||
|
|
||||||
public function __construct(NodesListBuilder $nodesListBuilder, TranslatorInterface $translator)
|
public function __construct(NodesListBuilder $nodesListBuilder, TranslatorInterface $translator, StructuralEntityChoiceHelper $choiceHelper)
|
||||||
{
|
{
|
||||||
$this->nodesListBuilder = $nodesListBuilder;
|
$this->nodesListBuilder = $nodesListBuilder;
|
||||||
$this->translator = $translator;
|
$this->translator = $translator;
|
||||||
|
$this->choiceHelper = $choiceHelper;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -167,10 +170,32 @@ class SelectAPIController extends AbstractController
|
||||||
foreach ($nodes_list as $node) {
|
foreach ($nodes_list as $node) {
|
||||||
if ($node instanceof AbstractStructuralDBElement) {
|
if ($node instanceof AbstractStructuralDBElement) {
|
||||||
$entry = [
|
$entry = [
|
||||||
|
'text' => $this->choiceHelper->generateChoiceLabel($node),
|
||||||
|
'value' => $this->choiceHelper->generateChoiceValue($node),
|
||||||
|
];
|
||||||
|
|
||||||
|
$data = $this->choiceHelper->generateChoiceAttr($node, [
|
||||||
|
'disable_not_selectable' => true,
|
||||||
|
]);
|
||||||
|
//Remove the data-* prefix for each key
|
||||||
|
$data = array_combine(
|
||||||
|
array_map(function ($key) {
|
||||||
|
if (strpos($key, 'data-') === 0) {
|
||||||
|
return substr($key, 5);
|
||||||
|
}
|
||||||
|
return $key;
|
||||||
|
}, array_keys($data)),
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
|
||||||
|
//Append the data to the entry
|
||||||
|
$entry += $data;
|
||||||
|
|
||||||
|
/*$entry = [
|
||||||
'text' => str_repeat(' ', $node->getLevel()).htmlspecialchars($node->getName()),
|
'text' => str_repeat(' ', $node->getLevel()).htmlspecialchars($node->getName()),
|
||||||
'value' => $node->getID(),
|
'value' => $node->getID(),
|
||||||
'data-subtext' => $node->getParent() ? $node->getParent()->getFullPath() : null,
|
'data-subtext' => $node->getParent() ? $node->getParent()->getFullPath() : null,
|
||||||
];
|
];*/
|
||||||
} elseif ($node instanceof AbstractNamedDBElement) {
|
} elseif ($node instanceof AbstractNamedDBElement) {
|
||||||
$entry = [
|
$entry = [
|
||||||
'text' => htmlspecialchars($node->getName()),
|
'text' => htmlspecialchars($node->getName()),
|
||||||
|
|
|
@ -25,6 +25,7 @@ namespace App\Form\Type;
|
||||||
use App\Entity\Attachments\AttachmentType;
|
use App\Entity\Attachments\AttachmentType;
|
||||||
use App\Entity\Base\AbstractStructuralDBElement;
|
use App\Entity\Base\AbstractStructuralDBElement;
|
||||||
use App\Entity\PriceInformations\Currency;
|
use App\Entity\PriceInformations\Currency;
|
||||||
|
use App\Form\Type\Helper\StructuralEntityChoiceHelper;
|
||||||
use App\Services\Attachments\AttachmentURLGenerator;
|
use App\Services\Attachments\AttachmentURLGenerator;
|
||||||
use App\Services\Trees\NodesListBuilder;
|
use App\Services\Trees\NodesListBuilder;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
@ -41,9 +42,9 @@ class CurrencyEntityType extends StructuralEntityType
|
||||||
{
|
{
|
||||||
protected ?string $base_currency;
|
protected ?string $base_currency;
|
||||||
|
|
||||||
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, AttachmentURLGenerator $attachmentURLGenerator, TranslatorInterface $translator, ?string $base_currency)
|
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, TranslatorInterface $translator, StructuralEntityChoiceHelper $choiceHelper, ?string $base_currency)
|
||||||
{
|
{
|
||||||
parent::__construct($em, $builder, $attachmentURLGenerator, $translator);
|
parent::__construct($em, $builder, $translator, $choiceHelper);
|
||||||
$this->base_currency = $base_currency;
|
$this->base_currency = $base_currency;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,6 +60,12 @@ class CurrencyEntityType extends StructuralEntityType
|
||||||
// This options allows you to override the currency shown for the null value
|
// This options allows you to override the currency shown for the null value
|
||||||
$resolver->setDefault('base_currency', null);
|
$resolver->setDefault('base_currency', null);
|
||||||
|
|
||||||
|
$resolver->setDefault('choice_attr', function (Options $options) {
|
||||||
|
return function ($choice) use ($options) {
|
||||||
|
return $this->choice_helper->generateChoiceAttrCurrency($choice, $options);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
$resolver->setDefault('empty_message', function (Options $options) {
|
$resolver->setDefault('empty_message', function (Options $options) {
|
||||||
//By default we use the global base currency:
|
//By default we use the global base currency:
|
||||||
$iso_code = $this->base_currency;
|
$iso_code = $this->base_currency;
|
||||||
|
@ -75,62 +82,4 @@ class CurrencyEntityType extends StructuralEntityType
|
||||||
//If short is set to true, then the name of the entity will only shown in the dropdown list not in the selected value.
|
//If short is set to true, then the name of the entity will only shown in the dropdown list not in the selected value.
|
||||||
$resolver->setDefault('short', false);
|
$resolver->setDefault('short', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function generateChoiceAttr(AbstractStructuralDBElement $choice, $key, $value, $options): array
|
|
||||||
{
|
|
||||||
$tmp = parent::generateChoiceAttr($choice, $key, $value, $options);
|
|
||||||
|
|
||||||
if (!$choice instanceof Currency) {
|
|
||||||
throw new RuntimeException('The choice must be an instance of '.Currency::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!empty($choice->getIsoCode())) {
|
|
||||||
$symbol = Currencies::getSymbol($choice->getIsoCode());
|
|
||||||
} else {
|
|
||||||
$symbol = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($options['short']) {
|
|
||||||
$tmp['data-short'] = $symbol;
|
|
||||||
} else {
|
|
||||||
$tmp['data-short'] = $choice->getName();
|
|
||||||
}
|
|
||||||
|
|
||||||
$tmp += [
|
|
||||||
'data-symbol' => $symbol,
|
|
||||||
];
|
|
||||||
|
|
||||||
return $tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getChoiceContent(AbstractStructuralDBElement $choice, $key, $value, $options): string
|
|
||||||
{
|
|
||||||
if(!$choice instanceof Currency) {
|
|
||||||
throw new RuntimeException('$choice must be an instance of Currency!');
|
|
||||||
}
|
|
||||||
|
|
||||||
//Generate the level spacing
|
|
||||||
/** @var AbstractStructuralDBElement|null $parent */
|
|
||||||
$parent = $options['subentities_of'];
|
|
||||||
/*** @var AbstractStructuralDBElement $choice */
|
|
||||||
$level = $choice->getLevel();
|
|
||||||
//If our base entity is not the root level, we need to change the level, to get zero position
|
|
||||||
if (null !== $options['subentities_of']) {
|
|
||||||
$level -= $parent->getLevel() - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
$tmp = str_repeat('<span class="picker-level"></span>', $level);
|
|
||||||
|
|
||||||
//Show currency symbol or ISO code and the name of the currency
|
|
||||||
if(!empty($choice->getIsoCode())) {
|
|
||||||
$tmp .= Currencies::getSymbol($choice->getIsoCode());
|
|
||||||
//Add currency name as badge
|
|
||||||
$tmp .= sprintf('<span class="badge bg-primary ms-2 %s">%s</span>', $options['short'] ? 'picker-hs' : '' , htmlspecialchars($choice->getName()));
|
|
||||||
} else {
|
|
||||||
$tmp .= htmlspecialchars($choice->getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
return $tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
161
src/Form/Type/Helper/StructuralEntityChoiceHelper.php
Normal file
161
src/Form/Type/Helper/StructuralEntityChoiceHelper.php
Normal file
|
@ -0,0 +1,161 @@
|
||||||
|
<?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\Form\Type\Helper;
|
||||||
|
|
||||||
|
use App\Entity\Attachments\AttachmentType;
|
||||||
|
use App\Entity\Base\AbstractStructuralDBElement;
|
||||||
|
use App\Entity\PriceInformations\Currency;
|
||||||
|
use App\Services\Attachments\AttachmentURLGenerator;
|
||||||
|
use RuntimeException;
|
||||||
|
use Symfony\Component\Intl\Currencies;
|
||||||
|
use Symfony\Component\OptionsResolver\Options;
|
||||||
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
|
|
||||||
|
class StructuralEntityChoiceHelper
|
||||||
|
{
|
||||||
|
|
||||||
|
private AttachmentURLGenerator $attachmentURLGenerator;
|
||||||
|
private TranslatorInterface $translator;
|
||||||
|
|
||||||
|
public function __construct(AttachmentURLGenerator $attachmentURLGenerator, TranslatorInterface $translator)
|
||||||
|
{
|
||||||
|
$this->attachmentURLGenerator = $attachmentURLGenerator;
|
||||||
|
$this->translator = $translator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates the choice attributes for the given AbstractStructuralDBElement.
|
||||||
|
* @param AbstractStructuralDBElement $choice
|
||||||
|
* @param Options|array $options
|
||||||
|
* @return array|string[]
|
||||||
|
*/
|
||||||
|
public function generateChoiceAttr(AbstractStructuralDBElement $choice, $options): array
|
||||||
|
{
|
||||||
|
$tmp = [];
|
||||||
|
|
||||||
|
//Disable attribute if the choice is marked as not selectable
|
||||||
|
if (($options['disable_not_selectable'] ?? false) && $choice->isNotSelectable()) {
|
||||||
|
$tmp += ['disabled' => 'disabled'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($choice instanceof AttachmentType) {
|
||||||
|
$tmp += ['data-filetype_filter' => $choice->getFiletypeFilter()];
|
||||||
|
}
|
||||||
|
|
||||||
|
$level = $choice->getLevel();
|
||||||
|
/** @var AbstractStructuralDBElement|null $parent */
|
||||||
|
$parent = $options['subentities_of'] ?? null;
|
||||||
|
if (null !== $parent) {
|
||||||
|
$level -= $parent->getLevel() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
$tmp += [
|
||||||
|
'data-level' => $level,
|
||||||
|
'data-parent' => $choice->getParent() ? $choice->getParent()->getFullPath() : null,
|
||||||
|
'data-path' => $choice->getFullPath('->'),
|
||||||
|
'data-image' => $choice->getMasterPictureAttachment() ? $this->attachmentURLGenerator->getThumbnailURL($choice->getMasterPictureAttachment(), 'thumbnail_xs') : null,
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($choice instanceof AttachmentType && !empty($choice->getFiletypeFilter())) {
|
||||||
|
$tmp += ['data-filetype_filter' => $choice->getFiletypeFilter()];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates the choice attributes for the given AbstractStructuralDBElement.
|
||||||
|
* @param Currency $choice
|
||||||
|
* @param Options|array $options
|
||||||
|
* @return array|string[]
|
||||||
|
*/
|
||||||
|
public function generateChoiceAttrCurrency(Currency $choice, $options): array
|
||||||
|
{
|
||||||
|
$tmp = $this->generateChoiceAttr($choice, $options);
|
||||||
|
|
||||||
|
if(!empty($choice->getIsoCode())) {
|
||||||
|
$symbol = Currencies::getSymbol($choice->getIsoCode());
|
||||||
|
} else {
|
||||||
|
$symbol = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($options['short']) {
|
||||||
|
$tmp['data-short'] = $symbol;
|
||||||
|
} else {
|
||||||
|
$tmp['data-short'] = $choice->getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
$tmp += [
|
||||||
|
'data-symbol' => $symbol,
|
||||||
|
];
|
||||||
|
|
||||||
|
return $tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the choice label for the given AbstractStructuralDBElement.
|
||||||
|
* @param AbstractStructuralDBElement $choice
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function generateChoiceLabel(AbstractStructuralDBElement $choice): string
|
||||||
|
{
|
||||||
|
return $choice->getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the choice value for the given AbstractStructuralDBElement.
|
||||||
|
* @param AbstractStructuralDBElement|null $element
|
||||||
|
* @return string|int|null
|
||||||
|
*/
|
||||||
|
public function generateChoiceValue(?AbstractStructuralDBElement $element)
|
||||||
|
{
|
||||||
|
if ($element === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Do not change the structure below, even when inspection says it can be replaced with a null coalescing operator.
|
||||||
|
* It is important that the value returned here for a existing element is an int, and for a new element a string.
|
||||||
|
* I dont really understand why, but it seems to be important for the choice_loader to work correctly.
|
||||||
|
* So please do not change this!
|
||||||
|
*/
|
||||||
|
if ($element->getID() === null) {
|
||||||
|
//Must be the same as the separator in the choice_loader, otherwise this will not work!
|
||||||
|
return $element->getFullPath('->');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $element->getID();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param AbstractStructuralDBElement|null $element
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
public function generateGroupBy(AbstractStructuralDBElement $element): ?string
|
||||||
|
{
|
||||||
|
//Show entities that are not added to DB yet separately from other entities
|
||||||
|
if ($element->getID() === null) {
|
||||||
|
return $this->translator->trans('entity.select.group.new_not_added_to_DB');
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,6 +24,7 @@ namespace App\Form\Type;
|
||||||
|
|
||||||
use App\Entity\Attachments\AttachmentType;
|
use App\Entity\Attachments\AttachmentType;
|
||||||
use App\Entity\Base\AbstractStructuralDBElement;
|
use App\Entity\Base\AbstractStructuralDBElement;
|
||||||
|
use App\Form\Type\Helper\StructuralEntityChoiceHelper;
|
||||||
use App\Form\Type\Helper\StructuralEntityChoiceLoader;
|
use App\Form\Type\Helper\StructuralEntityChoiceLoader;
|
||||||
use App\Services\Attachments\AttachmentURLGenerator;
|
use App\Services\Attachments\AttachmentURLGenerator;
|
||||||
use App\Services\Trees\NodesListBuilder;
|
use App\Services\Trees\NodesListBuilder;
|
||||||
|
@ -53,20 +54,20 @@ use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
class StructuralEntityType extends AbstractType
|
class StructuralEntityType extends AbstractType
|
||||||
{
|
{
|
||||||
protected EntityManagerInterface $em;
|
protected EntityManagerInterface $em;
|
||||||
protected AttachmentURLGenerator $attachmentURLGenerator;
|
|
||||||
protected TranslatorInterface $translator;
|
protected TranslatorInterface $translator;
|
||||||
|
protected StructuralEntityChoiceHelper $choice_helper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var NodesListBuilder
|
* @var NodesListBuilder
|
||||||
*/
|
*/
|
||||||
protected $builder;
|
protected NodesListBuilder $builder;
|
||||||
|
|
||||||
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, AttachmentURLGenerator $attachmentURLGenerator, TranslatorInterface $translator)
|
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, TranslatorInterface $translator, StructuralEntityChoiceHelper $choice_helper)
|
||||||
{
|
{
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
$this->builder = $builder;
|
$this->builder = $builder;
|
||||||
$this->attachmentURLGenerator = $attachmentURLGenerator;
|
|
||||||
$this->translator = $translator;
|
$this->translator = $translator;
|
||||||
|
$this->choice_helper = $choice_helper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||||
|
@ -105,44 +106,23 @@ class StructuralEntityType extends AbstractType
|
||||||
'subentities_of' => null, //Only show entities with the given parent class
|
'subentities_of' => null, //Only show entities with the given parent class
|
||||||
'disable_not_selectable' => false, //Disable entries with not selectable property
|
'disable_not_selectable' => false, //Disable entries with not selectable property
|
||||||
'choice_value' => function (?AbstractStructuralDBElement $element) {
|
'choice_value' => function (?AbstractStructuralDBElement $element) {
|
||||||
if ($element === null) {
|
return $this->choice_helper->generateChoiceValue($element);
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Do not change the structure below, even when inspection says it can be replaced with a null coalescing operator.
|
|
||||||
* It is important that the value returned here for a existing element is an int, and for a new element a string.
|
|
||||||
* I dont really understand why, but it seems to be important for the choice_loader to work correctly.
|
|
||||||
* So please do not change this!
|
|
||||||
*/
|
|
||||||
if ($element->getID() === null) {
|
|
||||||
//Must be the same as the separator in the choice_loader, otherwise this will not work!
|
|
||||||
return $element->getFullPath('->');
|
|
||||||
}
|
|
||||||
|
|
||||||
return $element->getID();
|
|
||||||
}, //Use the element id as option value and for comparing items
|
}, //Use the element id as option value and for comparing items
|
||||||
'choice_loader' => function (Options $options) {
|
'choice_loader' => function (Options $options) {
|
||||||
return new StructuralEntityChoiceLoader($options, $this->builder, $this->em);
|
return new StructuralEntityChoiceLoader($options, $this->builder, $this->em);
|
||||||
},
|
},
|
||||||
'choice_label' => function (Options $options) {
|
'choice_label' => function (Options $options) {
|
||||||
return function ($choice, $key, $value) use ($options) {
|
return function ($choice, $key, $value) use ($options) {
|
||||||
return $this->generateChoiceLabels($choice, $key, $value, $options);
|
return $this->choice_helper->generateChoiceLabel($choice, $options);
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
'choice_attr' => function (Options $options) {
|
'choice_attr' => function (Options $options) {
|
||||||
return function ($choice, $key, $value) use ($options) {
|
return function ($choice, $key, $value) use ($options) {
|
||||||
return $this->generateChoiceAttr($choice, $key, $value, $options);
|
return $this->choice_helper->generateChoiceAttr($choice, $options);
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
'group_by' => function (AbstractStructuralDBElement $element)
|
'group_by' => function (AbstractStructuralDBElement $element) {
|
||||||
{
|
return $this->choice_helper->generateGroupBy($element);
|
||||||
//Show entities that are not added to DB yet separately from other entities
|
|
||||||
if ($element->getID() === null) {
|
|
||||||
return $this->translator->trans('entity.select.group.new_not_added_to_DB');
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
},
|
},
|
||||||
'choice_translation_domain' => false, //Don't translate the entity names
|
'choice_translation_domain' => false, //Don't translate the entity names
|
||||||
]);
|
]);
|
||||||
|
@ -207,61 +187,4 @@ class StructuralEntityType extends AbstractType
|
||||||
//Otherwise just return the value
|
//Otherwise just return the value
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function generateChoiceAttr(AbstractStructuralDBElement $choice, $key, $value, $options): array
|
|
||||||
{
|
|
||||||
$tmp = [];
|
|
||||||
|
|
||||||
//Disable attribute if the choice is marked as not selectable
|
|
||||||
if ($options['disable_not_selectable'] && $choice->isNotSelectable()) {
|
|
||||||
$tmp += ['disabled' => 'disabled'];
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($choice instanceof AttachmentType) {
|
|
||||||
$tmp += ['data-filetype_filter' => $choice->getFiletypeFilter()];
|
|
||||||
}
|
|
||||||
|
|
||||||
$level = $choice->getLevel();
|
|
||||||
/** @var AbstractStructuralDBElement|null $parent */
|
|
||||||
$parent = $options['subentities_of'];
|
|
||||||
if (null !== $parent) {
|
|
||||||
$level -= $parent->getLevel() - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
$tmp += [
|
|
||||||
'data-level' => $level,
|
|
||||||
'data-parent' => $choice->getParent() ? $choice->getParent()->getFullPath() : null,
|
|
||||||
'data-path' => $choice->getFullPath('->'),
|
|
||||||
'data-image' => $choice->getMasterPictureAttachment() ? $this->attachmentURLGenerator->getThumbnailURL($choice->getMasterPictureAttachment(), 'thumbnail_xs') : null,
|
|
||||||
];
|
|
||||||
|
|
||||||
if ($choice instanceof AttachmentType && !empty($choice->getFiletypeFilter())) {
|
|
||||||
$tmp += ['data-filetype_filter' => $choice->getFiletypeFilter()];
|
|
||||||
}
|
|
||||||
|
|
||||||
return $tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getElementNameWithLevelWhitespace(AbstractStructuralDBElement $choice, $options, $whitespace = " "): string
|
|
||||||
{
|
|
||||||
/** @var AbstractStructuralDBElement|null $parent */
|
|
||||||
$parent = $options['subentities_of'];
|
|
||||||
|
|
||||||
/*** @var AbstractStructuralDBElement $choice */
|
|
||||||
$level = $choice->getLevel();
|
|
||||||
//If our base entity is not the root level, we need to change the level, to get zero position
|
|
||||||
if (null !== $options['subentities_of']) {
|
|
||||||
$level -= $parent->getLevel() - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
$tmp = str_repeat($whitespace, $level); //Use 3 spaces for intendation
|
|
||||||
$tmp .= htmlspecialchars($choice->getName());
|
|
||||||
|
|
||||||
return $tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function generateChoiceLabels(AbstractStructuralDBElement $choice, $key, $value, $options): string
|
|
||||||
{
|
|
||||||
return $choice->getName();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,36 +32,40 @@
|
||||||
{# <span id="select_count"></span> #}
|
{# <span id="select_count"></span> #}
|
||||||
<span class="badge bg-secondary">{% trans with {'%count%': '<span ' ~ stimulus_target('elements/datatables/parts', 'selectCount') ~ '></span>'} %}part_list.action.part_count{% endtrans %}</span>
|
<span class="badge bg-secondary">{% trans with {'%count%': '<span ' ~ stimulus_target('elements/datatables/parts', 'selectCount') ~ '></span>'} %}part_list.action.part_count{% endtrans %}</span>
|
||||||
|
|
||||||
<select class="selectpicker" name="action" data-controller="elements--selectpicker" {{ stimulus_action('elements/datatables/parts', 'updateTargetPicker', 'change') }}
|
<div class="d-inline-block w-25">
|
||||||
title="{% trans %}part_list.action.action.title{% endtrans %}" required>
|
<select name="action" data-controller="elements--select" {{ stimulus_action('elements/datatables/parts', 'updateTargetPicker', 'change') }}
|
||||||
<optgroup label="{% trans %}part_list.action.action.group.favorite{% endtrans %}">
|
title="{% trans %}part_list.action.action.title{% endtrans %}" required>
|
||||||
<option {% if not is_granted('@parts.change_favorite') %}disabled{% endif %} value="favorite">{% trans %}part_list.action.action.favorite{% endtrans %}</option>
|
<optgroup label="{% trans %}part_list.action.action.group.favorite{% endtrans %}">
|
||||||
<option {% if not is_granted('@parts.change_favorite') %}disabled{% endif %} value="unfavorite">{% trans %}part_list.action.action.unfavorite{% endtrans %}</option>
|
<option {% if not is_granted('@parts.change_favorite') %}disabled{% endif %} value="favorite">{% trans %}part_list.action.action.favorite{% endtrans %}</option>
|
||||||
</optgroup>
|
<option {% if not is_granted('@parts.change_favorite') %}disabled{% endif %} value="unfavorite">{% trans %}part_list.action.action.unfavorite{% endtrans %}</option>
|
||||||
<optgroup label="{% trans %}part_list.action.action.group.needs_review{% endtrans %}">
|
</optgroup>
|
||||||
<option {% if not is_granted('@parts.edit') %}disabled{% endif %} value="set_needs_review">{% trans %}part_list.action.action.set_needs_review{% endtrans %}</option>
|
<optgroup label="{% trans %}part_list.action.action.group.needs_review{% endtrans %}">
|
||||||
<option {% if not is_granted('@parts.edit') %}disabled{% endif %} value="unset_needs_review">{% trans %}part_list.action.action.unset_needs_review{% endtrans %}</option>
|
<option {% if not is_granted('@parts.edit') %}disabled{% endif %} value="set_needs_review">{% trans %}part_list.action.action.set_needs_review{% endtrans %}</option>
|
||||||
</optgroup>
|
<option {% if not is_granted('@parts.edit') %}disabled{% endif %} value="unset_needs_review">{% trans %}part_list.action.action.unset_needs_review{% endtrans %}</option>
|
||||||
<optgroup label="{% trans %}part_list.action.action.group.change_field{% endtrans %}">
|
</optgroup>
|
||||||
<option {% if not is_granted('@categories.read') %}disabled{% endif %} value="change_category" data-url="{{ path('select_category') }}">{% trans %}part_list.action.action.change_category{% endtrans %}</option>
|
<optgroup label="{% trans %}part_list.action.action.group.change_field{% endtrans %}">
|
||||||
<option {% if not is_granted('@footprints.read') %}disabled{% endif %} value="change_footprint" data-url="{{ path('select_footprint') }}">{% trans %}part_list.action.action.change_footprint{% endtrans %}</option>
|
<option {% if not is_granted('@categories.read') %}disabled{% endif %} value="change_category" data-url="{{ path('select_category') }}">{% trans %}part_list.action.action.change_category{% endtrans %}</option>
|
||||||
<option {% if not is_granted('@manufacturers.read') %}disabled{% endif %} value="change_manufacturer" data-url="{{ path('select_manufacturer') }}">{% trans %}part_list.action.action.change_manufacturer{% endtrans %}</option>
|
<option {% if not is_granted('@footprints.read') %}disabled{% endif %} value="change_footprint" data-url="{{ path('select_footprint') }}">{% trans %}part_list.action.action.change_footprint{% endtrans %}</option>
|
||||||
<option {% if not is_granted('@measurement_units.read') %}disabled{% endif %} value="change_unit" data-url="{{ path('select_measurement_unit') }}">{% trans %}part_list.action.action.change_unit{% endtrans %}</option>
|
<option {% if not is_granted('@manufacturers.read') %}disabled{% endif %} value="change_manufacturer" data-url="{{ path('select_manufacturer') }}">{% trans %}part_list.action.action.change_manufacturer{% endtrans %}</option>
|
||||||
</optgroup>
|
<option {% if not is_granted('@measurement_units.read') %}disabled{% endif %} value="change_unit" data-url="{{ path('select_measurement_unit') }}">{% trans %}part_list.action.action.change_unit{% endtrans %}</option>
|
||||||
<optgroup label="{% trans %}part_list.action.group.labels{% endtrans %}">
|
</optgroup>
|
||||||
<option {% if not is_granted('@labels.create_labels') %}disabled{% endif %} value="generate_label_lot" data-url="{{ path('select_label_profiles_lot')}}">{% trans %}part_list.action.projects.generate_label_lot{% endtrans %}</option>
|
<optgroup label="{% trans %}part_list.action.group.labels{% endtrans %}">
|
||||||
<option {% if not is_granted('@labels.create_labels') %}disabled{% endif %} value="generate_label" data-url="{{ path('select_label_profiles')}}">{% trans %}part_list.action.projects.generate_label{% endtrans %}</option>
|
<option {% if not is_granted('@labels.create_labels') %}disabled{% endif %} value="generate_label_lot" data-url="{{ path('select_label_profiles_lot')}}">{% trans %}part_list.action.projects.generate_label_lot{% endtrans %}</option>
|
||||||
</optgroup>
|
<option {% if not is_granted('@labels.create_labels') %}disabled{% endif %} value="generate_label" data-url="{{ path('select_label_profiles')}}">{% trans %}part_list.action.projects.generate_label{% endtrans %}</option>
|
||||||
<optgroup label="{% trans %}part_list.action.group.projects{% endtrans %}">
|
</optgroup>
|
||||||
<option {% if not is_granted('@projects.read') %}disabled{% endif %} value="add_to_project" data-url="{{ path('select_project')}}">{% trans %}part_list.action.projects.add_to_project{% endtrans %}</option>
|
<optgroup label="{% trans %}part_list.action.group.projects{% endtrans %}">
|
||||||
</optgroup>
|
<option {% if not is_granted('@projects.read') %}disabled{% endif %} value="add_to_project" data-url="{{ path('select_project')}}">{% trans %}part_list.action.projects.add_to_project{% endtrans %}</option>
|
||||||
|
</optgroup>
|
||||||
|
|
||||||
<option {% if not is_granted('@parts.delete') %}disabled{% endif %} value="delete">{% trans %}part_list.action.action.delete{% endtrans %}</option>
|
<option {% if not is_granted('@parts.delete') %}disabled{% endif %} value="delete">{% trans %}part_list.action.action.delete{% endtrans %}</option>
|
||||||
</select>
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
<select class="" style="display: none;" data-live-search="true" name="target" {{ stimulus_target('elements/datatables/parts', 'selectTargetPicker') }}>
|
<div class="d-none w-25">
|
||||||
{# This is left empty, as this will be filled by Javascript #}
|
<select class="" data-controller="elements--structural-entity-select" name="target" {{ stimulus_target('elements/datatables/parts', 'selectTargetPicker') }}>
|
||||||
</select>
|
{# This is left empty, as this will be filled by Javascript #}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
<button type="submit" class="btn btn-secondary" {% if not is_granted('@parts.edit') %}disabled{% endif %}>{% trans %}part_list.action.submit{% endtrans %}</button>
|
<button type="submit" class="btn btn-secondary" {% if not is_granted('@parts.edit') %}disabled{% endif %}>{% trans %}part_list.action.submit{% endtrans %}</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue