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

メールテンプレートを新規登録/削除できるようにする #4440

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/Eccube/Controller/Admin/Content/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public function delete(Request $request, $id = null, CacheUtil $cacheUtil)
// ユーザーが作ったページのみ削除する
if ($Page->getEditType() == Page::EDIT_TYPE_USER) {
$templatePath = $this->getParameter('eccube_theme_user_data_dir');
$file = $templatePath.'/'.$Page->getFileName().'.twig';
$file = $templatePath.'/'.$Page->getFileName();
$fs = new Filesystem();
if ($fs->exists($file)) {
$fs->remove($file);
Expand Down
169 changes: 131 additions & 38 deletions src/Eccube/Controller/Admin/Setting/Shop/MailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
use Eccube\Event\EventArgs;
use Eccube\Form\Type\Admin\MailType;
use Eccube\Repository\MailTemplateRepository;
use Eccube\Util\CacheUtil;
use Eccube\Util\StringUtil;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;

Expand All @@ -48,11 +50,83 @@ public function __construct(MailTemplateRepository $mailTemplateRepository)

/**
* @Route("/%eccube_admin_route%/setting/shop/mail", name="admin_setting_shop_mail")
* @Route("/%eccube_admin_route%/setting/shop/mail/{id}", requirements={"id" = "\d+"}, name="admin_setting_shop_mail_edit")
* @Template("@admin/Setting/Shop/mail.twig")
*/
public function index(Request $request, MailTemplate $Mail = null, Environment $twig)
public function index()
{
$MailTemplates = $this->mailTemplateRepository->getList();

return ['MailTemplates' => $MailTemplates];
}

/**
* @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, $id = null, CacheUtil $cacheUtil)
{
$this->isTokenValid();

/** @var MailTemplate $Mail */
$Mail = $this->mailTemplateRepository
->findOneBy([
'id' => $id,
]);

if (!$Mail) {
throw new NotFoundHttpException();
}

// ユーザーが作ったページのみ削除する
if ($Mail->getEditType() == MailTemplate::EDIT_TYPE_USER) {

$templatePath = $this->getParameter('eccube_theme_front_dir');
$file = $templatePath.'/'.$Mail->getFileName();
$htmlFile = $this->getHtmlFileName($file);

$fs = new Filesystem();
if ($fs->exists($file)) {
$fs->remove($file);
}
if ($fs->exists($htmlFile)) {
$fs->remove($htmlFile);
}
$this->entityManager->remove($Mail);
$this->entityManager->flush();

$event = new EventArgs(
[
'Mail' => $Mail,
],
$request
);
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_DELETE_COMPLETE, $event);

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

// キャッシュの削除
$cacheUtil->clearTwigCache();
$cacheUtil->clearDoctrineCache();
}

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

/**
* @Route("/%eccube_admin_route%/setting/shop/mail/edit/new", name="admin_setting_shop_mail_new")
* @Route("/%eccube_admin_route%/setting/shop/mail/edit/{id}", requirements={"id" = "\d+"}, name="admin_setting_shop_mail_edit")
* @Template("@admin/Setting/Shop/mail_edit.twig")
*/
public function edit(Request $request, $id = null, Environment $twig)
{

$Mail = null;
if ($id) {
$Mail = $this->mailTemplateRepository->find($id);
if (is_null($Mail)) {
throw new NotFoundHttpException();
}
}

$builder = $this->formFactory
->createBuilder(MailType::class, $Mail);

Expand All @@ -66,11 +140,14 @@ public function index(Request $request, MailTemplate $Mail = null, Environment $
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_INDEX_INITIALIZE, $event);

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

$isUserType = false;

// 更新時
if (!is_null($Mail)) {

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

// テンプレートファイルの取得
$source = $twig->getLoader()
->getSourceContext($Mail->getFileName())
Expand All @@ -84,57 +161,73 @@ public function index(Request $request, MailTemplate $Mail = null, Environment $

$form->get('html_tpl_data')->setData($source);
}
} else {
$isUserType = true;
}

if ('POST' === $request->getMethod()) {
$form->handleRequest($request);
$form->handleRequest($request);

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

return $this->redirectToRoute('admin_setting_shop_mail');
if (is_null($Mail)) {
$fileName = sprintf('Mail/%s.twig', $form->get('file_name')->getData());
} else {
$fileName = $Mail->getFileName();
}

if ($form->isValid()) {
$this->entityManager->flush();

// ファイル生成・更新
$templatePath = $this->getParameter('eccube_theme_front_dir');
$filePath = $templatePath.'/'.$Mail->getFileName();
// ファイル生成・更新
$templatePath = $this->getParameter('eccube_theme_front_dir');
$filePath = $templatePath.'/'.$fileName;

$fs = new Filesystem();
$mailData = $form->get('tpl_data')->getData();
$mailData = StringUtil::convertLineFeed($mailData);
$fs->dumpFile($filePath, $mailData);

// HTMLファイル用
$htmlMailData = $form->get('html_tpl_data')->getData();
$htmlFile = $this->getHtmlFileName($filePath);
if (!is_null($htmlMailData)) {
$htmlMailData = StringUtil::convertLineFeed($htmlMailData);
$fs->dumpFile($htmlFile, $htmlMailData);
} else {
$fs = new Filesystem();
$mailData = $form->get('tpl_data')->getData();
$mailData = StringUtil::convertLineFeed($mailData);
$fs->dumpFile($filePath, $mailData);

// HTMLファイル用
$htmlMailData = $form->get('html_tpl_data')->getData();
if (!is_null($htmlMailData)) {
$htmlMailData = StringUtil::convertLineFeed($htmlMailData);
$fs->dumpFile($templatePath.'/'.$htmlFileName, $htmlMailData);
if ($fs->exists($htmlFile)) {
$fs->remove($htmlFile);
}
}

$event = new EventArgs(
[
'form' => $form,
'Mail' => $Mail,
'templatePath' => $templatePath,
'filePath' => $filePath,
],
$request
);
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_INDEX_COMPLETE, $event);
/** @var MailTemplate $Mail */
$Mail = $form->getData();
if ($isUserType) {
$Mail->setEditType(MailTemplate::EDIT_TYPE_USER);
}
$Mail->setFileName($fileName);
$this->entityManager->persist($Mail);
$this->entityManager->flush();

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

return $this->redirectToRoute('admin_setting_shop_mail_edit', ['id' => $Mail->getId()]);
}
$event = new EventArgs(
[
'form' => $form,
'Mail' => $Mail,
'templatePath' => $templatePath,
'filePath' => $filePath,
],
$request
);
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_MAIL_INDEX_COMPLETE, $event);

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

return $this->redirectToRoute('admin_setting_shop_mail_edit', ['id' => $Mail->getId()]);
}


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

Expand Down
35 changes: 35 additions & 0 deletions src/Eccube/Entity/MailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
*/
class MailTemplate extends \Eccube\Entity\AbstractEntity
{

const EDIT_TYPE_USER = 0;
const EDIT_TYPE_DEFAULT = 1;

/**
* @return string
*/
Expand Down Expand Up @@ -65,6 +69,13 @@ public function __toString()
*/
private $mail_subject;

/**
* @var int
*
* @ORM\Column(name="edit_type", type="smallint", options={"unsigned":true,"default":1})
*/
private $edit_type = self::EDIT_TYPE_DEFAULT;

/**
* @var \DateTime
*
Expand Down Expand Up @@ -171,6 +182,30 @@ public function getMailSubject()
return $this->mail_subject;
}

/**
* Set editType.
*
* @param int $editType
*
* @return MailTemplate
*/
public function setEditType($editType)
{
$this->edit_type = $editType;

return $this;
}

/**
* Get editType.
*
* @return int
*/
public function getEditType()
{
return $this->edit_type;
}

/**
* Set createDate.
*
Expand Down
1 change: 1 addition & 0 deletions src/Eccube/Event/EccubeEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ final class EccubeEvents
// index
const ADMIN_SETTING_SHOP_MAIL_INDEX_INITIALIZE = 'admin.setting.shop.mail.index.initialize';
const ADMIN_SETTING_SHOP_MAIL_INDEX_COMPLETE = 'admin.setting.shop.mail.index.complete';
const ADMIN_SETTING_SHOP_MAIL_DELETE_COMPLETE = 'admin.setting.shop.mail.delete.complete';
// preview
const ADMIN_SETTING_SHOP_MAIL_PREVIEW_COMPLETE = 'admin.setting.shop.mail.preview.complete';

Expand Down
Loading