Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

メールテンプレートを新規登録できるように対応 #6140

Merged
merged 4 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

private function createBody($Order, $twig = 'Mail/order.twig')
{
return $this->renderView($twig, [
'Order' => $Order,
]);
$body = '';

Check warning on line 197 in src/Eccube/Controller/Admin/Order/MailController.php

View check run for this annotation

Codecov / codecov/patch

src/Eccube/Controller/Admin/Order/MailController.php#L197

Added line #L197 was not covered by tests
try {
$body = $this->renderView($twig, [
'Order' => $Order,
]);
} catch (\Exception $e) {
log_warning($e->getMessage());

Check warning on line 203 in src/Eccube/Controller/Admin/Order/MailController.php

View check run for this annotation

Codecov / codecov/patch

src/Eccube/Controller/Admin/Order/MailController.php#L199-L203

Added lines #L199 - L203 were not covered by tests
}

return $body;

Check warning on line 206 in src/Eccube/Controller/Admin/Order/MailController.php

View check run for this annotation

Codecov / codecov/patch

src/Eccube/Controller/Admin/Order/MailController.php#L206

Added line #L206 was not covered by tests
}
}
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 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 @@
$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 @@
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 @@

// 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);

Check warning on line 123 in src/Eccube/Controller/Admin/Setting/Shop/MailController.php

View check run for this annotation

Codecov / codecov/patch

src/Eccube/Controller/Admin/Setting/Shop/MailController.php#L123

Added line #L123 was not covered by tests
}
}

$event = new EventArgs(
Expand All @@ -139,7 +146,8 @@

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

Expand Down Expand Up @@ -168,6 +176,40 @@
];
}

/**
* @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)

Check warning on line 182 in src/Eccube/Controller/Admin/Setting/Shop/MailController.php

View check run for this annotation

Codecov / codecov/patch

src/Eccube/Controller/Admin/Setting/Shop/MailController.php#L182

Added line #L182 was not covered by tests
{
$this->isTokenValid();

Check warning on line 184 in src/Eccube/Controller/Admin/Setting/Shop/MailController.php

View check run for this annotation

Codecov / codecov/patch

src/Eccube/Controller/Admin/Setting/Shop/MailController.php#L184

Added line #L184 was not covered by tests

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

Check warning on line 187 in src/Eccube/Controller/Admin/Setting/Shop/MailController.php

View check run for this annotation

Codecov / codecov/patch

src/Eccube/Controller/Admin/Setting/Shop/MailController.php#L186-L187

Added lines #L186 - L187 were not covered by tests
}

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

Check warning on line 190 in src/Eccube/Controller/Admin/Setting/Shop/MailController.php

View check run for this annotation

Codecov / codecov/patch

src/Eccube/Controller/Admin/Setting/Shop/MailController.php#L190

Added line #L190 was not covered by tests

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

Check warning on line 193 in src/Eccube/Controller/Admin/Setting/Shop/MailController.php

View check run for this annotation

Codecov / codecov/patch

src/Eccube/Controller/Admin/Setting/Shop/MailController.php#L192-L193

Added lines #L192 - L193 were not covered by tests

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

Check warning on line 199 in src/Eccube/Controller/Admin/Setting/Shop/MailController.php

View check run for this annotation

Codecov / codecov/patch

src/Eccube/Controller/Admin/Setting/Shop/MailController.php#L195-L199

Added lines #L195 - L199 were not covered by tests
}
$htmlFilePath = $templatePath.'/'.$this->getHtmlFileName($Mail->getFileName());
if ($this->validateFilePath($htmlFilePath) && is_file($htmlFilePath)) {
$fs->remove($htmlFilePath);

Check warning on line 203 in src/Eccube/Controller/Admin/Setting/Shop/MailController.php

View check run for this annotation

Codecov / codecov/patch

src/Eccube/Controller/Admin/Setting/Shop/MailController.php#L201-L203

Added lines #L201 - L203 were not covered by tests
}

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

Check warning on line 206 in src/Eccube/Controller/Admin/Setting/Shop/MailController.php

View check run for this annotation

Codecov / codecov/patch

src/Eccube/Controller/Admin/Setting/Shop/MailController.php#L206

Added line #L206 was not covered by tests

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

Check warning on line 208 in src/Eccube/Controller/Admin/Setting/Shop/MailController.php

View check run for this annotation

Codecov / codecov/patch

src/Eccube/Controller/Admin/Setting/Shop/MailController.php#L208

Added line #L208 was not covered by tests

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

Check warning on line 210 in src/Eccube/Controller/Admin/Setting/Shop/MailController.php

View check run for this annotation

Codecov / codecov/patch

src/Eccube/Controller/Admin/Setting/Shop/MailController.php#L210

Added line #L210 was not covered by tests
}

/**
* HTML用テンプレート名を取得する
*
Expand All @@ -183,4 +225,18 @@

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 @@
],
])
;

$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')));

Check warning on line 105 in src/Eccube/Form/Type/Admin/MailType.php

View check run for this annotation

Codecov / codecov/patch

src/Eccube/Form/Type/Admin/MailType.php#L104-L105

Added lines #L104 - L105 were not covered by tests
} 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 @@ -1212,11 +1212,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 @@ -1212,11 +1212,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
Loading