This repository has been archived by the owner on Aug 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from systemseed/corp_gift_notification
[#152720803] Adding email notifications about corporate gift orders.
- Loading branch information
Showing
9 changed files
with
463 additions
and
1 deletion.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
backend-donations/web/modules/falcon/falcon_mail/config/schema/falcon_mail.schema.yml
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,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' |
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
10 changes: 10 additions & 0 deletions
10
backend-donations/web/modules/falcon/falcon_mail/falcon_mail.links.menu.yml
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,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.' |
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
15 changes: 15 additions & 0 deletions
15
backend-donations/web/modules/falcon/falcon_mail/falcon_mail.routing.yml
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,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' |
6 changes: 6 additions & 0 deletions
6
backend-donations/web/modules/falcon/falcon_mail/falcon_mail.services.yml
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,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' } |
173 changes: 173 additions & 0 deletions
173
...eb/modules/falcon/falcon_mail/src/EventSubscriber/OrderReceiptGiftCorporateSubscriber.php
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,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; | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
backend-donations/web/modules/falcon/falcon_mail/src/Form/SettingsForm.php
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,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(); | ||
} | ||
|
||
} |
Oops, something went wrong.