1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\NodeSearchBundle\Services; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\NodeBundle\Entity\NodeTranslation; |
6
|
|
|
use Kunstmaan\NodeBundle\Entity\PageInterface; |
7
|
|
|
use Kunstmaan\NodeBundle\Helper\RenderContext; |
8
|
|
|
use Kunstmaan\NodeSearchBundle\Helper\IndexablePagePartsService; |
9
|
|
|
use Kunstmaan\NodeSearchBundle\Helper\SearchViewTemplateInterface; |
10
|
|
|
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
12
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
13
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
14
|
|
|
use Twig\Environment; |
15
|
|
|
|
16
|
|
|
class SearchViewRenderer |
17
|
|
|
{ |
18
|
|
|
/** @var Environment */ |
19
|
|
|
private $twig; |
20
|
|
|
/** @var IndexablePagePartsService */ |
21
|
|
|
private $indexablePagePartsService; |
22
|
|
|
/** @var RequestStack */ |
23
|
|
|
private $requestStack; |
24
|
|
|
|
25
|
4 |
|
public function __construct(Environment $twig, IndexablePagePartsService $indexablePagePartsService, RequestStack $requestStack) |
26
|
|
|
{ |
27
|
4 |
|
$this->twig = $twig; |
28
|
4 |
|
$this->indexablePagePartsService = $indexablePagePartsService; |
29
|
4 |
|
$this->requestStack = $requestStack; |
30
|
4 |
|
} |
31
|
|
|
|
32
|
|
|
public function renderDefaultSearchView(NodeTranslation $nodeTranslation, HasPagePartsInterface $page, string $defaultView = '@KunstmaanNodeSearch/PagePart/view.html.twig') |
33
|
|
|
{ |
34
|
|
|
$html = $this->twig->render($defaultView, [ |
35
|
|
|
'locale' => $nodeTranslation->getLang(), |
36
|
|
|
'page' => $page, |
37
|
|
|
'pageparts' => $this->indexablePagePartsService->getIndexablePageParts($page), |
38
|
|
|
'indexMode' => true, |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
return $this->removeHtml($html); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function renderCustomSearchView(NodeTranslation $nodeTranslation, SearchViewTemplateInterface $page, ContainerInterface $container = null) |
45
|
|
|
{ |
46
|
|
|
$renderContext = new RenderContext([ |
47
|
|
|
'locale' => $nodeTranslation->getLang(), |
48
|
|
|
'page' => $page, |
49
|
|
|
'indexMode' => true, |
50
|
|
|
'nodetranslation' => $nodeTranslation, |
51
|
|
|
]); |
52
|
|
|
|
53
|
|
|
if ($page instanceof PageInterface && null !== $container) { |
54
|
|
|
$page->service($container, $this->requestStack->getCurrentRequest(), $renderContext); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$html = $this->twig->render($page->getSearchView(), $renderContext->getArrayCopy()); |
58
|
|
|
|
59
|
|
|
return $this->removeHtml($html); |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
public function removeHtml(string $text): string |
63
|
|
|
{ |
64
|
4 |
|
if (empty(trim($text))) { |
65
|
|
|
return ''; |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
$crawler = new Crawler(); |
69
|
4 |
|
$crawler->addHtmlContent($text); |
70
|
|
|
$crawler->filter('style, script')->each(function (Crawler $crawler) { |
71
|
|
|
foreach ($crawler as $node) { |
72
|
|
|
$node->parentNode->removeChild($node); |
73
|
|
|
} |
74
|
4 |
|
}); |
75
|
4 |
|
$text = $crawler->html(); |
76
|
|
|
|
77
|
4 |
|
$result = strip_tags($text); |
78
|
4 |
|
$result = trim(html_entity_decode($result, ENT_QUOTES)); |
79
|
|
|
|
80
|
4 |
|
return $result; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|