forked from EC-CUBE/maker-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaker.php
More file actions
executable file
·277 lines (215 loc) · 8.31 KB
/
Copy pathMaker.php
File metadata and controls
executable file
·277 lines (215 loc) · 8.31 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?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\Maker;
use Eccube\Common\Constant;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
class Maker
{
private $app;
public function __construct($app)
{
$this->app = $app;
}
public function onRenderAdminProductNewBefore(FilterResponseEvent $event)
{
$app = $this->app;
if (!$this->app->isGranted('ROLE_ADMIN')) {
return;
}
$request = $event->getRequest();
$response = $event->getResponse();
$id = $request->attributes->get('id');
list($html, $form) = $this->getHtml($request, $response, $id);
$response->setContent($html);
if ('POST' === $request->getMethod()) {
// RedirectResponseかどうかで判定する.
if (!$response instanceof RedirectResponse) {
return;
}
if (empty($id)) {
$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];
}
if ($form->isValid()) {
// 登録
$data = $form->getData();
$Makers = $this->app['eccube.plugin.maker.repository.maker']->findAll();
$Maker = $form->get('maker')->getData();
$makerUrl = $form->get('maker_url')->getData();
if (count($Makers) > 0 && !empty($Maker)) {
$ProductMaker = new \Plugin\Maker\Entity\ProductMaker();
$ProductMaker
->setId($id)
->setMakerUrl($makerUrl)
->setDelFlg(Constant::DISABLED)
->setMaker($Maker);
$app['orm.em']->persist($ProductMaker);
$app['orm.em']->flush($ProductMaker);
}
}
}
$event->setResponse($response);
}
public function onRenderAdminProductEditBefore(FilterResponseEvent $event)
{
$app = $this->app;
if (!$app->isGranted('ROLE_ADMIN')) {
return;
}
$request = $event->getRequest();
$response = $event->getResponse();
$id = $request->attributes->get('id');
list($html, $form) = $this->getHtml($request, $response, $id);
$response->setContent($html);
$event->setResponse($response);
}
public function onAdminProductEditAfter()
{
$app = $this->app;
if (!$app->isGranted('ROLE_ADMIN')) {
return;
}
$id = $app['request']->attributes->get('id');
$form = $app['form.factory']
->createBuilder('admin_product')
->getForm();
$ProductMaker = $app['eccube.plugin.maker.repository.product_maker']->find($id);
if (is_null($ProductMaker)) {
$ProductMaker = new \Plugin\Maker\Entity\ProductMaker();
}
$form->get('maker')->setData($ProductMaker->getMaker());
$form->handleRequest($app['request']);
if ('POST' === $app['request']->getMethod()) {
if ($form->isValid()) {
$maker_id = $form->get('maker')->getData();
if ($maker_id) {
// 登録・更新
$Maker = $app['eccube.plugin.maker.repository.maker']->find($maker_id);
// ※setIdはなんだか違う気がする
if ($id) {
$ProductMaker->setId($id);
}
$ProductMaker
->setMakerUrl($form->get('maker_url')->getData())
->setDelFlg(0)
->setMaker($Maker);
$app['orm.em']->persist($ProductMaker);
} else {
// 削除
// ※setIdはなんだか違う気がする
$ProductMaker->setId($id);
$app['orm.em']->remove($ProductMaker);
}
$app['orm.em']->flush();
}
}
}
private function getHtml($request, $response, $id)
{
// メーカーマスタから有効なメーカー情報を取得
$Makers = $this->app['eccube.plugin.maker.repository.maker']->findAll();
if (is_null($Makers)) {
$Makers = new \Plugin\Maker\Entity\Maker();
}
$ProductMaker = null;
if ($id) {
// 商品メーカーマスタから設定されているなメーカー情報を取得
$ProductMaker = $this->app['eccube.plugin.maker.repository.product_maker']->find($id);
}
// 商品登録・編集画面のHTMLを取得し、DOM化
$crawler = new Crawler($response->getContent());
$form = $this->app['form.factory']
->createBuilder('admin_product')
->getForm();
if ($ProductMaker) {
// 既に登録されている商品メーカー情報が設定されている場合、初期選択
$form->get('maker')->setData($ProductMaker->getMaker());
$form->get('maker_url')->setData($ProductMaker->getMakerUrl());
}
$form->handleRequest($request);
$parts = $this->app->renderView(
'Maker/View/admin/product_maker.twig',
array('form' => $form->createView())
);
// form1の最終項目に追加(レイアウトに依存
$html = $this->getHtmlFromCrawler($crawler);
try {
$oldHtml = $crawler->filter('#form1 .accordion')->last()->html();
$newHtml = $oldHtml.$parts;
$html = str_replace($oldHtml, $newHtml, $html);
} catch (\InvalidArgumentException $e) {
// no-op
}
return array($html, $form);
}
public function onRenderProductsDetailBefore(FilterResponseEvent $event)
{
$app = $this->app;
$request = $event->getRequest();
$response = $event->getResponse();
$id = $request->attributes->get('id');
$ProductMaker = null;
if ($id) {
// 商品メーカーマスタから設定されているなメーカー情報を取得
$ProductMaker = $this->app['eccube.plugin.maker.repository.product_maker']->find($id);
}
if (!$ProductMaker) {
return;
}
$Maker = $ProductMaker->getMaker();
if (is_null($Maker)) {
// 商品メーカーマスタにデータが存在しないまたは削除されていれば無視する
return;
}
// HTMLを取得し、DOM化
$crawler = new Crawler($response->getContent());
$html = $this->getHtmlFromCrawler($crawler);
if ($ProductMaker) {
$parts = $this->app->renderView(
'Maker/View/default/maker.twig',
array(
'maker_name' => $ProductMaker->getMaker()->getName(),
'maker_url' => $ProductMaker->getMakerUrl(),
)
);
try {
// ※商品コードの下に追加
$parts_item_code = $crawler->filter('.item_code')->html();
$new_html = $parts_item_code.$parts;
$html = str_replace($parts_item_code, $new_html, $html);
} catch (\InvalidArgumentException $e) {
// no-op
}
}
$response->setContent($html);
$event->setResponse($response);
}
/**
* 解析用HTMLを取得.
*
* @param Crawler $crawler
*
* @return string
*/
private function getHtmlFromCrawler(Crawler $crawler)
{
$html = '';
foreach ($crawler as $domElement) {
$domElement->ownerDocument->formatOutput = true;
$html .= $domElement->ownerDocument->saveHTML();
}
return html_entity_decode($html, ENT_NOQUOTES, 'UTF-8');
}
}