commit c73ae8f7267793f7ec5995ff3e111452697b345c
Author: Filip Jani <jsem@filek.cz>
Date:   Wed Jun 19 20:28:19 2019 +0200

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

diff --git a/app/FrontModule/component/Catalogue/CatalogueSearchResultList.latte b/app/FrontModule/component/Catalogue/CatalogueSearchResultList.latte
index d22557f..132678e 100644
--- a/app/FrontModule/component/Catalogue/CatalogueSearchResultList.latte
+++ b/app/FrontModule/component/Catalogue/CatalogueSearchResultList.latte
@@ -50,7 +50,7 @@
     </div>
 
     {if isset($row->id)}
-        {control transliterationView $row->id, $showAll}
+        {control transliterationView $row->id, $showAll, TRUE}
     {/if}
 {/snippet}
 
diff --git a/app/FrontModule/component/Transliteration/TransliterationSearchForm.php b/app/FrontModule/component/Transliteration/TransliterationSearchForm.php
index ee92ed2..9fee52e 100644
--- a/app/FrontModule/component/Transliteration/TransliterationSearchForm.php
+++ b/app/FrontModule/component/Transliteration/TransliterationSearchForm.php
@@ -50,8 +50,7 @@ class TransliterationSearchForm extends Control
     {
         $form = new Form();
 
-        $form->addText('word1', 'Word 1')
-            ->setAttribute("autofocus");
+        $form->addText('word1', 'Word 1');
         $form->addText('word2', 'Word 2');
         $form->addSelect('word2_condition', '', ESearchFormOperators::$wordSelectLabels);
         $form->addText('word3', 'Word 3');
diff --git a/app/FrontModule/component/Transliteration/TransliterationSearchResultList.php b/app/FrontModule/component/Transliteration/TransliterationSearchResultList.php
index 81eaf34..9f7624c 100644
--- a/app/FrontModule/component/Transliteration/TransliterationSearchResultList.php
+++ b/app/FrontModule/component/Transliteration/TransliterationSearchResultList.php
@@ -11,6 +11,7 @@ use App\Model\Repository\TransliterationRepository;
 use App\Model\TransliterationSearchModel;
 use App\Utils\Form;
 use App\Utils\Paginator;
+use App\Utils\WordHighlighter;
 use Nette\Application\UI\Control;
 
 class TransliterationSearchResultList extends Control
@@ -52,18 +53,24 @@ class TransliterationSearchResultList extends Control
      * @persistent
      */
     public $limit;
+    /**
+     * @var WordHighlighter
+     */
+    private $wordHighlighter;
 
     /**
      * TransliterationSearchResultList constructor.
      * @param TransliterationSearchModel $transliterationSearchModel
      * @param TransliterationRepository $transliterationRepository
      * @param LineRepository $lineRepository
+     * @param WordHighlighter $wordHighlighter
      * @throws \Nette\Application\AbortException
      */
     public function __construct(
         TransliterationSearchModel $transliterationSearchModel,
         TransliterationRepository $transliterationRepository,
-        LineRepository $lineRepository
+        LineRepository $lineRepository,
+        WordHighlighter $wordHighlighter
     )
     {
         parent::__construct();
@@ -89,6 +96,7 @@ class TransliterationSearchResultList extends Control
         {
             $this->presenter->redirect('Transliteration:search');
         }
+        $this->wordHighlighter = $wordHighlighter;
     }
 
 
@@ -99,7 +107,7 @@ class TransliterationSearchResultList extends Control
         $this->resultRows = $this->transliterationRepository->transliterationsFulltextSearch($this->searchTerms, $this->paginator->getOffset(), $this->paginator->getPageSize())->fetchAll();
         $this->paginator->setPageCount($this->totalCount);
 
-        $this->highlight();
+        $this->wordHighlighter->highlight($this->resultRows, $this->searchTerms);
         if(in_array($this->adjacentLines, EAdjacentLines::$lines) && $this->adjacentLines > 0)
         {
             foreach ($this->resultRows as &$row)
@@ -114,44 +122,6 @@ class TransliterationSearchResultList extends Control
         $this->template->render();
     }
 
-    public function highlight()
-    {
-        foreach ($this->resultRows as $resultRow)
-        {
-            //nejprve escape html znaků, které chceme nechat zobrazovat,
-            //jelikož následně přidáváme html tagy pro vyznačení a výstup v latte nemůže být escapovaný
-            $resultRow->transliteration = htmlspecialchars($resultRow->transliteration);
-
-            if(!empty($this->searchTerms['word1']))
-            {
-                $resultRow->transliteration = preg_replace(
-                    "/" . $this->transliterationRepository->prepareSearchRegExp($this->searchTerms['word1']) . "/",
-                    "<span class='found'>$0</span>",
-                    $resultRow->transliteration);
-            }
-
-            /**
-             * TODO: dořešit označování slov, když se zadají slova co se překrývají tak se tagy mezi sebou ruší,
-             * viz např. vyhledávání slov 'šu' a 'as'
-             */
-            if($this->searchTerms['word2_condition'] === ESearchFormOperators::AND || $this->searchTerms['word2_condition'] === ESearchFormOperators::OR )
-            {
-                $resultRow->transliteration = preg_replace(
-                    "/" . $this->transliterationRepository->prepareSearchRegExp($this->searchTerms['word2']) . "/",
-                    "<span class='found'>$0</span>",
-                    $resultRow->transliteration);
-            }
-
-            if($this->searchTerms['word3_condition'] === ESearchFormOperators::AND || $this->searchTerms['word3_condition'] === ESearchFormOperators::OR )
-            {
-                $resultRow->transliteration = preg_replace(
-                    "/" . $this->transliterationRepository->prepareSearchRegExp($this->searchTerms['word3']) . "/",
-                    "<span class='found'>$0</span>",
-                    $resultRow->transliteration);
-            }
-        }
-    }
-
     public function handleChangePage($page, $limit)
     {
         if($this->presenter->isAjax())
diff --git a/app/FrontModule/component/Transliteration/TransliterationView.latte b/app/FrontModule/component/Transliteration/TransliterationView.latte
index 482a6c1..80e7661 100644
--- a/app/FrontModule/component/Transliteration/TransliterationView.latte
+++ b/app/FrontModule/component/Transliteration/TransliterationView.latte
@@ -106,7 +106,7 @@
 
                     <i>{$surfaceType}</i> <br>
                     {foreach $lines as $line}
-                        {$line['line_number']}. {$line['transliteration']} <br>
+                        {$line['line_number']}. {$line['transliteration']|noescape} <br>
                     {/foreach}
                 </div>
             {/foreach}
diff --git a/app/FrontModule/component/Transliteration/TransliterationView.php b/app/FrontModule/component/Transliteration/TransliterationView.php
index 605b43f..c44de49 100644
--- a/app/FrontModule/component/Transliteration/TransliterationView.php
+++ b/app/FrontModule/component/Transliteration/TransliterationView.php
@@ -8,6 +8,8 @@ use App\Model\Repository\LineRepository;
 use App\Model\Repository\LitReferenceRepository;
 use App\Model\Repository\RevHistoryRepository;
 use App\Model\Repository\TransliterationRepository;
+use App\Model\TransliterationSearchModel;
+use App\Utils\WordHighlighter;
 use Nette\Application\UI\Control;
 
 class TransliterationView extends Control
@@ -28,11 +30,22 @@ class TransliterationView extends Control
      * @var RevHistoryRepository
      */
     private $revHistoryRepository;
+    /**
+     * @var WordHighlighter
+     */
+    private $wordHighlighter;
+    /**
+     * @var TransliterationSearchModel
+     */
+    private $transliterationSearchModel;
 
     public function __construct(TransliterationRepository $transliterationRepository,
                                 LineRepository $lineRepository,
                                 LitReferenceRepository $litReferenceRepository,
-                                RevHistoryRepository $revHistoryRepository)
+                                RevHistoryRepository $revHistoryRepository,
+                                WordHighlighter $wordHighlighter,
+                                TransliterationSearchModel $transliterationSearchModel
+    )
     {
         parent::__construct();
 
@@ -40,6 +53,8 @@ class TransliterationView extends Control
         $this->lineRepository = $lineRepository;
         $this->litReferenceRepository = $litReferenceRepository;
         $this->revHistoryRepository = $revHistoryRepository;
+        $this->wordHighlighter = $wordHighlighter;
+        $this->transliterationSearchModel = $transliterationSearchModel;
     }
 
     /**
@@ -47,9 +62,11 @@ class TransliterationView extends Control
      *
      * @param int $id : ID transliterace
      * @param bool $showAll : zobrazení katalogu i transliterací
+     * @param bool $isCatalogue : určuje jestli byla komponenta zobrazena z katalogu nebo z vyhledávání textů
+     *                            a podle toho zvýrazňuje texty
      * @throws \Nette\Application\AbortException
      */
-    public function render(int $id, bool $showAll = TRUE)
+    public function render(int $id, bool $showAll = TRUE, $isCatalogue = FALSE)
     {
         $this->template->setFile(__DIR__ . '/TransliterationView.latte');
 
@@ -64,6 +81,13 @@ class TransliterationView extends Control
         if($showAll)
         {
             $lines = $this->lineRepository->getAllLinesForTransliteration($id);
+
+            // Pokud se nejedná o vyhledávání z katalogu, tak zvýrazňujeme výsledky
+            if (!$isCatalogue)
+            {
+                $this->wordHighlighter->highlight($lines, $this->transliterationSearchModel->getSearchTerms());
+            }
+
             $lineArray = [];
             foreach ($lines as $line)
             {
diff --git a/app/config/model.neon b/app/config/model.neon
index bc30ec1..e89086e 100644
--- a/app/config/model.neon
+++ b/app/config/model.neon
@@ -27,4 +27,7 @@ services:
     #Sessions
     - App\Model\TransliterationSearchModel
     - App\Model\TransportModel
-    - App\Model\CatalogueSearchModel
\ No newline at end of file
+    - App\Model\CatalogueSearchModel
+
+    #Utils
+    - App\Utils\WordHighlighter
\ No newline at end of file
diff --git a/app/model/repository/LineRepository.php b/app/model/repository/LineRepository.php
index 074e70a..3b64024 100644
--- a/app/model/repository/LineRepository.php
+++ b/app/model/repository/LineRepository.php
@@ -37,7 +37,7 @@ class LineRepository extends Repository
                   WHERE t.id_transliteration = ?
                   ORDER BY st.sorter ASC, id_line ASC",
             $transliterationId
-        );
+        )->fetchAll();
     }
 
     /**
diff --git a/app/model/repository/TransliterationRepository.php b/app/model/repository/TransliterationRepository.php
index 7631b69..d05b09a 100644
--- a/app/model/repository/TransliterationRepository.php
+++ b/app/model/repository/TransliterationRepository.php
@@ -250,7 +250,7 @@ class TransliterationRepository extends Repository
      * @param bool $countOnly : flag pokud chceme vrátit pouze počet výsledků
      * @return \Nette\Database\ResultSet
      */
-    public function transliterationCatalogueSearch(ArrayHash $queryParams, int $offset = null, int $limit = null, bool $countOnly = false)
+    public function transliterationCatalogueSearch(ArrayHash $queryParams, int $offset = null, int $limit = null, $countOnly = false)
     {
         /**
          * @var $mc string : udává jestli je mezi vyhledávanými parametry logické AND nebo OR
diff --git a/app/utils/WordHighlighter.php b/app/utils/WordHighlighter.php
new file mode 100644
index 0000000..1161b65
--- /dev/null
+++ b/app/utils/WordHighlighter.php
@@ -0,0 +1,69 @@
+<?php
+
+
+namespace App\Utils;
+
+
+use App\Enum\ESearchFormOperators;
+use App\Model\Repository\LineRepository;
+use App\Model\Repository\TransliterationRepository;
+use Nette\Database\IRow;
+
+class WordHighlighter
+{
+    /**
+     * @var TransliterationRepository
+     */
+    private $transliterationRepository;
+
+    public function __construct(TransliterationRepository $transliterationRepository)
+    {
+        $this->transliterationRepository = $transliterationRepository;
+    }
+
+    /**
+     * Zvýrazňuje výsledky při vyhledávání textů
+     *
+     * @param IRow[] $resultRows : pole s nalezenými lines
+     * @param null $searchTerms : vyhledávané položky
+     */
+    public function highlight(&$resultRows, $searchTerms = NULL)
+    {
+        foreach ($resultRows as $resultRow)
+        {
+            //nejprve escape html znaků, které chceme nechat zobrazovat,
+            //jelikož následně přidáváme html tagy pro vyznačení a výstup v latte nemůže být escapovaný
+            $resultRow->{LineRepository::COLUMN_TRANSLITERATION} = htmlspecialchars($resultRow->{LineRepository::COLUMN_TRANSLITERATION});
+
+            if (isset($searchTerms['word1']) && !empty($searchTerms['word1']))
+            {
+                $resultRow->{LineRepository::COLUMN_TRANSLITERATION} = preg_replace(
+                    "/" . $this->transliterationRepository->prepareSearchRegExp($searchTerms['word1']) . "/",
+                    "<span class='found'>$0</span>",
+                    $resultRow->{LineRepository::COLUMN_TRANSLITERATION});
+            }
+
+            /**
+             * TODO: dořešit označování slov, když se zadají slova co se překrývají tak se tagy mezi sebou ruší,
+             * viz např. vyhledávání slov 'šu' a 'as'
+             */
+            if (isset($searchTerms['word2']) && isset($searchTerms['word2_condition']) &&
+                ($searchTerms['word2_condition'] === ESearchFormOperators:: AND || $searchTerms['word2_condition'] === ESearchFormOperators:: OR))
+            {
+                $resultRow->{LineRepository::COLUMN_TRANSLITERATION} = preg_replace(
+                    "/" . $this->transliterationRepository->prepareSearchRegExp($searchTerms['word2']) . "/",
+                    "<span class='found'>$0</span>",
+                    $resultRow->{LineRepository::COLUMN_TRANSLITERATION});
+            }
+
+            if (isset($searchTerms['word3']) && isset($searchTerms['word3_condition']) &&
+                $searchTerms['word3_condition'] === ESearchFormOperators:: AND || $searchTerms['word3_condition'] === ESearchFormOperators:: OR)
+            {
+                $resultRow->{LineRepository::COLUMN_TRANSLITERATION} = preg_replace(
+                    "/" . $this->transliterationRepository->prepareSearchRegExp($searchTerms['word3']) . "/",
+                    "<span class='found'>$0</span>",
+                    $resultRow->{LineRepository::COLUMN_TRANSLITERATION});
+            }
+        }
+    }
+}
\ No newline at end of file
