commit 853adcf7a7db5d700b896ed22be6ff78f1eca5c4
Author: hard456 <janpalcut@seznam.cz>
Date:   Tue Apr 23 19:26:38 2019 +0200

    Re #7334 správa typů knihy

diff --git a/app/AdminModule/component/BookType/BookTypeEditForm.latte b/app/AdminModule/component/BookType/BookTypeEditForm.latte
new file mode 100644
index 0000000..b6a732f
--- /dev/null
+++ b/app/AdminModule/component/BookType/BookTypeEditForm.latte
@@ -0,0 +1,5 @@
+<div class="row">
+    <div class="col-4">
+        {control form}
+    </div>
+</div>
\ No newline at end of file
diff --git a/app/AdminModule/component/BookType/BookTypeEditForm.php b/app/AdminModule/component/BookType/BookTypeEditForm.php
new file mode 100644
index 0000000..237f4cd
--- /dev/null
+++ b/app/AdminModule/component/BookType/BookTypeEditForm.php
@@ -0,0 +1,131 @@
+<?php
+
+namespace App\AdminModule\Components;
+
+use App\Enum\EFlashMessage;
+use App\Model\Repository\BookTypeRepository;
+use App\Utils\Form;
+use Nette\Application\UI\Control;
+
+class BookTypeEditForm extends Control
+{
+
+    /**
+     * @var Form
+     */
+    private $form;
+    /**
+     * @var int ID book type
+     */
+    private $bookTypeId;
+
+    /**
+     * @var BookTypeRepository
+     */
+    private $bookTypeRepository;
+
+    /**
+     * BookTypeEditForm constructor.
+     * @param Form $form
+     * @param int $bookTypeId
+     * @param BookTypeRepository $bookTypeRepository
+     */
+    public function __construct(BookTypeRepository $bookTypeRepository)
+    {
+        parent::__construct();
+
+        $this->form = new Form;
+        $this->bookTypeId = NULL;
+        $this->bookTypeRepository = $bookTypeRepository;
+    }
+
+    public function render()
+    {
+        $this->template->setFile(__DIR__ . '/BookTypeEditForm.latte');
+        $this->template->render();
+    }
+
+    public function createComponentForm()
+    {
+        $this->form->addText(BookTypeRepository::COLUMN_BOOK_TYPE, 'Book Type')
+            ->addRule(Form::REQUIRED, 'Pole %label je povinné.');
+
+        $this->form->addSubmit('submit', 'Save');
+
+        $this->form->setDefaults($this->getDefaults());
+
+        $this->form->onValidate[] = [$this, 'formValidate'];
+        $this->form->onSuccess[] = [$this, 'formSuccess'];
+
+        return $this->form;
+    }
+
+    public function formValidate(Form $form)
+    {
+        $values = $form->getValues();
+
+        //Vložení nového book type - kontrola zda již neexistuje se stejným názvem
+        if(empty($this->bookTypeId))
+        {
+            if ($this->bookTypeRepository->findByBookType($values[BookTypeRepository::COLUMN_BOOK_TYPE])->fetch())
+            {
+                $form->addError('Book type with the same name already exists. Please choose another name.');
+            }
+        }
+    }
+
+    public function formSuccess(Form $form)
+    {
+        $result = $this->bookTypeRepository->save($form->getValues(TRUE), $this->bookTypeId);
+
+        if ($result)
+        {
+            $this->presenter->flashMessage('Book type was added successfully.', EFlashMessage::SUCCESS);
+            $this->presenter->redirect('BookType:edit', ['id' => $result->{BookTypeRepository::COLUMN_ID}]);
+        } else
+        {
+            $form->addError('Book type couldn\'t be added. Please try it again later.');
+        }
+    }
+
+    /**
+     * Nastavení ID book type při editaci
+     *
+     * @param int $bookTypeId
+     */
+    public function setBookType(int $bookTypeId)
+    {
+        $this->bookTypeId = $bookTypeId;
+    }
+
+    /**
+     * Vrací hodnoty z databáze při upravování existujícího typu knížky
+     *
+     * @return array
+     */
+    public function getDefaults()
+    {
+        $defaultValues = array();
+
+        if (!empty($this->bookTypeId))
+        {
+            $bookTypeRow = $this->bookTypeRepository->findRow($this->bookTypeId);
+
+            if ($bookTypeRow)
+            {
+                $defaultValues = $bookTypeRow->toArray();
+            }
+        }
+
+        return $defaultValues;
+    }
+
+}
+
+interface IBookTypeEditFormFactory
+{
+    /**
+     * @return BookTypeEditForm
+     */
+    public function create();
+}
\ No newline at end of file
diff --git a/app/AdminModule/component/BookType/BookTypeGrid.php b/app/AdminModule/component/BookType/BookTypeGrid.php
new file mode 100644
index 0000000..a82e0dd
--- /dev/null
+++ b/app/AdminModule/component/BookType/BookTypeGrid.php
@@ -0,0 +1,75 @@
+<?php
+
+namespace App\AdminModule\Components;
+
+use App\Model\Repository\BookTypeRepository;
+use App\Utils\DataGrid\DataGrid;
+use Ublaboo\DataGrid\Exception\DataGridException;
+
+
+class BookTypeGrid extends DataGrid
+{
+
+    /**
+     * @var BookTypeRepository
+     */
+    private $bookTypeRepository;
+
+    /**
+     * BookTypeGrid constructor.
+     * @param BookTypeRepository $bookTypeRepository
+     * @throws DataGridException
+     */
+    public function __construct(BookTypeRepository $bookTypeRepository)
+    {
+        $this->bookTypeRepository = $bookTypeRepository;
+        parent::__construct(FALSE);
+    }
+
+
+    /**
+     * Abstraktní metoda, slouží k nastavení primárního klíče a nastavení datasource
+     *  1. $this->setPrimaryKey();
+     *  2. $this->setDataSource();
+     *
+     * @throws DataGridException
+     */
+    public function init()
+    {
+        $this->setPrimaryKey(BookTypeRepository::COLUMN_ID);
+        $this->setDataSource($this->bookTypeRepository->findAll());
+    }
+
+    /**
+     * Definice sloupečků, akcí, vyhledávácích filtrů gridu
+     *
+     * @throws DataGridException
+     */
+    public function define()
+    {
+        // Definice sloupečků
+        $this->addColumnNumber(BookTypeRepository::COLUMN_ID, 'ID')->setDefaultHide(TRUE);
+        $this->addColumnText(BookTypeRepository::COLUMN_BOOK_TYPE, 'Book type');
+
+
+        // Definice filtrů
+        $this->addFilterText(BookTypeRepository::COLUMN_BOOK_TYPE, 'Book type');
+
+        // Definice akcí
+        $this->addAction('edit', 'edit', 'BookType:edit', ['id' => BookTypeRepository::COLUMN_ID])
+            ->setTitle('Edit');
+
+        $this->addAction('delete', 'delete', 'deleteBookType!', ['id' => BookTypeRepository::COLUMN_ID])
+            ->setConfirm('Do you really want to delete book type %s.', BookTypeRepository::COLUMN_BOOK_TYPE)
+            ->setTitle('Delete')
+            ->setClass('btn btn-xs btn-danger ajax');
+    }
+}
+
+interface IBookTypeGridFactory
+{
+    /**
+     * @return BookTypeGrid
+     */
+    public function create();
+}
\ No newline at end of file
diff --git a/app/AdminModule/presenters/BookTypePresenter.php b/app/AdminModule/presenters/BookTypePresenter.php
new file mode 100644
index 0000000..b7276c0
--- /dev/null
+++ b/app/AdminModule/presenters/BookTypePresenter.php
@@ -0,0 +1,66 @@
+<?php
+
+
+namespace App\AdminModule\Presenters;
+
+use App\AdminModule\Components\IBookTypeEditFormFactory;
+use App\AdminModule\Components\IBookTypeGridFactory;
+use App\Model\Repository\BookTypeRepository;
+
+class BookTypePresenter extends BaseUserPresenter
+{
+
+    /**
+     * @var IBookTypeGridFactory
+     */
+    private $bookTypeGridFactory;
+
+    /**
+     * @var IBookTypeEditFormFactory
+     */
+    private $bookTypeEditFormFactory;
+
+    /**
+     * @var BookTypeRepository
+     */
+    private $bookTypeRepository;
+
+    public function __construct(IBookTypeGridFactory $bookTypeGridFactory,
+                                IBookTypeEditFormFactory $bookTypeEditFormFactory,
+                                BookTypeRepository $bookTypeRepository)
+    {
+        parent::__construct();
+
+        $this->bookTypeGridFactory = $bookTypeGridFactory;
+        $this->bookTypeEditFormFactory = $bookTypeEditFormFactory;
+        $this->bookTypeRepository = $bookTypeRepository;
+    }
+
+    public function actionEdit(int $id)
+    {
+        $this['bookTypeEditForm']->setBookType($id);
+    }
+
+    /**
+     * Handle používaný v MuseumGrid pro smazání muzea
+     *
+     * @param int $id : ID mazaného muzea
+     */
+    public function handleDeleteBookType(int $id)
+    {
+        if ($this->isAjax())
+        {
+            $this->bookTypeRepository->findRow($id)->delete();
+            $this['bookTypeGrid']->reload();
+        }
+    }
+
+    public function createComponentBookTypeGrid(){
+        return $this->bookTypeGridFactory->create();
+    }
+
+    public function createComponentBookTypeEditForm(){
+        return $this->bookTypeEditFormFactory->create();
+    }
+
+}
\ No newline at end of file
diff --git a/app/AdminModule/templates/@layout.latte b/app/AdminModule/templates/@layout.latte
index 56d3d38..5b1f1ce 100644
--- a/app/AdminModule/templates/@layout.latte
+++ b/app/AdminModule/templates/@layout.latte
@@ -103,7 +103,7 @@
             Book </a></li>
     <li class="nav-item"><a href="#" class="nav-link" title="Transliteration">
             Transliteration </a></li>
-    <li class="nav-item"><a href="#" class="nav-link" title="Book type">
+    <li class="nav-item"><a n:href="BookType:default" n:class="nav-link, $presenter->isLinkCurrent('BookType:*') ? active" title="Book type">
             Book type </a></li>
     <li class="nav-item"><a n:href="Object:default" n:class="nav-link, $presenter->isLinkCurrent('Object:*') ? active" title="Object type">
             Object&nbsp;type </a></li>
diff --git a/app/AdminModule/templates/BookType/add.latte b/app/AdminModule/templates/BookType/add.latte
new file mode 100644
index 0000000..16cd9be
--- /dev/null
+++ b/app/AdminModule/templates/BookType/add.latte
@@ -0,0 +1,10 @@
+{block content}
+<div class="row">
+    <div class="col-11">
+        <div class="display-5">New Book Type</div>
+    </div>
+    <div class="col-1">
+        <a n:href="BookType:default" class="btn btn-sm btn-success"><span class="fa fa-fw fa-list"></span> Book Type list</a>
+    </div>
+</div>
+{control bookTypeEditForm}
\ No newline at end of file
diff --git a/app/AdminModule/templates/BookType/default.latte b/app/AdminModule/templates/BookType/default.latte
new file mode 100644
index 0000000..a9d1118
--- /dev/null
+++ b/app/AdminModule/templates/BookType/default.latte
@@ -0,0 +1,14 @@
+{block content}
+<div class="row">
+    <div class="col-11">
+        <div class="display-5">Book Type List</div>
+    </div>
+    <div class="col-1">
+        <a n:href="BookType:add" class="btn btn-sm btn-success"><span class="fa fa-fw fa-plus"></span> New Book Type</a>
+    </div>
+</div>
+<div class="row">
+    <div class="col-10 offset-1">
+        {control bookTypeGrid}
+    </div>
+</div>
\ No newline at end of file
diff --git a/app/AdminModule/templates/BookType/edit.latte b/app/AdminModule/templates/BookType/edit.latte
new file mode 100644
index 0000000..bae9c55
--- /dev/null
+++ b/app/AdminModule/templates/BookType/edit.latte
@@ -0,0 +1,10 @@
+{block content}
+<div class="row">
+    <div class="col-11">
+        <div class="display-5">Edit Book Type</div>
+    </div>
+    <div class="col-1">
+        <a n:href="BookType:default" class="btn btn-sm btn-success"><span class="fa fa-fw fa-list"></span> Book Type list</a>
+    </div>
+</div>
+{control bookTypeEditForm}
\ No newline at end of file
diff --git a/app/config/components.neon b/app/config/components.neon
index 343e6e2..1033e2b 100644
--- a/app/config/components.neon
+++ b/app/config/components.neon
@@ -6,6 +6,7 @@ services:
     - App\AdminModule\Components\IObjectTypeGridFactory
     - App\AdminModule\Components\IOriginGridFactory
     - App\AdminModule\Components\ISurfaceTypeGridFactory
+    - App\AdminModule\Components\IBookTypeGridFactory
 
     # Formuláře
     - App\FrontModule\Components\ILoginFormFactory
@@ -14,4 +15,5 @@ services:
     - App\AdminModule\Components\IObjectTypeEditFormFactory
     - App\AdminModule\Components\IOriginEditFormFactory
     - App\AdminModule\Components\ISurfaceTypeEditFormFactory
-    - App\AdminModule\Components\IMuseumEditFormFactory
\ No newline at end of file
+    - App\AdminModule\Components\IMuseumEditFormFactory
+    - App\AdminModule\Components\IBookTypeEditFormFactory
\ No newline at end of file
diff --git a/app/model/repository/BookTypeRepository.php b/app/model/repository/BookTypeRepository.php
index dcb054b..a23b571 100644
--- a/app/model/repository/BookTypeRepository.php
+++ b/app/model/repository/BookTypeRepository.php
@@ -16,4 +16,16 @@ class BookTypeRepository extends Repository
     /** Sloupečky tabulky */
     const COLUMN_ID = 'id_book_type';
     const COLUMN_BOOK_TYPE = 'book_type';
+
+    /**
+     * Najde záznam podle typu knížky v DB
+     *
+     * @param string $bookType
+     * @return \Nette\Database\Table\Selection
+     */
+    public function findByBookType(string $bookType)
+    {
+        return $this->findBy([self::COLUMN_BOOK_TYPE => $bookType]);
+    }
+
 }
\ No newline at end of file
