Skip to content

Commit

Permalink
KUN-8981 | init... todo.. fix bc layer...
Browse files Browse the repository at this point in the history
  • Loading branch information
mart-insiders committed Aug 15, 2024
1 parent 6f4bdc7 commit f895afe
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 160 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions src/Kunstmaan/AdminBundle/Resources/public/js/admin-bundle.min.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

58 changes: 11 additions & 47 deletions src/Kunstmaan/AdminBundle/Resources/ui/js/_url-chooser.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ kunstmaanbundles.urlChooser = (function (window, undefined) {
}
};


// Save for Media-chooser
saveMediaChooserModal = function (cke, isCropable) {
if (!cke) {
Expand Down Expand Up @@ -201,7 +200,6 @@ kunstmaanbundles.urlChooser = (function (window, undefined) {
}
};


// Get Url Parameters
getUrlParam = function (paramName) {
var reParam = new RegExp('(?:[\?&]|&)' + paramName + '=([^&]+)', 'i'),
Expand All @@ -214,49 +212,16 @@ kunstmaanbundles.urlChooser = (function (window, undefined) {
adaptUrlChooser = function () {
$body.on('click', '.js-change-link-type', function (e) {
e.preventDefault();
var $form = $(this).closest('form'),
$urlChooser = $(this).parents('.urlchooser-wrapper'),
$urlChooserName = $urlChooser.data('chooser-name');

var values = {};

$.each($form.serializeArray(), function (i, field) {
// Only submit required values.
if (field.name.indexOf('link_type') !== -1 || field.name.indexOf('link_url') !== -1) {
if (field.name.indexOf($urlChooserName) !== -1 && field.name.indexOf('link_url') === -1) {
values[field.name] = field.value;
}
}
else {
// Main sequence can not be submitted.
if (field.name.indexOf('sequence') === -1) {
// handle array values
if (endsWith(field.name, '[]')) {
if (typeof values[field.name] === 'undefined' || typeof values[field.name] === 'string') {
values[field.name] = [field.value];
} else {
values[field.name].push(field.value);
}
} else {
values[field.name] = field.value;
}
}
}
});

// Add the selected li value.
values[$(this).data('name')] = $(this).data('value');

$.ajax({
url: $form.attr('action'),
type: $form.attr('method'),
data: values,
success: function (html) {
$urlChooser.replaceWith(
$(html).find('#' + $urlChooser.attr('id'))
);
}
});
const urlChooser = e.target.closest('.urlchooser-wrapper');
const urlChooserText = urlChooser.querySelector('.urlchooser__link-type .text');
const newValue = e.target.closest('[data-value]').dataset.value;
const oldActive = urlChooser.querySelector('[data-show-on]:not(.hidden)');
const newActive = urlChooser.querySelector('[data-show-on="' + newValue + '"]');
const typeInput = urlChooser.querySelector('input[type="hidden"][data-button-id="'+urlChooser.getAttribute('data-link-type-id')+'"]');
typeInput.value = newValue;
urlChooserText.innerHTML = e.target.innerHTML;
oldActive.classList.add('hidden');
newActive.classList.remove('hidden');
}
);
};
Expand All @@ -273,5 +238,4 @@ kunstmaanbundles.urlChooser = (function (window, undefined) {
init: init
};

})
(window);
})(window);
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('icon')->defaultNull()->end()
->scalarNode('hidden_from_tree')->end()
->booleanNode('is_homepage')->end()
->booleanNode('enable_improved_urlchooser')->defaultFalse()->end()
->arrayNode('allowed_children')
->prototype('array')
->beforeNormalization()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Kunstmaan\NodeBundle\DependencyInjection;

use Kunstmaan\NodeBundle\Entity\PageViewDataProviderInterface;
use Kunstmaan\NodeBundle\Form\EventListener\URLChooserFormSubscriber;
use Kunstmaan\NodeBundle\Form\EventListener\URLChooserLinkTypeSubscriber;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -40,8 +42,16 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('kunstmaan_node.lock_threshold', $config['lock']['threshold']);
$container->setParameter('kunstmaan_node.lock_enabled', $config['lock']['enabled']);

$enableImprovedUrlchooser = $config['pages']['enable_improved_urlchooser'];
$container->setParameter('kunstmaan_node.enable_improved_urlchooser', $enableImprovedUrlchooser);

$loader->load('services.yml');
$loader->load('commands.yml');

if ($enableImprovedUrlchooser) {
$container->removeDefinition(URLChooserFormSubscriber::class);
$container->removeDefinition(URLChooserLinkTypeSubscriber::class);
}
}

public function prepend(ContainerBuilder $container): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,44 @@ class URLChooserToLinkTransformer implements DataTransformerInterface
{
use URLValidator;

/**
* @return array
*/
public function transform($value)
public function transform($value): array
{
if ($value === null) {
return [
'link_type' => URLChooserType::INTERNAL,
'link_url' => $value,
];
}

$data = [];
if ($this->isEmailAddress($value)) {
$data['choice_email'] = $value;
$linkType = URLChooserType::EMAIL;
} elseif ($this->isInternalLink($value) || $this->isInternalMediaLink($value)) {
$data['choice_interal']['input'] = $value;
$linkType = URLChooserType::INTERNAL;
} else {
$data['choice_external'] = $value;
$linkType = URLChooserType::EXTERNAL;
}

return [
return array_merge($data, [
'link_type' => $linkType,
'link_url' => $value,
];
]);
}

/**
* @return string|null
*/
public function reverseTransform($value)
public function reverseTransform($value): ?string
{
if (empty($value)) {
return null;
if (!empty($value['link_type'])) {
switch ($value['link_type']) {
case URLChooserType::INTERNAL:
return $value['link_url'];
case URLChooserType::EXTERNAL:
return $value['choice_external'];
case URLChooserType::EMAIL:
return $value['choice_email'];
}
}

return $value['link_url'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Url;

/** @deprecated */
class URLChooserFormSubscriber implements EventSubscriberInterface
{
use URLValidator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Url;

/** @deprecated */
class URLChooserLinkTypeSubscriber implements EventSubscriberInterface
{
/**
Expand Down
30 changes: 30 additions & 0 deletions src/Kunstmaan/NodeBundle/Form/Type/InternalURLSelectorType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Kunstmaan\NodeBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class InternalURLSelectorType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('input', HiddenType::class, [
'label' => false,
]);
$builder->add('url', TextType::class, [
'label' => false,
'disabled' => true,
'mapped' => false,
]);
}

public function getBlockPrefix()
{
return 'internal_url_selector';
}
}
69 changes: 52 additions & 17 deletions src/Kunstmaan/NodeBundle/Form/Type/URLChooserType.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
<?php

declare(strict_types=1);

namespace Kunstmaan\NodeBundle\Form\Type;

use Kunstmaan\NodeBundle\Form\DataTransformer\URLChooserToLinkTransformer;
use Kunstmaan\NodeBundle\Form\EventListener\URLChooserFormSubscriber;
use Kunstmaan\NodeBundle\Form\EventListener\URLChooserLinkTypeSubscriber;
use Kunstmaan\NodeBundle\Validator\Constraint\ValidExternalUrl;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
* URLChooserType
Expand Down Expand Up @@ -50,22 +57,26 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}
}

$builder->add(
'link_type',
ChoiceType::class,
[
'required' => true,
'mapped' => false,
'attr' => [
'class' => 'js-change-link-type',
],
'choices' => $choices,
]
);
$builder->add('link_url', HiddenType::class, [
'required' => false,
'constraints' => [
new Callback([$this, 'validateLink']),
],
]);
$builder->add('link_type', URLTypeType::class, [
'required' => true,
'choices' => array_flip($choices),
'attr' => [
'class' => 'urlchooser-type',
],
]);

$builder->get('link_type')->addEventSubscriber(new URLChooserLinkTypeSubscriber());
$builder->add('choice_external', TextType::class, [
'attr' => ['placeholder' => 'https://'],
]);
$builder->add('choice_email', EmailType::class);
$builder->add('choice_internal', InternalURLSelectorType::class);

$builder->addEventSubscriber(new URLChooserFormSubscriber());
$builder->addViewTransformer(new URLChooserToLinkTransformer());
}

Expand Down Expand Up @@ -94,4 +105,28 @@ public function getBlockPrefix()
{
return 'urlchooser';
}

public function validateLink($value, ExecutionContextInterface $context): void
{
$urlChooserTypeForm = $context->getObject()?->getParent();
if (!$urlChooserTypeForm) {
return;
}

$type = $urlChooserTypeForm->get('link_type')->getData();
if ($type === self::EXTERNAL) {
$context->getValidator()->inContext($context)->validate($this->getFormDataValue($urlChooserTypeForm, 'choice_external'), [new ValidExternalUrl()]);

return;
}

if ($type === self::EMAIL) {
$context->getValidator()->inContext($context)->validate($this->getFormDataValue($urlChooserTypeForm, 'choice_email'), [new Email()]);
}
}

private function getFormDataValue(FormInterface $form, string $fieldName): ?string
{
return $form->has($fieldName) ? $form->get($fieldName)->getData() : null;
}
}
31 changes: 31 additions & 0 deletions src/Kunstmaan/NodeBundle/Form/Type/URLTypeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Kunstmaan\NodeBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class URLTypeType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('choices');
$resolver->setDefault('choices', []);
$resolver->setAllowedTypes('choices', 'array');
}

public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['choices'] = $options['choices'];
}

public function getParent(): string
{
return HiddenType::class;
}
}
Loading

0 comments on commit f895afe

Please sign in to comment.