diff --git a/Config/config.php b/Config/config.php index 59482d3..a5ab561 100644 --- a/Config/config.php +++ b/Config/config.php @@ -28,6 +28,7 @@ 'arguments' => [ 'translator', 'request_stack', + 'mautic.lead.model.lead' ], ], ], diff --git a/Controller/JsController.php b/Controller/JsController.php index 881561e..7c3a99c 100644 --- a/Controller/JsController.php +++ b/Controller/JsController.php @@ -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}" diff --git a/EventListener/FormValidationSubscriber.php b/EventListener/FormValidationSubscriber.php index 2f04abf..f10fd0e 100644 --- a/EventListener/FormValidationSubscriber.php +++ b/EventListener/FormValidationSubscriber.php @@ -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; @@ -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; } /** @@ -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], ]; } @@ -65,6 +73,27 @@ 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 *. */ @@ -72,7 +101,7 @@ 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 {