Skip to content

Commit

Permalink
Merge pull request #6140 from chihiro-adachi/dev-mail-template
Browse files Browse the repository at this point in the history
メールテンプレートを新規登録できるように対応
  • Loading branch information
ji-eunsoo authored Apr 15, 2024
2 parents 7a5b42e + 15d027f commit 3320043
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 102 deletions.
13 changes: 10 additions & 3 deletions src/Eccube/Controller/Admin/Order/MailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,15 @@ public function index(Request $request, Order $Order)

private function createBody($Order, $twig = 'Mail/order.twig')
{
return $this->renderView($twig, [
'Order' => $Order,
]);
$body = '';
try {
$body = $this->renderView($twig, [
'Order' => $Order,
]);
} catch (\Exception $e) {
log_warning($e->getMessage());
}

return $body;
}
}
80 changes: 68 additions & 12 deletions src/Eccube/Controller/Admin/Setting/Shop/MailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function __construct(MailTemplateRepository $mailTemplateRepository)
*/
public function index(Request $request, Environment $twig, CacheUtil $cacheUtil, MailTemplate $Mail = null)
{
$Mail = $Mail ?? new MailTemplate();
$builder = $this->formFactory
->createBuilder(MailType::class, $Mail);

Expand All @@ -68,17 +69,20 @@ public function index(Request $request, Environment $twig, CacheUtil $cacheUtil,
$this->eventDispatcher->dispatch($event, EccubeEvents::ADMIN_SETTING_SHOP_MAIL_INDEX_INITIALIZE);

$form = $builder->getForm();
$form['template']->setData($Mail);
$htmlFileName = $Mail ? $this->getHtmlFileName($Mail->getFileName()) : null;

// 更新時
if (!is_null($Mail)) {
if (null !== $Mail->getId()) {
$form['template']->setData($Mail);

// テンプレートファイルの取得
$source = $twig->getLoader()
->getSourceContext($Mail->getFileName())
->getCode();

$form->get('tpl_data')->setData($source);

$htmlFileName = $this->getHtmlFileName($Mail->getFileName());

if ($twig->getLoader()->exists($htmlFileName)) {
$source = $twig->getLoader()
->getSourceContext($htmlFileName)
Expand All @@ -91,14 +95,9 @@ public function index(Request $request, Environment $twig, CacheUtil $cacheUtil,
if ('POST' === $request->getMethod()) {
$form->handleRequest($request);

// 新規登録は現時点では未実装とする.
if (is_null($Mail)) {
$this->addError('admin.common.save_error', 'admin');

return $this->redirectToRoute('admin_setting_shop_mail');
}

if ($form->isValid()) {
if ($form->isSubmitted() && $form->isValid()) {
$Mail = $form->getData();
$this->entityManager->persist($Mail);
$this->entityManager->flush();

// ファイル生成・更新
Expand All @@ -112,9 +111,17 @@ public function index(Request $request, Environment $twig, CacheUtil $cacheUtil,

// HTMLファイル用
$htmlMailData = $form->get('html_tpl_data')->getData();
$htmlFileName = $this->getHtmlFileName($Mail->getFileName());

if (!is_null($htmlMailData)) {
$htmlMailData = StringUtil::convertLineFeed($htmlMailData);
$fs->dumpFile($templatePath.'/'.$htmlFileName, $htmlMailData);
} else {
// 空登録の場合は削除
$htmlFilePath = $templatePath.'/'.$htmlFileName;
if ($this->validateFilePath($htmlFilePath) && is_file($htmlFilePath) ) {
$fs->remove($htmlFilePath);
}
}

$event = new EventArgs(
Expand All @@ -139,7 +146,8 @@ public function index(Request $request, Environment $twig, CacheUtil $cacheUtil,

return [
'form' => $form->createView(),
'id' => is_null($Mail) ? null : $Mail->getId(),
'id' => $Mail->getId(),
'Mail' => $Mail,
];
}

Expand Down Expand Up @@ -168,6 +176,40 @@ public function preview(Request $request)
];
}

/**
* @Route("/%eccube_admin_route%/setting/shop/mail/{id}/delete", requirements={"id" = "\d+"}, name="admin_setting_shop_mail_delete", methods={"DELETE"})
*/
public function delete(Request $request, MailTemplate $Mail)
{
$this->isTokenValid();

if (!$Mail->isDeletable()) {
return $this->redirectToRoute('admin_setting_shop_mail');
}

log_info('メールテンプレート削除開始', [$Mail->getId()]);

$this->entityManager->remove($Mail);
$this->entityManager->flush();

$fs = new Filesystem();
$templatePath = $this->getParameter('eccube_theme_front_dir');
$filePath = $templatePath.'/'.$Mail->getFileName();
if ($this->validateFilePath($filePath) && is_file($filePath)) {
$fs->remove($filePath);
}
$htmlFilePath = $templatePath.'/'.$this->getHtmlFileName($Mail->getFileName());
if ($this->validateFilePath($htmlFilePath) && is_file($htmlFilePath)) {
$fs->remove($htmlFilePath);
}

$this->addSuccess('admin.common.delete_complete', 'admin');

log_info('メールテンプレート削除完了', [$Mail->getId()]);

return $this->redirectToRoute('admin_setting_shop_mail');
}

/**
* HTML用テンプレート名を取得する
*
Expand All @@ -183,4 +225,18 @@ protected function getHtmlFileName($fileName)

return $targetTemplate['dirname'].DIRECTORY_SEPARATOR.$targetTemplate['filename'].$suffix.'.'.$targetTemplate['extension'];
}

/**
* テンプレートディレクトリ配下のパスかどうかを検証する
*
* @param $path
* @return bool
*/
protected function validateFilePath($path)
{
$templatePath = realpath($this->getParameter('eccube_theme_front_dir'));
$path = realpath($path);

return \str_starts_with($path, $templatePath);
}
}
29 changes: 29 additions & 0 deletions src/Eccube/Entity/MailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ public function __toString()
*/
private $Creator;

/**
* テンプレートの削除可否。
*
* @var bool
*
* @ORM\Column(name="deletable", type="boolean", options={"default":false}))
*/
private bool $deletable = false;

/**
* Get id.
*
Expand Down Expand Up @@ -242,5 +251,25 @@ public function getCreator()
{
return $this->Creator;
}


/**
* @return bool
*/
public function isDeletable(): bool
{
return $this->deletable;
}

/**
* @param bool $deletable
* @return $this
*/
public function setDeletable(bool $deletable): self
{
$this->deletable = $deletable;

return $this;
}
}
}
64 changes: 63 additions & 1 deletion src/Eccube/Form/Type/Admin/MailType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,54 @@

namespace Eccube\Form\Type\Admin;

use Eccube\Common\EccubeConfig;
use Eccube\Entity\MailTemplate;
use Eccube\Form\Type\Master\MailTemplateType;
use Eccube\Form\Validator\TwigLint;
use Eccube\Repository\MailTemplateRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;

class MailType extends AbstractType
{
private MailTemplateRepository $mailTemplateRepository;

private EccubeConfig $eccubeConfig;

public function __construct(MailTemplateRepository $mailTemplateRepository, EccubeConfig $eccubeConfig)
{
$this->mailTemplateRepository = $mailTemplateRepository;
$this->eccubeConfig = $eccubeConfig;
}

/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('template', MailTemplateType::class, [
'required' => true,
'required' => false,
'mapped' => false,
])
->add('name', TextType::class, [
'constraints' => [
new Assert\NotBlank(),
new Assert\Length(['max' => $this->eccubeConfig['eccube_stext_len']]),
],
])
->add('mail_subject', TextType::class, [
'required' => true,
'constraints' => [
new Assert\NotBlank(),
new Assert\Length(['max' => $this->eccubeConfig['eccube_stext_len']]),
],
])
->add('tpl_data', TextareaType::class, [
Expand All @@ -59,6 +80,47 @@ public function buildForm(FormBuilderInterface $builder, array $options)
],
])
;

$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
$data = $event->getData();
if (null === $data->getId()) {
$form = $event->getForm();
$form->add('file_name', TextType::class, [
'constraints' => [
new Assert\NotBlank(),
new Assert\Regex(['pattern' => '/^[0-9a-z_-]+$/']),
new Assert\Length(['max' => $this->eccubeConfig['eccube_stext_len']]),
],
]);
}
});

$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$data = $event->getData();
if (null === $data->getId()) {
$filename = 'Mail/'.$data->getFileName().'.twig';
$MailTemplate = $this->mailTemplateRepository->findBy(['file_name' => $filename]);
if ($MailTemplate) {
$form = $event->getForm();
$form['file_name']->addError(new FormError(trans('admin.setting.shop.mail.file_exists')));
} else {
$data->setFileName('Mail/'.$data->getFileName().'.twig');
}

$data->setDeletable(true);
}
});

}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => MailTemplate::class,
]);
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Eccube/Form/Type/Admin/OrderMailType.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'mapped' => false,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('mt')
->andWhere('mt.id = :id')
->setParameter('id', $this->eccubeConfig['eccube_order_mail_template_id'])
->orderBy('mt.id', 'ASC');
},
])
Expand Down
20 changes: 10 additions & 10 deletions src/Eccube/Resource/doctrine/import_csv/en/dtb_mail_template.csv
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
id,creator_id,name,file_name,mail_subject,create_date,update_date,discriminator_type
1,,Order Receipt,Mail/order.twig,Thank you for your order!,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
2,,Temporary Registration,Mail/entry_confirm.twig,Member registration confirmation,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
3,,Regular Registration,Mail/entry_complete.twig,Your registration has been completed,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
4,,Membership Cancelation,Mail/customer_withdraw_mail.twig,Membership cancelation completed,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
5,,Inquiry Receipt,Mail/contact_mail.twig,Thank you for your inqury,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
6,,Password Reset,Mail/forgot_mail.twig,Reset your password,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
7,,Password Reminder,Mail/reset_complete_mail.twig,Your password has been changed,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
8,,Shipping Notice,Mail/shipping_notify.twig,Your order has been shipped,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
9,,Notification email,Mail/customer_change_notify.twig,Your account information has been changed.,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
id,creator_id,name,file_name,mail_subject,deletable,create_date,update_date,discriminator_type
1,,Order Receipt,Mail/order.twig,Thank you for your order!,0,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
2,,Temporary Registration,Mail/entry_confirm.twig,Member registration confirmation,0,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
3,,Regular Registration,Mail/entry_complete.twig,Your registration has been completed,0,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
4,,Membership Cancelation,Mail/customer_withdraw_mail.twig,Membership cancelation completed,0,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
5,,Inquiry Receipt,Mail/contact_mail.twig,Thank you for your inqury,0,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
6,,Password Reset,Mail/forgot_mail.twig,Reset your password,0,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
7,,Password Reminder,Mail/reset_complete_mail.twig,Your password has been changed,0,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
8,,Shipping Notice,Mail/shipping_notify.twig,Your order has been shipped,0,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
9,,Notification email,Mail/customer_change_notify.twig,Your account information has been changed.,0,2017-03-07 10:14:52,2017-03-07 10:14:52,mailtemplate
20 changes: 10 additions & 10 deletions src/Eccube/Resource/doctrine/import_csv/ja/dtb_mail_template.csv
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
id,creator_id,name,file_name,mail_subject,create_date,update_date,discriminator_type
"1",,"注文受付メール","Mail/order.twig","ご注文ありがとうございます","2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"2",,"会員仮登録メール","Mail/entry_confirm.twig","会員登録のご確認","2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"3",,"会員本登録メール","Mail/entry_complete.twig","会員登録が完了しました。","2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"4",,"会員退会メール","Mail/customer_withdraw_mail.twig","退会手続きのご完了","2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"5",,"問合受付メール","Mail/contact_mail.twig","お問い合わせを受け付けました。","2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"6",,"パスワードリセット","Mail/forgot_mail.twig","パスワード変更のご確認","2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"7",,"パスワードリマインダー","Mail/reset_complete_mail.twig","パスワード変更のお知らせ","2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"8",,"出荷通知メール","Mail/shipping_notify.twig","商品出荷のお知らせ","2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"9",,"会員情報変更通知メール","Mail/customer_change_notify.twig","会員情報変更のお知らせ","2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
id,creator_id,name,file_name,mail_subject,deletable,create_date,update_date,discriminator_type
"1",,"注文受付メール","Mail/order.twig","ご注文ありがとうございます",0,"2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"2",,"会員仮登録メール","Mail/entry_confirm.twig","会員登録のご確認",0,"2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"3",,"会員本登録メール","Mail/entry_complete.twig","会員登録が完了しました。",0,"2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"4",,"会員退会メール","Mail/customer_withdraw_mail.twig","退会手続きのご完了",0,"2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"5",,"問合受付メール","Mail/contact_mail.twig","お問い合わせを受け付けました。",0,"2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"6",,"パスワードリセット","Mail/forgot_mail.twig","パスワード変更のご確認",0,"2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"7",,"パスワードリマインダー","Mail/reset_complete_mail.twig","パスワード変更のお知らせ",0,"2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"8",,"出荷通知メール","Mail/shipping_notify.twig","商品出荷のお知らせ",0,"2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
"9",,"会員情報変更通知メール","Mail/customer_change_notify.twig","会員情報変更のお知らせ",0,"2017-03-07 10:14:52","2017-03-07 10:14:52","mailtemplate"
5 changes: 5 additions & 0 deletions src/Eccube/Resource/locale/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1236,11 +1236,16 @@ admin.setting.shop.tax.apply_date.available_error: The same date and time cannot

admin.setting.shop.mail.mail_template_edit: Edit Templates
admin.setting.shop.mail.mail_template: Template
admin.setting.shop.mail.mail_template_name: Template name
admin.setting.shop.mail.mail_file_name: File name
admin.setting.shop.mail.mail_subject: Title
admin.setting.shop.mail.mail_body: Body
admin.setting.shop.mail.mail_text: Text
admin.setting.shop.mail.mail_html: HTML
admin.setting.shop.mail.preview: Preview
admin.setting.shop.mail.file_exists: This file name is already in use.
admin.setting.shop.mail.delete__confirm_title: Delete a mail template
admin.setting.shop.mail.delete__confirm_message: Are you sure to delete this mail template?

#------------------------------------------------------------------------------------
# Settings : Store Settings : CSV Outputs
Expand Down
5 changes: 5 additions & 0 deletions src/Eccube/Resource/locale/messages.ja.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1236,11 +1236,16 @@ admin.setting.shop.tax.apply_date.available_error: 同時刻の適用日時を

admin.setting.shop.mail.mail_template_edit: テンプレート編集
admin.setting.shop.mail.mail_template: テンプレート
admin.setting.shop.mail.mail_template_name: テンプレート名
admin.setting.shop.mail.mail_file_name: ファイル名
admin.setting.shop.mail.mail_subject: 件名
admin.setting.shop.mail.mail_body: 本文
admin.setting.shop.mail.mail_text: テキスト
admin.setting.shop.mail.mail_html: HTML
admin.setting.shop.mail.preview: プレビュー
admin.setting.shop.mail.file_exists: このファイル名はすでに使用されています。
admin.setting.shop.mail.delete__confirm_title: メールテンプレートを削除します。
admin.setting.shop.mail.delete__confirm_message: メールテンプレートを削除してよろしいですか?

#------------------------------------------------------------------------------------
# 設定:店舗設定:CSV出力項目設定
Expand Down
Loading

0 comments on commit 3320043

Please sign in to comment.