commit 3ea90ef043c0e686e84be6d3fc4aa1ded3bf9433
Author: hard456 <janpalcut@seznam.cz>
Date:   Thu May 23 23:05:37 2019 +0200

    Re #7504 úprava informací o transliteraci

diff --git a/app/AdminModule/component/Transliteration/TransliterationEditForm.latte b/app/AdminModule/component/Transliteration/TransliterationEditForm.latte
index b6a732f..108584c 100644
--- a/app/AdminModule/component/Transliteration/TransliterationEditForm.latte
+++ b/app/AdminModule/component/Transliteration/TransliterationEditForm.latte
@@ -1,5 +1,119 @@
-<div class="row">
-    <div class="col-4">
-        {control form}
+{form form}
+    <div class="row">
+        <div class="col-4">
+            <div class="row mb-2">
+                <div class="col-4">
+                    {label \App\Model\Repository\TransliterationRepository::COLUMN_BOOK_ID}
+                </div>
+                <div class="col-8">
+                    {input \App\Model\Repository\TransliterationRepository::COLUMN_BOOK_ID}
+                </div>
+            </div>
+
+            <div class="row mb-2">
+                <div class="col-4">
+                    {label \App\Model\Repository\TransliterationRepository::COLUMN_CHAPTER}
+                </div>
+                <div class="col-8">
+                    {input \App\Model\Repository\TransliterationRepository::COLUMN_CHAPTER}
+                </div>
+            </div>
+
+            <div class="row mb-2">
+                <div class="col-4">
+                    {label \App\Model\Repository\TransliterationRepository::COLUMN_MUSEUM_ID}
+                </div>
+                <div class="col-8">
+                    {input \App\Model\Repository\TransliterationRepository::COLUMN_MUSEUM_ID}
+                </div>
+            </div>
+
+            <div class="row mb-2">
+                <div class="col-4">
+                    {label \App\Model\Repository\TransliterationRepository::COLUMN_MUSEUM_NO}
+                </div>
+                <div class="col-8">
+                    {input \App\Model\Repository\TransliterationRepository::COLUMN_MUSEUM_NO}
+                </div>
+            </div>
+
+            <div class="row mb-2">
+                <div class="col-4">
+                    {label \App\Model\Repository\TransliterationRepository::COLUMN_REG_NO}
+                </div>
+                <div class="col-8">
+                    {input \App\Model\Repository\TransliterationRepository::COLUMN_REG_NO}
+                </div>
+            </div>
+
+            <div class="row mb-2">
+                <div class="col-4">
+                    {label \App\Model\Repository\TransliterationRepository::COLUMN_ORIGIN_ID}
+                </div>
+                <div class="col-8">
+                    {input \App\Model\Repository\TransliterationRepository::COLUMN_ORIGIN_ID}
+                </div>
+            </div>
+
+            <div class="row mb-2">
+                <div class="col-4">
+                    {label \App\Model\Repository\TransliterationRepository::COLUMN_BOOK_TYPE_ID}
+                </div>
+                <div class="col-8">
+                    {input \App\Model\Repository\TransliterationRepository::COLUMN_BOOK_TYPE_ID}
+                </div>
+            </div>
+
+            <div class="row mb-2">
+                <div class="col-4">
+                    {label \App\Model\Repository\TransliterationRepository::COLUMN_DATE}
+                </div>
+                <div class="col-8">
+                    {input \App\Model\Repository\TransliterationRepository::COLUMN_DATE}
+                </div>
+            </div>
+
+            <div class="row mb-2">
+                <div class="col-4">
+                    {label \App\Model\Repository\TransliterationRepository::COLUMN_NOTE}
+                </div>
+                <div class="col-8">
+                    {input \App\Model\Repository\TransliterationRepository::COLUMN_NOTE}
+                </div>
+            </div>
+        </div>
     </div>
-</div>
\ No newline at end of file
+
+    <div class="row">
+        <div class="col-12">
+            <h5>References</h5>
+            <div n:multiplier="references">
+                <div class="row mb-2">
+
+                    <div class="col-2">
+                        <input n:name="\App\Model\Repository\LitReferenceRepository::COLUMN_SERIES" n:class="form-control"
+                                required></div>
+
+                    <div class="col-2">
+                        <input n:name="\App\Model\Repository\LitReferenceRepository::COLUMN_NUMBER" n:class="form-control">
+                    </div>
+
+                    <div class="col-2">
+                        <input n:name="\App\Model\Repository\LitReferenceRepository::COLUMN_PLATE" n:class="form-control">
+                    </div>
+                    <div class="col-2">{btnRemove 'class' => 'btn btn-danger'}</div>
+                </div>
+            </div>
+
+            {btnCreate references class => 'btn btn-info'}
+        </div>
+    </div>
+
+    <div class="row mt-2">
+        <div class="col-4">
+            {input submit}
+        </div>
+    </div>
+
+{/form}
+
diff --git a/app/AdminModule/component/Transliteration/TransliterationEditForm.php b/app/AdminModule/component/Transliteration/TransliterationEditForm.php
index 8e1f575..fab9038 100644
--- a/app/AdminModule/component/Transliteration/TransliterationEditForm.php
+++ b/app/AdminModule/component/Transliteration/TransliterationEditForm.php
@@ -3,9 +3,16 @@
 
 namespace App\AdminModule\Components;
 
+use App\Enum\EFlashMessage;
+use App\Model\Repository\BookRepository;
+use App\Model\Repository\BookTypeRepository;
+use App\Model\Repository\LitReferenceRepository;
+use App\Model\Repository\MuseumRepository;
+use App\Model\Repository\OriginRepository;
 use App\Model\Repository\TransliterationRepository;
 use App\Utils\Form;
 use Nette\Application\UI\Control;
+use Nette\Forms\Container;
 
 /**
  * Class TransliterationEditForm
@@ -28,17 +35,47 @@ class TransliterationEditForm extends Control
      */
     private $transliterationRepository;
 
+    /**
+     * @var BookRepository
+     */
+    private $bookRepository;
+
+    /**
+     * @var MuseumRepository
+     */
+    private $museumRepository;
+    /**
+     * @var OriginRepository
+     */
+    private $originRepository;
+    /**
+     * @var BookTypeRepository
+     */
+    private $bookTypeRepository;
+    /**
+     * @var LitReferenceRepository
+     */
+    private $litReferenceRepository;
+
     /**
      * TransliterationEditForm constructor.
      * @param TransliterationRepository $transliterationRepository
      */
-    public function __construct(TransliterationRepository $transliterationRepository)
+    public function __construct(TransliterationRepository $transliterationRepository, BookRepository $bookRepository,
+                                MuseumRepository $museumRepository, OriginRepository $originRepository,
+                                BookTypeRepository $bookTypeRepository, LitReferenceRepository $litReferenceRepository)
     {
         parent::__construct();
 
         $this->transliterationRepository = $transliterationRepository;
+        $this->bookRepository = $bookRepository;
+        $this->museumRepository = $museumRepository;
+
         $this->transliterationId = NULL;
         $this->form = new Form;
+        $this->originRepository = $originRepository;
+        $this->bookTypeRepository = $bookTypeRepository;
+        $this->litReferenceRepository = $litReferenceRepository;
     }
 
     public function render()
@@ -49,11 +86,128 @@ class TransliterationEditForm extends Control
 
     public function createComponentForm()
     {
-        $this->form->addText(TransliterationRepository::COLUMN_ID, 'Book Type')
-            ->addRule(Form::REQUIRED, 'Field %label is required.');
+        $this->form->addSelect(TransliterationRepository::COLUMN_BOOK_ID, 'Book', $this->bookRepository->getBookAbbrevForSelect());
+        $this->form->addText(TransliterationRepository::COLUMN_CHAPTER, 'Chapter');
+        $this->form->addSelect(TransliterationRepository::COLUMN_MUSEUM_ID, 'Museum', $this->museumRepository->getMuseumNameForSelect());
+        $this->form->addText(TransliterationRepository::COLUMN_MUSEUM_NO, 'Museum No');
+        $this->form->addText(TransliterationRepository::COLUMN_REG_NO, 'Reg No');
+        $this->form->addSelect(TransliterationRepository::COLUMN_ORIGIN_ID, 'Origin', $this->originRepository->getOriginsForSelect());
+        $this->form->addSelect(TransliterationRepository::COLUMN_BOOK_TYPE_ID, 'Book Type', $this->bookTypeRepository->getTypesForSelect());
+        $this->form->addText(TransliterationRepository::COLUMN_DATE, 'Date');
+        $this->form->addText(TransliterationRepository::COLUMN_NOTE, 'Note');
+
+        // Definice dynamických prvků
+        $multiplier = $this->form->addMultiplier('references', function (Container $container)
+        {
+            $container->addHidden(LitReferenceRepository::COLUMN_ID);
+            $container->addText(LitReferenceRepository::COLUMN_SERIES, 'Series');
+            $container->addText(LitReferenceRepository::COLUMN_NUMBER, 'Number');
+            $container->addText(LitReferenceRepository::COLUMN_PLATE, 'Page');
+        }, 0);
+
+        // Definice tlačítek pro přidání / odebrání řádku
+        $multiplier->addCreateButton('Add')->addClass('btn btn-primary');
+        $multiplier->addRemoveButton('Remove')->addClass('btn btn-danger');
+
+        $this->form->setDefaults($this->getDefaults());
+
+        $this->form->addSubmit('submit', 'Save');
+
+        $this->form->onSuccess[] = [$this, 'formSuccess'];
+
         return $this->form;
     }
 
+    /**
+     * Zpracování editace informací o transliteraci
+     *
+     * @param Form $form
+     */
+    public function formSuccess(Form $form)
+    {
+
+        $values = $form->getValues(true);
+        $references = $values['references'];
+
+        if ($this->isAnyReferenceEmpty($references))
+        {
+            $this->presenter->flashMessage('Transliteration could not be saved.', EFlashMessage::ERROR);
+            return;
+        }
+
+        unset($values['references']);
+
+        $this->transliterationRepository->save($values, $this->transliterationId);
+
+        $this->deleteRemovedReferences($references);
+
+        foreach ($references as $item)
+        {
+            $item[LitReferenceRepository::COLUMN_TRANSLITERATION_ID] = $this->transliterationId;
+            $this->litReferenceRepository->save($item, (int)$item[LitReferenceRepository::COLUMN_ID]);
+        }
+
+        $this->presenter->flashMessage('Transliteration was successfully saved.', EFlashMessage::SUCCESS);
+
+    }
+
+    /**
+     * Zkontroluje jestli není nějaká refence prázná
+     *
+     * @param $references pole referencí
+     * @return bool
+     */
+    public function isAnyReferenceEmpty($references)
+    {
+        foreach ($references as $r)
+        {
+            if (empty($r[LitReferenceRepository::COLUMN_SERIES]) && empty($r[LitReferenceRepository::COLUMN_NUMBER])
+                && empty($r[LitReferenceRepository::COLUMN_PLATE]))
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Odstraní odebrané reference transliterace
+     *
+     * @param $newReferences pole nových transliterací
+     */
+    public function deleteRemovedReferences($newReferences)
+    {
+        $references = $this->litReferenceRepository->findByTransliterationId($this->transliterationId);
+
+        foreach ($references as $ref)
+        {
+            if ($this->isRemovedReference($ref, $newReferences))
+            {
+                $this->litReferenceRepository->delete($ref[LitReferenceRepository::COLUMN_ID]);
+            }
+        }
+
+    }
+
+    /**
+     * Zkontroluje jestli je refence v poli referencí
+     *
+     * @param $reference reference ke kontrole
+     * @param $newReferences pole referencí
+     * @return bool
+     */
+    public function isRemovedReference($reference, $newReferences)
+    {
+        foreach ($newReferences as $ref)
+        {
+            if ($ref[LitReferenceRepository::COLUMN_ID] == $reference[LitReferenceRepository::COLUMN_ID])
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
     /**
      * Nastavení ID transliterace při editaci
      *
@@ -64,9 +218,28 @@ class TransliterationEditForm extends Control
         $this->transliterationId = $transliterationId;
     }
 
+    /**
+     * Vrátí výchozí informace o transliteraci
+     *
+     * @return array
+     */
+    private function getDefaults()
+    {
+        $array = $this->transliterationRepository->findRow($this->transliterationId)->toArray();
+        $references = $this->litReferenceRepository->findByTransliterationId($this->transliterationId)->fetchAll();
+
+        foreach ($references as $activeRow)
+        {
+            $array['references'][] = $activeRow->toArray();
+        }
+
+        return $array;
+    }
+
 }
 
-interface ITransliterationEditFormFactory{
+interface ITransliterationEditFormFactory
+{
 
     /**
      * @return TransliterationEditForm
diff --git a/app/AdminModule/templates/Transliteration/edit.latte b/app/AdminModule/templates/Transliteration/edit.latte
index f976602..4902b29 100644
--- a/app/AdminModule/templates/Transliteration/edit.latte
+++ b/app/AdminModule/templates/Transliteration/edit.latte
@@ -1,3 +1,8 @@
 {block content}
+    <div class="row">
+        <div class="col-12">
+            <div class="display-5">Edit Transliteration</div>
+        </div>
+    </div>
     {control transliterationEditForm}
 {/block}
\ No newline at end of file
diff --git a/app/model/repository/BookRepository.php b/app/model/repository/BookRepository.php
index e9a14d4..3122a7c 100644
--- a/app/model/repository/BookRepository.php
+++ b/app/model/repository/BookRepository.php
@@ -37,4 +37,14 @@ class BookRepository extends Repository
     {
         return $this->findAll()->where(self::COLUMN_BOOK_ABREV . ' LIKE', '%' . $bookName . '%');
     }
+
+    /**
+     * Vrátí všechny abbrev
+     *
+     * @return array
+     */
+    public function getBookAbbrevForSelect(){
+        return $this->findAll()->fetchPairs(BookRepository::COLUMN_ID,BookRepository::COLUMN_BOOK_ABREV);
+    }
+
 }
\ No newline at end of file
diff --git a/app/model/repository/LitReferenceRepository.php b/app/model/repository/LitReferenceRepository.php
index 0440684..81a278c 100644
--- a/app/model/repository/LitReferenceRepository.php
+++ b/app/model/repository/LitReferenceRepository.php
@@ -19,4 +19,16 @@ class LitReferenceRepository extends Repository
     const COLUMN_NUMBER = 'number';
     const COLUMN_PLATE = 'plate';
     const COLUMN_TRANSLITERATION_ID = 'id_transliteration';
+
+    /**
+     * Vrátí reference podle id transliterace
+     *
+     * @param int $transliterationId
+     * @return \Nette\Database\Table\Selection
+     */
+    public function findByTransliterationId(int $transliterationId)
+    {
+        return $this->findBy([self::COLUMN_TRANSLITERATION_ID => $transliterationId]);
+    }
+
 }
\ No newline at end of file
diff --git a/app/model/repository/MuseumRepository.php b/app/model/repository/MuseumRepository.php
index e8a5f04..344e6af 100644
--- a/app/model/repository/MuseumRepository.php
+++ b/app/model/repository/MuseumRepository.php
@@ -39,4 +39,14 @@ class MuseumRepository extends Repository
     {
         return $this->findAll()->where(self::COLUMN_NAME . ' LIKE', '%' . $name . '%');
     }
+
+    /**
+     * Vrátí názvy muzeí
+     *
+     * @return array
+     */
+    public function getMuseumNameForSelect()
+    {
+        return $this->findAll()->fetchPairs(MuseumRepository::COLUMN_ID,MuseumRepository::COLUMN_NAME);
+    }
 }
\ No newline at end of file
