diff --git a/backend-donations/web/modules/falcon/falcon_mail/config/schema/falcon_mail.schema.yml b/backend-donations/web/modules/falcon/falcon_mail/config/schema/falcon_mail.schema.yml new file mode 100644 index 0000000..cb38585 --- /dev/null +++ b/backend-donations/web/modules/falcon/falcon_mail/config/schema/falcon_mail.schema.yml @@ -0,0 +1,13 @@ +# Schema for the configuration files of the Falcon Mail module. + +falcon_mail.settings: + type: config_object + label: 'Falcon Mail settings' + mapping: + notifications: + type: mapping + label: 'Notifications' + mapping: + list_gift_corporate: + type: sting + label: 'Email addresses to notify about corporate gift order' diff --git a/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.info.yml b/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.info.yml index 8af4f4b..604eee3 100644 --- a/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.info.yml +++ b/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.info.yml @@ -3,3 +3,5 @@ type: module description: E-mail customisations for Falcon. core: 8.x package: Falcon +dependencies: + - commerce:commerce_order diff --git a/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.links.menu.yml b/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.links.menu.yml new file mode 100644 index 0000000..67e4f30 --- /dev/null +++ b/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.links.menu.yml @@ -0,0 +1,10 @@ +falcon_mail.admin_index: + title: Falcon + route_name: falcon_mail.admin_index + parent: system.admin_config + description: 'Configure Falcon settings.' +falcon_mail.settings: + title: 'Falcon Mail' + route_name: falcon_mail.settings + parent: falcon_mail.admin_index + description: 'Falcon Mail settings.' diff --git a/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.module b/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.module index c529b4b..fadb494 100644 --- a/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.module +++ b/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.module @@ -16,7 +16,7 @@ function falcon_mail_help($route_name, RouteMatchInterface $route_match) { case 'help.page.falcon_mail': $output = ''; $output .= '
' . t('E-mail customisations for CW') . '
'; + $output .= '' . t('E-mail customisations for Falcon') . '
'; return $output; default: @@ -40,3 +40,36 @@ function falcon_mail_mandrill_mail_alter(&$mandrill_params, $message) { $mandrill_params['message']['html'] = nl2br($mandrill_params['message']['html']); } } + +/** + * Implements hook_theme(). + */ +function falcon_mail_theme($existing, $type, $theme, $path) { + return [ + 'falcon_mail_commerce_order_receipt_gift_corporate' => [ + 'variables' => [ + 'order_entity' => NULL, + 'order_url' => NULL, + 'billing_information' => NULL, + 'shipping_information' => NULL, + 'payment_method' => NULL, + 'totals' => NULL, + ], + ], + ]; +} + +/** + * Implements hook_mail(). + * + * Captures the outgoing mail and sets appropriate message body and headers. + */ +function falcon_mail_mail($key, &$message, $params) { + if (isset($params['headers'])) { + $message['headers'] = array_merge($message['headers'], $params['headers']); + } + + $message['from'] = $params['from']; + $message['subject'] = $params['subject']; + $message['body'][] = $params['body']; +} diff --git a/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.routing.yml b/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.routing.yml new file mode 100644 index 0000000..37e2fe5 --- /dev/null +++ b/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.routing.yml @@ -0,0 +1,15 @@ +falcon_mail.admin_index: + path: '/admin/config/falcon' + defaults: + _controller: '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage' + _title: 'Falcon' + requirements: + _permission: 'access administration pages' + +falcon_mail.settings: + path: 'admin/config/falcon/falcon_mail' + defaults: + _form: '\Drupal\falcon_mail\Form\SettingsForm' + _title: 'Falcon Mail settings' + requirements: + _permission: 'administer site configuration' diff --git a/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.services.yml b/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.services.yml new file mode 100644 index 0000000..2a4b3ff --- /dev/null +++ b/backend-donations/web/modules/falcon/falcon_mail/falcon_mail.services.yml @@ -0,0 +1,6 @@ +services: + falcon_mail.corporate_gift_notification_subscriber: + class: Drupal\falcon_mail\EventSubscriber\OrderReceiptGiftCorporateSubscriber + arguments: ['@entity_type.manager', '@language_manager', '@plugin.manager.mail', '@commerce_order.order_total_summary', '@renderer', '@config.factory'] + tags: + - { name: 'event_subscriber' } diff --git a/backend-donations/web/modules/falcon/falcon_mail/src/EventSubscriber/OrderReceiptGiftCorporateSubscriber.php b/backend-donations/web/modules/falcon/falcon_mail/src/EventSubscriber/OrderReceiptGiftCorporateSubscriber.php new file mode 100644 index 0000000..3097a33 --- /dev/null +++ b/backend-donations/web/modules/falcon/falcon_mail/src/EventSubscriber/OrderReceiptGiftCorporateSubscriber.php @@ -0,0 +1,173 @@ +orderTypeStorage = $entity_type_manager->getStorage('commerce_order_type'); + $this->orderTotalSummary = $order_total_summary; + $this->profileViewBuilder = $entity_type_manager->getViewBuilder('profile'); + $this->languageManager = $language_manager; + $this->mailManager = $mail_manager; + $this->renderer = $renderer; + $this->configFactory = $config_factory; + } + + /** + * Sends corporate gift notification email. + * + * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event + * The event we subscribed to. + */ + public function sendOrderReceiptGiftCorporate(WorkflowTransitionEvent $event) { + // Send notification only if there are configured email addresses. + $to = $this->configFactory->get('falcon_mail.settings')->get('notifications.list_gift_corporate'); + if (!$to) { + $this->getLogger('falcon_mail')->notice('There are no email addresses defined for corporate gift notifications.'); + return; + } + + /** @var \Drupal\commerce_order\Entity\OrderInterface $order */ + $order = $event->getEntity(); + + // Send notification only for corporate gift orders. + foreach ($order->getItems() as $order_item) { + $purchased_entity = $order_item->getPurchasedEntity(); + if (!$purchased_entity instanceof ProductVariationInterface) { + return; + } + $product = $purchased_entity->getProduct(); + if ($product->bundle() != 'gift_corporate') { + $this->getLogger('falcon_mail')->info('Order #@number contains non-corporate gifts, no need to send corporate gift notification.', [ + '@number' => $order->getOrderNumber(), + ]); + return; + } + } + + $this->getLogger('falcon_mail')->info('Sending corporate gift notification for order #@number.', [ + '@number' => $order->getOrderNumber(), + ]); + + $params = [ + 'headers' => [ + 'Content-Type' => 'text/html; charset=UTF-8;', + 'Content-Transfer-Encoding' => '8Bit', + ], + 'from' => $order->getStore()->getEmail(), + 'subject' => $this->t('Corporate Gift Order #@number', ['@number' => $order->getOrderNumber()]), + 'order' => $order, + ]; + + $build = [ + '#theme' => 'falcon_mail_commerce_order_receipt_gift_corporate', + '#order_entity' => $order, + '#order_url' => $order->toUrl('canonical', ['absolute' => TRUE])->toString(), + '#totals' => $this->orderTotalSummary->buildTotals($order), + ]; + if ($billing_profile = $order->getBillingProfile()) { + $build['#billing_information'] = $this->profileViewBuilder->view($billing_profile); + } + $params['body'] = $this->renderer->executeInRenderContext(new RenderContext(), function () use ($build) { + return $this->renderer->render($build); + }); + + $langcode = $this->languageManager->getDefaultLanguage()->getId(); + + $this->mailManager->mail('falcon_mail', 'commerce_order_receipt_gift_corporate', $to, $langcode, $params); + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events = ['commerce_order.place.post_transition' => ['sendOrderReceiptGiftCorporate', -100]]; + return $events; + } + +} diff --git a/backend-donations/web/modules/falcon/falcon_mail/src/Form/SettingsForm.php b/backend-donations/web/modules/falcon/falcon_mail/src/Form/SettingsForm.php new file mode 100644 index 0000000..2973b89 --- /dev/null +++ b/backend-donations/web/modules/falcon/falcon_mail/src/Form/SettingsForm.php @@ -0,0 +1,61 @@ +config('falcon_mail.settings'); + + $form['notifications'] = [ + '#type' => 'details', + '#title' => $this->t('Notifications'), + '#open' => TRUE, + ]; + $form['notifications']['list_gift_corporate'] = [ + '#type' => 'textfield', + '#title' => $this->t('Corporate gift order'), + '#description' => $this->t('Email addresses to notify about corporate gift order. Separate several values by comma.'), + '#default_value' => $config->get('notifications.list_gift_corporate'), + '#size' => 128, + '#maxlength' => 256, + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + parent::submitForm($form, $form_state); + + $this->config('falcon_mail.settings') + ->set('notifications.list_gift_corporate', $form_state->getValue('list_gift_corporate')) + ->save(); + } + +} diff --git a/backend-donations/web/modules/falcon/falcon_mail/templates/falcon-mail-commerce-order-receipt-gift-corporate.html.twig b/backend-donations/web/modules/falcon/falcon_mail/templates/falcon-mail-commerce-order-receipt-gift-corporate.html.twig new file mode 100644 index 0000000..d9b8860 --- /dev/null +++ b/backend-donations/web/modules/falcon/falcon_mail/templates/falcon-mail-commerce-order-receipt-gift-corporate.html.twig @@ -0,0 +1,149 @@ +{# +/** + * @file + * Template for the corporate gift notification. + * + * Available variables: + * - order_entity: The order entity. + * - order_url: Link to the order in backend. + * - billing_information: The rendered billing information. + * - shipping_information: The rendered shipping information. + * - payment_method: The rendered payment method. + * - totals: An array of order totals values with the following keys: + * - subtotal: The order subtotal price. + * - adjustments: An array of adjustment totals: + * - type: The adjustment type. + * - label: The adjustment label. + * - total: The adjustment total price. + * - weight: The adjustment weight, taken from the adjustment type. + * - total: The order total price. + * + * @ingroup themeable + */ +#} +
+
|
+