<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Controller;
use Eccube\Controller\ContactController as BaseContactController;
use Eccube\Controller\AbstractController;
use Eccube\Entity\Customer;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Form\Type\Front\ContactType;
use Eccube\Repository\PageRepository;
use Eccube\Service\MailService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
// ▼お問い合わせ項目プルダウン①/⑤ マスターデータ管理に登録されていない場合は、プルダウンを非表示にするため。
use Customize\Entity\Master\ContactOption;
use Customize\Repository\Master\ContactOptionRepository;
// ▼商品お問い合わせ①/⑤
use Eccube\Entity\Product;
use Eccube\Repository\ProductRepository;
class CustomContactController extends BaseContactController
{
/**
* @var MailService
*/
protected $mailService;
/**
* @var PageRepository
*/
private $pageRepository;
/**
* ContactController constructor.
*
* @param MailService $mailService
* @param PageRepository $pageRepository
*/
// ▼お問い合わせ項目プルダウン②/⑤ ▼商品お問い合わせ②/⑤
public function __construct(
MailService $mailService,
PageRepository $pageRepository,
ContactOptionRepository $contactOptionRepository,
ProductRepository $productRepository)
{
$this->mailService = $mailService;
$this->pageRepository = $pageRepository;
$this->contactOptionRepository = $contactOptionRepository;
$this->productRepository = $productRepository;
}
/**
* お問い合わせ画面.
*
* @Route("/contact", name="contact", methods={"GET", "POST"})
* @Route("/contact", name="contact_confirm", methods={"GET", "POST"})
* @Template("Contact/index.twig")
*/
public function index(Request $request)
{
$builder = $this->formFactory->createBuilder(ContactType::class);
// ▼商品お問い合わせ③/⑤
$product_id = $request->query->get('product_id');
$Product = $this->productRepository
->findOneBy(
['id' => $product_id],
['id' => 'DESC']
);
if ($this->isGranted('ROLE_USER') and $product_id) {
/** @var Customer $user */
$user = $this->getUser();
$builder->setData(
[
'name01' => $user->getName01(),
'name02' => $user->getName02(),
'kana01' => $user->getKana01(),
'kana02' => $user->getKana02(),
'postal_code' => $user->getPostalCode(),
'pref' => $user->getPref(),
'addr01' => $user->getAddr01(),
'addr02' => $user->getAddr02(),
'phone_number' => $user->getPhoneNumber(),
'email' => $user->getEmail(),
'product' => '商品ID:'.$Product->getID().'「'.$Product->getName().'」',
]
);
} elseif ($this->isGranted('ROLE_USER')) {
$user = $this->getUser();
$builder->setData(
[
'name01' => $user->getName01(),
'name02' => $user->getName02(),
'kana01' => $user->getKana01(),
'kana02' => $user->getKana02(),
'postal_code' => $user->getPostalCode(),
'pref' => $user->getPref(),
'addr01' => $user->getAddr01(),
'addr02' => $user->getAddr02(),
'phone_number' => $user->getPhoneNumber(),
'email' => $user->getEmail(),
]
);
} elseif($product_id) {
$builder->setData(
[
'product' => '商品ID:'.$Product->getID().'「'.$Product->getName().'」',
]
);
}
// FRONT_CONTACT_INDEX_INITIALIZE
$event = new EventArgs(
[
'builder' => $builder,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_CONTACT_INDEX_INITIALIZE);
$form = $builder->getForm();
$form->handleRequest($request);
// ▼お問い合わせ項目プルダウン③/⑤
$ContactOption = $this->contactOptionRepository
->findBy(
[],
['id' => 'ASC']
);
if ($form->isSubmitted() && $form->isValid()) {
switch ($request->get('mode')) {
case 'confirm':
return $this->render('Contact/confirm.twig', [
'form' => $form->createView(),
'Page' => $this->pageRepository->getPageByRoute('contact_confirm'),
'ContactOption' => $ContactOption, //▼お問い合わせ項目プルダウン④/⑤
'product_id' => $product_id, //▼商品お問い合わせ④/⑤
'Product' => $Product,
]);
case 'complete':
$data = $form->getData();
$event = new EventArgs(
[
'form' => $form,
'data' => $data,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_CONTACT_INDEX_COMPLETE);
$data = $event->getArgument('data');
// メール送信
$this->mailService->sendContactMail($data);
return $this->redirect($this->generateUrl('contact_complete'));
}
}
return [
'form' => $form->createView(),
'ContactOption' => $ContactOption, // ▼お問い合わせ項目プルダウン⑤/⑤
'product_id' => $product_id, //▼商品お問い合わせ⑤/⑤
'Product' => $Product,
];
}
}