Skip to content

Commit

Permalink
Merge pull request #512 from magento-api/MAGETWO-40818-Transactional-…
Browse files Browse the repository at this point in the history
…Email-Templates

[API] Transactional Email Templates - Merging outsourced PR
  • Loading branch information
Tulika,Eugene(etulika) committed Aug 10, 2015
2 parents eef95ce + 226654b commit e3ddc8c
Show file tree
Hide file tree
Showing 27 changed files with 539 additions and 494 deletions.
118 changes: 37 additions & 81 deletions app/code/Magento/Customer/Model/AccountManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* See COPYING.txt for license details.
*/

// @codingStandardsIgnoreFile

namespace Magento\Customer\Model;

use Magento\Customer\Api\AccountManagementInterface;
Expand All @@ -20,6 +18,7 @@
use Magento\Customer\Model\Customer as CustomerModel;
use Magento\Customer\Model\Metadata\Validator;
use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Encryption\EncryptorInterface as Encryptor;
use Magento\Framework\Encryption\Helper\Security;
Expand All @@ -35,6 +34,7 @@
use Magento\Framework\Exception\State\InvalidTransitionException;
use Magento\Framework\DataObjectFactory as ObjectFactory;
use Magento\Framework\Registry;
use Magento\Store\Model\ScopeInterface;
use Psr\Log\LoggerInterface as PsrLogger;
use Magento\Framework\Exception\MailException;
use Magento\Framework\Mail\Template\TransportBuilder;
Expand Down Expand Up @@ -394,7 +394,7 @@ public function validateResetPasswordLinkToken($customerId, $resetPasswordLinkTo
*/
public function initiatePasswordReset($email, $template, $websiteId = null)
{
if (is_null($websiteId)) {
if ($websiteId === null) {
$websiteId = $this->storeManager->getStore()->getWebsiteId();
}
// load customer by email
Expand Down Expand Up @@ -464,12 +464,9 @@ public function getConfirmationStatus($customerId)
/**
* {@inheritdoc}
*/
public function createAccount(
CustomerInterface $customer,
$password = null,
$redirectUrl = ''
) {
if (!is_null($password)) {
public function createAccount(CustomerInterface $customer, $password = null, $redirectUrl = '')
{
if ($password !== null) {
$this->checkPasswordStrength($password);
} else {
$password = $this->mathRandom->getRandomString(self::MIN_PASSWORD_LENGTH);
Expand All @@ -483,11 +480,8 @@ public function createAccount(
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function createAccountWithPasswordHash(
CustomerInterface $customer,
$hash,
$redirectUrl = ''
) {
public function createAccountWithPasswordHash(CustomerInterface $customer, $hash, $redirectUrl = '')
{
// This logic allows an existing customer to be added to a different store. No new account is created.
// The plan is to move this logic into a new method called something like 'registerAccountWithStore'
if ($customer->getId()) {
Expand Down Expand Up @@ -568,20 +562,11 @@ protected function sendEmailConfirmation(CustomerInterface $customer, $redirectU
{
try {
if ($this->isConfirmationRequired($customer)) {
$this->sendNewAccountEmail(
$customer,
self::NEW_ACCOUNT_EMAIL_CONFIRMATION,
$redirectUrl,
$customer->getStoreId()
);
$templateType = self::NEW_ACCOUNT_EMAIL_CONFIRMATION;
} else {
$this->sendNewAccountEmail(
$customer,
self::NEW_ACCOUNT_EMAIL_REGISTERED,
$redirectUrl,
$customer->getStoreId()
);
$templateType = self::NEW_ACCOUNT_EMAIL_REGISTERED;
}
$this->sendNewAccountEmail($customer, $templateType, $redirectUrl, $customer->getStoreId());
} catch (MailException $e) {
// If we are not able to send a new account email, this should be ignored
$this->logger->critical($e);
Expand Down Expand Up @@ -683,13 +668,11 @@ protected function createPasswordHash($password)
/**
* {@inheritdoc}
*/
public function validate(\Magento\Customer\Api\Data\CustomerInterface $customer)
public function validate(CustomerInterface $customer)
{
$customerErrors = $this->validator->validateData(
$this->extensibleDataObjectConverter->toFlatArray($customer, [], '\Magento\Customer\Api\Data\CustomerInterface'),
[],
'customer'
);
$customerData = $this->extensibleDataObjectConverter
->toFlatArray($customer, [], '\Magento\Customer\Api\Data\CustomerInterface');
$customerErrors = $this->validator->validateData($customerData, [], 'customer');

$validationResults = $this->validationResultsDataFactory->create();
if ($customerErrors !== true) {
Expand Down Expand Up @@ -718,7 +701,7 @@ public function validate(\Magento\Customer\Api\Data\CustomerInterface $customer)
public function isEmailAvailable($customerEmail, $websiteId = null)
{
try {
if (is_null($websiteId)) {
if ($websiteId === null) {
$websiteId = $this->storeManager->getStore()->getWebsiteId();
}
$this->customerRepository->get($customerEmail, $websiteId);
Expand Down Expand Up @@ -786,7 +769,7 @@ private function validateResetPasswordToken($customerId, $resetPasswordLinkToken
* @param int $customerId
* @return bool
* @throws \Magento\Framework\Exception\NoSuchEntityException If group is not found
* @throws \Magento\Framework\Exception\LocalizedException
* @throws LocalizedException
*/
public function isReadonly($customerId)
{
Expand All @@ -803,7 +786,7 @@ public function isReadonly($customerId)
* @param string $storeId
* @param string $sendemailStoreId
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
* @throws LocalizedException
*/
protected function sendNewAccountEmail(
$customer,
Expand All @@ -815,9 +798,7 @@ protected function sendNewAccountEmail(
$types = $this->getTemplateTypes();

if (!isset($types[$type])) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Please correct the transactional account email type.')
);
throw new LocalizedException(__('Please correct the transactional account email type.'));
}

if (!$storeId) {
Expand Down Expand Up @@ -853,28 +834,14 @@ protected function sendPasswordResetNotificationEmail($customer)
}

$customerEmailData = $this->getFullCustomerObject($customer);
/** @var \Magento\Framework\Mail\TransportInterface $transport */
$transport = $this->transportBuilder->setTemplateIdentifier(
$this->scopeConfig->getValue(
self::XML_PATH_RESET_PASSWORD_TEMPLATE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
)
)->setTemplateOptions(
['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId]
)->setTemplateVars(
['customer' => $customerEmailData, 'store' => $this->storeManager->getStore($storeId)]
)->setFrom(
$this->scopeConfig->getValue(
self::XML_PATH_FORGOT_EMAIL_IDENTITY,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
)
)->addTo(
$customer->getEmail(),
$this->customerViewHelper->getCustomerName($customer)
)->getTransport();
$transport->sendMessage();

$this->sendEmailTemplate(
$customer,
self::XML_PATH_RESET_PASSWORD_TEMPLATE,
self::XML_PATH_FORGOT_EMAIL_IDENTITY,
['customer' => $customerEmailData, 'store' => $this->storeManager->getStore($storeId)],
$storeId
);

return $this;
}
Expand Down Expand Up @@ -926,19 +893,14 @@ protected function getTemplateTypes()
*/
protected function sendEmailTemplate($customer, $template, $sender, $templateParams = [], $storeId = null)
{
/** @var \Magento\Framework\Mail\TransportInterface $transport */
$transport = $this->transportBuilder->setTemplateIdentifier(
$this->scopeConfig->getValue($template, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId)
)->setTemplateOptions(
['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId]
)->setTemplateVars(
$templateParams
)->setFrom(
$this->scopeConfig->getValue($sender, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId)
)->addTo(
$customer->getEmail(),
$this->customerViewHelper->getCustomerName($customer)
)->getTransport();
$templateId = $this->scopeConfig->getValue($template, ScopeInterface::SCOPE_STORE, $storeId);
$transport = $this->transportBuilder->setTemplateIdentifier($templateId)
->setTemplateOptions(['area' => Area::AREA_FRONTEND, 'store' => $storeId])
->setTemplateVars($templateParams)
->setFrom($this->scopeConfig->getValue($sender, ScopeInterface::SCOPE_STORE, $storeId))
->addTo($customer->getEmail(), $this->customerViewHelper->getCustomerName($customer))
->getTransport();

$transport->sendMessage();

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

return (bool)$this->scopeConfig->getValue(
self::XML_PATH_IS_CONFIRM,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
);
return (bool)$this->scopeConfig->getValue(self::XML_PATH_IS_CONFIRM, ScopeInterface::SCOPE_STORE, $storeId);
}

/**
Expand Down Expand Up @@ -1039,9 +997,7 @@ public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
if (is_string($passwordLinkToken) && !empty($passwordLinkToken)) {
$customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId());
$customerSecure->setRpToken($passwordLinkToken);
$customerSecure->setRpTokenCreatedAt(
(new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT)
);
$customerSecure->setRpTokenCreatedAt((new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT));
$this->customerRepository->save($customer);
}
return true;
Expand Down
15 changes: 14 additions & 1 deletion app/code/Magento/Email/Block/Adminhtml/Template/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
*/
namespace Magento\Email\Block\Adminhtml\Template;

use Magento\Backend\Block\Widget;
use Magento\Backend\Block\Widget\ContainerInterface;

/**
* Adminhtml system template edit block
*
* @author Magento Core Team <core@magentocommerce.com>
* @method array getTemplateOptions()
*/
class Edit extends \Magento\Backend\Block\Widget implements \Magento\Backend\Block\Widget\ContainerInterface
class Edit extends Widget implements ContainerInterface
{
/**
* @var \Magento\Framework\Registry
Expand Down Expand Up @@ -336,6 +339,16 @@ public function isTextType()
return $this->getEmailTemplate()->isPlain();
}

/**
* Return template type from template object
*
* @return int
*/
public function getTemplateType()
{
return $this->getEmailTemplate()->getType();
}

/**
* Return delete url for customer group
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
namespace Magento\Email\Controller\Adminhtml\Email\Template;

use Magento\Framework\App\TemplateTypesInterface;

class Save extends \Magento\Email\Controller\Adminhtml\Email\Template
{
/**
Expand Down Expand Up @@ -43,11 +45,11 @@ public function execute()
);

if (!$template->getId()) {
$template->setTemplateType(\Magento\Email\Model\Template::TYPE_HTML);
$template->setTemplateType(TemplateTypesInterface::TYPE_HTML);
}

if ($request->getParam('_change_type_flag')) {
$template->setTemplateType(\Magento\Framework\App\TemplateTypesInterface::TYPE_TEXT);
$template->setTemplateType(TemplateTypesInterface::TYPE_TEXT);
$template->setTemplateStyles('');
}

Expand Down
Loading

0 comments on commit e3ddc8c

Please sign in to comment.