-
Notifications
You must be signed in to change notification settings - Fork 720
Expand file tree
/
Copy pathContactController.php
More file actions
132 lines (115 loc) · 4.39 KB
/
Copy pathContactController.php
File metadata and controls
132 lines (115 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?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 Eccube\Controller;
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 Symfony\Bridge\Twig\Attribute\Template;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class ContactController extends AbstractController
{
/**
* ContactController constructor.
*/
public function __construct(protected MailService $mailService, private readonly PageRepository $pageRepository)
{
}
/**
* お問い合わせ画面.
*
* @return Response|RedirectResponse|array<string, mixed>
*/
#[Route(path: '/contact', name: 'contact', methods: ['GET', 'POST'])]
#[Route(path: '/contact', name: 'contact_confirm', methods: ['GET', 'POST'])]
#[Template(template: 'Contact/index.twig')]
public function index(Request $request): Response|RedirectResponse|array
{
$builder = $this->formFactory->createBuilder(ContactType::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(),
]
);
}
// 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);
if ($form->isSubmitted() && $form->isValid()) {
switch ($request->get('mode')) {
case 'confirm':
$Page = $this->pageRepository->getPageByRoute('contact_confirm');
return $this->render('Contact/confirm.twig', [
'form' => $form->createView(),
'Page' => $Page,
// contact と contact_confirm は同一パス '/contact' のため, ルータは常に
// contact にマッチする. TwigInitializeListener が _route から引く
// twig グローバル title は「入力ページ」のままになるので,
// title より優先される subtitle で確認ページ名を上書きする.
'subtitle' => $Page->getName(),
]);
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->redirectToRoute('contact_complete');
}
}
return [
'form' => $form->createView(),
];
}
/**
* お問い合わせ完了画面.
*
* @return array<empty>
*/
#[Route(path: '/contact/complete', name: 'contact_complete', methods: ['GET'])]
#[Template(template: 'Contact/complete.twig')]
public function complete(): array
{
return [];
}
}