1
|
<?php
|
2
|
|
3
|
|
4
|
namespace App\Utils;
|
5
|
|
6
|
|
7
|
use App\Enum\ESearchFormOperators;
|
8
|
use App\Model\Repository\LineRepository;
|
9
|
use App\Model\Repository\TransliterationRepository;
|
10
|
use Nette\Database\IRow;
|
11
|
|
12
|
class WordHighlighter
|
13
|
{
|
14
|
/**
|
15
|
* @var TransliterationRepository
|
16
|
*/
|
17
|
private $transliterationRepository;
|
18
|
|
19
|
public function __construct(TransliterationRepository $transliterationRepository)
|
20
|
{
|
21
|
$this->transliterationRepository = $transliterationRepository;
|
22
|
}
|
23
|
|
24
|
/**
|
25
|
* Zvýrazňuje výsledky při vyhledávání textů
|
26
|
*
|
27
|
* @param IRow[] $resultRows : pole s nalezenými lines
|
28
|
* @param null $searchTerms : vyhledávané položky
|
29
|
*/
|
30
|
public function highlight(&$resultRows, $searchTerms = NULL)
|
31
|
{
|
32
|
foreach ($resultRows as $resultRow)
|
33
|
{
|
34
|
//nejprve escape html znaků, které chceme nechat zobrazovat,
|
35
|
//jelikož následně přidáváme html tagy pro vyznačení a výstup v latte nemůže být escapovaný
|
36
|
$resultRow->{LineRepository::COLUMN_TRANSLITERATION} = htmlspecialchars($resultRow->{LineRepository::COLUMN_TRANSLITERATION});
|
37
|
|
38
|
if (isset($searchTerms['word1']) && !empty($searchTerms['word1']))
|
39
|
{
|
40
|
$resultRow->{LineRepository::COLUMN_TRANSLITERATION} = preg_replace(
|
41
|
"/" . $this->transliterationRepository->prepareSearchRegExp($searchTerms['word1']) . "/",
|
42
|
"<span class='found'>$0</span>",
|
43
|
$resultRow->{LineRepository::COLUMN_TRANSLITERATION});
|
44
|
}
|
45
|
|
46
|
/**
|
47
|
* TODO: dořešit označování slov, když se zadají slova co se překrývají tak se tagy mezi sebou ruší,
|
48
|
* viz např. vyhledávání slov 'šu' a 'as'
|
49
|
*/
|
50
|
if (isset($searchTerms['word2']) && isset($searchTerms['word2_condition']) &&
|
51
|
($searchTerms['word2_condition'] === ESearchFormOperators:: AND || $searchTerms['word2_condition'] === ESearchFormOperators:: OR))
|
52
|
{
|
53
|
$resultRow->{LineRepository::COLUMN_TRANSLITERATION} = preg_replace(
|
54
|
"/" . $this->transliterationRepository->prepareSearchRegExp($searchTerms['word2']) . "/",
|
55
|
"<span class='found'>$0</span>",
|
56
|
$resultRow->{LineRepository::COLUMN_TRANSLITERATION});
|
57
|
}
|
58
|
|
59
|
if (isset($searchTerms['word3']) && isset($searchTerms['word3_condition']) &&
|
60
|
$searchTerms['word3_condition'] === ESearchFormOperators:: AND || $searchTerms['word3_condition'] === ESearchFormOperators:: OR)
|
61
|
{
|
62
|
$resultRow->{LineRepository::COLUMN_TRANSLITERATION} = preg_replace(
|
63
|
"/" . $this->transliterationRepository->prepareSearchRegExp($searchTerms['word3']) . "/",
|
64
|
"<span class='found'>$0</span>",
|
65
|
$resultRow->{LineRepository::COLUMN_TRANSLITERATION});
|
66
|
}
|
67
|
}
|
68
|
}
|
69
|
}
|