Use tomselect for StructuralEntityType

This commit is contained in:
Jan Böhmer 2023-01-29 18:52:24 +01:00
parent f085402cba
commit 8d5427a1c3
6 changed files with 119 additions and 73 deletions

View file

@ -60,11 +60,6 @@ class PartLotType extends AbstractType
'label' => 'part_lot.edit.location',
'required' => false,
'disable_not_selectable' => true,
'attr' => [
'data-controller' => 'elements--selectpicker',
'title' => 'selectpicker.nothing_selected',
'data-live-search' => true,
],
]);
$builder->add('amount', SIUnitType::class, [

View file

@ -25,6 +25,7 @@ namespace App\Form\Type;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\PriceInformations\Currency;
use App\Services\Attachments\AttachmentURLGenerator;
use App\Services\Trees\NodesListBuilder;
use Doctrine\ORM\EntityManagerInterface;
use RuntimeException;
@ -39,9 +40,9 @@ class CurrencyEntityType extends StructuralEntityType
{
protected ?string $base_currency;
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, ?string $base_currency)
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, AttachmentURLGenerator $attachmentURLGenerator, ?string $base_currency)
{
parent::__construct($em, $builder);
parent::__construct($em, $builder, $attachmentURLGenerator);
$this->base_currency = $base_currency;
}

View file

@ -24,6 +24,7 @@ namespace App\Form\Type;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Services\Attachments\AttachmentURLGenerator;
use App\Services\Trees\NodesListBuilder;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\AbstractType;
@ -44,16 +45,18 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
class StructuralEntityType extends AbstractType
{
protected EntityManagerInterface $em;
protected AttachmentURLGenerator $attachmentURLGenerator;
/**
* @var NodesListBuilder
*/
protected $builder;
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder)
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, AttachmentURLGenerator $attachmentURLGenerator)
{
$this->em = $em;
$this->builder = $builder;
$this->attachmentURLGenerator = $attachmentURLGenerator;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
@ -94,17 +97,14 @@ class StructuralEntityType extends AbstractType
$resolver->setDefault('empty_message', null);
$resolver->setDefault('controller', 'elements--selectpicker');
$resolver->setDefault('controller', 'elements--structural-entity-select');
$resolver->setDefault('attr', static function (Options $options) {
$tmp = [
'data-controller' => $options['controller'],
'data-live-search' => true,
'title' => 'selectpicker.nothing_selected',
];
if ($options['empty_message']) {
$tmp['data-none-Selected-Text'] = $options['empty_message'];
$tmp['title'] = $options['empty_message'];
$tmp['data-empty_message'] = $options['empty_message'];
}
return $tmp;
@ -119,15 +119,6 @@ class StructuralEntityType extends AbstractType
return $this->builder->typeToNodesList($options['class'], null);
}
public function buildView(FormView $view, FormInterface $form, array $options): void
{
//Allow HTML in labels. You must override the 'choice_widget_options' block, so that can work
//See extendedBootstrap4_layout.html.twig for that...
$view->vars['use_html_in_labels'] = true;
parent::buildView($view, $form, $options);
}
public function getParent(): string
{
return ChoiceType::class;
@ -221,35 +212,10 @@ class StructuralEntityType extends AbstractType
return $this->em->find($options['class'], $value->getID());
}
/**
* This generates the HTML code that will be rendered by selectpicker
* @return string
*/
protected function getChoiceContent(AbstractStructuralDBElement $choice, $key, $value, $options): string
{
$html = "";
//Add element name, use a class as whitespace which hides when not used in dropdown list
$html .= $this->getElementNameWithLevelWhitespace($choice, $options, '<span class="picker-level"></span>');
if ($options['show_fullpath_in_subtext'] && null !== $choice->getParent()) {
$html .= '<span class="ms-3 badge rounded-pill bg-secondary float-end picker-us"><i class="fa-solid fa-folder-tree"></i>&nbsp;' . trim(htmlspecialchars($choice->getParent()->getFullPath())) . '</span>';
}
if ($choice instanceof AttachmentType && !empty($choice->getFiletypeFilter())) {
$html .= '<span class="ms-3 badge bg-warning"><i class="fa-solid fa-file-circle-exclamation"></i>&nbsp;' . trim(htmlspecialchars($choice->getFiletypeFilter())) . '</span>';
}
return $html;
}
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'];
@ -259,8 +225,22 @@ class StructuralEntityType extends AbstractType
$tmp += ['data-filetype_filter' => $choice->getFiletypeFilter()];
}
//Add the HTML content that will be shown finally in the selectpicker
$tmp += ['data-content' => $this->getChoiceContent($choice, $key, $value, $options)];
$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-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;
}
@ -285,7 +265,6 @@ class StructuralEntityType extends AbstractType
protected function generateChoiceLabels(AbstractStructuralDBElement $choice, $key, $value, $options): string
{
//Just for compatibility reasons for the case selectpicker should not work. The real value is generated in the getChoiceContent() method
return $this->getElementNameWithLevelWhitespace($choice, $options, " ");
return $choice->getName();
}
}

View file

@ -101,13 +101,13 @@ class AttachmentURLGenerator
/**
* Returns a URL under which the attachment file can be viewed.
* @return string|null The URL or null if the attachment file is not existing
*/
public function getViewURL(Attachment $attachment): string
public function getViewURL(Attachment $attachment): ?string
{
$absolute_path = $this->attachmentHelper->toAbsoluteFilePath($attachment);
if (null === $absolute_path) {
throw new RuntimeException('The given attachment is external or has no valid file, so no URL can get generated for it!
Use Attachment::getURL() to get the external URL!');
return null;
}
$asset_path = $this->absolutePathToAssetPath($absolute_path);
@ -122,8 +122,9 @@ class AttachmentURLGenerator
/**
* Returns a URL to an thumbnail of the attachment file.
* @return string|null The URL or null if the attachment file is not existing
*/
public function getThumbnailURL(Attachment $attachment, string $filter_name = 'thumbnail_sm'): string
public function getThumbnailURL(Attachment $attachment, string $filter_name = 'thumbnail_sm'): ?string
{
if (!$attachment->isPicture()) {
throw new InvalidArgumentException('Thumbnail creation only works for picture attachments!');
@ -135,7 +136,7 @@ class AttachmentURLGenerator
$absolute_path = $this->attachmentHelper->toAbsoluteFilePath($attachment);
if (null === $absolute_path) {
throw new RuntimeException('The given attachment is external or has no valid file, so no URL can get generated for it!');
return null;
}
$asset_path = $this->absolutePathToAssetPath($absolute_path);