Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MTC-6662] Change DWC filter "Device Type" to work without core patch #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vendor
composer.lock
.idea
.php-cs-fixer.cache
.phpunit.result.cache
phpunit.xml
.~
31 changes: 31 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

$finder = PhpCsFixer\Finder::create()
->name('*.php')
->in(__DIR__)
->exclude('Library')
->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);
26 changes: 0 additions & 26 deletions Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,4 @@

'routes' => [],
'menu' => [],

'services' => [
'integrations' => [
'mautic.integration.leuchtfeuerdwcdevicetype' => [
'class' => \MauticPlugin\LeuchtfeuerDwcDeviceTypeBundle\Integration\LeuchtfeuerDwcDeviceTypeIntegration::class,
'arguments' => [
'event_dispatcher',
'mautic.helper.cache_storage',
'doctrine.orm.entity_manager',
'session',
'request_stack',
'router',
'translator',
'logger',
'mautic.helper.encryption',
'mautic.lead.model.lead',
'mautic.lead.model.company',
'mautic.helper.paths',
'mautic.core.model.notification',
'mautic.lead.model.field',
'mautic.plugin.model.integration_entity',
'mautic.lead.model.dnc',
],
],
],
],
];
4 changes: 4 additions & 0 deletions Config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@

$services->load('MauticPlugin\\LeuchtfeuerDwcDeviceTypeBundle\\', '../')
->exclude('../{'.implode(',', array_merge(MauticCoreExtension::DEFAULT_EXCLUDES, $excludes)).'}');
$services->alias(
'mautic.integration.leuchtfeuerdwcdevicetype',
MauticPlugin\LeuchtfeuerDwcDeviceTypeBundle\Integration\LeuchtfeuerDwcDeviceTypeIntegration::class
);
};
81 changes: 41 additions & 40 deletions EventListener/DynamicContentSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

use Mautic\DynamicContentBundle\DynamicContentEvents;
use Mautic\DynamicContentBundle\Event\ContactFiltersEvaluateEvent;
use Mautic\DynamicContentBundle\Helper\DynamicContentHelper;
use Mautic\EmailBundle\EventListener\MatchFilterForLeadTrait;
use Mautic\LeadBundle\Model\DeviceModel;
use Mautic\PluginBundle\Helper\IntegrationHelper;
use MauticPlugin\LeuchtfeuerDwcDeviceTypeBundle\Integration\Config;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class DynamicContentSubscriber implements EventSubscriberInterface
{
public function __construct(private IntegrationHelper $helper, private DeviceModel $deviceModel)
use MatchFilterForLeadTrait;

public function __construct(private Config $config, private DeviceModel $deviceModel, private DynamicContentHelper $dynamicContentHelper)
{
}

Expand All @@ -23,54 +27,51 @@ public static function getSubscribedEvents(): array

public function onContactFiltersEvaluate(ContactFiltersEvaluateEvent $event): void
{
$myIntegration = $this->helper->getIntegrationObject('LeuchtfeuerDwcDeviceType');

if (false === $myIntegration || !$myIntegration->getIntegrationSettings()->getIsPublished()) {
if (!$this->config->isPublished()) {
return;
}

$filters = $event->getFilters();
$contact = $event->getContact();
$filters = $event->getFilters();
$contact = $event->getContact();
$leadDeviceRepository = $this->deviceModel->getRepository();
$leadDevice = $leadDeviceRepository->getLeadDevices($contact);
if (empty($leadDevice)) {
$leadDevice = $leadDeviceRepository->getLeadDevices($contact);
if ([] === $leadDevice) {
return;
}

$deviceType = $leadDevice[0]['device'];
$evaluated = false;
$matched = false;
foreach ($filters as $filter) {
if ('device_type' === $filter['type']) {
switch ($filter['operator']) {
case 'in':
if (in_array($deviceType, $filter['filter'])) {
$event->setIsEvaluated(true);
$event->setIsMatched(in_array($deviceType, $filter['filter']));
}
break;
case '!in':
if (!in_array($deviceType, $filter['filter'])) {
$event->setIsEvaluated(true);
$event->setIsMatched(!in_array($deviceType, $filter['filter']));
}
break;
case 'empty':
if (empty($deviceType)) {
$event->setIsEvaluated(true);
$event->setIsMatched(empty($deviceType));
}
break;
case '!empty':
if (!empty($deviceType)) {
$event->setIsEvaluated(true);
$event->setIsMatched(!empty($deviceType));
}
break;
default:
$event->setIsEvaluated(true);
$event->setIsMatched(false);
break;
}
if ('device_type' !== $filter['type']) {
continue;
}

$evaluated = true;

switch ($filter['operator']) {
case 'in':
$matched = in_array($deviceType, $filter['filter'], true);
break;
case '!in':
$matched = !in_array($deviceType, $filter['filter'], true);
break;
case 'empty':
$matched = null === $deviceType || '' === $deviceType;
break;
case '!empty':
$matched = null !== $deviceType && '' !== $deviceType;
break;
default:
throw new \InvalidArgumentException('Invalid filter operator: '.$filter['operator']);
}
}

if (!$evaluated) {
return;
}

$event->setIsEvaluated(true);
$event->setIsMatched($matched);
}
}
30 changes: 13 additions & 17 deletions EventListener/LeadListSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,37 @@
use Mautic\LeadBundle\LeadEvents;
use Mautic\LeadBundle\Model\ListModel;
use Mautic\LeadBundle\Provider\FieldChoicesProviderInterface;
use Mautic\PluginBundle\Helper\IntegrationHelper;
use MauticPlugin\LeuchtfeuerDwcDeviceTypeBundle\Integration\Config;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class LeadListSubscriber implements EventSubscriberInterface
{

public function __construct(private IntegrationHelper $helper, private ListModel $listModel, private TranslatorInterface $translator, private FieldChoicesProviderInterface $fieldChoicesProvider)
public function __construct(private Config $config, private ListModel $listModel, private TranslatorInterface $translator, private FieldChoicesProviderInterface $fieldChoicesProvider)
{

}

public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
LeadEvents::LIST_FILTERS_CHOICES_ON_GENERATE => ['onFilterChoiceFieldsGenerate', 0],
LeadEvents::LIST_FILTERS_CHOICES_ON_GENERATE => ['onFilterChoiceFieldsGenerate', 0],
];
}

public function onFilterChoiceFieldsGenerate(LeadListFiltersChoicesEvent $event)
public function onFilterChoiceFieldsGenerate(LeadListFiltersChoicesEvent $event): void
{
$myIntegration = $this->helper->getIntegrationObject('LeuchtfeuerDwcDeviceType');

if (false === $myIntegration || !$myIntegration->getIntegrationSettings()->getIsPublished()) {
if (!$this->config->isPublished()) {
return;
}

$config = [
'label' => $this->translator->trans('mautic.plugin.device_type'),
'properties' => [
'type' => 'device_type',
'list' => $this->fieldChoicesProvider->getChoicesForField('select', 'device_type'),
'label' => $this->translator->trans('mautic.plugin.device_type'),
'properties' => [
'type' => 'device_type',
'list' => $this->fieldChoicesProvider->getChoicesForField('select', 'device_type'),
],
'operators' => $this->listModel->getOperatorsForFieldType('multiselect'),
'object' => 'lead',
'operators' => $this->listModel->getOperatorsForFieldType('multiselect'),
'object' => 'lead',
];

$event->addChoice('lead', 'device_type', $config);
Expand Down
37 changes: 37 additions & 0 deletions Integration/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace MauticPlugin\LeuchtfeuerDwcDeviceTypeBundle\Integration;

use Mautic\IntegrationsBundle\Exception\IntegrationNotFoundException;
use Mautic\IntegrationsBundle\Helper\IntegrationsHelper;
use Mautic\PluginBundle\Entity\Integration;

class Config
{
public function __construct(private IntegrationsHelper $integrationsHelper)
{
}

public function isPublished(): bool
{
try {
$integration = $this->getIntegrationEntity();

return (bool) $integration->getIsPublished();
} catch (IntegrationNotFoundException) {
return false;
}
}

/**
* @throws IntegrationNotFoundException
*/
public function getIntegrationEntity(): Integration
{
$integrationObject = $this->integrationsHelper->getIntegration(LeuchtfeuerDwcDeviceTypeIntegration::PLUGIN_NAME);

return $integrationObject->getIntegrationConfiguration();
}
}
22 changes: 13 additions & 9 deletions Integration/LeuchtfeuerDwcDeviceTypeIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@

namespace MauticPlugin\LeuchtfeuerDwcDeviceTypeBundle\Integration;

use Mautic\PluginBundle\Integration\AbstractIntegration;
use Mautic\IntegrationsBundle\Integration\BasicIntegration;
use Mautic\IntegrationsBundle\Integration\DefaultConfigFormTrait;
use Mautic\IntegrationsBundle\Integration\Interfaces\BasicInterface;
use Mautic\IntegrationsBundle\Integration\Interfaces\ConfigFormInterface;

class LeuchtfeuerDwcDeviceTypeIntegration extends AbstractIntegration
class LeuchtfeuerDwcDeviceTypeIntegration extends BasicIntegration implements BasicInterface, ConfigFormInterface
{
public const PLUGIN_NAME = 'LeuchtfeuerDwcDeviceType';
public const DISPLAY_NAME = 'Device type filter for DWC';
public const AUTHENTICATION_TYPE = 'none';
use DefaultConfigFormTrait;

public function getName()
public const PLUGIN_NAME = 'LeuchtfeuerDwcDeviceType';
public const DISPLAY_NAME = 'Device type filter for DWC';

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

public function getDisplayName()
public function getDisplayName(): string
{
return self::DISPLAY_NAME;
}

public function getAuthenticationType()
public function getIcon(): string
{
return self::AUTHENTICATION_TYPE;
return 'plugins/LeuchtfeuerDwcDeviceTypeBundle/Assets/img/leuchtfeuerdwcdevicetype.png';
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
- Before installing plugin, please make sure to install changes from PR https://github.com/mautic/mautic/pull/13226, otherwise plugin will not work

## Installation
### Composer
This plugin can be installed through composer

### Manual install
- Plugin has to be saved within plugins/LeuchtfeuerDwcDeviceTypeBundle/
- After completed installing of plugin, the cache has to be cleared (php bin/console c:c )
- Reload plugins page and press "Install/Upgrade Plugins" button
Expand Down
53 changes: 53 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "leuchtfeuer/mautic-dwc-device-type-bundle",
"description": "TODO",
"license": "TODO",
"type": "mautic-plugin",
"homepage": "https://www.Leuchtfeuer.com",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^8.0",
"ext-json": "*",
"mautic/core-lib": "^5.0"
},
"extra": {
"install-directory-name": "LeuchtfeuerDwcDeviceTypeBundle"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.4"
},
"autoload": {
"psr-4": {
"MauticPlugin\\LeuchtfeuerCdnBundle\\": ""
}
},
"autoload-dev": {
"psr-4": {
"MauticPlugin\\LeuchtfeuerCdnBundle\\Tests\\": "Tests"
}
},
"repositories": [
{
"type": "git",
"url": "https://github.com/mautic/FOSOAuthServerBundle.git"
},
{
"type": "git",
"url": "https://github.com/mautic/SpBundle.git"
},
{
"type": "git",
"url": "https://github.com/mautic/SymfonyBridgeBundle.git"
}
],
"scripts": {
"fix-cs": "./vendor/bin/php-cs-fixer fix --config .php-cs-fixer.php --show-progress dots -v"
},
"config": {
"allow-plugins": {
"symfony/flex": true,
"php-http/discovery": false
}
}
}