Skip to content

Commit

Permalink
FIX: Custom Point Action triggering and form mautic#13868 (mautic#14218)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
putzwasser authored Nov 5, 2024
1 parent 6a11649 commit fde2585
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 12 deletions.
7 changes: 4 additions & 3 deletions app/bundles/PointBundle/Controller/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

/**
* @extends AbstractType<array<mixed>>
*
* @deprecated To be removed in 6.0. No longer necessary as of https://github.com/mautic/mautic/pull/13868
*/
class GenericPointSettingsType extends AbstractType
{
Expand Down
7 changes: 5 additions & 2 deletions app/bundles/PointBundle/Form/Type/PointActionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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' => [],
]);
}
Expand Down
12 changes: 7 additions & 5 deletions app/bundles/PointBundle/Form/Type/PointType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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'],
Expand Down Expand Up @@ -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',
Expand Down
5 changes: 3 additions & 2 deletions app/bundles/PointBundle/Helper/EventHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
26 changes: 26 additions & 0 deletions app/bundles/PointBundle/Tests/Unit/Helper/EventHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Mautic\PointBundle\Tests\Unit\Helper;

use Mautic\LeadBundle\Entity\Lead;
use Mautic\PointBundle\Helper\EventHelper;
use PHPUnit\Framework\TestCase;

class EventHelperTest extends TestCase
{
public function testEngagePointAction(): void
{
$lead = new Lead();

// Define the action array
$action = ['id' => 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.');
}
}

0 comments on commit fde2585

Please sign in to comment.