Skip to content

Commit

Permalink
Flux RSS (génération périodique)
Browse files Browse the repository at this point in the history
  • Loading branch information
Klass24 committed Aug 30, 2024
1 parent a84773e commit 7246807
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 23 deletions.
69 changes: 64 additions & 5 deletions components/RssFeedBundle/src/bundle/Form/RssFeedItemsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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;
}

/**
Expand All @@ -53,6 +62,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
{
$contentTypeList = $this->getContentTypeList();
$fieldTypeMap = $this->fieldTypeMap;
$taxonomyByField = $this->taxonomyByField;

$builder
->add(
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
);
Expand Down Expand Up @@ -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 [];
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,42 +72,80 @@
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`)
.addEventListener('change', function (e) {
const val = e.currentTarget.value;
const prefixItem = "#rss_feeds_feed_items";
const selectFields = ["title", "description", "category", "media"];
const selectFieldsTaxonomy = ["chTaxonomy"];
const loader = htmlToElement('<div class="loading-image">' +
'<img src="' + templateValues.dataset.loaderPath + '" class="img-responsive" alt=""/>' +
'</div>');
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("<option value='' selected>[Passer]</option>"));
const response = JSON.parse(this.responseText);
for (const responseElement in response.field) {
const option = htmlToElement('<option value="">' + responseElement + '</option>');
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("<option value='' selected>[Passer]</option>"));
const response = JSON.parse(this.responseText);
for (const responseElement in response.taxonomy) {
const option = htmlToElement('<option value="">' + responseElement + '</option>');
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('<div class="loading-image">' +
'<img src="' + templateValues.dataset.loaderPath + '" class="img-responsive" alt=""/>' +
'</div>');
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("<option value='' selected>[Passer]</option>"));
const response = JSON.parse(this.responseText);
for (const responseElement in response) {
for (const responseElement in response.Alltaxonomy) {
console.log(responseElement)
const option = htmlToElement('<option value="">' + responseElement + '</option>');
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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,17 @@
</div>
</div>
{% endblock _rss_feeds_feed_items_entry_media_row %}

{% block _rss_feeds_feed_items_entry_chtaxonomy_row %}
<div class="form-group col-md-6 col-sm-12 col-xs-12">
<label class="form-label">
{{- form_label(form) -}}
</label>
<div class="controls">
{{- form_errors(form) -}}
{{- form_widget(form) -}}
</div>
</div>
{% endblock _rss_feeds_feed_items_entry_chtaxonomy_row %}
{% block _rss_feeds_feed_items_entry_row %}
{%- if compound is defined and compound -%}
{%- set element = 'fieldset' -%}
Expand All @@ -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) -}}
</{{ element|default('div') }}>
{% endblock _rss_feeds_feed_items_entry_row %}

Expand Down

0 comments on commit 7246807

Please sign in to comment.