<?php
namespace Plugin\ProductContactB\Controller;
use Plugin\ProductContactB\Form\Type\ProductContactType;
use Plugin\ProductContactB\Service\ProductContactMailService;
use Eccube\Controller\AbstractController;
use Eccube\Entity\Customer;
use Eccube\Repository\ProductRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class ProductContactController extends AbstractController
{
/**
* @var ProductRepository
*/
protected $productRepository;
/**
* @var ProductContactMailService
*/
protected $mailService;
/**
* ContactController constructor.
*
* @param ProductRepository $productRepository
* @param ProductContactMailService $mailService
*/
public function __construct(
ProductRepository $productRepository,
ProductContactMailService $mailService)
{
$this->productRepository = $productRepository;
$this->mailService = $mailService;
}
/**
* お問い合わせ画面.
*
* @Route("/products/contact", name="product_contact")
* @Template("@ProductContactB/default/index.twig")
*/
public function index(Request $request)
{
$builder = $this->formFactory->createBuilder(ProductContactType::class);
if ($this->isGranted('ROLE_USER')) {
/** @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(),
]
);
}
// confirmのときはpostのほうがよいかも
$id = $request->query->get('product_id');
$builder->setData(['product_id' => $id]);
$Product = $this->productRepository->find($id);
$form = $builder->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
switch ($request->get('mode')) {
case 'confirm':
$form = $builder->getForm();
$form->handleRequest($request);
return $this->render('@ProductContactB/default/confirm.twig', [
'form' => $form->createView(),
'Product' => $Product,
]);
case 'complete':
$data = $form->getData();
// メール送信
$this->mailService->sendProductContactMail($Product, $data);
return $this->redirect($this->generateUrl('product_contact_complete'));
}
}
return [
'form' => $form->createView(),
'Product' => $Product,
];
}
/**
* お問い合わせ完了画面.
*
* @Route("/products/contact/complete", name="product_contact_complete")
* @Template("@ProductContactB/default/complete.twig")
*/
public function complete()
{
return [];
}
}