commit 2d082bc234959c93e3cfc101eea58b242e0f2af5
Author: Petr Lukašík <lukasik@rtsoft.cz>
Date:   Sun Jun 2 12:26:10 2019 +0200

    Re #7510 Úprava vzhledu, implementace parsování souboru a funkcionalita importu, preview

diff --git a/app/AdminModule/component/Transport/ExportForm.latte b/app/AdminModule/component/Transport/ExportForm.latte
index b6a732f..98189dd 100644
--- a/app/AdminModule/component/Transport/ExportForm.latte
+++ b/app/AdminModule/component/Transport/ExportForm.latte
@@ -1,5 +1 @@
-<div class="row">
-    <div class="col-4">
-        {control form}
-    </div>
-</div>
\ No newline at end of file
+{control form}
diff --git a/app/AdminModule/component/Transport/ImportForm.latte b/app/AdminModule/component/Transport/ImportForm.latte
index b6a732f..98189dd 100644
--- a/app/AdminModule/component/Transport/ImportForm.latte
+++ b/app/AdminModule/component/Transport/ImportForm.latte
@@ -1,5 +1 @@
-<div class="row">
-    <div class="col-4">
-        {control form}
-    </div>
-</div>
\ No newline at end of file
+{control form}
diff --git a/app/AdminModule/component/Transport/ImportForm.php b/app/AdminModule/component/Transport/ImportForm.php
index 20dd574..c897d3d 100644
--- a/app/AdminModule/component/Transport/ImportForm.php
+++ b/app/AdminModule/component/Transport/ImportForm.php
@@ -3,17 +3,20 @@
 namespace App\AdminModule\Components;
 
 use App\Utils\Form;
+use App\Utils\TextParser;
 use Nette\Application\UI\Control;
-use Nette\Utils\ArrayHash;
+use Tracy\Debugger;
 
 class ImportForm extends Control
 {
+    /** @var TextParser */
+    private $parser;
+
     public function __construct()
     {
         parent::__construct();
     }
 
-
     public function render()
     {
         $this->template->setFile(__DIR__ . '/ImportForm.latte');
@@ -24,20 +27,28 @@ class ImportForm extends Control
     {
         $form = new Form();
 
-        $form->addUpload('import', 'File')->addRule(Form::REQUIRED, 'Field %label is required.');
+        $form->addUpload('import', 'File')
+            ->addRule(Form::REQUIRED, 'Field %label is required.')
+            ->addRule(Form::MIME_TYPE, "Uploaded file must be text file", "text/plain"); // mozne nekdy pridat text/csv
 
         $form->addSubmit('submit', 'Import file');
+
+        $form->onValidate[] = [$this, 'formValidate'];
         $form->onSuccess[] = [$this, 'formSuccess'];
 
         return $form;
     }
 
+    public function formValidate(Form $form){
+        $this->parser = new TextParser($form->getValues()['import']);
+        if (!$this->parser->isFileParsable()){
+            $form->addError('Wrong format of imported file');
+        }
+    }
+
     public function formSuccess(Form $form)
     {
-        /** @var ArrayHash $values */
-        $values = $form->getValues();
-        $this->transliterationSearchModel->setSearchTerms($values);
-        $this->presenter->redirect('Transliteration:searchResult');
+        $this->presenter->forward('Transport:previewImport', $this->parser);
     }
 }
 
diff --git a/app/AdminModule/presenters/TransportPresenter.php b/app/AdminModule/presenters/TransportPresenter.php
index 1bb0caa..ba3635c 100644
--- a/app/AdminModule/presenters/TransportPresenter.php
+++ b/app/AdminModule/presenters/TransportPresenter.php
@@ -4,6 +4,9 @@ namespace App\AdminModule\Presenters;
 
 use App\AdminModule\Components\IExportFormFactory;
 use App\AdminModule\Components\IImportFormFactory;
+use App\Model\Repository\ObjectTypeRepository;
+use App\Model\Repository\SurfaceTypeRepository;
+use App\Utils\TextParser;
 
 class TransportPresenter extends BaseUserPresenter
 {
@@ -13,14 +16,43 @@ class TransportPresenter extends BaseUserPresenter
     /** @var IExportFormFactory  */
     private $exportFormFactory;
 
+    /** @var SurfaceTypeRepository */
+    private $surfaceTypeRepository;
+
+    /** @var ObjectTypeRepository */
+    private $objectTypeRepository;
+
     public function __construct(
         IImportFormFactory $importFormFactory,
-        IExportFormFactory $exportFormFactory
+        IExportFormFactory $exportFormFactory,
+        SurfaceTypeRepository $surfaceTypeRepository,
+        ObjectTypeRepository $objectTypeRepository
     )
     {
         parent::__construct();
         $this->importFormFactory = $importFormFactory;
         $this->exportFormFactory = $exportFormFactory;
+        $this->surfaceTypeRepository = $surfaceTypeRepository;
+        $this->objectTypeRepository = $objectTypeRepository;
+    }
+
+    public function actionPreviewImport(TextParser $parser){
+        $objectTypes = $this->objectTypeRepository->fetchObjectTypes();
+        $surfaceTypes = $this->surfaceTypeRepository->fetchSurfaceTypes();
+        $parser->setTypes($objectTypes, $surfaceTypes);
+
+        $this->template->fileName = $parser->textFile->getName();
+        $this->template->parsedFile = $parser->parseText();
+        $this->template->transliterationNum = $this->countTransliterations();
+        $this->template->surfaces = $surfaceTypes;
+    }
+
+    public function countTransliterations(){
+        $num=0;
+        foreach ($this->template->parsedFile as $book){
+            $num += count($book);
+        }
+        return $num;
     }
 
     public function createComponentImportForm()
diff --git a/app/AdminModule/templates/Transport/default.latte b/app/AdminModule/templates/Transport/default.latte
index 8bf2a75..160e425 100644
--- a/app/AdminModule/templates/Transport/default.latte
+++ b/app/AdminModule/templates/Transport/default.latte
@@ -1,13 +1,21 @@
 {block content}
-    <div class="row col-12">
-        <div class="display-5">Import text</div>
-    </div>
-
-    {control importForm}
+    <div class="row">
+        <div class="col-3">
+            <div class="display-5">Import or export text</div>
 
-    <div class="row col-12" style="margin-top: 5%">
-        <div class="display-5">Export text</div>
+            <fieldset class="mt-5">
+                <legend>Text import</legend>
+                {control importForm}
+            </fieldset>
+        </div>
     </div>
 
-    {control exportForm}
+    <div class="row mt-5">
+        <div class="col-3">
+            <fieldset>
+                <legend>Text export</legend>
+                {control exportForm}
+            </fieldset>
+        </div>
+    </div>
 {/block}
\ No newline at end of file
diff --git a/app/AdminModule/templates/Transport/previewImport.latte b/app/AdminModule/templates/Transport/previewImport.latte
new file mode 100644
index 0000000..7fee820
--- /dev/null
+++ b/app/AdminModule/templates/Transport/previewImport.latte
@@ -0,0 +1,28 @@
+{block content}
+    <div class="row">
+        <div class="col-12">
+            <div class="display-5">Preview of import: {$fileName}</div>
+        </div>
+        <div class="col-12">
+            Imported {$transliterationNum} transliterations
+        </div>
+    </div>
+
+    <div class="mt-5 border p-3 col-8">
+        <div class="col-12 display-5" n:foreach="$parsedFile as $book=>$chapters">Book {$book}</div>
+        <div class="row" n:foreach="$chapters as $chapter=>$references">
+            <div class="col-12 mt-3">
+                <fieldset>
+                    <legend>Chapter {$chapter}</legend>
+                    <div n:foreach="$references as $reference=>$element">
+                        <i>{$surfaces[$reference]}</i><br>
+                        {foreach $element as $value}
+                            {$value}.<br>
+                        {/foreach}
+                        <hr>
+                    </div>
+                </fieldset>
+            </div>
+        </div>
+    </div>
+{/block}
\ No newline at end of file
diff --git a/app/utils/TextParser.php b/app/utils/TextParser.php
new file mode 100644
index 0000000..ff84ee2
--- /dev/null
+++ b/app/utils/TextParser.php
@@ -0,0 +1,104 @@
+<?php
+
+
+namespace App\Utils;
+
+class TextParser
+{
+    public $textFile;
+    private $objectTypes;
+    private $surfaceTypes;
+
+    public function __construct($file)
+    {
+        $this->textFile = $file;
+    }
+
+    public function parseText()
+    {
+        $array = explode("\n", file_get_contents($this->textFile));
+        $array = array_map('trim', $array);
+
+        $output = array();
+        $currentBook = "";
+        $currentChapter = "";
+        $currentSurface = $this->transformAbbrev("obverse");
+
+        //TODO kontrola a parsovani object types (tablet, envelope)
+        foreach ($array as $value) {
+            if ($value[0] == '|' && !is_numeric($value[1])) {
+                $currentBook = substr($value, 1);
+                $currentChapter = "";
+                $currentSurface = $this->transformAbbrev("obverse");
+            }
+            elseif ($value[0] == '|' && is_numeric($value[1])){
+                $currentChapter = substr($value, 1);
+                $currentSurface = $this->transformAbbrev("obverse");
+            }else{
+                if (strchr($value, '$')) {
+                    $output[$currentBook][$currentChapter][$currentSurface][] = trim(substr($value,0,stripos($value,"&")));
+                    $currentSurface = $this->transformAbbrev(substr(strchr($value, "&"),1, -1));
+                }else{
+                    $output[$currentBook][$currentChapter][$currentSurface][] = $value;
+                }
+            }
+        }
+
+        return $output;
+    }
+
+    /** Nejvíc kontrola souboru*/
+    public function isFileParsable()
+    {
+        if (file_exists($this->textFile)) {
+            $checker = fopen($this->textFile, "r");
+            if (fgets($checker)[0] == '|') {
+                return true;
+            } else {
+                return false;
+            }
+        } else {
+            return false;
+        }
+    }
+
+    public function setTypes($objects, $surfaces)
+    {
+        $this->objectTypes = $objects;
+        $this->surfaceTypes = $surfaces;
+    }
+
+    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);
+        }
+    }
+}
\ No newline at end of file
