Skip to content

Commit e3ddc8c

Browse files
author
Tulika,Eugene(etulika)
committed
Merge pull request #512 from magento-api/MAGETWO-40818-Transactional-Email-Templates
[API] Transactional Email Templates - Merging outsourced PR
2 parents eef95ce + 226654b commit e3ddc8c

File tree

27 files changed

+539
-494
lines changed

27 files changed

+539
-494
lines changed

app/code/Magento/Customer/Model/AccountManagement.php

Lines changed: 37 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
// @codingStandardsIgnoreFile
8-
97
namespace Magento\Customer\Model;
108

119
use Magento\Customer\Api\AccountManagementInterface;
@@ -20,6 +18,7 @@
2018
use Magento\Customer\Model\Customer as CustomerModel;
2119
use Magento\Customer\Model\Metadata\Validator;
2220
use Magento\Framework\Api\ExtensibleDataObjectConverter;
21+
use Magento\Framework\App\Area;
2322
use Magento\Framework\App\Config\ScopeConfigInterface;
2423
use Magento\Framework\Encryption\EncryptorInterface as Encryptor;
2524
use Magento\Framework\Encryption\Helper\Security;
@@ -35,6 +34,7 @@
3534
use Magento\Framework\Exception\State\InvalidTransitionException;
3635
use Magento\Framework\DataObjectFactory as ObjectFactory;
3736
use Magento\Framework\Registry;
37+
use Magento\Store\Model\ScopeInterface;
3838
use Psr\Log\LoggerInterface as PsrLogger;
3939
use Magento\Framework\Exception\MailException;
4040
use Magento\Framework\Mail\Template\TransportBuilder;
@@ -394,7 +394,7 @@ public function validateResetPasswordLinkToken($customerId, $resetPasswordLinkTo
394394
*/
395395
public function initiatePasswordReset($email, $template, $websiteId = null)
396396
{
397-
if (is_null($websiteId)) {
397+
if ($websiteId === null) {
398398
$websiteId = $this->storeManager->getStore()->getWebsiteId();
399399
}
400400
// load customer by email
@@ -464,12 +464,9 @@ public function getConfirmationStatus($customerId)
464464
/**
465465
* {@inheritdoc}
466466
*/
467-
public function createAccount(
468-
CustomerInterface $customer,
469-
$password = null,
470-
$redirectUrl = ''
471-
) {
472-
if (!is_null($password)) {
467+
public function createAccount(CustomerInterface $customer, $password = null, $redirectUrl = '')
468+
{
469+
if ($password !== null) {
473470
$this->checkPasswordStrength($password);
474471
} else {
475472
$password = $this->mathRandom->getRandomString(self::MIN_PASSWORD_LENGTH);
@@ -483,11 +480,8 @@ public function createAccount(
483480
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
484481
* @SuppressWarnings(PHPMD.NPathComplexity)
485482
*/
486-
public function createAccountWithPasswordHash(
487-
CustomerInterface $customer,
488-
$hash,
489-
$redirectUrl = ''
490-
) {
483+
public function createAccountWithPasswordHash(CustomerInterface $customer, $hash, $redirectUrl = '')
484+
{
491485
// This logic allows an existing customer to be added to a different store. No new account is created.
492486
// The plan is to move this logic into a new method called something like 'registerAccountWithStore'
493487
if ($customer->getId()) {
@@ -568,20 +562,11 @@ protected function sendEmailConfirmation(CustomerInterface $customer, $redirectU
568562
{
569563
try {
570564
if ($this->isConfirmationRequired($customer)) {
571-
$this->sendNewAccountEmail(
572-
$customer,
573-
self::NEW_ACCOUNT_EMAIL_CONFIRMATION,
574-
$redirectUrl,
575-
$customer->getStoreId()
576-
);
565+
$templateType = self::NEW_ACCOUNT_EMAIL_CONFIRMATION;
577566
} else {
578-
$this->sendNewAccountEmail(
579-
$customer,
580-
self::NEW_ACCOUNT_EMAIL_REGISTERED,
581-
$redirectUrl,
582-
$customer->getStoreId()
583-
);
567+
$templateType = self::NEW_ACCOUNT_EMAIL_REGISTERED;
584568
}
569+
$this->sendNewAccountEmail($customer, $templateType, $redirectUrl, $customer->getStoreId());
585570
} catch (MailException $e) {
586571
// If we are not able to send a new account email, this should be ignored
587572
$this->logger->critical($e);
@@ -683,13 +668,11 @@ protected function createPasswordHash($password)
683668
/**
684669
* {@inheritdoc}
685670
*/
686-
public function validate(\Magento\Customer\Api\Data\CustomerInterface $customer)
671+
public function validate(CustomerInterface $customer)
687672
{
688-
$customerErrors = $this->validator->validateData(
689-
$this->extensibleDataObjectConverter->toFlatArray($customer, [], '\Magento\Customer\Api\Data\CustomerInterface'),
690-
[],
691-
'customer'
692-
);
673+
$customerData = $this->extensibleDataObjectConverter
674+
->toFlatArray($customer, [], '\Magento\Customer\Api\Data\CustomerInterface');
675+
$customerErrors = $this->validator->validateData($customerData, [], 'customer');
693676

694677
$validationResults = $this->validationResultsDataFactory->create();
695678
if ($customerErrors !== true) {
@@ -718,7 +701,7 @@ public function validate(\Magento\Customer\Api\Data\CustomerInterface $customer)
718701
public function isEmailAvailable($customerEmail, $websiteId = null)
719702
{
720703
try {
721-
if (is_null($websiteId)) {
704+
if ($websiteId === null) {
722705
$websiteId = $this->storeManager->getStore()->getWebsiteId();
723706
}
724707
$this->customerRepository->get($customerEmail, $websiteId);
@@ -786,7 +769,7 @@ private function validateResetPasswordToken($customerId, $resetPasswordLinkToken
786769
* @param int $customerId
787770
* @return bool
788771
* @throws \Magento\Framework\Exception\NoSuchEntityException If group is not found
789-
* @throws \Magento\Framework\Exception\LocalizedException
772+
* @throws LocalizedException
790773
*/
791774
public function isReadonly($customerId)
792775
{
@@ -803,7 +786,7 @@ public function isReadonly($customerId)
803786
* @param string $storeId
804787
* @param string $sendemailStoreId
805788
* @return $this
806-
* @throws \Magento\Framework\Exception\LocalizedException
789+
* @throws LocalizedException
807790
*/
808791
protected function sendNewAccountEmail(
809792
$customer,
@@ -815,9 +798,7 @@ protected function sendNewAccountEmail(
815798
$types = $this->getTemplateTypes();
816799

817800
if (!isset($types[$type])) {
818-
throw new \Magento\Framework\Exception\LocalizedException(
819-
__('Please correct the transactional account email type.')
820-
);
801+
throw new LocalizedException(__('Please correct the transactional account email type.'));
821802
}
822803

823804
if (!$storeId) {
@@ -853,28 +834,14 @@ protected function sendPasswordResetNotificationEmail($customer)
853834
}
854835

855836
$customerEmailData = $this->getFullCustomerObject($customer);
856-
/** @var \Magento\Framework\Mail\TransportInterface $transport */
857-
$transport = $this->transportBuilder->setTemplateIdentifier(
858-
$this->scopeConfig->getValue(
859-
self::XML_PATH_RESET_PASSWORD_TEMPLATE,
860-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
861-
$storeId
862-
)
863-
)->setTemplateOptions(
864-
['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId]
865-
)->setTemplateVars(
866-
['customer' => $customerEmailData, 'store' => $this->storeManager->getStore($storeId)]
867-
)->setFrom(
868-
$this->scopeConfig->getValue(
869-
self::XML_PATH_FORGOT_EMAIL_IDENTITY,
870-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
871-
$storeId
872-
)
873-
)->addTo(
874-
$customer->getEmail(),
875-
$this->customerViewHelper->getCustomerName($customer)
876-
)->getTransport();
877-
$transport->sendMessage();
837+
838+
$this->sendEmailTemplate(
839+
$customer,
840+
self::XML_PATH_RESET_PASSWORD_TEMPLATE,
841+
self::XML_PATH_FORGOT_EMAIL_IDENTITY,
842+
['customer' => $customerEmailData, 'store' => $this->storeManager->getStore($storeId)],
843+
$storeId
844+
);
878845

879846
return $this;
880847
}
@@ -926,19 +893,14 @@ protected function getTemplateTypes()
926893
*/
927894
protected function sendEmailTemplate($customer, $template, $sender, $templateParams = [], $storeId = null)
928895
{
929-
/** @var \Magento\Framework\Mail\TransportInterface $transport */
930-
$transport = $this->transportBuilder->setTemplateIdentifier(
931-
$this->scopeConfig->getValue($template, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId)
932-
)->setTemplateOptions(
933-
['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId]
934-
)->setTemplateVars(
935-
$templateParams
936-
)->setFrom(
937-
$this->scopeConfig->getValue($sender, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId)
938-
)->addTo(
939-
$customer->getEmail(),
940-
$this->customerViewHelper->getCustomerName($customer)
941-
)->getTransport();
896+
$templateId = $this->scopeConfig->getValue($template, ScopeInterface::SCOPE_STORE, $storeId);
897+
$transport = $this->transportBuilder->setTemplateIdentifier($templateId)
898+
->setTemplateOptions(['area' => Area::AREA_FRONTEND, 'store' => $storeId])
899+
->setTemplateVars($templateParams)
900+
->setFrom($this->scopeConfig->getValue($sender, ScopeInterface::SCOPE_STORE, $storeId))
901+
->addTo($customer->getEmail(), $this->customerViewHelper->getCustomerName($customer))
902+
->getTransport();
903+
942904
$transport->sendMessage();
943905

944906
return $this;
@@ -957,11 +919,7 @@ protected function isConfirmationRequired($customer)
957919
}
958920
$storeId = $customer->getStoreId() ? $customer->getStoreId() : null;
959921

960-
return (bool)$this->scopeConfig->getValue(
961-
self::XML_PATH_IS_CONFIRM,
962-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
963-
$storeId
964-
);
922+
return (bool)$this->scopeConfig->getValue(self::XML_PATH_IS_CONFIRM, ScopeInterface::SCOPE_STORE, $storeId);
965923
}
966924

967925
/**
@@ -1039,9 +997,7 @@ public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
1039997
if (is_string($passwordLinkToken) && !empty($passwordLinkToken)) {
1040998
$customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId());
1041999
$customerSecure->setRpToken($passwordLinkToken);
1042-
$customerSecure->setRpTokenCreatedAt(
1043-
(new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT)
1044-
);
1000+
$customerSecure->setRpTokenCreatedAt((new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT));
10451001
$this->customerRepository->save($customer);
10461002
}
10471003
return true;

app/code/Magento/Email/Block/Adminhtml/Template/Edit.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
*/
66
namespace Magento\Email\Block\Adminhtml\Template;
77

8+
use Magento\Backend\Block\Widget;
9+
use Magento\Backend\Block\Widget\ContainerInterface;
10+
811
/**
912
* Adminhtml system template edit block
1013
*
1114
* @author Magento Core Team <[email protected]>
1215
* @method array getTemplateOptions()
1316
*/
14-
class Edit extends \Magento\Backend\Block\Widget implements \Magento\Backend\Block\Widget\ContainerInterface
17+
class Edit extends Widget implements ContainerInterface
1518
{
1619
/**
1720
* @var \Magento\Framework\Registry
@@ -336,6 +339,16 @@ public function isTextType()
336339
return $this->getEmailTemplate()->isPlain();
337340
}
338341

342+
/**
343+
* Return template type from template object
344+
*
345+
* @return int
346+
*/
347+
public function getTemplateType()
348+
{
349+
return $this->getEmailTemplate()->getType();
350+
}
351+
339352
/**
340353
* Return delete url for customer group
341354
*

app/code/Magento/Email/Controller/Adminhtml/Email/Template/Save.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
namespace Magento\Email\Controller\Adminhtml\Email\Template;
88

9+
use Magento\Framework\App\TemplateTypesInterface;
10+
911
class Save extends \Magento\Email\Controller\Adminhtml\Email\Template
1012
{
1113
/**
@@ -43,11 +45,11 @@ public function execute()
4345
);
4446

4547
if (!$template->getId()) {
46-
$template->setTemplateType(\Magento\Email\Model\Template::TYPE_HTML);
48+
$template->setTemplateType(TemplateTypesInterface::TYPE_HTML);
4749
}
4850

4951
if ($request->getParam('_change_type_flag')) {
50-
$template->setTemplateType(\Magento\Framework\App\TemplateTypesInterface::TYPE_TEXT);
52+
$template->setTemplateType(TemplateTypesInterface::TYPE_TEXT);
5153
$template->setTemplateStyles('');
5254
}
5355

0 commit comments

Comments
 (0)