Skip to content

Commit

Permalink
Save internation phone number to contact profile after submit
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzmany committed Apr 25, 2021
1 parent 50f00ca commit f0bc991
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'arguments' => [
'translator',
'request_stack',
'mautic.lead.model.lead'
],
],
],
Expand Down
2 changes: 1 addition & 1 deletion Controller/JsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function generateCountryCodeAction($formName)
var elem = elems[i];
window.intlTelInput(elem , {
hiddenInput: elem.getAttribute('data-field-alias'),
hiddenInput: elem.getAttribute('data-field-alias')+'_full',
separateDialCode: true,
initialCountry: "{$countryCode}",
utilsScript: "{$utilsUrl}"
Expand Down
37 changes: 33 additions & 4 deletions EventListener/FormValidationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Mautic\CoreBundle\Helper\ArrayHelper;
use Mautic\FormBundle\Event as Events;
use Mautic\FormBundle\FormEvents;
use Mautic\LeadBundle\Model\LeadModel;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Translation\TranslatorInterface;
Expand All @@ -33,10 +34,16 @@ class FormValidationSubscriber implements EventSubscriberInterface
*/
private $request;

public function __construct(TranslatorInterface $translator, RequestStack $requestStack)
/**
* @var LeadModel
*/
private $leadModel;

public function __construct(TranslatorInterface $translator, RequestStack $requestStack, LeadModel $leadModel)
{
$this->translator = $translator;
$this->request = $requestStack->getCurrentRequest();
$this->leadModel = $leadModel;
}

/**
Expand All @@ -45,8 +52,9 @@ public function __construct(TranslatorInterface $translator, RequestStack $reque
public static function getSubscribedEvents()
{
return [
FormEvents::FORM_ON_BUILD => ['onFormBuilder', 0],
FormEvents::ON_FORM_VALIDATE => ['onFormValidate', 0],
FormEvents::FORM_ON_BUILD => ['onFormBuilder', 0],
FormEvents::ON_FORM_VALIDATE => ['onFormValidate', 0],
FormEvents::FORM_ON_SUBMIT => ['onFormSubmit', 0],
];
}

Expand All @@ -65,14 +73,35 @@ public function onFormBuilder(Events\FormBuilderEvent $event)
);
}

public function onFormSubmit(Events\SubmissionEvent $submissionEvent)
{
if (!$contact = $submissionEvent->getLead()) {
return;
}

$fields = $submissionEvent->getForm()->getFields();

foreach ($fields as $field) {
if (FormSubscriber::FIELD_NAME === $field->getType() && $field->getLeadField()) {
if($fullPhoneNumber = ArrayHelper::getValue($field->getAlias().'_full', $this->request ? $this->request->request->get('mauticform') : [])) {
$this->leadModel->setFieldValues($contact, [$field->getLeadField() => $fullPhoneNumber]);
}
}
}

if (!empty($contact->getChanges())) {
$this->leadModel->saveEntity($contact);
}
}

/**
* Custom validation *.
*/
public function onFormValidate(Events\ValidationEvent $event)
{
$field = $event->getField();
$phoneNumber = $event->getValue();
$fullPhoneNumber = ArrayHelper::getValue($field->getAlias(), $this->request ? $this->request->request->get('mauticform') : []);
$fullPhoneNumber = ArrayHelper::getValue($field->getAlias().'_full', $this->request ? $this->request->request->get('mauticform') : []);
if (!empty($phoneNumber) && FormSubscriber::FIELD_NAME === $field->getType() && !empty($field->getValidation()['international'])) {
$phoneUtil = PhoneNumberUtil::getInstance();
try {
Expand Down

0 comments on commit f0bc991

Please sign in to comment.