From fde2585509d119bbaa0eabab489143feb1e19d07 Mon Sep 17 00:00:00 2001 From: putzwasser <26040044+putzwasser@users.noreply.github.com> Date: Tue, 5 Nov 2024 10:55:37 +0100 Subject: [PATCH] FIX: Custom Point Action triggering and form #13868 (#14218) * TASK: Removes `GenericPointSettingsType` from ajax call * TASK: Removes GenericPointSettingsType * FIX: Makes PointBundle `EventHelper` use the right array item to access points * FIX: Sets initiated state * TASK: Adds test * FIX: phpstan * TASK: Re-creates `GenericPointSettingsType.php` for semver and adds depreciation notice --- .../PointBundle/Controller/AjaxController.php | 7 ++--- .../Form/Type/GenericPointSettingsType.php | 2 ++ .../PointBundle/Form/Type/PointActionType.php | 7 +++-- .../PointBundle/Form/Type/PointType.php | 12 +++++---- .../PointBundle/Helper/EventHelper.php | 5 ++-- .../Tests/Unit/Helper/EventHelperTest.php | 26 +++++++++++++++++++ 6 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 app/bundles/PointBundle/Tests/Unit/Helper/EventHelperTest.php diff --git a/app/bundles/PointBundle/Controller/AjaxController.php b/app/bundles/PointBundle/Controller/AjaxController.php index 2211fadedbf..a2032d2de7e 100644 --- a/app/bundles/PointBundle/Controller/AjaxController.php +++ b/app/bundles/PointBundle/Controller/AjaxController.php @@ -4,7 +4,6 @@ use Mautic\CoreBundle\Controller\AjaxController as CommonAjaxController; use Mautic\CoreBundle\Helper\InputHelper; -use Mautic\PointBundle\Form\Type\GenericPointSettingsType; use Mautic\PointBundle\Form\Type\PointActionType; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\HttpFoundation\Request; @@ -19,6 +18,7 @@ public function reorderTriggerEventsAction(Request $request): \Symfony\Component $sessionName = 'mautic.point.'.$triggerId.'.triggerevents.modified'; $order = InputHelper::clean($request->request->get('triggerEvent')); $components = $session->get($sessionName); + if (!empty($order) && !empty($components)) { $components = array_replace(array_flip($order), $components); $session->set($sessionName, $components); @@ -30,11 +30,11 @@ public function reorderTriggerEventsAction(Request $request): \Symfony\Component public function getActionFormAction(Request $request, FormFactoryInterface $formFactory): \Symfony\Component\HttpFoundation\JsonResponse { + $type = InputHelper::clean($request->request->get('actionType')); $dataArray = [ 'success' => 0, 'html' => '', ]; - $type = InputHelper::clean($request->request->get('actionType')); if (!empty($type)) { // get the HTML for the form @@ -44,11 +44,12 @@ public function getActionFormAction(Request $request, FormFactoryInterface $form if (isset($actions['actions'][$type])) { $themes = ['@MauticPoint/FormTheme/Action/_pointaction_properties_row.html.twig']; + if (!empty($actions['actions'][$type]['formTheme'])) { $themes[] = $actions['actions'][$type]['formTheme']; } - $formType = (!empty($actions['actions'][$type]['formType'])) ? $actions['actions'][$type]['formType'] : GenericPointSettingsType::class; + $formType = (!empty($actions['actions'][$type]['formType'])) ? $actions['actions'][$type]['formType'] : null; $formTypeOptions = (!empty($actions['actions'][$type]['formTypeOptions'])) ? $actions['actions'][$type]['formTypeOptions'] : []; $form = $formFactory->create(PointActionType::class, [], ['formType' => $formType, 'formTypeOptions' => $formTypeOptions]); $html = $this->renderView('@MauticPoint/Point/actionform.html.twig', [ diff --git a/app/bundles/PointBundle/Form/Type/GenericPointSettingsType.php b/app/bundles/PointBundle/Form/Type/GenericPointSettingsType.php index f514fe693fc..a77941e0741 100644 --- a/app/bundles/PointBundle/Form/Type/GenericPointSettingsType.php +++ b/app/bundles/PointBundle/Form/Type/GenericPointSettingsType.php @@ -9,6 +9,8 @@ /** * @extends AbstractType> + * + * @deprecated To be removed in 6.0. No longer necessary as of https://github.com/mautic/mautic/pull/13868 */ class GenericPointSettingsType extends AbstractType { diff --git a/app/bundles/PointBundle/Form/Type/PointActionType.php b/app/bundles/PointBundle/Form/Type/PointActionType.php index 27d726bbab7..fbf3ed4ad8c 100644 --- a/app/bundles/PointBundle/Form/Type/PointActionType.php +++ b/app/bundles/PointBundle/Form/Type/PointActionType.php @@ -21,7 +21,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void if (!empty($options['formTypeOptions'])) { $formTypeOptions = array_merge($formTypeOptions, $options['formTypeOptions']); } - $builder->add('properties', $options['formType'], $formTypeOptions); + + if (isset($options['formType'])) { + $builder->add('properties', $options['formType'], $formTypeOptions); + } if (isset($options['settings']['formTypeCleanMasks'])) { $masks['properties'] = $options['settings']['formTypeCleanMasks']; @@ -33,7 +36,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ - 'formType' => GenericPointSettingsType::class, + 'formType' => null, 'formTypeOptions' => [], ]); } diff --git a/app/bundles/PointBundle/Form/Type/PointType.php b/app/bundles/PointBundle/Form/Type/PointType.php index 8a449c6778a..dd3f6475884 100644 --- a/app/bundles/PointBundle/Form/Type/PointType.php +++ b/app/bundles/PointBundle/Form/Type/PointType.php @@ -36,7 +36,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $builder->add( 'name', - TextType::class, [ + TextType::class, + [ 'label' => 'mautic.core.name', 'label_attr' => ['class' => 'control-label'], 'attr' => ['class' => 'form-control'], @@ -45,7 +46,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $builder->add( 'description', - TextareaType::class, [ + TextareaType::class, + [ 'label' => 'mautic.core.description', 'label_attr' => ['class' => 'control-label'], 'attr' => ['class' => 'form-control editor'], @@ -83,9 +85,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ); $type = (!empty($options['actionType'])) ? $options['actionType'] : $options['data']->getType(); - if ($type) { - $formType = (!empty($options['pointActions']['actions'][$type]['formType'])) ? - $options['pointActions']['actions'][$type]['formType'] : GenericPointSettingsType::class; + + if ($type && !empty($options['pointActions']['actions'][$type]['formType'])) { + $formType = $options['pointActions']['actions'][$type]['formType']; $properties = ($options['data']) ? $options['data']->getProperties() : []; $builder->add( 'properties', diff --git a/app/bundles/PointBundle/Helper/EventHelper.php b/app/bundles/PointBundle/Helper/EventHelper.php index 200b9b3a8c0..7e4dbf1ca4b 100644 --- a/app/bundles/PointBundle/Helper/EventHelper.php +++ b/app/bundles/PointBundle/Helper/EventHelper.php @@ -20,8 +20,9 @@ public static function engagePointAction($lead, $action) // only initiate once per lead per type if (empty($initiated[$lead->getId()][$action['type']])) { - if (!empty($action['properties']['delta'])) { - $pointsChange = $action['properties']['delta']; + if (!empty($action['points'])) { + $pointsChange = $action['points']; + $initiated[$lead->getId()][$action['type']] = true; } } diff --git a/app/bundles/PointBundle/Tests/Unit/Helper/EventHelperTest.php b/app/bundles/PointBundle/Tests/Unit/Helper/EventHelperTest.php new file mode 100644 index 00000000000..511ebac838d --- /dev/null +++ b/app/bundles/PointBundle/Tests/Unit/Helper/EventHelperTest.php @@ -0,0 +1,26 @@ + 1, 'type' => 'helloworld.action.custom_action', 'name' => 'My custom point action', 'properties' => [], 'points' => 50]; + + $points = EventHelper::engagePointAction($lead, $action); + $this->assertEquals(50, $points); + + $points = EventHelper::engagePointAction($lead, $action); + $this->assertEquals(0, $points, 'Second call should return 0 points because the action is already initiated for this lead and type and session.'); + } +}