From 7246807e9d61690c57cf671ee3d8ddbea2243c7f Mon Sep 17 00:00:00 2001 From: klass24 Date: Fri, 30 Aug 2024 17:43:37 +0200 Subject: [PATCH] =?UTF-8?q?Flux=20RSS=20(g=C3=A9n=C3=A9ration=20p=C3=A9rio?= =?UTF-8?q?dique)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/bundle/Form/RssFeedItemsType.php | 69 +++++++++++++++++-- .../public/admin/js/collection_rss.js | 66 ++++++++++++++---- .../views/Form/form_fields.html.twig | 18 +++-- 3 files changed, 130 insertions(+), 23 deletions(-) diff --git a/components/RssFeedBundle/src/bundle/Form/RssFeedItemsType.php b/components/RssFeedBundle/src/bundle/Form/RssFeedItemsType.php index 0973c3022..bd642170c 100644 --- a/components/RssFeedBundle/src/bundle/Form/RssFeedItemsType.php +++ b/components/RssFeedBundle/src/bundle/Form/RssFeedItemsType.php @@ -14,6 +14,7 @@ use Ibexa\Contracts\Core\Repository\ContentTypeService; use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException; +use Ibexa\Contracts\Core\Repository\Values\Content\Content; use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType; use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface; use Novactive\EzRssFeedBundle\Entity\RssFeedItems; @@ -26,6 +27,8 @@ use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Ibexa\Contracts\Taxonomy\Service\TaxonomyEntryAssignmentServiceInterface; + class RssFeedItemsType extends AbstractType { @@ -38,12 +41,18 @@ class RssFeedItemsType extends AbstractType /** @var array */ protected $fieldTypeMap; + protected $taxonomyByField; + + protected $taxonomyEntryAssignmentService; + public function __construct( ContentTypeService $contentTypeService, - ConfigResolverInterface $configResolver + ConfigResolverInterface $configResolver, + TaxonomyEntryAssignmentServiceInterface $taxonomyEntryAssignmentService ) { $this->contentTypeService = $contentTypeService; $this->configResolver = $configResolver; + $this->taxonomyEntryAssignmentService = $taxonomyEntryAssignmentService; } /** @@ -53,6 +62,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void { $contentTypeList = $this->getContentTypeList(); $fieldTypeMap = $this->fieldTypeMap; + $taxonomyByField = $this->taxonomyByField; $builder ->add( @@ -88,6 +98,24 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'label' => 'ez_rss_feed.form.contenttype', 'choices' => $contentTypeList, ] + )->add( + 'chTaxonomy', + ChoiceType::class, + [ + 'label' => 'ez_rss_feed.form.chtaxonomy', + 'required' => false, + 'choices' => $taxonomyByField, + 'empty_data' => null, + ] + )->add( + 'taxonomy', + ChoiceType::class, + [ + 'label' => 'ez_rss_feed.form.taxonomy', + 'required' => false, + 'choices' => $taxonomyByField, + 'empty_data' => null, + ] ) ->add( 'title', @@ -129,6 +157,28 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $formModifier = function (FormInterface $form, ContentType $contentType) { $fieldTypeMap = $this->getFieldTypeByContentType($contentType); + $fieldTypeMapTax = $this->getFieldTypeByTaxonomy($contentType); + $taxonomyByField = $this->getTaxonomyByFieldType($fieldTypeMapTax,$contentType); + $form->add( + 'chTaxonomy', + ChoiceType::class, + [ + 'label' => 'ez_rss_feed.form.chtaxonomy', + 'required' => false, + 'choices' => $fieldTypeMapTax, + 'empty_data' => null, + ] + ); + $form->add( + 'taxonomy', + ChoiceType::class, + [ + 'label' => 'ez_rss_feed.form.taxonomy', + 'required' => false, + 'choices' => $taxonomyByField, + 'empty_data' => null, + ] + ); $form->add( 'title', ChoiceType::class, @@ -139,7 +189,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ); $fieldTypeMap = array_merge(['[Passer]' => ''], $fieldTypeMap); - + $fieldTypeMapTax = array_merge(['[Passer]' => ''], $fieldTypeMapTax); $form->add( 'description', ChoiceType::class, @@ -176,11 +226,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($formModifier) { $data = $event->getData(); - if (null !== $data) { $contentTypeId = $data->getContentTypeId(); $contentType = $this->contentTypeService->loadContentType($contentTypeId); - $formModifier($event->getForm(), $contentType); } } @@ -191,7 +239,6 @@ function (FormEvent $event) use ($formModifier) { function (FormEvent $event) use ($formModifier) { $contentTypeId = $event->getData(); $contentType = $this->contentTypeService->loadContentType($contentTypeId); - $formModifier($event->getForm()->getParent(), $contentType); } ); @@ -232,6 +279,7 @@ public function getContentTypeList(): array $defaultContentType = $this->contentTypeService ->loadContentType(array_values($contentTypesMap)[0]); $this->fieldTypeMap = $this->getFieldTypeByContentType($defaultContentType); + $this->fieldTypeMapTax = $this->getFieldTypeByTaxonomy($defaultContentType); } } catch (NotFoundException $e) { return []; @@ -251,6 +299,17 @@ public function getFieldTypeByContentType(ContentType $contentType): array return $fieldsMap; } + public function getFieldTypeByTaxonomy(ContentType $contentType): array + { + $fieldsMapTax = []; + + foreach ($contentType->getFieldDefinitions()->filterByType('ibexa_taxonomy_entry_assignment') as $fieldDefinition) { + $fieldsMapTax[ucfirst($fieldDefinition->getName())] = $fieldDefinition->identifier; + } + ksort($fieldsMapTax); + + return $fieldsMapTax; + } public function configureOptions(OptionsResolver $resolver): void { diff --git a/components/RssFeedBundle/src/bundle/Resources/public/admin/js/collection_rss.js b/components/RssFeedBundle/src/bundle/Resources/public/admin/js/collection_rss.js index e773fbe20..1aba042e7 100644 --- a/components/RssFeedBundle/src/bundle/Resources/public/admin/js/collection_rss.js +++ b/components/RssFeedBundle/src/bundle/Resources/public/admin/js/collection_rss.js @@ -72,7 +72,6 @@ doc.dispatchEvent(new CustomEvent("rss.item.add", {detail : {"selector": newRow}})); setCTEvent(index); } - function setCTEvent(index) { const itemContainer = doc.querySelector(`#rss_feeds_feed_items_${index}`) itemContainer.querySelector(`#rss_feeds_feed_items_${index}_contenttype_id`) @@ -80,34 +79,73 @@ const val = e.currentTarget.value; const prefixItem = "#rss_feeds_feed_items"; const selectFields = ["title", "description", "category", "media"]; + const selectFieldsTaxonomy = ["chTaxonomy"]; const loader = htmlToElement('
' + '' + '
'); e.currentTarget.after(loader); - const xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { - if (this.readyState !== 4) return; - - if (this.status === 200) { - for (const fieldName of selectFields) { - const mainSelector = document.querySelector(prefixItem + "_" + index + "_" + fieldName); + if (this.readyState !== 4) return; + if (this.status === 200) { + console.log(this.response.field) + for (const fieldName of selectFields) { + const mainSelector = document.querySelector(prefixItem + "_" + index + "_" + fieldName); + mainSelector.innerHTML = ''; + mainSelector.append(htmlToElement("")); + const response = JSON.parse(this.responseText); + for (const responseElement in response.field) { + const option = htmlToElement(''); + option.setAttribute("value", response[responseElement]); + mainSelector.append(option); + } + } + for (const fieldName of selectFieldsTaxonomy) { + const mainSelector = document.querySelector(prefixItem + "_" + index + "_" + fieldName); + console.log(prefixItem + "_" + index + "_" + fieldName) + mainSelector.innerHTML = ''; + mainSelector.append(htmlToElement("")); + const response = JSON.parse(this.responseText); + for (const responseElement in response.taxonomy) { + const option = htmlToElement(''); + option.setAttribute("value", response.taxonomy[responseElement]); + mainSelector.append(option); + } + } + loader.remove(); + } + }; + console.log(templateValues.dataset.rssFieldsPath) + xhr.open('POST', templateValues.dataset.rssFieldsPath + '?contenttype_id=' + val, true); + xhr.send(); + }); + itemContainer.querySelector(`#rss_feeds_feed_items_${index}_chTaxonomy`) + .addEventListener('change', function (e) { + const val = e.currentTarget.value; + const prefixItem = "#rss_feeds_feed_items"; + const loader = htmlToElement('
' + + '' + + '
'); + e.currentTarget.after(loader); + const xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function () { + if (this.status === 200) { + const mainSelector = document.querySelector(prefixItem + "_" + index + "_taxonomy"); mainSelector.innerHTML = ''; mainSelector.append(htmlToElement("")); const response = JSON.parse(this.responseText); - for (const responseElement in response) { + for (const responseElement in response.Alltaxonomy) { + console.log(responseElement) const option = htmlToElement(''); option.setAttribute("value", response[responseElement]); mainSelector.append(option); } } - loader.remove(); } - }; - - xhr.open('POST', templateValues.dataset.rssFieldsPath + '?contenttype_id=' + val, true); - xhr.send(); - }); + console.log(templateValues.dataset.rssFieldsPath + '?chTaxonomy=' + val) + xhr.open('POST', templateValues.dataset.rssFieldsPath + '?chTaxonomy=' + val, true); + xhr.send(); + }); const selectLocationButton = itemContainer.querySelector('.js-novaezrssfeed-select-location-id'); diff --git a/components/RssFeedBundle/src/bundle/Resources/views/Form/form_fields.html.twig b/components/RssFeedBundle/src/bundle/Resources/views/Form/form_fields.html.twig index 0c0c5f369..19fec9d4c 100644 --- a/components/RssFeedBundle/src/bundle/Resources/views/Form/form_fields.html.twig +++ b/components/RssFeedBundle/src/bundle/Resources/views/Form/form_fields.html.twig @@ -98,7 +98,17 @@ {% endblock _rss_feeds_feed_items_entry_media_row %} - +{% block _rss_feeds_feed_items_entry_chtaxonomy_row %} +
+ +
+ {{- form_errors(form) -}} + {{- form_widget(form) -}} +
+
+{% endblock _rss_feeds_feed_items_entry_chtaxonomy_row %} {% block _rss_feeds_feed_items_entry_row %} {%- if compound is defined and compound -%} {%- set element = 'fieldset' -%} @@ -108,9 +118,9 @@ {%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%} {%- endif -%} <{{ element|default('div') }}{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group ibexa-container')|trim})} %}{{ block('attributes') }}{% endwith %}> - {{- form_widget(form, widget_attr) -}} - {{- form_help(form) -}} - {{- form_errors(form) -}} + {{- form_widget(form, widget_attr) -}} + {{- form_help(form) -}} + {{- form_errors(form) -}} {% endblock _rss_feeds_feed_items_entry_row %}