Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 853adcf7

Přidáno uživatelem Jan Palcút před téměř 6 roky(ů)

Re #7334 správa typů knihy

Zobrazit rozdíly:

app/AdminModule/component/BookType/BookTypeEditForm.latte
1
<div class="row">
2
    <div class="col-4">
3
        {control form}
4
    </div>
5
</div>
app/AdminModule/component/BookType/BookTypeEditForm.php
1
<?php
2

  
3
namespace App\AdminModule\Components;
4

  
5
use App\Enum\EFlashMessage;
6
use App\Model\Repository\BookTypeRepository;
7
use App\Utils\Form;
8
use Nette\Application\UI\Control;
9

  
10
class BookTypeEditForm extends Control
11
{
12

  
13
    /**
14
     * @var Form
15
     */
16
    private $form;
17
    /**
18
     * @var int ID book type
19
     */
20
    private $bookTypeId;
21

  
22
    /**
23
     * @var BookTypeRepository
24
     */
25
    private $bookTypeRepository;
26

  
27
    /**
28
     * BookTypeEditForm constructor.
29
     * @param Form $form
30
     * @param int $bookTypeId
31
     * @param BookTypeRepository $bookTypeRepository
32
     */
33
    public function __construct(BookTypeRepository $bookTypeRepository)
34
    {
35
        parent::__construct();
36

  
37
        $this->form = new Form;
38
        $this->bookTypeId = NULL;
39
        $this->bookTypeRepository = $bookTypeRepository;
40
    }
41

  
42
    public function render()
43
    {
44
        $this->template->setFile(__DIR__ . '/BookTypeEditForm.latte');
45
        $this->template->render();
46
    }
47

  
48
    public function createComponentForm()
49
    {
50
        $this->form->addText(BookTypeRepository::COLUMN_BOOK_TYPE, 'Book Type')
51
            ->addRule(Form::REQUIRED, 'Pole %label je povinné.');
52

  
53
        $this->form->addSubmit('submit', 'Save');
54

  
55
        $this->form->setDefaults($this->getDefaults());
56

  
57
        $this->form->onValidate[] = [$this, 'formValidate'];
58
        $this->form->onSuccess[] = [$this, 'formSuccess'];
59

  
60
        return $this->form;
61
    }
62

  
63
    public function formValidate(Form $form)
64
    {
65
        $values = $form->getValues();
66

  
67
        //Vložení nového book type - kontrola zda již neexistuje se stejným názvem
68
        if(empty($this->bookTypeId))
69
        {
70
            if ($this->bookTypeRepository->findByBookType($values[BookTypeRepository::COLUMN_BOOK_TYPE])->fetch())
71
            {
72
                $form->addError('Book type with the same name already exists. Please choose another name.');
73
            }
74
        }
75
    }
76

  
77
    public function formSuccess(Form $form)
78
    {
79
        $result = $this->bookTypeRepository->save($form->getValues(TRUE), $this->bookTypeId);
80

  
81
        if ($result)
82
        {
83
            $this->presenter->flashMessage('Book type was added successfully.', EFlashMessage::SUCCESS);
84
            $this->presenter->redirect('BookType:edit', ['id' => $result->{BookTypeRepository::COLUMN_ID}]);
85
        } else
86
        {
87
            $form->addError('Book type couldn\'t be added. Please try it again later.');
88
        }
89
    }
90

  
91
    /**
92
     * Nastavení ID book type při editaci
93
     *
94
     * @param int $bookTypeId
95
     */
96
    public function setBookType(int $bookTypeId)
97
    {
98
        $this->bookTypeId = $bookTypeId;
99
    }
100

  
101
    /**
102
     * Vrací hodnoty z databáze při upravování existujícího typu knížky
103
     *
104
     * @return array
105
     */
106
    public function getDefaults()
107
    {
108
        $defaultValues = array();
109

  
110
        if (!empty($this->bookTypeId))
111
        {
112
            $bookTypeRow = $this->bookTypeRepository->findRow($this->bookTypeId);
113

  
114
            if ($bookTypeRow)
115
            {
116
                $defaultValues = $bookTypeRow->toArray();
117
            }
118
        }
119

  
120
        return $defaultValues;
121
    }
122

  
123
}
124

  
125
interface IBookTypeEditFormFactory
126
{
127
    /**
128
     * @return BookTypeEditForm
129
     */
130
    public function create();
131
}
app/AdminModule/component/BookType/BookTypeGrid.php
1
<?php
2

  
3
namespace App\AdminModule\Components;
4

  
5
use App\Model\Repository\BookTypeRepository;
6
use App\Utils\DataGrid\DataGrid;
7
use Ublaboo\DataGrid\Exception\DataGridException;
8

  
9

  
10
class BookTypeGrid extends DataGrid
11
{
12

  
13
    /**
14
     * @var BookTypeRepository
15
     */
16
    private $bookTypeRepository;
17

  
18
    /**
19
     * BookTypeGrid constructor.
20
     * @param BookTypeRepository $bookTypeRepository
21
     * @throws DataGridException
22
     */
23
    public function __construct(BookTypeRepository $bookTypeRepository)
24
    {
25
        $this->bookTypeRepository = $bookTypeRepository;
26
        parent::__construct(FALSE);
27
    }
28

  
29

  
30
    /**
31
     * Abstraktní metoda, slouží k nastavení primárního klíče a nastavení datasource
32
     *  1. $this->setPrimaryKey();
33
     *  2. $this->setDataSource();
34
     *
35
     * @throws DataGridException
36
     */
37
    public function init()
38
    {
39
        $this->setPrimaryKey(BookTypeRepository::COLUMN_ID);
40
        $this->setDataSource($this->bookTypeRepository->findAll());
41
    }
42

  
43
    /**
44
     * Definice sloupečků, akcí, vyhledávácích filtrů gridu
45
     *
46
     * @throws DataGridException
47
     */
48
    public function define()
49
    {
50
        // Definice sloupečků
51
        $this->addColumnNumber(BookTypeRepository::COLUMN_ID, 'ID')->setDefaultHide(TRUE);
52
        $this->addColumnText(BookTypeRepository::COLUMN_BOOK_TYPE, 'Book type');
53

  
54

  
55
        // Definice filtrů
56
        $this->addFilterText(BookTypeRepository::COLUMN_BOOK_TYPE, 'Book type');
57

  
58
        // Definice akcí
59
        $this->addAction('edit', 'edit', 'BookType:edit', ['id' => BookTypeRepository::COLUMN_ID])
60
            ->setTitle('Edit');
61

  
62
        $this->addAction('delete', 'delete', 'deleteBookType!', ['id' => BookTypeRepository::COLUMN_ID])
63
            ->setConfirm('Do you really want to delete book type %s.', BookTypeRepository::COLUMN_BOOK_TYPE)
64
            ->setTitle('Delete')
65
            ->setClass('btn btn-xs btn-danger ajax');
66
    }
67
}
68

  
69
interface IBookTypeGridFactory
70
{
71
    /**
72
     * @return BookTypeGrid
73
     */
74
    public function create();
75
}
app/AdminModule/presenters/BookTypePresenter.php
1
<?php
2

  
3

  
4
namespace App\AdminModule\Presenters;
5

  
6
use App\AdminModule\Components\IBookTypeEditFormFactory;
7
use App\AdminModule\Components\IBookTypeGridFactory;
8
use App\Model\Repository\BookTypeRepository;
9

  
10
class BookTypePresenter extends BaseUserPresenter
11
{
12

  
13
    /**
14
     * @var IBookTypeGridFactory
15
     */
16
    private $bookTypeGridFactory;
17

  
18
    /**
19
     * @var IBookTypeEditFormFactory
20
     */
21
    private $bookTypeEditFormFactory;
22

  
23
    /**
24
     * @var BookTypeRepository
25
     */
26
    private $bookTypeRepository;
27

  
28
    public function __construct(IBookTypeGridFactory $bookTypeGridFactory,
29
                                IBookTypeEditFormFactory $bookTypeEditFormFactory,
30
                                BookTypeRepository $bookTypeRepository)
31
    {
32
        parent::__construct();
33

  
34
        $this->bookTypeGridFactory = $bookTypeGridFactory;
35
        $this->bookTypeEditFormFactory = $bookTypeEditFormFactory;
36
        $this->bookTypeRepository = $bookTypeRepository;
37
    }
38

  
39
    public function actionEdit(int $id)
40
    {
41
        $this['bookTypeEditForm']->setBookType($id);
42
    }
43

  
44
    /**
45
     * Handle používaný v MuseumGrid pro smazání muzea
46
     *
47
     * @param int $id : ID mazaného muzea
48
     */
49
    public function handleDeleteBookType(int $id)
50
    {
51
        if ($this->isAjax())
52
        {
53
            $this->bookTypeRepository->findRow($id)->delete();
54
            $this['bookTypeGrid']->reload();
55
        }
56
    }
57

  
58
    public function createComponentBookTypeGrid(){
59
        return $this->bookTypeGridFactory->create();
60
    }
61

  
62
    public function createComponentBookTypeEditForm(){
63
        return $this->bookTypeEditFormFactory->create();
64
    }
65

  
66
}
app/AdminModule/templates/@layout.latte
103 103
            Book </a></li>
104 104
    <li class="nav-item"><a href="#" class="nav-link" title="Transliteration">
105 105
            Transliteration </a></li>
106
    <li class="nav-item"><a href="#" class="nav-link" title="Book type">
106
    <li class="nav-item"><a n:href="BookType:default" n:class="nav-link, $presenter->isLinkCurrent('BookType:*') ? active" title="Book type">
107 107
            Book type </a></li>
108 108
    <li class="nav-item"><a n:href="Object:default" n:class="nav-link, $presenter->isLinkCurrent('Object:*') ? active" title="Object type">
109 109
            Object&nbsp;type </a></li>
app/AdminModule/templates/BookType/add.latte
1
{block content}
2
<div class="row">
3
    <div class="col-11">
4
        <div class="display-5">New Book Type</div>
5
    </div>
6
    <div class="col-1">
7
        <a n:href="BookType:default" class="btn btn-sm btn-success"><span class="fa fa-fw fa-list"></span> Book Type list</a>
8
    </div>
9
</div>
10
{control bookTypeEditForm}
app/AdminModule/templates/BookType/default.latte
1
{block content}
2
<div class="row">
3
    <div class="col-11">
4
        <div class="display-5">Book Type List</div>
5
    </div>
6
    <div class="col-1">
7
        <a n:href="BookType:add" class="btn btn-sm btn-success"><span class="fa fa-fw fa-plus"></span> New Book Type</a>
8
    </div>
9
</div>
10
<div class="row">
11
    <div class="col-10 offset-1">
12
        {control bookTypeGrid}
13
    </div>
14
</div>
app/AdminModule/templates/BookType/edit.latte
1
{block content}
2
<div class="row">
3
    <div class="col-11">
4
        <div class="display-5">Edit Book Type</div>
5
    </div>
6
    <div class="col-1">
7
        <a n:href="BookType:default" class="btn btn-sm btn-success"><span class="fa fa-fw fa-list"></span> Book Type list</a>
8
    </div>
9
</div>
10
{control bookTypeEditForm}
app/config/components.neon
6 6
    - App\AdminModule\Components\IObjectTypeGridFactory
7 7
    - App\AdminModule\Components\IOriginGridFactory
8 8
    - App\AdminModule\Components\ISurfaceTypeGridFactory
9
    - App\AdminModule\Components\IBookTypeGridFactory
9 10

  
10 11
    # Formuláře
11 12
    - App\FrontModule\Components\ILoginFormFactory
......
14 15
    - App\AdminModule\Components\IObjectTypeEditFormFactory
15 16
    - App\AdminModule\Components\IOriginEditFormFactory
16 17
    - App\AdminModule\Components\ISurfaceTypeEditFormFactory
17
    - App\AdminModule\Components\IMuseumEditFormFactory
18
    - App\AdminModule\Components\IMuseumEditFormFactory
19
    - App\AdminModule\Components\IBookTypeEditFormFactory
app/model/repository/BookTypeRepository.php
16 16
    /** Sloupečky tabulky */
17 17
    const COLUMN_ID = 'id_book_type';
18 18
    const COLUMN_BOOK_TYPE = 'book_type';
19

  
20
    /**
21
     * Najde záznam podle typu knížky v DB
22
     *
23
     * @param string $bookType
24
     * @return \Nette\Database\Table\Selection
25
     */
26
    public function findByBookType(string $bookType)
27
    {
28
        return $this->findBy([self::COLUMN_BOOK_TYPE => $bookType]);
29
    }
30

  
19 31
}

Také k dispozici: Unified diff