Skip to content
This repository has been archived by the owner on Aug 8, 2018. It is now read-only.

Commit

Permalink
Merge pull request #11 from systemseed/corp_gift_notification
Browse files Browse the repository at this point in the history
[#152720803] Adding email notifications about corporate gift orders.
  • Loading branch information
Kate authored Apr 3, 2018
2 parents 68542c8 + da10222 commit efb1f47
Show file tree
Hide file tree
Showing 9 changed files with 463 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ type: module
description: E-mail customisations for Falcon.
core: 8.x
package: Falcon
dependencies:
- commerce:commerce_order
Original file line number Diff line number Diff line change
@@ -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.'
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function falcon_mail_help($route_name, RouteMatchInterface $route_match) {
case 'help.page.falcon_mail':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('E-mail customisations for CW') . '</p>';
$output .= '<p>' . t('E-mail customisations for Falcon') . '</p>';
return $output;

default:
Expand All @@ -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'];
}
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -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' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php

namespace Drupal\falcon_mail\EventSubscriber;

use Drupal\commerce_order\OrderTotalSummaryInterface;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Logger\LoggerChannelTrait;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\Renderer;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Sends a corporate gift notification email when an order is placed.
*/
class OrderReceiptGiftCorporateSubscriber implements EventSubscriberInterface {

use StringTranslationTrait;
use LoggerChannelTrait;

/**
* The order type entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $orderTypeStorage;

/**
* The order total summary.
*
* @var \Drupal\commerce_order\OrderTotalSummaryInterface
*/
protected $orderTotalSummary;

/**
* The entity view builder for profiles.
*
* @var \Drupal\profile\ProfileViewBuilder
*/
protected $profileViewBuilder;

/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;

/**
* The mail manager.
*
* @var \Drupal\Core\Mail\MailManagerInterface
*/
protected $mailManager;

/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;

/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* Constructs a new OrderReceiptGiftCorporateSubscriber object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
* The mail manager.
* @param \Drupal\commerce_order\OrderTotalSummaryInterface $order_total_summary
* The order total summary.
* @param \Drupal\Core\Render\Renderer $renderer
* The renderer.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, MailManagerInterface $mail_manager, OrderTotalSummaryInterface $order_total_summary, Renderer $renderer, ConfigFactoryInterface $config_factory) {
$this->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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Drupal\falcon_mail\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

class SettingsForm extends ConfigFormBase {

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'falcon_mail_admin_settings';
}

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'falcon_mail.settings',
];
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$config = $this->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();
}

}
Loading

0 comments on commit efb1f47

Please sign in to comment.