commit 42c20ae606500d619e1e12c8d7d521de9d7e96d6
Author: Jan Šedivý <honza.seeda@gmail.com>
Date:   Sun Jun 9 21:07:13 2019 +0200

    Re #7575 , Re #7572 Refaktoring importu/exportu

diff --git a/app/AdminModule/component/Transliteration/TransliterationGrid.php b/app/AdminModule/component/Transliteration/TransliterationGrid.php
index d0251c2..2e6ff46 100644
--- a/app/AdminModule/component/Transliteration/TransliterationGrid.php
+++ b/app/AdminModule/component/Transliteration/TransliterationGrid.php
@@ -15,6 +15,7 @@ use App\Model\Repository\TransliterationRepository;
 use App\Utils\DataGrid\DataGrid;
 use Nette\Database\Table\ActiveRow;
 use Nette\Database\Table\Selection;
+use Tracy\Debugger;
 use Ublaboo\DataGrid\Exception\DataGridException;
 
 class TransliterationGrid extends DataGrid
@@ -249,30 +250,37 @@ class TransliterationGrid extends DataGrid
     private function exportData($dataSource, $grid)
     {
         $exportData = [];
-        /** @var ActiveRow $activeRow */
-        foreach ($dataSource as $activeRow)
+
+        try
         {
-            $lines = $this->lineRepository->getAllLinesForTransliteration($activeRow->getPrimary());
-            $chapterLines = [];
-            foreach ($lines as $line)
+            /** @var ActiveRow $activeRow */
+            foreach ($dataSource as $activeRow)
             {
-                $chapterLines[$line->object_type][$line->surface_type][] = array('transliteration' => $line->transliteration, 'line_number' => $line->line_number);
+                //Přeskočí transliterace které nejsou přiřazené k žádné knížce
+                if(!$activeRow->ref('id_book'))
+                {
+                    continue;
+                }
+                $lines = $this->lineRepository->getAllLinesForTransliteration($activeRow->getPrimary());
+                $chapterLines = [];
+                foreach ($lines as $line)
+                {
+                    $chapterLines[$line->object_type][$line->surface_type][] = array('transliteration' => $line->transliteration, 'line_number' => $line->line_number);
+                }
+                $exportData[] = [
+                    'bookName' => $activeRow->ref('id_book')->book_abrev,
+                    'chapter' => $activeRow->chapter,
+                    'transliterationData' => $chapterLines
+                ];
             }
-            $exportData[] = [
-                'bookName' => $activeRow->ref('id_book')->book_abrev,
-                'chapter' => $activeRow->chapter,
-                'transliterationData' => $chapterLines
-            ];
-        }
 
-        $uniqueBooks = array_unique(array_map(function ($i) { return $i['bookName']; }, $exportData));
+            $uniqueBooks = array_unique(array_map(function ($i) { return $i['bookName']; }, $exportData));
+
+            $currentDate = new \DateTime();
+            $exportFileName =  'text-export-' .
+                $currentDate->format('Y-m-d-H-i-s') .
+                self::EXPORT_FILE_TYPE;
 
-        $currentDate = new \DateTime();
-        $exportFileName =  'text-export-' .
-            $currentDate->format('Y-m-d-H-i-s') .
-            self::EXPORT_FILE_TYPE;
-        try
-        {
             if (!file_exists(self::EXPORT_FILE_PATH)) {
                 mkdir(self::EXPORT_FILE_PATH, 0777, true);
             }
@@ -321,6 +329,7 @@ class TransliterationGrid extends DataGrid
         catch (\Exception $e)
         {
             $this->presenter->flashMessage('There was an error creating the export file', EFlashMessage::ERROR);
+            Debugger::log($e, 'export-error');
         }
 
         $this->presenter->flashMessage('There was an error downloading the export file', EFlashMessage::ERROR);
diff --git a/app/AdminModule/component/Transport/ExportForm.latte b/app/AdminModule/component/Transport/ExportForm.latte
deleted file mode 100644
index 98189dd..0000000
--- a/app/AdminModule/component/Transport/ExportForm.latte
+++ /dev/null
@@ -1 +0,0 @@
-{control form}
diff --git a/app/AdminModule/component/Transport/ExportForm.php b/app/AdminModule/component/Transport/ExportForm.php
deleted file mode 100644
index f1c8e27..0000000
--- a/app/AdminModule/component/Transport/ExportForm.php
+++ /dev/null
@@ -1,39 +0,0 @@
-<?php
-
-namespace App\AdminModule\Components;
-
-use App\Utils\Form;
-use Nette\Application\UI\Control;
-
-class ExportForm extends Control
-{
-
-    public function __construct()
-    {
-        parent::__construct();
-    }
-
-
-    public function render()
-    {
-        $this->template->setFile(__DIR__ . '/ExportForm.latte');
-        $this->template->render();
-    }
-
-    public function createComponentForm()
-    {
-        $form = new Form();
-
-        $form->addButton('export', 'Export něco...');
-
-        return $form;
-    }
-}
-
-interface IExportFormFactory
-{
-    /**
-     * @return ExportForm
-     */
-    public function create();
-}
\ No newline at end of file
diff --git a/app/AdminModule/presenters/TransportPresenter.php b/app/AdminModule/presenters/TransportPresenter.php
index 66ece7b..a950cc4 100644
--- a/app/AdminModule/presenters/TransportPresenter.php
+++ b/app/AdminModule/presenters/TransportPresenter.php
@@ -16,9 +16,6 @@ class TransportPresenter extends BaseUserPresenter
     /** @var IImportFormFactory */
     private $importFormFactory;
 
-    /** @var IExportFormFactory */
-    private $exportFormFactory;
-
     /** @var SurfaceTypeRepository */
     private $surfaceTypeRepository;
 
@@ -32,7 +29,6 @@ class TransportPresenter extends BaseUserPresenter
     private $transportModel;
 
     public function __construct(IImportFormFactory $importFormFactory,
-                                IExportFormFactory $exportFormFactory,
                                 SurfaceTypeRepository $surfaceTypeRepository,
                                 ObjectTypeRepository $objectTypeRepository,
                                 TransportFacade $transportFacade,
@@ -41,7 +37,6 @@ class TransportPresenter extends BaseUserPresenter
     {
         parent::__construct();
         $this->importFormFactory = $importFormFactory;
-        $this->exportFormFactory = $exportFormFactory;
         $this->surfaceTypeRepository = $surfaceTypeRepository;
         $this->objectTypeRepository = $objectTypeRepository;
         $this->transportFacade = $transportFacade;
@@ -99,9 +94,4 @@ class TransportPresenter extends BaseUserPresenter
     {
         return $this->importFormFactory->create();
     }
-
-    public function createComponentExportForm()
-    {
-        return $this->exportFormFactory->create();
-    }
 }
\ No newline at end of file
diff --git a/app/AdminModule/templates/Transport/default.latte b/app/AdminModule/templates/Transport/default.latte
index e862d85..3c386f9 100644
--- a/app/AdminModule/templates/Transport/default.latte
+++ b/app/AdminModule/templates/Transport/default.latte
@@ -10,12 +10,7 @@
         </div>
     </div>
 
-{*    <div class="row mt-5">*}
-{*        <div class="col-3">*}
-{*            <fieldset>*}
-{*                <legend>Text export</legend>*}
-{*                {control exportForm}*}
-{*            </fieldset>*}
-{*        </div>*}
-{*    </div>*}
+    <div class="row mt-5">
+        <span>You can export selected texts using the export button in <a n:href="Transliteration:">Transliteration list</a> (Top right corner of the list).</span>
+    </div>
 {/block}
\ No newline at end of file
diff --git a/app/config/components.neon b/app/config/components.neon
index 8f81f7e..803f277 100644
--- a/app/config/components.neon
+++ b/app/config/components.neon
@@ -23,7 +23,6 @@ services:
     - App\AdminModule\Components\ITransliterationEditFormFactory
     - App\AdminModule\Components\ITransliterationDataEditFormFactory
     - App\AdminModule\Components\IImportFormFactory
-    - App\AdminModule\Components\IExportFormFactory
 
     - App\FrontModule\Components\IKeyboard
     - App\FrontModule\Components\ITransliterationSearchResultListFactory
diff --git a/app/enum/ESurfaceTypes.php b/app/enum/ESurfaceTypes.php
index 67bcc66..e1bc092 100644
--- a/app/enum/ESurfaceTypes.php
+++ b/app/enum/ESurfaceTypes.php
@@ -45,4 +45,14 @@ class ESurfaceTypes
 
         return "&" . self::$exportFileAbbrev[$type] . "$";
     }
+
+    public static function getSurfaceTypeFromAbbrev($abbrev)
+    {
+        $type = array_search($abbrev, self::$exportFileAbbrev);
+        if(!$type)
+        {
+            $type = self::OBVERSE;
+        }
+        return $type;
+    }
 }
\ No newline at end of file
diff --git a/app/model/facade/TransportFacade.php b/app/model/facade/TransportFacade.php
index 20c09bf..aeb3485 100644
--- a/app/model/facade/TransportFacade.php
+++ b/app/model/facade/TransportFacade.php
@@ -64,15 +64,7 @@ class TransportFacade
                 {
                     $bookId = $this->bookRepository->insert([BookRepository::COLUMN_BOOK_ABREV => $book])->getPrimary();
                 }
-            }
-            catch (\Exception $exception)
-            {
-                \Tracy\Debugger::log('Nepodařila se uložit nová kniha. Chyba: ' . $exception->getMessage(), 'transport-facade');
-                return FALSE;
-            }
-            foreach ($chapters as $chapter => $surfaces)
-            {
-                try
+                foreach ($chapters as $chapter => $surfaces)
                 {
                     $transliterationId = $this->getTransliterationIdCurrentBookChapter($bookId, trim(explode(" ", $chapter)[0]));
                     if (!$transliterationId)
@@ -81,30 +73,32 @@ class TransportFacade
                         if (isset($chapterReg[1]))
                         {
                             $chapterReg[1] = str_replace(array('(', ')'), '', $chapterReg[1]);
-                            $this->transliterationRepository->insert([
+                            $transliteration = $this->transliterationRepository->insert([
                                 TransliterationRepository::COLUMN_BOOK_ID => $bookId,
                                 TransliterationRepository::COLUMN_CHAPTER => $chapterReg[0],
                                 TransliterationRepository::COLUMN_REG_NO => $chapterReg[1]
                             ]);
-                            $transliterationId = $this->context->getInsertId(TransliterationRepository::COLUMN_ID);
+                            if($transliteration)
+                            {
+                                $transliterationId = $transliteration->getPrimary();
+                            }
+                            else continue;
+//                            $transliterationId = $this->context->getInsertId(TransliterationRepository::COLUMN_ID);
                         } else
                         {
-                            $this->transliterationRepository->insert([
+                            $transliteration = $this->transliterationRepository->insert([
                                 TransliterationRepository::COLUMN_BOOK_ID => $bookId,
                                 TransliterationRepository::COLUMN_CHAPTER => $chapterReg[0]
                             ]);
-                            $transliterationId = $this->context->getInsertId(TransliterationRepository::COLUMN_ID);
+                            if($transliteration)
+                            {
+                                $transliterationId = $transliteration->getPrimary();
+                            }
+                            else continue;
+//                            $transliterationId = $this->context->getInsertId(TransliterationRepository::COLUMN_ID);
                         }
                     }
-                }
-                catch (\Exception $exception)
-                {
-                    \Tracy\Debugger::log('Nepodařila se uložit nová transliterace. Chyba: ' . $exception->getMessage(), 'transport-facade');
-                    return FALSE;
-                }
-                foreach ($surfaces as $surface => $values)
-                {
-                    try
+                    foreach ($surfaces as $surface => $values)
                     {
                         $surfaceId = $this->getSurfaceIdByCurrentTransSurfType($transliterationId, $surface);
                         if (!$surfaceId)
@@ -115,16 +109,8 @@ class TransportFacade
                                 SurfaceRepository::COLUMN_OBJECT_TYPE_ID => 1
                             ])->getPrimary();
                         }
-                    }
-                    catch (\Exception $exception)
-                    {
-                        \Tracy\Debugger::log('Nepodařil se uložit nový surface. Chyba: ' . $exception->getMessage(), 'transport-facade');
-                        return FALSE;
-                    }
 
-                    foreach ($values as $line)
-                    {
-                        try
+                        foreach ($values as $line)
                         {
                             $lineId = $this->getLineIdByCurrentSurfLineNum($surfaceId, (explode(' ', $line)[0]));
                             if (!$lineId)
@@ -141,13 +127,12 @@ class TransportFacade
                                 ]);
                             }
                         }
-                        catch (\Exception $exception)
-                        {
-                            \Tracy\Debugger::log('Nepodařil se uložit nová řádka (line) transliterace. Chyba: ' . $exception->getMessage(), 'transport-facade');
-                            return FALSE;
-                        }
                     }
                 }
+            } catch (\Exception $exception)
+            {
+                \Tracy\Debugger::log($exception, 'transport-facade');
+                return FALSE;
             }
         }
         return TRUE;
diff --git a/app/utils/TextParser.php b/app/utils/TextParser.php
index 849e6f9..39abd20 100644
--- a/app/utils/TextParser.php
+++ b/app/utils/TextParser.php
@@ -3,6 +3,8 @@
 
 namespace App\Utils;
 
+use App\Enum\ESurfaceTypes;
+
 class TextParser
 {
     private $textFile;
@@ -75,37 +77,7 @@ class TextParser
     /** Funkce pro transformaci zkratek surfaceType na jejich příslušná idčka v db */
     private function transformAbbrev($abbrev)
     {
-        switch ($abbrev) {
-            case "rev.":
-                return array_search("reverse", $this->surfaceTypes);
-            case "u.e.":
-                return array_search("upper edge", $this->surfaceTypes);
-            case "lo.e.":
-                return array_search("lower edge", $this->surfaceTypes);
-            case "le.e.":
-                return array_search("left edge", $this->surfaceTypes);
-            // Domnenky, v test souborech se nenachazi -> udelat podle toho export
-            case "r.e.":
-                return array_search("right edge", $this->surfaceTypes);
-            case "s.i.1":
-                return array_search("seal impression 1", $this->surfaceTypes);
-            case "s.i.2":
-                return array_search("seal impression 2", $this->surfaceTypes);
-            case "s.i.3":
-                return array_search("seal impression 3", $this->surfaceTypes);
-            case "s.i.4":
-                return array_search("seal impression 4", $this->surfaceTypes);
-            case "s.i.5":
-                return array_search("seal impression 5", $this->surfaceTypes);
-            case "s.i.6":
-                return array_search("seal impression 6", $this->surfaceTypes);
-            case "s.i.7":
-                return array_search("seal impression 7", $this->surfaceTypes);
-            case "s.i.8":
-                return array_search("seal impression 8", $this->surfaceTypes);
-            default:
-                return array_search("obverse", $this->surfaceTypes);
-        }
+        return array_search(ESurfaceTypes::getSurfaceTypeFromAbbrev($abbrev), $this->surfaceTypes);
     }
 
     public function getTextFile()
