1 <?php
2
3 namespace Objednavky;
4
5 use Nette\Application\UI;
6 use Nette\Utils\Html;
7 use Nette\Database\Table\Selection;
8 use Kdyby\BootstrapFormRenderer\BootstrapRenderer;
9 use PLus\Orders\Repositories;
10 11 12 13 14
15 class UnitPresenter extends BasePresenter {
16
17
18
19 public $departmentRepository;
20
21
22 public $unitRepository;
23
24
25 public function createComponentGrid($name) {
26
27 $user = $this->getUser();
28
29 $grid = new \Grido\Grid($this, $name);
30 $grid->setmodel($this->unitRepository->getGridSelection());
31
32 $grid->translator->lang = 'cs';
33 $grid->defaultPerPage = 15;
34 $grid->filterRenderType = \Grido\Components\Filters\Filter::RENDER_INNER;
35 $grid->setRememberState(TRUE);
36
37 $grid->addColumnText('name', 'Nákladové středisko')
38 ->setSortable()
39 ->setColumn('name')
40 ->setFilterText()
41 ->setColumn('name')
42 ->setWhere(function ($value, $connection){ $connection->where("unit.name LIKE ?", "%$value%");})
43 ->setSuggestion();
44 $grid->getColumn('name')->headerPrototype->style['width'] = '25%';
45
46 $grid->addColumnText('ns', 'Číslo NS')
47 ->setSortable()
48 ->setFilterNumber();
49 $grid->getColumn('ns')->cellPrototype->class[] = 'center';
50 $grid->getColumn('ns')->headerPrototype->style['width'] = '8%';
51
52
53 $column = new \Grido\Components\Columns\Boolean($grid, 'validity', 'Platnost');
54 $column->setSortable()
55 ->headerPrototype->style['width'] ='4%';
56 $grid->getColumn('validity')->headerPrototype->class[] = 'center';
57
58 $list = array(''=>'','0'=>'ne','1'=>'ano');
59 $grid->addFilterSelect('validity', '', $list);
60
61 $grid->addColumnText('dname', 'Oddělení')
62 ->setSortable()
63 ->setColumn('dname')
64 ->setFilterText()
65 ->setColumn('dname')
66 ->setWhere(function ($value, $connection){ $connection->where("department.name LIKE ?", "%$value%");});
67 $grid->getColumn('dname')->headerPrototype->style['width'] = '25%';
68
69 $grid->addActionHref('view', 'Zobrazit')
70 ->setIcon('fa fa-eye fa-fw');
71
72 if ($user->isAllowed('setup', 'edit')) {
73 $grid->addActionHref('edit', 'Editovat')
74 ->setIcon('pencil fa-fw');
75 }
76
77
78 $grid->setExport();
79
80 }
81
82
83 public function renderView($id){
84 $item = $this->unitRepository->findById($id);
85 if (!$item) {
86 $this->redirect('default');
87 }
88
89 $this->template->items = $item;
90 }
91
92
93 public function actionEdit($id){
94
95 $item = $this->unitRepository->findById($id);
96
97
98 if ($id > 0 && $item === FALSE) {
99 $this->redirect('default');
100 }
101
102
103 if ($id > 0) {
104 $this['editForm']->setDefaults($item);
105 }
106
107
108 $this->template->id = $id;
109 }
110
111
112 protected function createComponentEditForm() {
113 $session = $this->getSession()->getSection('history');
114
115 if (isset($session->secunit)) {
116 $backlink = $session->secunit;
117 } else {
118 $backlink = $this->link('default');
119 }
120
121
122 $form = $this->createEditForm($backlink);
123 $form->onSuccess[] = $this->editFormSubmitted;
124
125 return $form;
126 }
127
128
129 public function editFormSubmitted($form) {
130 $fdata = $form->getValues();
131 $id = $this->getParameter('id');
132
133
134 if ($id) {
135 $this->unitRepository->updateById($id, $fdata);
136 $this->flashMessage("Záznam byl upraven.", "success");
137 } else {
138 $this->unitRepository->insertData($fdata);
139 $this->flashMessage("Záznam byl vložen.", "success");
140 }
141
142 $session = $this->getSession()->getSection('history');
143
144 if (isset($session->secunit)) {
145 $backlink = $session->secunit;
146 } else {
147 $backlink = $this->link('default');
148 }
149
150 $this->redirectUrl($backlink);
151 }
152
153 function createEditForm($backlink){
154
155 $user = $this->getUser();
156 $form = new UI\Form();
157 $form->setRenderer(new BootstrapRenderer());
158
159 $form->getElementPrototype()->class = 'form-horizontal';
160
161
162 $form->addText('name', 'Název:')
163 ->setRequired('Zadejte prosím název')
164 ->setAttribute('class', 'form-control');
165
166 $form->addText('ns', 'Číslo NS:')
167 ->setRequired('Zadejte prosím celočíselnou hodnotu')
168 ->setAttribute('class', 'form-control');
169
170 $form->addCheckbox('validity')
171 ->setAttribute('class', 'checkbox');
172
173 $form->addSelect('department_id', 'Oddělení:', $this->departmentRepository->getPairData())
174 ->setAttribute('class', 'form-control2');
175
176 if ($user->isAllowed('setup', 'edit')) {
177 $form->addSubmit('saving', 'Uložit a zpět')
178 ->setAttribute('class', 'btn btn-danger');
179 }
180
181 $form->addSubmit('back')
182 ->setAttribute('class', 'btn btn-outline btn-danger')
183 ->getControlPrototype()
184 ->setName('a')
185 ->href($backlink)
186 ->add(Html::el('i')->class('fa fa-reply'))
187 ->add(Html::el('span', ' Zpět'));
188
189 return $form;
190 }
191
192
193
194
195 }
196