forked from EC-CUBE/category-content-plugin
/
CategoryContent.php
154 lines (122 loc) · 4.65 KB
/
CategoryContent.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
<?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\CategoryContent;
use Eccube\Event\RenderEvent;
use Eccube\Event\ShoppingEvent;
use Symfony\Component\CssSelector\CssSelector;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
class CategoryContent
{
private $app;
public function __construct($app)
{
$this->app = $app;
}
public function onRenderProductListBefore(FilterResponseEvent $event)
{
$app = $this->app;
$request = $event->getRequest();
$response = $event->getResponse();
$id = $request->query->get('category_id');
// category_idがない場合、レンダリングを変更しない
if (is_null($id)) {
return;
}
$CategoryContent = $app['category_content.repository.category_content']
->findOneBy(array('category_id' => $id));
// 登録がない、もしくは空で登録されている場合、レンダリングを変更しない
if (is_null($CategoryContent) || $CategoryContent->getContent() == '') {
return;
}
// 書き換えhtmlの初期化
$html = $response->getContent();
libxml_use_internal_errors(true);
$dom = new \DOMDocument();
$dom->loadHTML('<?xml encoding="UTF-8">' . $html);
$dom->encoding = "UTF-8";
$dom->formatOutput = true;
// 挿入対象を取得
$navElement = $dom->getElementById('page_navi_top');
if (!$navElement instanceof \DOMElement) {
return;
}
$template = $dom->createDocumentFragment();
$template->appendXML($CategoryContent->getContent());
$node = $dom->importNode($template, true);
$navElement->insertBefore($node);
$response->setContent($dom->saveHTML());
$event->setResponse($response);
}
public function onRenderAdminProductCategoryEditBefore(FilterResponseEvent $event)
{
$app = $this->app;
$request = $event->getRequest();
$response = $event->getResponse();
$id = $request->attributes->get('id');
// DomCrawlerにHTMLを食わせる
$html = $response->getContent();
$crawler = new Crawler($html);
$CategoryContent = $app['category_content.repository.category_content']->find($id);
if (is_null($CategoryContent)) {
$CategoryContent = new \Plugin\CategoryContent\Entity\CategoryContent();
}
$form = $app['form.factory']
->createBuilder('admin_category')
->getForm();
$form->get('content')->setData($CategoryContent->getContent());
$form->handleRequest($request);
$twig = $app->renderView(
'CategoryContent/Resource/template/Admin/category.twig',
array('form' => $form->createView())
);
$oldCrawler = $crawler
->filter('form')
->first();
// DomCrawlerからHTMLを吐き出す
$html = $crawler->html();
$oldHtml = '';
$newHtml = '';
if (count($oldCrawler) > 0) {
$oldHtml = $oldCrawler->html();
$newHtml = $oldHtml . $twig;
}
$html = str_replace($oldHtml, $newHtml, $html);
$response->setContent($html);
$event->setResponse($response);
}
public function onAdminProductCategoryEditAfter()
{
$app = $this->app;
$id = $app['request']->attributes->get('id');
$form = $app['form.factory']
->createBuilder('admin_category')
->getForm();
$CategoryContent = $app['category_content.repository.category_content']->find($id);
if (is_null($CategoryContent)) {
$CategoryContent = new \Plugin\CategoryContent\Entity\CategoryContent();
}
$form->get('content')->setData($CategoryContent->getContent());
$form->handleRequest($app['request']);
if ('POST' === $app['request']->getMethod()) {
if ($form->isValid()) {
$content = $form->get('content')->getData();
$Category = $app['eccube.repository.category']->find($id);
$CategoryContent
->setCategoryId($Category->getId())
->setCategory($Category)
->setContent($content);
$app['orm.em']->persist($CategoryContent);
$app['orm.em']->flush();
}
}
}
}