forked from EC-CUBE/related-product-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event.php
215 lines (180 loc) · 6.73 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
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
206
207
208
209
210
211
212
213
214
215
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
* http://www.lockon.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\RelatedProduct;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
class Event
{
private $app;
public function __construct($app)
{
$this->app = $app;
}
// フロント:商品詳細画面に関連商品を表示
public function showRelatedProduct(FilterResponseEvent $event)
{
$app = $this->app;
$id = $app['request']->attributes->get('id');
$Product = $app['eccube.repository.product']->find($id);
$RelatedProducts = $app['eccube.plugin.repository.related_product']->findBy(array('Product' => $Product));
if (count($RelatedProducts) > 0) {
$twig = $app->renderView(
'RelatedProduct/Resource/template/Front/related_product.twig',
array(
'RelatedProducts' => $RelatedProducts,
)
);
$response = $event->getResponse();
if ($response instanceof RedirectResponse) {
return;
}
$html = $response->getContent();
$crawler = new Crawler($html);
$oldHtml = $crawler->filter('#main')->html();
$oldHtml = html_entity_decode($oldHtml, ENT_NOQUOTES, 'UTF-8');
$newHtml = $oldHtml.$twig;
$html = $this->getHtml($crawler);
$html = str_replace($oldHtml, $newHtml, $html);
$response->setContent($html);
$event->setResponse($response);
}
}
public function registerRelatedProduct(FilterResponseEvent $event)
{
$app = $this->app;
if (!$app->isGranted('ROLE_ADMIN')) {
return;
}
if ('POST' === $app['request']->getMethod()) {
// ProductControllerの登録成功時のみ処理を通す
// RedirectResponseかどうかで判定する.
$response = $event->getResponse();
if (!$response instanceof RedirectResponse) {
return;
}
/* @var $Product \Eccube\Entity\Product */
$Product = $this->getTargetProduct($event);
$builder = $app['form.factory']->createBuilder('admin_product');
if ($Product->hasProductClass()) {
$builder->remove('class');
}
$form = $builder->getForm();
$form->handleRequest($app['request']);
if ($form->isValid()) {
$app['eccube.plugin.repository.related_product']->removeChildProduct($Product);
$app['orm.em']->flush();
$RelatedProducts = $form->get('related_collection')->getData();
foreach ($RelatedProducts as $RelatedProduct) {
/* @var $RelatedProduct \Plugin\RelatedProduct\Entity\RelatedProduct */
if ($RelatedProduct->getChildProduct() instanceof \Eccube\Entity\Product) {
$RelatedProduct->setProduct($Product);
$app['orm.em']->persist($RelatedProduct);
}
}
$app['orm.em']->flush();
}
}
}
private function getTargetProduct($event)
{
$request = $event->getRequest();
$response = $event->getResponse();
if ($request->attributes->get('id')) {
$id = $request->attributes->get('id');
} else {
$location = explode('/', $response->headers->get('location'));
$url = explode('/', $this->app->url('admin_product_product_edit', array('id' => '0')));
$diffs = array_values(array_diff($location, $url));
$id = $diffs[0];
}
$Product = $this->app['eccube.repository.product']->find($id);
return $Product;
}
public function addContentOnProductEdit(FilterResponseEvent $event)
{
$app = $this->app;
if (!$app->isGranted('ROLE_ADMIN')) {
return;
}
$request = $event->getRequest();
$response = $event->getResponse();
$id = $request->attributes->get('id');
$html = $response->getContent();
$crawler = new Crawler($html);
$form = $app['form.factory']
->createBuilder('admin_product')
->getForm();
if ($id) {
$Product = $app['eccube.repository.product']->find($id);
} else {
$Product = new \Eccube\Entity\Product();
}
$RelatedProducts = $app['eccube.plugin.repository.related_product']->findBy(
array(
'Product' => $Product,
));
$loop = 5 - count($RelatedProducts);
for ($i = 0; $i < $loop; $i++) {
$RelatedProduct = new \Plugin\RelatedProduct\Entity\RelatedProduct();
$RelatedProduct
->setProductId($id)
->setProduct($Product);
$RelatedProducts[] = $RelatedProduct;
}
$form->get('related_collection')->setData($RelatedProducts);
$form->handleRequest($request);
// 商品検索フォーム
$searchForm = $app['form.factory']
->createBuilder('admin_search_product')
->getForm();
$twig = $app->renderView(
'RelatedProduct/Resource/template/Admin/related_product.twig',
array(
'form' => $form->createView(),
)
);
$modal = $app->renderView(
'RelatedProduct/Resource/template/Admin/modal.twig',
array(
'searchForm' => $searchForm->createView(),
'Product' => $Product,
)
);
$oldElement = $crawler
->filter('.accordion')
->last();
if ($oldElement->count() > 0) {
$oldHtml = $oldElement->html();
$newHtml = $oldHtml.$twig;
$html = $this->getHtml($crawler);
$html = $html.$modal;
$html = str_replace($oldHtml, $newHtml, $html);
$response->setContent($html);
$event->setResponse($response);
}
}
/**
* 解析用HTMLを取得
*
* @param Crawler $crawler
* @return string
*/
private function getHtml(Crawler $crawler)
{
$html = '';
foreach ($crawler as $domElement) {
$domElement->ownerDocument->formatOutput = true;
$html .= $domElement->ownerDocument->saveHTML();
}
return html_entity_decode($html, ENT_NOQUOTES, 'UTF-8');
}
}