-
Notifications
You must be signed in to change notification settings - Fork 6
/
ShoppingMallEvent.php
205 lines (185 loc) · 6.33 KB
/
ShoppingMallEvent.php
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace Plugin\ShoppingMall;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Entity\Member;
use Eccube\Entity\Order;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Eccube\Request\Context;
use Plugin\ShoppingMall\Doctrine\Filter\OwnShopFilter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
class ShoppingMallEvent implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var Context
*/
private $requestContext;
/**
* @var RouterInterface
*/
private $router;
/**
* ShoppingMallEvent constructor.
*
* @param EntityManagerInterface $entityManager
* @param Context $requestContext
* @param RouterInterface $router
*/
public function __construct(EntityManagerInterface $entityManager, Context $requestContext, RouterInterface $router)
{
$this->entityManager = $entityManager;
$this->requestContext = $requestContext;
$this->router = $router;
}
/**
* {@inheritdoc}
*
* @return array
*/
public static function getSubscribedEvents()
{
return [
// 管理画面側
'@admin/index.twig' => ['onTemplateAdminHome'],
'@admin/Product/index.twig' => ['onTemplateAdminProductIndex'],
'@admin/Product/product.twig' => ['onTemplateAdminProductEdit'],
'@admin/Order/index.twig' => ['onTemplateAdminOrderIndex'],
'@admin/Order/edit.twig' => ['onTemplateAdminOrderEdit'],
'@admin/Setting/System/member_edit.twig' => ['onTemplateAdminMemberEdit'],
KernelEvents::CONTROLLER => ['onKernelController'],
KernelEvents::RESPONSE => ['onKernelResponse'],
// フロント画面側
'Product/list.twig' => ['onTemplateProductList'],
'Product/detail.twig' => ['onTemplateProductDetail'],
EccubeEvents::MAIL_ORDER => ['onMailOrder'],
];
}
/**
* @param TemplateEvent $templateEvent
*/
public function onTemplateAdminHome(TemplateEvent $templateEvent)
{
if ($this->isShop()) {
$templateEvent->addSnippet('@ShoppingMall/admin/index.twig');
}
}
/**
* @param TemplateEvent $templateEvent
*/
public function onTemplateAdminProductIndex(TemplateEvent $templateEvent)
{
if ($this->isShop()) {
$templateEvent->addSnippet('@ShoppingMall/admin/Product/index.twig');
}
}
/**
* @param TemplateEvent $templateEvent
*/
public function onTemplateAdminProductEdit(TemplateEvent $templateEvent)
{
$templateEvent->addSnippet('@ShoppingMall/admin/Product/product.twig');
}
/**
* @param TemplateEvent $templateEvent
*/
public function onTemplateAdminOrderIndex(TemplateEvent $templateEvent)
{
if ($this->isShop()) {
$templateEvent->addSnippet('@ShoppingMall/admin/Order/index.twig');
}
}
/**
* @param TemplateEvent $templateEvent
*/
public function onTemplateAdminOrderEdit(TemplateEvent $templateEvent)
{
if ($this->isShop()) {
$templateEvent->addSnippet('@ShoppingMall/admin/Order/edit.twig');
}
}
/**
* @param TemplateEvent $templateEvent
*/
public function onTemplateAdminMemberEdit(TemplateEvent $templateEvent)
{
$templateEvent->addSnippet('@ShoppingMall/admin/Setting/System/member_edit.twig');
}
/**
* @param FilterControllerEvent $event
*/
public function onKernelController(FilterControllerEvent $event)
{
if ($this->requestContext->isAdmin()) {
$Member = $this->requestContext->getCurrentUser();
if ($Member instanceof Member && $Member->isShop()) {
// 自ショップの情報のみ取得するフィルター設定
$config = $this->entityManager->getConfiguration();
$config->addFilter('own_shop_product', OwnShopFilter::class);
/** @var OwnShopFilter $filter */
$filter = $this->entityManager->getFilters()->enable('own_shop_product');
$filter->setShopId($Member->getShop());
}
}
}
/**
* @param FilterResponseEvent $events
*/
public function onKernelResponse(FilterResponseEvent $event)
{
if ($this->requestContext->isAdmin()) {
$Member = $this->requestContext->getCurrentUser();
if ($Member instanceof Member && $Member->isShop()) {
// ショップメンバーは店舗設定の基本設定はアクセス不可、配送方法設定へリダイレクト
if ($event->getRequest()->getRequestUri() === $this->router->generate('admin_setting_shop')) {
$event->setResponse(new RedirectResponse($this->router->generate('admin_setting_shop_delivery')));
}
}
}
}
/**
* @param TemplateEvent $templateEvent
*/
public function onTemplateProductList(TemplateEvent $templateEvent)
{
$templateEvent->addSnippet('@ShoppingMall/default/Product/list.twig');
}
/**
* @param TemplateEvent $templateEvent
*/
public function onTemplateProductDetail(TemplateEvent $templateEvent)
{
$templateEvent->addSnippet('@ShoppingMall/default/Product/detail.twig');
}
/**
* @param EventArgs $event
*/
public function onMailOrder(EventArgs $event)
{
/** @var Order $Order */
$Order = $event->getArgument('Order');
$Shop = $Order->getShopFromItems();
if (!is_null($Shop) && !is_null($Shop->getOrderEmail())) {
/** @var \Swift_Message $message */
$message = $event->getArgument('message');
$message->addBcc($Shop->getOrderEmail());
}
}
/**
* @return bool
*/
private function isShop()
{
$Member = $this->requestContext->getCurrentUser();
return $Member instanceof Member && $Member->isShop();
}
}