Projekt

Obecné

Profil

« Předchozí | Další » 

Revize c73ae8f7

Přidáno uživatelem Filip Jani před téměř 6 roky(ů)

Zvýrazňování hledaných slov i v detailu transliterace

Zobrazit rozdíly:

app/FrontModule/component/Catalogue/CatalogueSearchResultList.latte
50 50
    </div>
51 51

  
52 52
    {if isset($row->id)}
53
        {control transliterationView $row->id, $showAll}
53
        {control transliterationView $row->id, $showAll, TRUE}
54 54
    {/if}
55 55
{/snippet}
56 56

  
app/FrontModule/component/Transliteration/TransliterationSearchForm.php
50 50
    {
51 51
        $form = new Form();
52 52

  
53
        $form->addText('word1', 'Word 1')
54
            ->setAttribute("autofocus");
53
        $form->addText('word1', 'Word 1');
55 54
        $form->addText('word2', 'Word 2');
56 55
        $form->addSelect('word2_condition', '', ESearchFormOperators::$wordSelectLabels);
57 56
        $form->addText('word3', 'Word 3');
app/FrontModule/component/Transliteration/TransliterationSearchResultList.php
11 11
use App\Model\TransliterationSearchModel;
12 12
use App\Utils\Form;
13 13
use App\Utils\Paginator;
14
use App\Utils\WordHighlighter;
14 15
use Nette\Application\UI\Control;
15 16

  
16 17
class TransliterationSearchResultList extends Control
......
52 53
     * @persistent
53 54
     */
54 55
    public $limit;
56
    /**
57
     * @var WordHighlighter
58
     */
59
    private $wordHighlighter;
55 60

  
56 61
    /**
57 62
     * TransliterationSearchResultList constructor.
58 63
     * @param TransliterationSearchModel $transliterationSearchModel
59 64
     * @param TransliterationRepository $transliterationRepository
60 65
     * @param LineRepository $lineRepository
66
     * @param WordHighlighter $wordHighlighter
61 67
     * @throws \Nette\Application\AbortException
62 68
     */
63 69
    public function __construct(
64 70
        TransliterationSearchModel $transliterationSearchModel,
65 71
        TransliterationRepository $transliterationRepository,
66
        LineRepository $lineRepository
72
        LineRepository $lineRepository,
73
        WordHighlighter $wordHighlighter
67 74
    )
68 75
    {
69 76
        parent::__construct();
......
89 96
        {
90 97
            $this->presenter->redirect('Transliteration:search');
91 98
        }
99
        $this->wordHighlighter = $wordHighlighter;
92 100
    }
93 101

  
94 102

  
......
99 107
        $this->resultRows = $this->transliterationRepository->transliterationsFulltextSearch($this->searchTerms, $this->paginator->getOffset(), $this->paginator->getPageSize())->fetchAll();
100 108
        $this->paginator->setPageCount($this->totalCount);
101 109

  
102
        $this->highlight();
110
        $this->wordHighlighter->highlight($this->resultRows, $this->searchTerms);
103 111
        if(in_array($this->adjacentLines, EAdjacentLines::$lines) && $this->adjacentLines > 0)
104 112
        {
105 113
            foreach ($this->resultRows as &$row)
......
114 122
        $this->template->render();
115 123
    }
116 124

  
117
    public function highlight()
118
    {
119
        foreach ($this->resultRows as $resultRow)
120
        {
121
            //nejprve escape html znaků, které chceme nechat zobrazovat,
122
            //jelikož následně přidáváme html tagy pro vyznačení a výstup v latte nemůže být escapovaný
123
            $resultRow->transliteration = htmlspecialchars($resultRow->transliteration);
124

  
125
            if(!empty($this->searchTerms['word1']))
126
            {
127
                $resultRow->transliteration = preg_replace(
128
                    "/" . $this->transliterationRepository->prepareSearchRegExp($this->searchTerms['word1']) . "/",
129
                    "<span class='found'>$0</span>",
130
                    $resultRow->transliteration);
131
            }
132

  
133
            /**
134
             * TODO: dořešit označování slov, když se zadají slova co se překrývají tak se tagy mezi sebou ruší,
135
             * viz např. vyhledávání slov 'šu' a 'as'
136
             */
137
            if($this->searchTerms['word2_condition'] === ESearchFormOperators::AND || $this->searchTerms['word2_condition'] === ESearchFormOperators::OR )
138
            {
139
                $resultRow->transliteration = preg_replace(
140
                    "/" . $this->transliterationRepository->prepareSearchRegExp($this->searchTerms['word2']) . "/",
141
                    "<span class='found'>$0</span>",
142
                    $resultRow->transliteration);
143
            }
144

  
145
            if($this->searchTerms['word3_condition'] === ESearchFormOperators::AND || $this->searchTerms['word3_condition'] === ESearchFormOperators::OR )
146
            {
147
                $resultRow->transliteration = preg_replace(
148
                    "/" . $this->transliterationRepository->prepareSearchRegExp($this->searchTerms['word3']) . "/",
149
                    "<span class='found'>$0</span>",
150
                    $resultRow->transliteration);
151
            }
152
        }
153
    }
154

  
155 125
    public function handleChangePage($page, $limit)
156 126
    {
157 127
        if($this->presenter->isAjax())
app/FrontModule/component/Transliteration/TransliterationView.latte
106 106

  
107 107
                    <i>{$surfaceType}</i> <br>
108 108
                    {foreach $lines as $line}
109
                        {$line['line_number']}. {$line['transliteration']} <br>
109
                        {$line['line_number']}. {$line['transliteration']|noescape} <br>
110 110
                    {/foreach}
111 111
                </div>
112 112
            {/foreach}
app/FrontModule/component/Transliteration/TransliterationView.php
8 8
use App\Model\Repository\LitReferenceRepository;
9 9
use App\Model\Repository\RevHistoryRepository;
10 10
use App\Model\Repository\TransliterationRepository;
11
use App\Model\TransliterationSearchModel;
12
use App\Utils\WordHighlighter;
11 13
use Nette\Application\UI\Control;
12 14

  
13 15
class TransliterationView extends Control
......
28 30
     * @var RevHistoryRepository
29 31
     */
30 32
    private $revHistoryRepository;
33
    /**
34
     * @var WordHighlighter
35
     */
36
    private $wordHighlighter;
37
    /**
38
     * @var TransliterationSearchModel
39
     */
40
    private $transliterationSearchModel;
31 41

  
32 42
    public function __construct(TransliterationRepository $transliterationRepository,
33 43
                                LineRepository $lineRepository,
34 44
                                LitReferenceRepository $litReferenceRepository,
35
                                RevHistoryRepository $revHistoryRepository)
45
                                RevHistoryRepository $revHistoryRepository,
46
                                WordHighlighter $wordHighlighter,
47
                                TransliterationSearchModel $transliterationSearchModel
48
    )
36 49
    {
37 50
        parent::__construct();
38 51

  
......
40 53
        $this->lineRepository = $lineRepository;
41 54
        $this->litReferenceRepository = $litReferenceRepository;
42 55
        $this->revHistoryRepository = $revHistoryRepository;
56
        $this->wordHighlighter = $wordHighlighter;
57
        $this->transliterationSearchModel = $transliterationSearchModel;
43 58
    }
44 59

  
45 60
    /**
......
47 62
     *
48 63
     * @param int $id : ID transliterace
49 64
     * @param bool $showAll : zobrazení katalogu i transliterací
65
     * @param bool $isCatalogue : určuje jestli byla komponenta zobrazena z katalogu nebo z vyhledávání textů
66
     *                            a podle toho zvýrazňuje texty
50 67
     * @throws \Nette\Application\AbortException
51 68
     */
52
    public function render(int $id, bool $showAll = TRUE)
69
    public function render(int $id, bool $showAll = TRUE, $isCatalogue = FALSE)
53 70
    {
54 71
        $this->template->setFile(__DIR__ . '/TransliterationView.latte');
55 72

  
......
64 81
        if($showAll)
65 82
        {
66 83
            $lines = $this->lineRepository->getAllLinesForTransliteration($id);
84

  
85
            // Pokud se nejedná o vyhledávání z katalogu, tak zvýrazňujeme výsledky
86
            if (!$isCatalogue)
87
            {
88
                $this->wordHighlighter->highlight($lines, $this->transliterationSearchModel->getSearchTerms());
89
            }
90

  
67 91
            $lineArray = [];
68 92
            foreach ($lines as $line)
69 93
            {
app/config/model.neon
27 27
    #Sessions
28 28
    - App\Model\TransliterationSearchModel
29 29
    - App\Model\TransportModel
30
    - App\Model\CatalogueSearchModel
30
    - App\Model\CatalogueSearchModel
31

  
32
    #Utils
33
    - App\Utils\WordHighlighter
app/model/repository/LineRepository.php
37 37
                  WHERE t.id_transliteration = ?
38 38
                  ORDER BY st.sorter ASC, id_line ASC",
39 39
            $transliterationId
40
        );
40
        )->fetchAll();
41 41
    }
42 42

  
43 43
    /**
app/model/repository/TransliterationRepository.php
250 250
     * @param bool $countOnly : flag pokud chceme vrátit pouze počet výsledků
251 251
     * @return \Nette\Database\ResultSet
252 252
     */
253
    public function transliterationCatalogueSearch(ArrayHash $queryParams, int $offset = null, int $limit = null, bool $countOnly = false)
253
    public function transliterationCatalogueSearch(ArrayHash $queryParams, int $offset = null, int $limit = null, $countOnly = false)
254 254
    {
255 255
        /**
256 256
         * @var $mc string : udává jestli je mezi vyhledávanými parametry logické AND nebo OR
app/utils/WordHighlighter.php
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
}

Také k dispozici: Unified diff