diff --git a/blueprints.yaml b/blueprints.yaml index 358387a..2d4d786 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -93,3 +93,16 @@ form: placeholder: '{"attribute": "value"}' validate: type: string + + tx: + type: tab + title: PLUGIN_LISTMONK.TX + fields: + tx.template: + type: integer + label: PLUGIN_LISTMONK.TX_TEMPLATE_ID + help: PLUGIN_LISTMONK.TX_TEMPLATE_ID_HELP + size: small + validate: + type: number + min: 1 diff --git a/composer.lock b/composer.lock index 7004a80..e80206c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "096d3c9059b402b96784b2897ab5dba7", + "content-hash": "4c5328ecf25c0457de8450b214fe18de", "packages": [ { "name": "adnanhussainturki/listmonk-php", @@ -34,7 +34,7 @@ ], "description": "Package aims to ease up the API calls to a listmonk API from PHP based application", "transport-options": { - "symlink": false, + "symlink": true, "relative": false } }, diff --git a/languages.yaml b/languages.yaml index 55e10da..844052f 100644 --- a/languages.yaml +++ b/languages.yaml @@ -20,3 +20,6 @@ de: NEWSLETTER_STATUS_HELP: Standard-Status für neue Newsletter-Empfänger NEWSLETTER_ATTRIBUTES: Globale Attribute (JSON) NEWSLETTER_ATTRIBUTES_HELP: JSON-Objekt mit globalen Attributen für Newsletter-Empfänger + TX: Transaktionen + TX_TEMPLATE_ID: ID der Vorlage + TX_TEMPLATE_ID_HELP: Listmonk-ID der Standard-Vorlage diff --git a/listmonk.php b/listmonk.php index 0ea1107..aa03dbe 100644 --- a/listmonk.php +++ b/listmonk.php @@ -3,6 +3,7 @@ namespace Grav\Plugin; use AdnanHussainTurki\ListMonk\ListMonk; use AdnanHussainTurki\ListMonk\Models\MonkSubscriber; +use AdnanHussainTurki\ListMonk\Models\TransactionalEmail; use Composer\Autoload\ClassLoader; use Grav\Common\Data\Data; use Grav\Common\Plugin; @@ -109,7 +110,7 @@ class ListmonkPlugin extends Plugin 'source' => 'gravcms', ])); - $this->grav->fireEvent('onListmonkSubscriberObjectCreated', new Event([ + $this->grav->fireEvent('onListmonkSubscriberInitialized', new Event([ 'subscriber' => $subscriber, ])); @@ -131,28 +132,42 @@ class ListmonkPlugin extends Plugin /** * Send a transactional mail using listmonk * - * (Not implemented yet!) - * * @param Event $event * @return void */ private function processTransactionalForm(Event $event, Form $form, array $params): void { - $this->grav->fireEvent('onFormValidationError', new Event([ - 'form' => $form, - 'message' => 'Sending transactional emails is not implemented yet', - ])); + try { + $txEmail = new TransactionalEmail(); + $txEmail->setSubscriberEmail($params['email']); + $txEmail->setTemplateId($this->config->get('config.plugins.listmonk.tx.template')); + $txEmail->setFromEmail($this->config->get('config.plugins.email.from')); - #try { - # $client = $this->getListmonkClient(); - #} catch (ListmonkClientException $e) { - # $this->grav->fireEvent('onFormValidationError', new Event([ - # 'form' => $form, - # 'message' => $e->getMessage(), - # ])); - #} catch (ListmonkException $e) { - # throw new \RuntimeException('Error while processing form', -1, $e); - #} + if (isset($params['data']) && is_array($params['data'])) { + $txEmail->setData($params['data']); + } + + $this->grav->fireEvent('onTransactionalEmailInitialized', new Event([ + 'email' => $txEmail, + 'params' => $params, + 'form' => $form, + ])); + + $result = $this->getListmonkClient() + ->transactional() + ->send($txEmail); + + $this->grav->fireEvent('onTransactionalEmailSent', new Event([ + 'email' => $txEmail, + 'result' => $result, + ])); + + } catch (\Exception $e) { + $this->grav->fireEvent('onFormValidationError', new Event([ + 'form' => $form, + 'message' => $e->getMessage(), + ])); + } } private function processAttributes(array $attributes): array diff --git a/listmonk.yaml b/listmonk.yaml index 4fee601..2701928 100644 --- a/listmonk.yaml +++ b/listmonk.yaml @@ -7,3 +7,5 @@ newsletter: list: 1 subscriber: status: enabled +tx: + template: 1