Revize cc435e16
Přidáno uživatelem Petr Lukašík před asi 6 roky(ů)
app/AdminModule/component/Book/BookEditForm.latte | ||
---|---|---|
1 |
<div class="row"> |
|
2 |
<div class="col-4"> |
|
3 |
{control form} |
|
4 |
</div> |
|
5 |
</div> |
app/AdminModule/component/Book/BookEditForm.php | ||
---|---|---|
1 |
<?php |
|
2 |
|
|
3 |
|
|
4 |
namespace App\AdminModule\Components; |
|
5 |
|
|
6 |
|
|
7 |
use App\Enum\EFlashMessage; |
|
8 |
use App\Model\Repository\BookRepository; |
|
9 |
use App\Utils\Form; |
|
10 |
use Nette\Application\UI\Control; |
|
11 |
|
|
12 |
class BookEditForm extends Control |
|
13 |
{ |
|
14 |
private $bookId; |
|
15 |
|
|
16 |
/** |
|
17 |
* @var Form |
|
18 |
*/ |
|
19 |
private $form; |
|
20 |
/** |
|
21 |
* @var BookRepository |
|
22 |
*/ |
|
23 |
private $bookRepository; |
|
24 |
|
|
25 |
public function __construct(BookRepository $bookRepository) |
|
26 |
{ |
|
27 |
parent::__construct(); |
|
28 |
|
|
29 |
$this->form = new Form; |
|
30 |
$this->bookId = NULL; |
|
31 |
$this->bookRepository = $bookRepository; |
|
32 |
} |
|
33 |
|
|
34 |
public function render() |
|
35 |
{ |
|
36 |
$this->template->setFile(__DIR__ . '/BookEditForm.latte'); |
|
37 |
$this->template->render(); |
|
38 |
} |
|
39 |
|
|
40 |
public function createComponentForm() |
|
41 |
{ |
|
42 |
$this->form->addText(BookRepository::COLUMN_BOOK_ABREV, 'Abbreviation') |
|
43 |
->addRule(Form::REQUIRED, 'Field %label is required', TRUE); |
|
44 |
$this->form->addText(BookRepository::COLUMN_BOOK_AUTHOR, 'Author'); |
|
45 |
$this->form->addText(BookRepository::COLUMN_BOOK_NAME, 'Name') |
|
46 |
->addRule(Form::REQUIRED, 'Field %label is required', TRUE); |
|
47 |
$this->form->addText(BookRepository::COLUMN_BOOK_SUBTITLE, 'Alternate name'); |
|
48 |
$this->form->addTextArea(BookRepository::COLUMN_BOOK_DESCRIPTION, 'Description'); |
|
49 |
$this->form->addText(BookRepository::COLUMN_PLACE_OF_PUB, 'Publication place'); |
|
50 |
$this->form->addText(BookRepository::COLUMN_DATE_OF_PUB, 'Publication date'); |
|
51 |
$this->form->addText(BookRepository::COLUMN_PAGES, 'Pages'); |
|
52 |
$this->form->addText(BookRepository::COLUMN_VOLUME, 'Volume'); |
|
53 |
$this->form->addText(BookRepository::COLUMN_VOLUME_NO, 'Volume No.'); |
|
54 |
|
|
55 |
$this->form->setDefaults($this->getDefaults()); |
|
56 |
|
|
57 |
$this->form->addSubmit('submit', 'Save'); |
|
58 |
|
|
59 |
$this->form->onSuccess[] = [$this, 'formSuccess']; |
|
60 |
|
|
61 |
return $this->form; |
|
62 |
} |
|
63 |
|
|
64 |
public function formSuccess(Form $form) |
|
65 |
{ |
|
66 |
$result = $this->bookRepository->save($form->getValues(TRUE), $this->bookId); |
|
67 |
|
|
68 |
if ($result) |
|
69 |
{ |
|
70 |
$this->presenter->flashMessage('Book was added successfully.', EFlashMessage::SUCCESS); |
|
71 |
$this->presenter->redirect('Book:edit', ['id' => $result->{BookRepository::COLUMN_ID}]); |
|
72 |
} else |
|
73 |
{ |
|
74 |
$form->addError('Book couldn\'t be added. Please try it again later.'); |
|
75 |
} |
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* Nastavení ID při editaci |
|
80 |
* |
|
81 |
* @param int $bookId |
|
82 |
*/ |
|
83 |
public function setId(int $bookId) |
|
84 |
{ |
|
85 |
$this->bookId = $bookId; |
|
86 |
} |
|
87 |
|
|
88 |
/** |
|
89 |
* Vrací hodnoty z databáze při editaci existující knihy |
|
90 |
* |
|
91 |
* @return array |
|
92 |
*/ |
|
93 |
public function getDefaults() |
|
94 |
{ |
|
95 |
$defaultValues = array(); |
|
96 |
|
|
97 |
if (!empty($this->bookId)) |
|
98 |
{ |
|
99 |
$bookRow = $this->bookRepository->findRow($this->bookId); |
|
100 |
|
|
101 |
if ($bookRow) |
|
102 |
{ |
|
103 |
$defaultValues = $bookRow->toArray(); |
|
104 |
} |
|
105 |
} |
|
106 |
|
|
107 |
return $defaultValues; |
|
108 |
} |
|
109 |
} |
|
110 |
|
|
111 |
interface IBookEditFormFactory |
|
112 |
{ |
|
113 |
/** |
|
114 |
* @return BookEditForm |
|
115 |
*/ |
|
116 |
public function create(); |
|
117 |
} |
app/AdminModule/component/Book/BookGrid.php | ||
---|---|---|
1 |
<?php |
|
2 |
|
|
3 |
|
|
4 |
namespace App\AdminModule\Components; |
|
5 |
|
|
6 |
use App\Model\Repository\BookRepository; |
|
7 |
use App\Utils\DataGrid\DataGrid; |
|
8 |
use Ublaboo\DataGrid\Exception\DataGridException; |
|
9 |
|
|
10 |
class BookGrid extends DataGrid |
|
11 |
{ |
|
12 |
/** |
|
13 |
* @var BookRepository |
|
14 |
*/ |
|
15 |
private $bookRepository; |
|
16 |
|
|
17 |
public function __construct(BookRepository $bookRepository) |
|
18 |
{ |
|
19 |
$this->bookRepository = $bookRepository; |
|
20 |
|
|
21 |
parent::__construct(false); |
|
22 |
} |
|
23 |
|
|
24 |
/** |
|
25 |
* Abstraktní metoda, slouží k nastavení primárního klíče a nastavení datasource |
|
26 |
* 1. $this->setPrimaryKey(); |
|
27 |
* 2. $this->setDataSource(); |
|
28 |
* |
|
29 |
* @throws DataGridException |
|
30 |
*/ |
|
31 |
public function init() |
|
32 |
{ |
|
33 |
$this->setPrimaryKey(BookRepository::COLUMN_ID); |
|
34 |
$this->setDataSource($this->bookRepository->findAll()->order(BookRepository::COLUMN_BOOK_ABREV)); |
|
35 |
} |
|
36 |
|
|
37 |
/** |
|
38 |
* Definice sloupečků, akcí, vyhledávácích filtrů gridu |
|
39 |
* |
|
40 |
* @throws DataGridException |
|
41 |
*/ |
|
42 |
public function define() |
|
43 |
{ |
|
44 |
// Definice sloupečků |
|
45 |
$this->addColumnNumber(BookRepository::COLUMN_ID, 'ID')->setDefaultHide(TRUE); |
|
46 |
$this->addColumnText(BookRepository::COLUMN_BOOK_ABREV, 'Abbreviation')->setAlign('center'); |
|
47 |
$this->addColumnText(BookRepository::COLUMN_BOOK_AUTHOR, 'Author')->addAttributes(["width" => "10%"])->setAlign('center'); |
|
48 |
$this->addColumnText(BookRepository::COLUMN_BOOK_NAME, 'Name')->setAlign('center'); |
|
49 |
$this->addColumnText(BookRepository::COLUMN_BOOK_SUBTITLE, 'Alternate name')->setAlign('center'); |
|
50 |
$this->addColumnText(BookRepository::COLUMN_BOOK_DESCRIPTION, 'Description')->addAttributes(["width" => "20%"])->setAlign('center'); |
|
51 |
$this->addColumnText(BookRepository::COLUMN_PLACE_OF_PUB, 'Publication place')->setAlign('center'); |
|
52 |
$this->addColumnText(BookRepository::COLUMN_DATE_OF_PUB, 'Publication date')->setAlign('center'); |
|
53 |
$this->addColumnText(BookRepository::COLUMN_PAGES, 'Pages')->setAlign('center'); |
|
54 |
$this->addColumnText(BookRepository::COLUMN_VOLUME, 'Volume')->setAlign('center'); |
|
55 |
$this->addColumnText(BookRepository::COLUMN_VOLUME_NO, 'Volume No.')->setAlign('center'); |
|
56 |
|
|
57 |
// Definice filtrů |
|
58 |
$this->addFilterText(BookRepository::COLUMN_BOOK_ABREV, 'Abbreviation'); |
|
59 |
$this->addFilterText(BookRepository::COLUMN_BOOK_AUTHOR, 'Author'); |
|
60 |
$this->addFilterText(BookRepository::COLUMN_BOOK_NAME, 'Name'); |
|
61 |
$this->addFilterText(BookRepository::COLUMN_BOOK_SUBTITLE, 'Alternate name'); |
|
62 |
$this->addFilterText(BookRepository::COLUMN_BOOK_DESCRIPTION, 'Description'); |
|
63 |
$this->addFilterText(BookRepository::COLUMN_PLACE_OF_PUB, 'Publication place'); |
|
64 |
$this->addFilterText(BookRepository::COLUMN_DATE_OF_PUB, 'Publication date'); |
|
65 |
$this->addFilterText(BookRepository::COLUMN_PAGES, 'Pages'); |
|
66 |
$this->addFilterText(BookRepository::COLUMN_VOLUME, 'Volume'); |
|
67 |
$this->addFilterText(BookRepository::COLUMN_VOLUME_NO, 'Volume No.'); |
|
68 |
|
|
69 |
// Definice akcí |
|
70 |
$this->addAction('edit', 'edit', 'Book:edit', ['id' => BookRepository::COLUMN_ID]) |
|
71 |
->setTitle('Edit'); |
|
72 |
|
|
73 |
$this->addAction('delete', 'delete', 'deleteBook!', ['id' => BookRepository::COLUMN_ID]) |
|
74 |
->setConfirm('Do you really want to delete book %s.', BookRepository::COLUMN_BOOK_ABREV) |
|
75 |
->setTitle('Delete') |
|
76 |
->setClass('btn btn-xs btn-danger ajax'); |
|
77 |
} |
|
78 |
} |
|
79 |
|
|
80 |
interface IBookGridFactory |
|
81 |
{ |
|
82 |
/** |
|
83 |
* @return BookGrid |
|
84 |
*/ |
|
85 |
public function create(); |
|
86 |
} |
app/AdminModule/presenters/BookPresenter.php | ||
---|---|---|
1 |
<?php |
|
2 |
|
|
3 |
|
|
4 |
namespace App\AdminModule\Presenters; |
|
5 |
|
|
6 |
use App\AdminModule\Components\IBookEditFormFactory; |
|
7 |
use App\AdminModule\Components\IBookGridFactory; |
|
8 |
use App\Enum\EFlashMessage; |
|
9 |
use App\Model\Repository\BookRepository; |
|
10 |
|
|
11 |
class BookPresenter extends BaseUserPresenter |
|
12 |
{ |
|
13 |
/** |
|
14 |
* @var IBookGridFactory |
|
15 |
*/ |
|
16 |
private $bookGridFactory; |
|
17 |
/** |
|
18 |
* @var IBookEditFormFactory |
|
19 |
*/ |
|
20 |
private $bookEditFormFactory; |
|
21 |
/** |
|
22 |
* @var BookRepository |
|
23 |
*/ |
|
24 |
private $bookRepository; |
|
25 |
|
|
26 |
public function __construct( |
|
27 |
IBookGridFactory $bookGridFactory, |
|
28 |
IBookEditFormFactory $bookEditFormFactory, |
|
29 |
BookRepository $bookRepository |
|
30 |
) |
|
31 |
{ |
|
32 |
parent::__construct(); |
|
33 |
|
|
34 |
$this->bookGridFactory = $bookGridFactory; |
|
35 |
$this->bookEditFormFactory = $bookEditFormFactory; |
|
36 |
$this->bookRepository = $bookRepository; |
|
37 |
} |
|
38 |
|
|
39 |
public function actionEdit(int $id) |
|
40 |
{ |
|
41 |
$this->template->id = $id; |
|
42 |
$this['bookEditForm']->setId($id); |
|
43 |
} |
|
44 |
|
|
45 |
/** |
|
46 |
* Akce pro odstranění knihy |
|
47 |
* |
|
48 |
* @param int $id |
|
49 |
* @throws \Nette\Application\AbortException |
|
50 |
*/ |
|
51 |
public function actionDelete(int $id) |
|
52 |
{ |
|
53 |
$result = $this->bookRepository->findRow($id)->delete(); |
|
54 |
if ($result) { |
|
55 |
$this->flashMessage('Book was successfully deleted.', EFlashMessage::SUCCESS); |
|
56 |
} else { |
|
57 |
$this->flashMessage('Book wasn\'t deleted.', EFlashMessage::ERROR); |
|
58 |
} |
|
59 |
|
|
60 |
$this->redirect('Book:default'); |
|
61 |
} |
|
62 |
|
|
63 |
/** |
|
64 |
* Handle používaný v BookGrid pro smazání knihy |
|
65 |
* |
|
66 |
* @param int $id : ID mazané knihy |
|
67 |
*/ |
|
68 |
public function handleDeleteBook(int $id) |
|
69 |
{ |
|
70 |
if ($this->isAjax()) { |
|
71 |
$this->bookRepository->findRow($id)->delete(); |
|
72 |
$this['bookGrid']->reload(); |
|
73 |
} |
|
74 |
} |
|
75 |
|
|
76 |
public function createComponentBookGrid() |
|
77 |
{ |
|
78 |
return $this->bookGridFactory->create(); |
|
79 |
} |
|
80 |
|
|
81 |
public function createComponentBookEditForm() |
|
82 |
{ |
|
83 |
return $this->bookEditFormFactory->create(); |
|
84 |
} |
|
85 |
} |
app/AdminModule/templates/@layout.latte | ||
---|---|---|
99 | 99 |
<li class="nav-item"><a n:href="User:default" n:class="nav-link, $presenter->isLinkCurrent('User:*') ? active" title="Users"> |
100 | 100 |
Users </a></li> |
101 | 101 |
|
102 |
<li class="nav-item"><a href="#" class="nav-link" title="Book">
|
|
102 |
<li class="nav-item"><a n:href="Book:default" n:class="nav-link, $presenter->isLinkCurrent('Book:*') ? active" title="Book">
|
|
103 | 103 |
Book </a></li> |
104 | 104 |
<li class="nav-item"><a href="#" class="nav-link" title="Transliteration"> |
105 | 105 |
Transliteration </a></li> |
app/AdminModule/templates/Book/add.latte | ||
---|---|---|
1 |
{block content} |
|
2 |
<div class="row"> |
|
3 |
<div class="col-11"> |
|
4 |
<div class="display-5">New book</div> |
|
5 |
</div> |
|
6 |
<div class="col-1"> |
|
7 |
<a n:href="Book:default" class="btn btn-sm btn-success"><span class="fa fa-fw fa-list"></span> Book list</a> |
|
8 |
</div> |
|
9 |
</div> |
|
10 |
{control bookEditForm} |
app/AdminModule/templates/Book/default.latte | ||
---|---|---|
1 |
{block content} |
|
2 |
<div class="row"> |
|
3 |
<div class="col-11"> |
|
4 |
<div class="display-5">Book List</div> |
|
5 |
</div> |
|
6 |
<div class="col-1"> |
|
7 |
<a n:href="Book:add" class="btn btn-sm btn-success"><span class="fa fa-fw fa-plus"></span> New Book</a> |
|
8 |
</div> |
|
9 |
</div> |
|
10 |
<div class="row"> |
|
11 |
<div class="col-10 offset-1"> |
|
12 |
{control bookGrid} |
|
13 |
</div> |
|
14 |
</div> |
app/AdminModule/templates/Book/edit.latte | ||
---|---|---|
1 |
{block content} |
|
2 |
<div class="row"> |
|
3 |
<div class="col-10"> |
|
4 |
<div class="display-5">Edit Book</div> |
|
5 |
</div> |
|
6 |
<div class="col-2 text-right"> |
|
7 |
<a n:href="Book:default" class="btn btn-sm btn-success"><span class="fa fa-fw fa-list"></span> Book list</a> |
|
8 |
<a n:href="Book:delete $id" class="btn btn-sm btn-danger" onclick="return confirm('Do you really want to delete this book?')"><span class="fa fa-fw fa-trash"></span> Delete book</a> |
|
9 |
</div> |
|
10 |
</div> |
|
11 |
{control bookEditForm} |
app/config/components.neon | ||
---|---|---|
7 | 7 |
- App\AdminModule\Components\IOriginGridFactory |
8 | 8 |
- App\AdminModule\Components\ISurfaceTypeGridFactory |
9 | 9 |
- App\AdminModule\Components\IBookTypeGridFactory |
10 |
- App\AdminModule\Components\IBookGridFactory |
|
10 | 11 |
|
11 | 12 |
# Formuláře |
12 | 13 |
- App\FrontModule\Components\ILoginFormFactory |
... | ... | |
16 | 17 |
- App\AdminModule\Components\IOriginEditFormFactory |
17 | 18 |
- App\AdminModule\Components\ISurfaceTypeEditFormFactory |
18 | 19 |
- App\AdminModule\Components\IMuseumEditFormFactory |
19 |
- App\AdminModule\Components\IBookTypeEditFormFactory |
|
20 |
- App\AdminModule\Components\IBookTypeEditFormFactory |
|
21 |
- App\AdminModule\Components\IBookEditFormFactory |
Také k dispozici: Unified diff
Re #7331 Implementace správy knih v administraci