forked from EC-CUBE/listing-ad-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListingAdCsvLegacyEvent.php
100 lines (85 loc) · 2.63 KB
/
ListingAdCsvLegacyEvent.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
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) 2000-2016 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\ListingAdCsv;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
class ListingAdCsvLegacyEvent
{
/** @var \Eccube\Application $app */
private $app;
/**
* GiftWrappingEvent constructor.
* @param \Eccube\Application $app
*/
public function __construct($app)
{
$this->app = $app;
}
/**
* @param FilterResponseEvent $event
*/
public function onRenderAdminProductBefore(FilterResponseEvent $event)
{
$request = $event->getRequest();
$response = $event->getResponse();
$html = $this->getHtmlWrapping($request, $response);
$response->setContent($html);
$event->setResponse($response);
}
/**
* HTMLの加工
*
* @param Request $request
* @param Response $response
* @return string
*/
private function getHtmlWrapping($request, $response)
{
$crawler = new Crawler($response->getContent());
$html = $this->getHtml($crawler);
/** @var FormFactory $formFactory */
// $formFactory = $this->app['form.factory'];
// $form = $formFactory->createBuilder('shopping')->getForm();
//
$parts = $this->app->renderView('ListingAdCsv\Resource\template\Admin\dropdown_parts.twig');
// TODO 挿入位置の指定方法、もっとよい方法ないのか?
try {
$crawler = $crawler->filter('ul.sort-dd');
if ($crawler->count() != 0) {
$oldHtml = $crawler->last()->html();
$newHtml = $oldHtml . $parts;
$html = str_replace($oldHtml, $newHtml, $html);
}
} catch (\InvalidArgumentException $e) {
// ignore
}
return $html;
}
/**
* 解析用HTMLを取得
*
* @param Crawler $crawler
* @return string
*/
private function getHtml(Crawler $crawler)
{
$html = '';
/** @var \DOMElement $domElement */
foreach ($crawler as $domElement) {
$domElement->ownerDocument->formatOutput = true;
$html .= $domElement->ownerDocument->saveHTML();
}
return html_entity_decode($html, ENT_NOQUOTES, 'UTF-8');
}
}