Skip to content

Commit

Permalink
Merge pull request #1 from lenonleite/feature/first-version
Browse files Browse the repository at this point in the history
first version purge contact field
  • Loading branch information
oltmanns-leuchtfeuer authored Jan 9, 2024
2 parents b27d9f3 + 3dcf743 commit 200bc67
Show file tree
Hide file tree
Showing 22 changed files with 614 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vendor
composer.lock
.idea
.php-cs-fixer.cache
30 changes: 30 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

$finder = PhpCsFixer\Finder::create()
->name('*.php')
->in(__DIR__)
->exclude('vendor');

return (new PhpCsFixer\Config())
->setRules([
'@Symfony' => true,
'binary_operator_spaces' => [
'operators' => [
'=>' => 'align',
'=' => 'align',
],
],
'phpdoc_to_comment' => false,
'ordered_imports' => true,
'array_syntax' => [
'syntax' => 'short',
],
'no_unused_imports' => true,
/**
* Our templates rely heavily on things like endforeach, endif, etc.
* This setting should be turned off at least until we've switched to Twig
* (which is required for Symfony 5).
*/
'no_alternative_syntax' => false,
])
->setFinder($finder);
Binary file added Assets/img/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/img/img-active.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/img/img-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/img/img-plugin-deactived.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions Config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

return [
'name' => 'Purge Contact Fields by Leuchtfeuer',
'description' => 'Adds Mautic campaign action to purge a contact field',
'version' => '1.0.0',
'author' => 'Leuchtfeuer Digital Marketing GmbH',
'services' => [
'integrations' => [
'mautic.integration.actionpurgecontactfield' => [
'class' => \MauticPlugin\LeuchtfeuerPurgeContactFieldBundle\Integration\ActionPurgeContactFieldIntegration::class,
'tags' => [
'mautic.integration',
'mautic.basic_integration',
],
],
'actionpurgecontactfield.integration.configuration' => [
'class' => \MauticPlugin\LeuchtfeuerPurgeContactFieldBundle\Integration\Support\ConfigSupport::class,
'tags' => [
'mautic.config_integration',
],
],
],
],
];
20 changes: 20 additions & 0 deletions Config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Mautic\CoreBundle\DependencyInjection\MauticCoreExtension;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return function (ContainerConfigurator $configurator): void {
$services = $configurator->services()
->defaults()
->autowire()
->autoconfigure()
->public();

$excludes = [
];

$services->load('MauticPlugin\\LeuchtfeuerPurgeContactFieldBundle\\', '../')
->exclude('../{'.implode(',', array_merge(MauticCoreExtension::DEFAULT_EXCLUDES, $excludes)).'}');
};
17 changes: 17 additions & 0 deletions DependencyInjection/LeuchtfeuerPurgeContactFieldExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace MauticPlugin\LeuchtfeuerPurgeContactFieldBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;

class LeuchtfeuerPurgeContactFieldExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../Config'));
$loader->load('services.php');
}
}
54 changes: 54 additions & 0 deletions EventListener/PurgeContactFieldsSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace MauticPlugin\LeuchtfeuerPurgeContactFieldBundle\EventListener;

use Mautic\CampaignBundle\CampaignEvents;
use Mautic\CampaignBundle\Event\CampaignBuilderEvent;
use Mautic\CampaignBundle\Event\CampaignExecutionEvent;
use Mautic\LeadBundle\Model\LeadModel;
use MauticPlugin\LeuchtfeuerPurgeContactFieldBundle\Form\Type\PurgeContactFieldType;
use MauticPlugin\LeuchtfeuerPurgeContactFieldBundle\LeuchtfeuerPurgeContactFieldEvents;
use MauticPlugin\LeuchtfeuerPurgeContactFieldBundle\Model\LfFieldModel;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class PurgeContactFieldsSubscriber implements EventSubscriberInterface
{
public function __construct(
private LeadModel $leadModel,
private LfFieldModel $lfFieldModel
) {
}

public static function getSubscribedEvents(): array
{
return [
CampaignEvents::CAMPAIGN_ON_BUILD => ['addAction', 0],
LeuchtfeuerPurgeContactFieldEvents::ON_PURGE_CONTACT_FIELD => ['purgeContactField', 0],
];
}

public function addAction(CampaignBuilderEvent $event)
{
$event->addAction(
'campaign.purgecontactfields',
[
'label' => 'lf.campaign.action.purgecontactfield.title',
'description' => 'lf.campaign.action.purgecontactfield.description',
'formType' => PurgeContactFieldType::class,
'eventName' => LeuchtfeuerPurgeContactFieldEvents::ON_PURGE_CONTACT_FIELD,
]
);
}

public function purgeContactField(CampaignExecutionEvent $event): void
{
$fieldsPurged = $event->getConfig()['fields'];
$purgeFieldsValues = [];
foreach ($fieldsPurged as $fieldPurged) {
$purgeFieldsValues[$fieldPurged] = $this->lfFieldModel->getPurgeValueByAlias($fieldPurged);
}
$lead = $event->getLead();
$this->leadModel->setFieldValues($lead, $purgeFieldsValues, true);
$this->leadModel->saveEntity($lead);
}
}
48 changes: 48 additions & 0 deletions Form/Type/ContactFieldType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace MauticPlugin\LeuchtfeuerPurgeContactFieldBundle\Form\Type;

use Mautic\LeadBundle\Model\FieldModel;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ContactFieldType extends AbstractType
{
public function __construct(
private FieldModel $fieldModel
) {
}

public function configureOptions(OptionsResolver $resolver): void
{
$fields = $this->fieldModel->getLeadFields();
$choices = [];
foreach ($fields as $field) {
$key = $field->getLabel();
if (!empty($field->isUniqueIdentifer())) {
$key .= '*';
}
$choices[$key] = $field->getAlias();
}

$resolver->setDefaults([
'choices' => $choices,
'required' => false,
'multiple' => true,
'expanded' => false,
'placeholder' => 'mautic.core.form.chooseone',
'label' => 'lf.campaign.action.purgecontactfield.type.title',
]);
}

public function getParent()
{
return ChoiceType::class;
}

public function getBlockPrefix()
{
return 'fieldlist_choices';
}
}
29 changes: 29 additions & 0 deletions Form/Type/PurgeContactFieldType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace MauticPlugin\LeuchtfeuerPurgeContactFieldBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class PurgeContactFieldType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('fields', ContactFieldType::class, [
'label' => 'lf.campaign.action.purgecontactfield.type.title',
'label_attr' => ['class' => 'control-label'],
'attr' => [
'class' => 'form-control',
'tooltip' => 'lf.campaign.action.purgecontactfield.type.desc',
],
'multiple' => true,
'expanded' => false,
'required' => true,
]);
}

public function getBlockPrefix()
{
return 'purgecontactfield';
}
}
30 changes: 30 additions & 0 deletions Integration/ActionPurgeContactFieldIntegration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace MauticPlugin\LeuchtfeuerPurgeContactFieldBundle\Integration;

use Mautic\IntegrationsBundle\Integration\BasicIntegration;
use Mautic\IntegrationsBundle\Integration\ConfigurationTrait;
use Mautic\IntegrationsBundle\Integration\Interfaces\BasicInterface;

class ActionPurgeContactFieldIntegration extends BasicIntegration implements BasicInterface
{
use ConfigurationTrait;

public const INTEGRATION_NAME = 'actionpurgecontactfield';
public const DISPLAY_NAME = 'Purge Contact Field';

public function getIcon(): string
{
return 'plugins/LeuchtfeuerPurgeContactFieldBundle/Assets/img/icon.png';
}

public function getName(): string
{
return self::INTEGRATION_NAME;
}

public function getDisplayName(): string
{
return self::DISPLAY_NAME;
}
}
14 changes: 14 additions & 0 deletions Integration/Support/ConfigSupport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace MauticPlugin\LeuchtfeuerPurgeContactFieldBundle\Integration\Support;

use Mautic\IntegrationsBundle\Integration\DefaultConfigFormTrait;
use Mautic\IntegrationsBundle\Integration\Interfaces\ConfigFormInterface;
use MauticPlugin\LeuchtfeuerPurgeContactFieldBundle\Integration\ActionPurgeContactFieldIntegration;

class ConfigSupport extends ActionPurgeContactFieldIntegration implements ConfigFormInterface
{
use DefaultConfigFormTrait;
}
9 changes: 9 additions & 0 deletions LeuchtfeuerPurgeContactFieldBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace MauticPlugin\LeuchtfeuerPurgeContactFieldBundle;

use Mautic\PluginBundle\Bundle\PluginBundleBase;

class LeuchtfeuerPurgeContactFieldBundle extends PluginBundleBase
{
}
8 changes: 8 additions & 0 deletions LeuchtfeuerPurgeContactFieldEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MauticPlugin\LeuchtfeuerPurgeContactFieldBundle;

final class LeuchtfeuerPurgeContactFieldEvents
{
public const ON_PURGE_CONTACT_FIELD = 'leuchtfeuer_action_purge_contact_field.on_purge_contact_field';
}
91 changes: 91 additions & 0 deletions Model/LfFieldModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace MauticPlugin\LeuchtfeuerPurgeContactFieldBundle\Model;

use Mautic\LeadBundle\Helper\FormFieldHelper;
use Mautic\LeadBundle\Model\FieldModel;

class LfFieldModel extends FieldModel
{
public const EMPTY_TEXT = [
'text',
'email',
'textarea',
'timezone',
'country',
'locale',
'region',
];

public const EMPTY_NUMBER = [
'number',
'tel',
];

public const EMPTY_LIST = [
'select',
'multiselect',
'lookup',
];

public const EMPTY_DATE = [
'date',
'datetime',
];

public const EMPTY_TIME = [
'time',
];

public function getPurgeValueByAlias(string $alias)
{
$field = $this->getEntityByAlias($alias);
if (!$field instanceof \Mautic\LeadBundle\Entity\LeadField) {
new \Exception('Field not found');
}

return $this->getPurgeFieldValueByType($field->getType());
}

public function getPurgeFieldValueByType($type)
{
return $this->getPurgeFieldValues()[$type];
}

public function getPurgeFieldValues(): array
{
$fieldHelper = new FormFieldHelper();
$fieldsType = array_keys($fieldHelper->getTypes());
$result = [];
foreach ($fieldsType as $fieldType) {
switch ($fieldType) {
case in_array($fieldType, self::EMPTY_TEXT):
$value = null;
break;
case in_array($fieldType, self::EMPTY_NUMBER):
$value = 0;
break;
case in_array($fieldType, self::EMPTY_LIST):
$value = [];
break;
case in_array($fieldType, self::EMPTY_DATE):
// $value = new \DateTime();
$value = null;
break;
case in_array($fieldType, self::EMPTY_TIME):
// $value = new \DateTime();
$value = null;
break;
case 'boolean':
$value = false;
break;
default:
$value = null;
break;
}
$result[$fieldType] = $value;
}

return $result;
}
}
Loading

0 comments on commit 200bc67

Please sign in to comment.