commit 32b3fe213f3abbb65b1bc26a787e2b1e47acd7cb
Author: Filip Jani <jsem@filek.cz>
Date:   Sat May 25 23:45:55 2019 +0200

    Re #7505 možnost editace záznamů

diff --git a/app/AdminModule/component/Transliteration/TransliterationDataEditForm.latte b/app/AdminModule/component/Transliteration/TransliterationDataEditForm.latte
index 18d5d51..0ffd040 100644
--- a/app/AdminModule/component/Transliteration/TransliterationDataEditForm.latte
+++ b/app/AdminModule/component/Transliteration/TransliterationDataEditForm.latte
@@ -53,10 +53,11 @@
                                         <div id="{$name}" class="collapse{$show}">
                                             <div class="card-body">
                                                 <div n:multiplier="$name" n:class="row, mb-2">
-                                                    <div class="col-5">
+                                                    <input n:name="\App\Model\Repository\LineRepository::COLUMN_ID">
+                                                    <div class="col-2">
                                                         <input n:name="\App\Model\Repository\LineRepository::COLUMN_LINE_NUMBER" n:class="form-control, form-control-sm">
                                                     </div>
-                                                    <div class="col-5">
+                                                    <div class="col-8">
                                                         <input n:name="\App\Model\Repository\LineRepository::COLUMN_TRANSLITERATION" n:class="form-control, form-control-sm">
                                                     </div>
                                                     <div class="col-2">
diff --git a/app/AdminModule/component/Transliteration/TransliterationDataEditForm.php b/app/AdminModule/component/Transliteration/TransliterationDataEditForm.php
index d4a76fd..2a3fe6d 100644
--- a/app/AdminModule/component/Transliteration/TransliterationDataEditForm.php
+++ b/app/AdminModule/component/Transliteration/TransliterationDataEditForm.php
@@ -16,11 +16,12 @@ use WebChemistry\Forms\Controls\Multiplier;
 class TransliterationDataEditForm extends Control
 {
     /** @var int ID transliterace */
-    private $id;
+    private $id = NULL;
 
     /** @var array Pole objektů a typu povrch pro strom dat transliterace */
     private $containers;
 
+    /** @var array Pole s výchozími hodnotami při upravování */
     private $defaults;
 
     /**
@@ -52,13 +53,13 @@ class TransliterationDataEditForm extends Control
         $containers = [];
 
         // Načtení typů objektů a typu povrchů do pole pro vykreslení v šabloně
-        foreach ($objectTypes as $objectType)
+        foreach ($objectTypes as $oId =>$objectType)
         {
-            $containers[$this->transliterationFacade->getInputName($objectType)]['title'] = $objectType;
+            $containers[$oId]['title'] = $objectType;
 
-            foreach ($surfaceTypes as $surfaceType)
+            foreach ($surfaceTypes as $sId => $surfaceType)
             {
-                $containers[$this->transliterationFacade->getInputName($objectType)][$this->transliterationFacade->getInputName($surfaceType)] = $surfaceType;
+                $containers[$oId][$sId] = $surfaceType;
             }
         }
 
@@ -100,6 +101,7 @@ class TransliterationDataEditForm extends Control
                 {
                     $container->addInteger(LineRepository::COLUMN_LINE_NUMBER);
                     $container->addText(LineRepository::COLUMN_TRANSLITERATION);
+                    $container->addHidden(LineRepository::COLUMN_ID);
                 }, 0);
 
                 $multiplier->addCreateButton('Add line', 1, $redrawCallback);
@@ -118,7 +120,7 @@ class TransliterationDataEditForm extends Control
 
     public function formSuccess(Form $form)
     {
-
+        $result = $this->transliterationFacade->saveTransliterationData($this->id, $form->getValues());
     }
 
     /**
diff --git a/app/model/facade/TransliterationFacade.php b/app/model/facade/TransliterationFacade.php
index ebc6256..6c8c1ef 100644
--- a/app/model/facade/TransliterationFacade.php
+++ b/app/model/facade/TransliterationFacade.php
@@ -9,6 +9,7 @@ use App\Model\Repository\ObjectTypeRepository;
 use App\Model\Repository\SurfaceRepository;
 use App\Model\Repository\SurfaceTypeRepository;
 use Nette\Database\Table\ActiveRow;
+use Nette\Utils\ArrayHash;
 
 class TransliterationFacade
 {
@@ -44,12 +45,47 @@ class TransliterationFacade
     /**
      * Uloží data transliterace
      *
-     * @param int $id
-     * @param array $values
+     * @param int|null $id : ID transliterace
+     * @param ArrayHash $formValues
      */
-    public function saveTransliterationData(int $id, array $values)
+    public function saveTransliterationData(int $id, ArrayHash $formValues)
     {
-
+        // Škaredý zanoření hodnot z formuláře v kontainerech :(
+        foreach ($formValues as $objectTypeId => $surfaceContainers)
+        {
+            foreach ($surfaceContainers as $surfaceTypeId => $inputs)
+            {
+                foreach ($inputs as $key => $values)
+                {
+                    $lineId = (int)$values->{LineRepository::COLUMN_ID};
+
+                    // Editace již existující řádky
+                    if (!empty($lineId))
+                    {
+                        $this->lineRepository->fetchById($lineId)->update($values);
+
+                    } else
+                    {
+                        $surface = $this->surfaceRepository->fetchSurface($id, $objectTypeId, $surfaceTypeId);
+
+                        if ($surface === FALSE)
+                        {
+                            // TODO: předělat do repository
+                            $surface = $this->surfaceRepository->insert(
+                                [
+                                    SurfaceRepository::COLUMN_SURFACE_TYPE_ID => $surfaceTypeId,
+                                    SurfaceRepository::COLUMN_OBJECT_TYPE_ID => $objectTypeId,
+                                    SurfaceRepository::COLUMN_TRANSLITERATION_ID => $id
+                                ]
+                            );
+                        }
+
+                        $values->{LineRepository::COLUMN_SURFACE_ID} = $surface->{SurfaceRepository::COLUMN_ID};
+                        $this->lineRepository->insert($values);
+                    }
+                }
+            }
+        }
     }
 
     /**
@@ -62,9 +98,6 @@ class TransliterationFacade
     {
         $surfaces = $this->surfaceRepository->findByTransliterationId($id)->fetchAll();
 
-        $surfaceTypes = $this->surfaceTypeRepository->fetchSurfaceTypes();
-        $objectTypes = $this->objectTypeRepository->fetchObjectTypes();
-
         $defaults = array();
 
         /** @var ActiveRow $surface */
@@ -77,14 +110,10 @@ class TransliterationFacade
                 continue;
             }
 
-            // Získání názvu typu povrchu a typu objektu
-            $surfaceType = $surfaceTypes[$surface->{SurfaceRepository::COLUMN_SURFACE_TYPE_ID}];
-            $objectType = $objectTypes[$surface->{SurfaceRepository::COLUMN_OBJECT_TYPE_ID}];
-
-            $objectName = $this->getInputName($objectType);
-            $surfaceName = $this->getInputName($surfaceType);
+            $objectId = $surface->{SurfaceRepository::COLUMN_OBJECT_TYPE_ID};
+            $surfaceId = $surface->{SurfaceRepository::COLUMN_SURFACE_TYPE_ID};
 
-            $defaults[$objectName][$surfaceName] = $lineRows;
+            $defaults[$objectId][$surfaceId] = $lineRows;
         }
 
         return $defaults;
diff --git a/app/model/repository/LineRepository.php b/app/model/repository/LineRepository.php
index 96ed042..f1e8cbb 100644
--- a/app/model/repository/LineRepository.php
+++ b/app/model/repository/LineRepository.php
@@ -75,4 +75,15 @@ class LineRepository extends Repository
         return $adjacentLines;
     }
 
+    /**
+     * Vrací záznam řádku podle ID (z nějakého důvodu mi nefungovalo findRow);
+     *
+     * @param int $id : ID řádky
+     * @return false|\Nette\Database\Table\ActiveRow
+     */
+    public function fetchById(int $id)
+    {
+        return $this->findBy([self::COLUMN_ID => $id])->fetch();
+    }
+
 }
\ No newline at end of file
diff --git a/app/model/repository/ObjectTypeRepository.php b/app/model/repository/ObjectTypeRepository.php
index b35f41b..28c27ab 100644
--- a/app/model/repository/ObjectTypeRepository.php
+++ b/app/model/repository/ObjectTypeRepository.php
@@ -26,4 +26,15 @@ class ObjectTypeRepository extends Repository
     {
         return $this->findAll()->fetchPairs(self::COLUMN_ID, self::COLUMN_OBJECT_TYPE);
     }
+
+    /**
+     * Vrací typ objektu podle názvu
+     *
+     * @param string $name
+     * @return false|\Nette\Database\Table\ActiveRow
+     */
+    public function fetchObjectTypeByName(string $name)
+    {
+        return $this->findBy([self::COLUMN_OBJECT_TYPE => $name])->fetch();
+    }
 }
\ No newline at end of file
diff --git a/app/model/repository/Repository.php b/app/model/repository/Repository.php
index c3ae7be..ad149bb 100644
--- a/app/model/repository/Repository.php
+++ b/app/model/repository/Repository.php
@@ -94,7 +94,7 @@ class Repository
      * @param array $data
      * @return bool|int|Nette\Database\Table\ActiveRow
      */
-    public function insert(array $data)
+    public function insert($data)
     {
         return $this->getTable()->insert($data);
     }
diff --git a/app/model/repository/SurfaceRepository.php b/app/model/repository/SurfaceRepository.php
index 88409f1..fb006c9 100644
--- a/app/model/repository/SurfaceRepository.php
+++ b/app/model/repository/SurfaceRepository.php
@@ -30,4 +30,14 @@ class SurfaceRepository extends Repository
     {
         return $this->findBy([self::COLUMN_TRANSLITERATION_ID => $id]);
     }
+
+    public function fetchSurface(int $transliterationId, int $objectTypeId, int $surfaceTypeId){
+        return $this->findBy(
+            [
+                self::COLUMN_TRANSLITERATION_ID => $transliterationId,
+                self::COLUMN_OBJECT_TYPE_ID => $objectTypeId,
+                self::COLUMN_SURFACE_TYPE_ID => $surfaceTypeId
+            ]
+        )->fetch();
+    }
 }
\ No newline at end of file
