-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathEvent.php
168 lines (151 loc) · 5.14 KB
/
Event.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
<?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 Plugin\Coupon42;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Entity\Order;
use Eccube\Event\TemplateEvent;
use Eccube\Repository\OrderRepository;
use Plugin\Coupon42\Entity\Coupon;
use Plugin\Coupon42\Repository\CouponOrderRepository;
use Plugin\Coupon42\Repository\CouponRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class Event.
*/
class Event implements EventSubscriberInterface
{
/**
* @var CouponOrderRepository
*/
private $couponOrderRepository;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var CouponRepository
*/
private $couponRepository;
/**
* @var OrderRepository
*/
private $orderRepository;
/**
* @var \Twig\Environment
*/
private $twig;
/**
* Event constructor.
*
* @param CouponOrderRepository $couponOrderRepository
* @param EntityManagerInterface $entityManager
* @param CouponRepository $couponRepository
* @param OrderRepository $orderRepository
* @param \Twig\Environment $twig
*/
public function __construct(CouponOrderRepository $couponOrderRepository, EntityManagerInterface $entityManager, CouponRepository $couponRepository, OrderRepository $orderRepository, \Twig\Environment $twig)
{
$this->couponOrderRepository = $couponOrderRepository;
$this->entityManager = $entityManager;
$this->couponRepository = $couponRepository;
$this->orderRepository = $orderRepository;
$this->twig = $twig;
}
/**
* Todo: admin.order.delete.complete has been deleted.
*
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Shopping/index.twig' => 'index',
'Shopping/confirm.twig' => 'index',
'Mypage/history.twig' => 'onRenderMypageHistory',
'@admin/Order/edit.twig' => 'onRenderAdminOrderEdit',
];
}
/**
* @param TemplateEvent $event
*/
public function index(TemplateEvent $event)
{
$parameters = $event->getParameters();
// 登録がない、レンダリングをしない
/** @var Order $Order */
$Order = $parameters['Order'];
// クーポンが未入力でクーポン情報が存在すればクーポン情報を削除
$CouponOrder = $this->couponOrderRepository->getCouponOrder($Order->getPreOrderId());
$parameters['CouponOrder'] = $CouponOrder;
$event->setParameters($parameters);
if (strpos($event->getView(), 'index.twig') !== false) {
$event->addSnippet('@Coupon42/default/coupon_shopping_item.twig');
} else {
$event->addSnippet('@Coupon42/default/coupon_shopping_item_confirm.twig');
}
}
/**
* Hook point add coupon information to mypage history.
*
* @param TemplateEvent $event
*/
public function onRenderMypageHistory(TemplateEvent $event)
{
log_info('Coupon trigger onRenderMypageHistory start');
$parameters = $event->getParameters();
if (is_null($parameters['Order'])) {
return;
}
$Order = $parameters['Order'];
// クーポン受注情報を取得する
$CouponOrder = $this->couponOrderRepository->findOneBy([
'order_id' => $Order->getId(),
]);
if (is_null($CouponOrder)) {
return;
}
// set parameter for twig files
$parameters['coupon_cd'] = $CouponOrder->getCouponCd();
$parameters['coupon_name'] = $CouponOrder->getCouponName();
$event->setParameters($parameters);
$event->addSnippet('@Coupon42/default/mypage_history_coupon.twig');
log_info('Coupon trigger onRenderMypageHistory finish');
}
/**
* [order/{id}/edit]表示の時のEvent Fork.
* クーポン関連項目を追加する.
*
* @param TemplateEvent $event
*/
public function onRenderAdminOrderEdit(TemplateEvent $event)
{
log_info('Coupon trigger onRenderAdminOrderEdit start');
$parameters = $event->getParameters();
if (is_null($parameters['Order'])) {
return;
}
$Order = $parameters['Order'];
// クーポン受注情報を取得する
$CouponOrder = $this->couponOrderRepository->findOneBy(['order_id' => $Order->getId()]);
if (is_null($CouponOrder)) {
return;
}
// set parameter for twig files
$parameters['coupon_cd'] = $CouponOrder->getCouponCd();
$parameters['coupon_name'] = $CouponOrder->getCouponName();
$parameters['coupon_change_status'] = $CouponOrder->getOrderChangeStatus();
$event->setParameters($parameters);
// add twig
$event->addSnippet('@Coupon42/admin/order_edit_coupon.twig');
log_info('Coupon trigger onRenderAdminOrderEdit finish');
}
}