forked from sulu/SuluFormBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sendinblue integration (sulu#310)
- Loading branch information
Showing
15 changed files
with
409 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\FormBundle\Dynamic\Helper; | ||
|
||
use SendinBlue\Client\Api\ContactsApi; | ||
use SendinBlue\Client\Configuration; | ||
|
||
/** | ||
* @final | ||
* @internal | ||
*/ | ||
class SendinblueListSelect | ||
{ | ||
/** | ||
* @var ContactsApi | ||
*/ | ||
private $contactsApi; | ||
|
||
public function __construct(?string $apiKey) | ||
{ | ||
$config = new Configuration(); | ||
$config->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\FormBundle\Dynamic\Helper; | ||
|
||
use SendinBlue\Client\Api\TransactionalEmailsApi; | ||
use SendinBlue\Client\Configuration; | ||
|
||
/** | ||
* @final | ||
* @internal | ||
*/ | ||
class SendinblueMailTemplateSelect | ||
{ | ||
/** | ||
* @var TransactionalEmailsApi | ||
*/ | ||
private $transactionalEmailsApi; | ||
|
||
public function __construct(?string $apiKey) | ||
{ | ||
$config = new Configuration(); | ||
$config->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\FormBundle\Dynamic\Types; | ||
|
||
use Sulu\Bundle\FormBundle\Dynamic\FormFieldTypeConfiguration; | ||
use Sulu\Bundle\FormBundle\Dynamic\FormFieldTypeInterface; | ||
use Sulu\Bundle\FormBundle\Entity\FormField; | ||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType as TypeCheckboxType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
/** | ||
* The Sendinblue form field type. | ||
*/ | ||
class SendinblueType implements FormFieldTypeInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getConfiguration(): FormFieldTypeConfiguration | ||
{ | ||
return new FormFieldTypeConfiguration( | ||
'sulu_form.type.sendinblue', | ||
__DIR__ . '/../../Resources/config/form-fields/field_sendinblue.xml', | ||
'special' | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function build(FormBuilderInterface $builder, FormField $field, string $locale, array $options): void | ||
{ | ||
$type = TypeCheckboxType::class; | ||
$builder->add($field->getKey(), $type, $options); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefaultValue(FormField $field, string $locale) | ||
{ | ||
return $field->getTranslation($locale)->getDefaultValue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\FormBundle\Event; | ||
|
||
use SendinBlue\Client\Api\ContactsApi; | ||
use SendinBlue\Client\Configuration; | ||
use SendinBlue\Client\Model\CreateDoiContact; | ||
use Sulu\Bundle\FormBundle\Entity\Dynamic; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
/** | ||
* @final | ||
* @internal | ||
*/ | ||
class SendinblueListSubscriber implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @var RequestStack | ||
*/ | ||
private $requestStack; | ||
|
||
/** | ||
* @var ContactsApi | ||
*/ | ||
private $contactsApi; | ||
|
||
public function __construct(RequestStack $requestStack, ?string $apiKey) | ||
{ | ||
$this->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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<properties xmlns="http://schemas.sulu.io/template/template" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:xi="http://www.w3.org/2001/XInclude" | ||
xsi:schemaLocation="http://schemas.sulu.io/template/template http://schemas.sulu.io/template/properties-1.0.xsd"> | ||
<xi:include href="default_field.xml" xpointer="xmlns(sulu=http://schemas.sulu.io/template/template) | ||
xpointer(//sulu:property)"/> | ||
<property name="options/listId" type="single_select" mandatory="true"> | ||
<meta> | ||
<title>sulu_form.sendinblue_list</title> | ||
</meta> | ||
<params> | ||
<param | ||
name="values" | ||
type="expression" | ||
value="service('sulu_form.dynamic.sendinblue_list_select').getValues()" | ||
/> | ||
</params> | ||
</property> | ||
<property name="options/mailTemplateId" type="single_select" mandatory="true"> | ||
<meta> | ||
<title>sulu_form.sendinblue_mail_template</title> | ||
</meta> | ||
<params> | ||
<param | ||
name="values" | ||
type="expression" | ||
value="service('sulu_form.dynamic.sendinblue_mail_template_select').getValues()" | ||
/> | ||
</params> | ||
</property> | ||
</properties> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="sulu_form.subscriber.sendinblue_list_subscriber" class="Sulu\Bundle\FormBundle\Event\SendinblueListSubscriber"> | ||
<argument type="service" id="request_stack"/> | ||
<argument>%sulu_form.sendinblue_api_key%</argument> | ||
<tag name="kernel.event_subscriber" /> | ||
</service> | ||
|
||
<service id="sulu_form.dynamic.type_sendinblue" class="Sulu\Bundle\FormBundle\Dynamic\Types\SendinblueType"> | ||
<tag name="sulu_form.dynamic.type" alias="sendinblue"/> | ||
</service> | ||
|
||
<service id="sulu_form.dynamic.sendinblue_list_select" class="Sulu\Bundle\FormBundle\Dynamic\Helper\SendinblueListSelect" public="true"> | ||
<argument>%sulu_form.sendinblue_api_key%</argument> | ||
</service> | ||
|
||
<service id="sulu_form.dynamic.sendinblue_mail_template_select" class="Sulu\Bundle\FormBundle\Dynamic\Helper\SendinblueMailTemplateSelect" public="true"> | ||
<argument>%sulu_form.sendinblue_api_key%</argument> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.