1 <?php
2
3 namespace Objednavky;
4
5
6 use Nette\Application\UI;
7 use Nette\Utils\Html;
8 use Kdyby\BootstrapFormRenderer\BootstrapRenderer;
9 use PLus\Orders\Repositories;
10
11 12 13 14 15
16 class BranchPresenter extends BasePresenter {
17
18
19 public $branchRepository;
20
21
22 protected function createComponentGrid($name) {
23
24 $user = $this->getUser();
25
26 $grid = new \Grido\Grid($this, $name);
27 $grid->setmodel($this->branchRepository->getGridSelection());
28
29 $grid->translator->lang = 'cs';
30 $grid->defaultPerPage = 15;
31 $grid->filterRenderType = \Grido\Components\Filters\Filter::RENDER_INNER;
32 $grid->setRememberState(TRUE);
33
34 $grid->addColumnText('name', 'Název')
35 ->setFilterText()
36 ->setSuggestion();
37 $grid->getColumn('name')->headerPrototype->style['width'] = '50%';
38
39 $grid->addActionHref('view', 'Zobrazit')
40 ->setIcon('fa fa-eye fa-fw');
41
42 if ($user->isAllowed('setup', 'edit')) {
43 $grid->addActionHref('edit', 'Editovat')
44 ->setIcon('pencil fa-fw');
45 }
46
47
48 $grid->setExport();
49
50 return $grid;
51 }
52
53 public function renderView($id){
54 $item = $this->branchRepository->findById($id);
55 if (!$item) {
56 $this->error('Položka nebyla nalezena');
57 }
58
59 $this->template->item = $item;
60 }
61
62
63 public function actionEdit($id){
64
65 $item = $this->branchRepository->findById($id);
66
67
68 if ($id > 0 && $item === FALSE) {
69 $this->redirect('default');
70 }
71
72
73 if ($id > 0) {
74 $this['editForm']->setDefaults($item);
75
76 }
77
78
79 $this->template->id = $id;
80 }
81
82
83 protected function createComponentEditForm() {
84
85 $session = $this->getSession()->getSection('history');
86
87 if (isset($session->secbranch)) {
88 $backlink = $session->secbranch;
89 } else {
90 $backlink = $this->link('default');
91 }
92
93 $form = $this->createEditForm($backlink);
94 $form->onSuccess[] = $this->editFormSubmitted;
95
96 return $form;
97 }
98
99
100 public function editFormSubmitted($form) {
101
102 $fdata = $form->getValues();
103 $id = $this->getParameter('id');
104
105 if ($id) {
106 $this->branchRepository->updateById($id, $fdata);
107
108 $this->flashMessage("Záznam byl upraven.", "success");
109 } else {
110 $this->branchRepository->insertData($fdata);
111
112 $this->flashMessage("Záznam byl vložen.", "success");
113 }
114
115 $session = $this->getSession()->getSection('history');
116
117 if (isset($session->secbranch)) {
118 $backlink = $session->secbranch;
119 } else {
120 $backlink = $this->link('default');
121 }
122
123 $this->redirectUrl($backlink);
124 }
125
126
127
128
129 function createEditForm($backlink){
130
131 $user = $this->getUser();
132 $form = new UI\Form;
133 $form->setRenderer(new BootstrapRenderer());
134
135 $form->getElementPrototype()->class = 'form-horizontal';
136
137
138 $form->addText('name', 'Název:')
139 ->setRequired('Zadejte prosím název')
140 ->setAttribute('class', 'form-control');
141
142
143 $form->addSubmit('saving', 'Uložit a zpět')
144 ->setAttribute('class', 'btn btn-danger');
145
146
147 $form->addSubmit('back')
148 ->setAttribute('class', 'btn btn-outline btn-danger')
149 ->getControlPrototype()
150 ->setName('a')
151 ->href($backlink)
152 ->add(Html::el('i')->class('fa fa-reply'))
153 ->add(Html::el('span', ' Zpět'));
154
155 return $form;
156 }
157
158 }
159