diff --git a/Command/FormGeneratorCommand.php b/Command/FormGeneratorCommand.php index ebda48fe..02ec992c 100644 --- a/Command/FormGeneratorCommand.php +++ b/Command/FormGeneratorCommand.php @@ -267,9 +267,6 @@ private function loadTestForm(): ?Form } } - /** - * @return string - */ private function getChoices(): string { return diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index dcbb82e6..c03f8573 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -36,6 +36,7 @@ public function getConfigTreeBuilder() $rootNode = $treeBuilder->getRootNode(); $rootNode->children() + ->scalarNode('sendinblue_api_key')->defaultValue(null)->end() ->scalarNode('mailchimp_api_key')->defaultValue(null)->end() ->scalarNode('mailchimp_subscribe_status')->defaultValue('subscribed')->end() ->enumNode('media_collection_strategy') diff --git a/DependencyInjection/SuluFormExtension.php b/DependencyInjection/SuluFormExtension.php index 04567a00..0f5f382e 100644 --- a/DependencyInjection/SuluFormExtension.php +++ b/DependencyInjection/SuluFormExtension.php @@ -148,6 +148,7 @@ public function load(array $configs, ContainerBuilder $container): void $container->setParameter('sulu_form.ajax_templates', $config['ajax_templates']); $container->setParameter('sulu_form.dynamic_widths', $config['dynamic_widths']); $container->setParameter('sulu_form.dynamic_auto_title', $config['dynamic_auto_title']); + $container->setParameter('sulu_form.sendinblue_api_key', $config['sendinblue_api_key']); $container->setParameter('sulu_form.mailchimp_api_key', $config['mailchimp_api_key']); $container->setParameter('sulu_form.mailchimp_subscribe_status', $config['mailchimp_subscribe_status']); $container->setParameter('sulu_form.dynamic_lists.config', $config['dynamic_lists']); @@ -188,6 +189,14 @@ public function load(array $configs, ContainerBuilder $container): void $loader->load('types.xml'); $loader->load('title-providers.xml'); + if ($config['sendinblue_api_key']) { + if (!class_exists(\SendinBlue\Client\Configuration::class)) { + throw new \LogicException('You need to install the "sendinblue/api-v3-sdk" package to use the sendinblue type.'); + } + + $loader->load('type_sendinblue.xml'); + } + if ($config['mailchimp_api_key']) { if (!class_exists(\DrewM\MailChimp\MailChimp::class)) { throw new \LogicException('You need to install the "drewm/mailchimp-api" package to use the mailchimp type.'); diff --git a/Dynamic/Helper/SendinblueListSelect.php b/Dynamic/Helper/SendinblueListSelect.php new file mode 100644 index 00000000..cff060b3 --- /dev/null +++ b/Dynamic/Helper/SendinblueListSelect.php @@ -0,0 +1,55 @@ +setApiKey('api-key', $apiKey); + + $this->contactsApi = new ContactsApi(null, $config); + } + + /** + * Returns array of Sendinblue lists of given account defined by the API key. + * + * @return mixed[] + */ + public function getValues(): array + { + $lists = []; + + $listObjects = $this->contactsApi->getLists()->getLists(); + foreach ($listObjects as $list) { + $lists[] = [ + 'name' => $list['id'], + 'title' => $list['name'], + ]; + } + + return $lists; + } +} diff --git a/Dynamic/Helper/SendinblueMailTemplateSelect.php b/Dynamic/Helper/SendinblueMailTemplateSelect.php new file mode 100644 index 00000000..c4a4f950 --- /dev/null +++ b/Dynamic/Helper/SendinblueMailTemplateSelect.php @@ -0,0 +1,59 @@ +setApiKey('api-key', $apiKey); + + $this->transactionalEmailsApi = new TransactionalEmailsApi(null, $config); + } + + /** + * Returns array of Sendinblue mail templates of given account defined by the API key. + * + * @return mixed[] + */ + public function getValues(): array + { + $mailTemplates = []; + + $mailTemplateObjects = $this->transactionalEmailsApi->getSmtpTemplates('true')->getTemplates(); + foreach ($mailTemplateObjects as $template) { + if (($template['tag'] ?? null) !== 'optin') { + continue; + } + + $mailTemplates[] = [ + 'name' => $template['id'], + 'title' => $template['name'], + ]; + } + + return $mailTemplates; + } +} diff --git a/Dynamic/Types/SendinblueType.php b/Dynamic/Types/SendinblueType.php new file mode 100644 index 00000000..ad9a3a44 --- /dev/null +++ b/Dynamic/Types/SendinblueType.php @@ -0,0 +1,53 @@ +add($field->getKey(), $type, $options); + } + + /** + * {@inheritdoc} + */ + public function getDefaultValue(FormField $field, string $locale) + { + return $field->getTranslation($locale)->getDefaultValue(); + } +} diff --git a/Event/SendinblueListSubscriber.php b/Event/SendinblueListSubscriber.php new file mode 100644 index 00000000..a0d9e2c8 --- /dev/null +++ b/Event/SendinblueListSubscriber.php @@ -0,0 +1,114 @@ +requestStack = $requestStack; + + $config = new Configuration(); + $config->setApiKey('api-key', $apiKey); + + $this->contactsApi = new ContactsApi(null, $config); + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() + { + return [ + FormSavePostEvent::NAME => 'listSubscribe', + ]; + } + + public function listSubscribe(FormSavePostEvent $event): void + { + $dynamic = $event->getData(); + $request = $this->requestStack->getCurrentRequest(); + + if (!$dynamic instanceof Dynamic) { + return; + } + + if (!$request) { + return; + } + + $form = $dynamic->getForm()->serializeForLocale($dynamic->getLocale(), $dynamic); + + $email = ''; + $firstName = ''; + $lastName = ''; + $redirectionUrl = $request->getUriForPath('') . '?subscribe=true'; + $listIdsByMailTemplate = []; + + foreach ($form['fields'] as $field) { + if ('firstName' === $field['type'] && !$firstName) { + $firstName = $field['value']; + } elseif ('lastName' === $field['type'] && !$lastName) { + $lastName = $field['value']; + } elseif ('email' === $field['type'] && !$email) { + $email = $field['value']; + } elseif ('sendinblue' == $field['type'] && $field['value']) { + $mailTemplateId = $field['options']['mailTemplateId'] ?? null; + $listId = $field['options']['listId'] ?? null; + + if (!$mailTemplateId || !$listId) { + continue; + } + + $listIdsByMailTemplate[$mailTemplateId][] = $listId; + } + } + + if ($email && count($listIdsByMailTemplate) > 0) { + foreach ($listIdsByMailTemplate as $mailTemplateId => $listIds) { + $createDoiContact = new CreateDoiContact([ + 'email' => $email, + 'templateId' => $mailTemplateId, + 'includeListIds' => $listIds, + 'redirectionUrl' => $redirectionUrl, + 'attributes' => [ + 'FIRST_NAME' => $firstName, + 'LAST_NAME' => $firstName, + ], + ]); + + $this->contactsApi->createDoiContact($createDoiContact); + } + } + } +} diff --git a/Repository/FormRepository.php b/Repository/FormRepository.php index 8215e0b1..257b813d 100644 --- a/Repository/FormRepository.php +++ b/Repository/FormRepository.php @@ -16,7 +16,6 @@ use Sulu\Bundle\FormBundle\Entity\Form; /** - * * @template-extends EntityRepository