Skip to content

Commit

Permalink
Merge pull request #92 from unikrn/3.x
Browse files Browse the repository at this point in the history
Dependency Injection on event subscribers for 3.x compatibility
  • Loading branch information
Heath Dutton ☕ authored May 27, 2020
2 parents 7ff772b + 1b3366e commit 53e9c49
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 50 deletions.
22 changes: 12 additions & 10 deletions Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,62 @@
'services' => [
'events' => [
'mautic.extended_field.config.subscriber' => [
'class' => 'MauticPlugin\MauticExtendedFieldBundle\EventListener\ConfigSubscriber',
'class' => \MauticPlugin\MauticExtendedFieldBundle\EventListener\ConfigSubscriber::class,
],
'mautic.extended_field.report.subscriber' => [
'class' => 'MauticPlugin\MauticExtendedFieldBundle\EventListener\ReportSubscriber',
'class' => \MauticPlugin\MauticExtendedFieldBundle\EventListener\ReportSubscriber::class,
'arguments' => [
'mautic.lead.reportbundle.fields_builder',
'event_dispatcher'
],
],
'mautic.extended_field.lead_subscriber' => [
'class' => 'MauticPlugin\MauticExtendedFieldBundle\EventListener\LeadSubscriber',
'class' => \MauticPlugin\MauticExtendedFieldBundle\EventListener\LeadSubscriber::class,
'arguments' => [
'mautic.lead.model.field',
],
],
'mautic.extended_field.import_subscriber' => [
'class' => 'MauticPlugin\MauticExtendedFieldBundle\EventListener\ImportSubscriber',
'class' => \MauticPlugin\MauticExtendedFieldBundle\EventListener\ImportSubscriber::class,
'arguments' => [
'doctrine.orm.entity_manager',
],
],
],
'forms' => [
'mautic.extended_field.form.config' => [
'class' => 'MauticPlugin\MauticExtendedFieldBundle\Form\ConfigType',
'class' => \MauticPlugin\MauticExtendedFieldBundle\Form\ConfigType::class,
'alias' => 'extendedField_config',
],
],
'other' => [
// Form extensions
'mautic.form.extension.updatelead_action' => [
'class' => 'MauticPlugin\MauticExtendedFieldBundle\Form\UpdateLeadActionExtension',
'class' => \MauticPlugin\MauticExtendedFieldBundle\Form\UpdateLeadActionExtension::class,
'arguments' => ['mautic.factory'],
'tag' => 'form.type_extension',
'tagArguments' => [
'extended_type' => 'Mautic\LeadBundle\Form\Type\UpdateLeadActionType',
],
],
'mautic.form.extension.extended_field' => [
'class' => 'MauticPlugin\MauticExtendedFieldBundle\Form\ExtendedFieldExtension',
'class' => \MauticPlugin\MauticExtendedFieldBundle\Form\ExtendedFieldExtension::class,
'arguments' => ['mautic.factory'],
'tag' => 'form.type_extension',
'tagArguments' => [
'extended_type' => 'Mautic\LeadBundle\Form\Type\FieldType',
],
],
'mautic.form.extension.extended_lead' => [
'class' => 'MauticPlugin\MauticExtendedFieldBundle\Form\LeadTypeExtension',
'class' => \MauticPlugin\MauticExtendedFieldBundle\Form\LeadTypeExtension::class,
'arguments' => ['mautic.factory', 'mautic.lead.model.company'],
'tag' => 'form.type_extension',
'tagArguments' => [
'extended_type' => 'Mautic\LeadBundle\Form\Type\LeadType',
],
],
'mautic.form.extension.extended_list' => [
'class' => 'MauticPlugin\MauticExtendedFieldBundle\Form\ListTypeExtension',
'class' => \MauticPlugin\MauticExtendedFieldBundle\Form\ListTypeExtension::class,
'arguments' => [
'translator',
'mautic.lead.model.list',
Expand All @@ -93,7 +95,7 @@
],
'models' => [
'mautic.lead.model.extended_field' => [
'class' => 'MauticPlugin\MauticExtendedFieldBundle\Model\ExtendedFieldModel',
'class' => \MauticPlugin\MauticExtendedFieldBundle\Model\ExtendedFieldModel::class,
'arguments' => [
'mautic.lead.model.field',
],
Expand Down
6 changes: 4 additions & 2 deletions EventListener/ConfigSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

use Mautic\ConfigBundle\ConfigEvents;
use Mautic\ConfigBundle\Event\ConfigBuilderEvent;
use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use MauticPlugin\MauticExtendedFieldBundle\Form\ConfigType;

/**
* Class ConfigSubscriber.
*/
class ConfigSubscriber extends CommonSubscriber
class ConfigSubscriber implements EventSubscriberInterface
{
/**
* @var
Expand Down Expand Up @@ -52,6 +53,7 @@ public function onConfigGenerate(ConfigBuilderEvent $event)
'bundle' => 'MauticExtendedFieldBundle',
'formAlias' => 'extendedField_config',
'formTheme' => 'MauticExtendedFieldBundle:Config',
'formType' => ConfigType::class,
'parameters' => $params,
]
);
Expand Down
21 changes: 17 additions & 4 deletions EventListener/ImportSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

namespace MauticPlugin\MauticExtendedFieldBundle\EventListener;

use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Mautic\LeadBundle\Event\ImportEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Mautic\LeadBundle\LeadEvents;
use MauticPlugin\MauticExtendedFieldBundle\Entity\ExtendedFieldBoolean;
use MauticPlugin\MauticExtendedFieldBundle\Entity\ExtendedFieldBooleanSecure;
Expand All @@ -25,17 +24,31 @@
use MauticPlugin\MauticExtendedFieldBundle\Entity\ExtendedFieldTextSecure;
use MauticPlugin\MauticExtendedFieldBundle\Entity\ExtendedFieldTime;
use MauticPlugin\MauticExtendedFieldBundle\Entity\ExtendedFieldTimeSecure;
use Doctrine\ORM\EntityManager;

class ImportSubscriber extends CommonSubscriber
class ImportSubscriber implements EventSubscriberInterface
{

/**
* @var \Doctrine\ORM\EntityManager
*/
protected $em;

/**
* @param EntityManager $em
*/
public function __construct(EntityManager $em) {
$this->em = $em;
}

public static function getSubscribedEvents()
{
return [
LeadEvents::IMPORT_BATCH_PROCESSED => 'clearExtendedFieldEntities',
];
}

public function clearExtendedFieldEntities(ImportEvent $event)
public function clearExtendedFieldEntities()
{
$this->em->clear(ExtendedFieldBoolean::class);
$this->em->clear(ExtendedFieldBooleanSecure::class);
Expand Down
4 changes: 2 additions & 2 deletions EventListener/LeadSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace MauticPlugin\MauticExtendedFieldBundle\EventListener;

use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Mautic\LeadBundle\Event\LeadListFilteringEvent;
use Mautic\LeadBundle\Event\LeadListQueryBuilderGeneratedEvent;
use Mautic\LeadBundle\LeadEvents;
Expand All @@ -20,7 +20,7 @@
/**
* Class LeadSubscriber.
*/
class LeadSubscriber extends CommonSubscriber
class LeadSubscriber implements EventSubscriberInterface
{
/** @var ExtendedFieldModel */
protected $leadModel;
Expand Down
25 changes: 12 additions & 13 deletions EventListener/ReportSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

namespace MauticPlugin\MauticExtendedFieldBundle\EventListener;

use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Mautic\LeadBundle\Report\FieldsBuilder;
use Mautic\ReportBundle\Event\ReportBuilderEvent;
use Mautic\ReportBundle\Event\ReportGeneratorEvent;
Expand All @@ -30,7 +31,7 @@
/**
* Class ConfigSubscriber.
*/
class ReportSubscriber extends CommonSubscriber
class ReportSubscriber implements EventSubscriberInterface
{
/**
* @var
Expand Down Expand Up @@ -92,15 +93,22 @@ class ReportSubscriber extends CommonSubscriber
*/
private $fieldsBuilder;

/**
* @var EventDispatcherInterface
*/
private $dispatcher;

const SEGMENT_MEMBERSHIP = 'segment.membership';
const GROUP_CONTACTS = 'contacts';

/**
* @param FieldsBuilder $fieldsBuilder
* @param EventDispatcherInterface $dispatcher
*/
public function __construct(FieldsBuilder $fieldsBuilder)
public function __construct(FieldsBuilder $fieldsBuilder, EventDispatcherInterface $dispatcher)
{
$this->fieldsBuilder = $fieldsBuilder;
$this->fieldsBuilder = $fieldsBuilder;
$this->dispatcher = $dispatcher;
}

/**
Expand Down Expand Up @@ -193,9 +201,6 @@ private function convertToExtendedFieldQuery()
}
}

/**
* @return mixed
*/
private function alterSelect()
{
foreach ($this->selectParts as $key => $selectPart) {
Expand Down Expand Up @@ -259,9 +264,6 @@ private function alterSelect()
}
}

/**
* @return mixed
*/
private function alterOrderBy()
{
foreach ($this->orderByParts as $key => $orderByPart) {
Expand Down Expand Up @@ -305,9 +307,6 @@ private function alterOrderBy()
}
}

/**
* @return mixed
*/
private function alterGroupBy()
{
foreach ($this->groupByParts as $key => $groupByPart) {
Expand Down
3 changes: 2 additions & 1 deletion Form/ConfigType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace MauticPlugin\MauticExtendedFieldBundle\Form;

use Mautic\CoreBundle\Form\Type\YesNoButtonGroupType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

Expand All @@ -27,7 +28,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'disable_lead_table_fields',
'yesno_button_group',
YesNoButtonGroupType::class,
[
'label' => 'mautic.extendedField.disable_lead_table_fields',
'data' => $options['data']['disable_lead_table_fields'],
Expand Down
37 changes: 19 additions & 18 deletions Form/ExtendedFieldExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\LeadBundle\Form\Type\FieldType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;

/**
Expand Down Expand Up @@ -63,7 +64,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)

$builder->add(
'object',
'choice',
ChoiceType::class,
[
'choices' => [
'mautic.lead.contact' => 'lead',
Expand All @@ -72,7 +73,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'mautic.lead.extendedFieldSecure' => 'extendedFieldSecure',
],
'choices_as_values' => true,
'choice_attr' => function ($key, $val, $index) use ($disallowLead) {
'choice_attr' => function ($key) use ($disallowLead) {
// set "Contact" option disabled to true based on key or index of the choice.
if ('lead' === $key && $disallowLead) {
return ['disabled' => 'disabled'];
Expand All @@ -83,7 +84,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'expanded' => false,
'multiple' => false,
'label' => 'mautic.lead.field.object',
'empty_value' => false,
'placeholder' => false,
'attr' => [
'class' => 'form-control',
],
Expand All @@ -96,22 +97,22 @@ public function buildForm(FormBuilderInterface $builder, array $options)
// Add a bunch more 'custom' groups beside the original 4
$builder->add(
'group',
'choice',
ChoiceType::class,
[
'choices' => [
'core' => 'mautic.lead.field.group.core', // Personally Identifiable
'auto' => 'mautic.lead.field.group.auto',
'client' => 'mautic.lead.field.group.client',
'consent' => 'mautic.lead.field.group.consent',
'education' => 'mautic.lead.field.group.education',
'enhancement' => 'mautic.lead.field.group.enhancement',
'finance' => 'mautic.lead.field.group.finance',
'personal' => 'mautic.lead.field.group.personal', // Health
'home' => 'mautic.lead.field.group.home',
'politics' => 'mautic.lead.field.group.politics',
'professional' => 'mautic.lead.field.group.professional',
'social' => 'mautic.lead.field.group.social',
'system' => 'mautic.lead.field.group.system',
'core' => 'core', // Personally Identifiable
'auto' => 'auto',
'client' => 'client',
'consent' => 'consent',
'education' => 'education',
'enhancement' => 'enhancement',
'finance' => 'finance',
'personal' => 'personal', // Health
'home' => 'home',
'politics' => 'politics',
'professional' => 'professional',
'social' => 'social',
'system' => 'system',
],
'attr' => [
'class' => 'form-control',
Expand All @@ -120,7 +121,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'expanded' => false,
'multiple' => false,
'label' => 'mautic.lead.field.group',
'empty_value' => false,
'placeholder' => false,
'required' => false,
'disabled' => $disabled,
]
Expand Down

0 comments on commit 53e9c49

Please sign in to comment.