Skip to content

Commit

Permalink
Merge pull request #1 from TheDMSGroup/ENG-165
Browse files Browse the repository at this point in the history
[Eng-165] US State Normalization
  • Loading branch information
heathdutton committed Apr 26, 2018
2 parents 3591965 + f2a5e99 commit 2a3d041
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 64 deletions.
62 changes: 62 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Standard .travis.yml for a Mautic plugin. Adjust env variables as needed.
env:
global:
# The exact plugin folder/bundle name.
- "MAUTIC_PLUGIN=MauticUSStateNormalizerBundle"

dist: precise

language: php

services:
- mysql

php:
- 5.6.19
- 7.0
- 7.1

before_install:

# Create mautictest database.
- mysql -e 'CREATE DATABASE mautictest;'

# Turn off XDebug.
- phpenv config-rm xdebug.ini || return

# Install dependencies in parallel.
- travis_retry composer global require hirak/prestissimo

# Set to test environment for Symfony's commands in post install commands.
- export SYMFONY_ENV="test"

# Install PHPSTAN for PHP 7+
- if [[ ${TRAVIS_PHP_VERSION:0:3} != "5.6" ]]; then composer global require phpstan/phpstan-shim:0.8.5; fi

# Clone the latest core release.
- git clone -b master --single-branch --depth 1 https://github.com/mautic/mautic.git /tmp/mautic

# Combine core with our plugin.
- mkdir -p /tmp/mautic/plugins/$MAUTIC_PLUGIN
- rsync -r --delete-after --quiet $TRAVIS_BUILD_DIR/ /tmp/mautic/plugins/$MAUTIC_PLUGIN
- rsync -r --delete-after --quiet /tmp/mautic/ $TRAVIS_BUILD_DIR/

install:

# Install core dependencies.
- composer install

# Install plugin dependencies (if any).
- composer require wikimedia/composer-merge-plugin
- composer config extra.merge-plugin.include plugins/$MAUTIC_PLUGIN/composer.json

script:

# Run PHPUnit including core tests to find potential BC breaks.
- bin/phpunit -d memory_limit=2048M --bootstrap vendor/autoload.php --configuration app/phpunit.xml.dist --fail-on-warning

# Run PHPSTAN analysis for PHP 7+ only in the scope of this plugin.
- if [[ ${TRAVIS_PHP_VERSION:0:3} != "5.6" ]]; then ~/.composer/vendor/phpstan/phpstan-shim/phpstan.phar analyse plugins/$MAUTIC_PLUGIN; fi

# Check code standards for PHP 7.1 only in the scope of this plugin.
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.1" ]]; then bin/php-cs-fixer fix -v --dry-run --diff plugins/$MAUTIC_PLUGIN; fi
11 changes: 7 additions & 4 deletions Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
'forms' => [
'plugin.mautic.usstatenormalizer.config_form' => [
'class' => 'MauticPlugin\MauticUSStateNormalizerBundle\Form\Type\ConfigType',
'alias' => 'usstatenormalizer_config'
'alias' => 'usstatenormalizer_config',
],
],
'events' => [
'plugin.mautic.ussstatenormalizer.config_event' => [
'plugin.mautic.ussstatenormalizer.config_subscriber' => [
'class' => 'MauticPlugin\MauticUSStateNormalizerBundle\EventListener\ConfigSubscriber',
],
'plugin.mautic.ussstatenormalizer.lead_subscriber' => [
'class' => 'MauticPlugin\MauticUSStateNormalizerBundle\EventListener\LeadSubscriber',
],
],
'integrations' => [
'plugin.mautic' => [
Expand All @@ -34,7 +37,7 @@
],
],
'parameters' => [
'store_as' => 'abbreviation',
'store_as' => 'abbreviation',
'display_as' => 'properName',
],
];
];
21 changes: 21 additions & 0 deletions DependencyInjection/Compiler/USStateNormalizerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Created by PhpStorm.
* User: nbush
* Date: 4/25/18
* Time: 9:00 AM.
*/

namespace MauticPlugin\MauticUSStateNormalizerBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class USStateNormalizerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$formFieldHelper = $container->getDefinition('mautic.helper.form.field_helper');
$formFieldHelper->setClass('\MauticPlugin\MauticUSStateNormalizerBundle\Helper\USStateFormFieldHelper');
}
}
16 changes: 8 additions & 8 deletions EventListener/ConfigSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
* Created by PhpStorm.
* User: nbush
* Date: 4/19/18
* Time: 12:41 PM
* Time: 12:41 PM.
*/

namespace MauticPlugin\MauticUSStateNormalizerBundle\EventListener;

use Mautic\ConfigBundle\Event\ConfigEvent;
use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Mautic\ConfigBundle\ConfigEvents;
use Mautic\ConfigBundle\Event\ConfigBuilderEvent;
use Mautic\ConfigBundle\Event\ConfigEvent;
use Mautic\CoreBundle\EventListener\CommonSubscriber;

/**
* Class ConfigSubscriber
* Class ConfigSubscriber.
*/
class ConfigSubscriber extends CommonSubscriber
{
public static function getSubscribedEvents()
{
return [
ConfigEvents::CONFIG_ON_GENERATE => ['onConfigGenerate', 0],
ConfigEvents::CONFIG_PRE_SAVE => ['onConfigSave', 0]
ConfigEvents::CONFIG_PRE_SAVE => ['onConfigSave', 0],
];
}

Expand All @@ -31,9 +31,9 @@ public function onConfigGenerate(ConfigBuilderEvent $event)
$config = $event->getParametersFromConfig('MauticUSStateNormalizerBundle');

$event->addForm([
'formAlias' => 'usstatenormalizer_config',
'formTheme' => 'MauticUSStateNormalizerBundle:FormTheme\Config',
'parameters' => $config
'formAlias' => 'usstatenormalizer_config',
'formTheme' => 'MauticUSStateNormalizerBundle:FormTheme\Config',
'parameters' => $config,
]);
}

Expand Down
71 changes: 57 additions & 14 deletions EventListener/LeadSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
* Created by PhpStorm.
* User: nbush
* Date: 4/20/18
* Time: 8:24 AM
* Time: 8:24 AM.
*/

namespace MauticPlugin\MauticUSStateNormalizerBundle\EventListener;


use Mautic\CoreBundle\EventListener\CommonSubscriber;
use Mautic\LeadBundle\Entity\ListLead;
use Mautic\LeadBundle\Event\LeadEvent;
use Mautic\LeadBundle\Event\LeadListFilteringEvent;
use Mautic\LeadBundle\Event\LeadListEvent;
use Mautic\LeadBundle\LeadEvents;
use MauticPlugin\MauticUSStateNormalizerBundle\Helper\USStateMapHelper;

class LeadSubscriber extends CommonSubscriber
{
Expand All @@ -22,23 +21,67 @@ public static function getSubscribedEvents()
return [
LeadEvents::LEAD_PRE_SAVE => ['doContactNormalizedSave', -1],
LeadEvents::LIST_PRE_SAVE => ['doSegmentNormalizedSave', 0],
LeadEvents::LIST_PRE_PROCESS_LIST => ['doSegmentPreProcess', 0]

];
}

public function doContactNormalizedSave(LeadEvent $event)
{
$stop = 'here';
try {
$helper = new USStateMapHelper();
$lead = $event->getLead();
if (($this->params['store_as'] == 'properName' &&
!in_array($lead->getState(), $helper->getProperNames()))) {
$lead->addUpdatedField('state', $helper->getStateForAbbreviation($lead->getState()));
} elseif (!in_array($lead->getState(), $helper->getAbbreviations())) {
$lead->addUpdatedField('state', $helper->getAbbreviationForState($lead->getState()));
}
} catch (\Exception $e) {
}
}

public function doSegmentNormalizedSave(ListLead $event)
public function doSegmentNormalizedSave(LeadListEvent $event)
{
$stop = 'here';
}
$filters = $event->getList()->getFilters();
$normalized = [];
foreach ($filters as $filter) {
if ($filter['field'] === 'state') {
$replacement = [];
$helper = new USStateMapHelper();
if (is_array($filter['filter'])) {
foreach ($filter['filter'] as $state) {
$result = false;
try {
if ($this->params['store_as'] == 'properName' &&
!in_array($state, $helper->getProperNames())) {
$result = $helper->getStateForAbbreviation($state);
} elseif (!in_array($state, $helper->getAbbreviations())) {
$result = $helper->getAbbreviationForState($state);
}
} catch (\Exception $e) {
}
$replacement[] = $result ? $result : $state;
}
} else {
try {
$state = $filter['filter'];
if ($this->params['store_as'] == 'properName' &&
!in_array($state, $helper->getProperNames())) {
$result = $helper->getStateForAbbreviation($state);
} elseif (!in_array($filter['filter'], $helper->getAbbreviations())) {
$result = $helper->getAbbreviationForState($state);
}
} catch (\Exception $e) {
}
$replacement[] = $result ? $result : $state;
}
$filter['filter'] = $replacement;
}
$normalized[] = $filter;
}
$changes = $event->getList()->getChanges(true);
$changes['filters'][1] = $normalized;
$event->getList()->setChanges($changes);

public function doSegmentPreProcess(LeadListFilteringEvent $event)
{
$stop = 'here';
return true;
}
}
}
23 changes: 11 additions & 12 deletions Form/Type/ConfigType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Created by PhpStorm.
* User: nbush
* Date: 4/19/18
* Time: 1:25 PM
* Time: 1:25 PM.
*/

namespace MauticPlugin\MauticUSStateNormalizerBundle\Form\Type;
Expand All @@ -12,13 +12,13 @@
use Symfony\Component\Form\FormBuilderInterface;

/**
* Class ConfigType
* Class ConfigType.
*/
class ConfigType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
Expand All @@ -33,12 +33,12 @@ public function buildForm(FormBuilderInterface $builder, array $options)
],
'choices_as_values' => true,
//'choice_attr' => ['class' => 'radio'],
'data' => $options['data']['store_as'],
'label' => 'mautic.plugin.usstatenormalizer.store_as.label',
'data' => $options['data']['store_as'],
'label' => 'mautic.plugin.usstatenormalizer.store_as.label',
'label_attr' => ['class' => 'control-label'],
'attr' => [
'attr' => [
'tooltip' => 'mautic.plugin.usstatenormalizer.store_as.tooltip',
'class' => 'form-control'
'class' => 'form-control',
],
'expanded' => false,
'multiple' => false,
Expand All @@ -54,12 +54,12 @@ public function buildForm(FormBuilderInterface $builder, array $options)
],
'choices_as_values' => true,
//'choice_attr' => ['class' => 'radio'],
'data' => $options['data']['display_as'],
'label' => 'mautic.plugin.usstatenormalizer.display_as.label',
'data' => $options['data']['display_as'],
'label' => 'mautic.plugin.usstatenormalizer.display_as.label',
'label_attr' => ['class' => 'control-label'],
'attr' => [
'attr' => [
'tooltip' => 'mautic.plugin.usstatenormalizer.display_as.tooltip',
'class' => 'form-control'
'class' => 'form-control',
],
'expanded' => false,
'multiple' => false,
Expand All @@ -74,5 +74,4 @@ public function getName()
{
return 'usstatenormalizer_config';
}

}
23 changes: 23 additions & 0 deletions Helper/USStateFormFieldHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Created by PhpStorm.
* User: nbush
* Date: 4/25/18
* Time: 9:06 AM.
*/

namespace MauticPlugin\MauticUSStateNormalizerBundle\Helper;

use Mautic\LeadBundle\Helper\FormFieldHelper as OriginalHelper;

class USStateFormFieldHelper extends OriginalHelper
{
public static function getRegionChoices()
{
$helper = new USStateMapHelper();
$choices = parent::getRegionChoices();
$choices['United States'] = $helper::getStateOptions();

return $choices;
}
}
Loading

0 comments on commit 2a3d041

Please sign in to comment.