<?php
namespace Plugin\PiaContact\Controller;
use Eccube\Controller\AbstractController;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Plugin\PiaContact\Form\Type\ProductContactType;
use Plugin\PiaContact\Repository\PiaProductContactRepository;
use Plugin\PiaContact\Service\PiaContactMailService;
use Plugin\PiaContact\Service\PiaContactSpreadsheetService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class ProductContactController extends AbstractController
{
/**
* @var PiaProductContactRepository
*/
protected $piaProductContactRepository;
/**
* @var PiaContactMailService
*/
protected $piaContactMailService;
/**
* @var PiaContactSpreadsheetService
*/
protected $piaContactSpreadsheetService;
/**
* @var AuthorizationCheckerInterface
*/
protected $authorizationChecker;
public function __construct(
PiaProductContactRepository $piaProductContactRepository,
PiaContactMailService $piaContactMailService,
AuthorizationCheckerInterface $authorizationChecker
) {
$this->piaProductContactRepository = $piaProductContactRepository;
$this->piaContactMailService = $piaContactMailService;
$this->authorizationChecker = $authorizationChecker;
$this->piaContactSpreadsheetService = new PiaContactSpreadsheetService();
}
/**
* 商品お問い合わせフォーム
*
* @Route("/product_contact", name="plugin_pia_contact")
* @Template("@PiaContact/default/product_contact/index.twig")
*/
public function index(Request $request)
{
$productId = $request->query->get('id') ?: $request->request->get('id');
if (empty($productId)) {
throw new \Exception("商品IDを指定してください。");
}
$Product = $this->entityManager->getRepository('Eccube\Entity\Product')->find($productId);
if (empty($Product)) {
throw new \Exception("商品情報を取得出来ませんでした。");
}
$builder = $this->formFactory->createBuilder(ProductContactType::class);
$builder->setData(['product_id' => $productId]);
// ログインユーザーの情報を設定
if ($this->authorizationChecker->isGranted('ROLE_USER')) {
$user = $this->getUser();
$builder->setData([
'product_id' => $productId,
'name' => [
'name01' => $user->getName01(),
'name02' => $user->getName02(),
],
'email' => $user->getEmail(),
]);
}
$form = $builder->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
switch ($request->get('mode')) {
case 'confirm':
// 確認画面
return $this->render('@PiaContact/default/product_contact/confirm.twig', [
'form' => $form->createView(),
'Product' => $Product,
]);
case 'back':
// 入力画面に戻る
return [
'form' => $form->createView(),
'Product' => $Product,
];
case 'complete':
// 完了処理
$data = $form->getData();
$data['product_id'] = $productId;
$data['phone_number'] = "";
$data['Product'] = $Product;
$data['contents'] =$data['message'];
// イベントを実行(ContactManagement42でDBに保存される)
$event = new EventArgs(
[
'form' => $form,
'data' => $data,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_CONTACT_INDEX_COMPLETE);
$data = $event->getArgument('data');
// DBに登録されたIDを受付番号として使用
$contactReplyId = isset($_SESSION["eccube.contact.contact_reply_id"]) ? $_SESSION["eccube.contact.contact_reply_id"] : date('YmdHis');
$data['id'] = $contactReplyId;
// メール送信
try {
$result = $this->piaContactMailService->sendPiaContactMail($data, $Product);
if (!$result) {
$this->addError('メール送信に失敗しました。');
}
} catch (\Exception $e) {
$this->addError('メール送信エラー: ' . $e->getMessage());
return [
'form' => $form->createView(),
'Product' => $Product,
];
}
// スプレッドシート保存
try {
$result = $this->piaContactSpreadsheetService->saveProductContact($data, $Product, $contactReplyId);
if (!$result) {
$this->addError('スプレッドシート保存に失敗しました。');
}
} catch (\Exception $e) {
dump($e->getMessage());exit;
$this->addError('スプレッドシート保存エラー: ' . $e->getMessage());
return [
'form' => $form->createView(),
'Product' => $Product,
];
}
$this->addSuccess('商品お問い合わせを受け付けました。', 'front');
return $this->redirectToRoute('plugin_pia_contact_complete');
}
}
return [
'form' => $form->createView(),
'Product' => $Product,
];
}
/**
* 商品お問い合わせ完了画面
*
* @Route("/product_contact/complete", name="plugin_pia_contact_complete")
*/
public function complete()
{
return $this->render('@PiaContact/default/product_contact/complete.twig');
}
}