1 <?php
2
3 namespace Objednavky;
4
5 use Nette\Application\UI;
6 use Nette\Utils\Html;
7 use Kdyby\BootstrapFormRenderer\BootstrapRenderer;
8 use PLus\Orders\Repositories;
9
10 11 12 13 14
15 class JustifyPresenter extends BasePresenter {
16
17
18
19 public $justifyRepository;
20
21
22
23 protected function createComponentGrid($name) {
24
25 $user = $this->getUser();
26
27 $grid = new \Grido\Grid($this, $name);
28 $grid->setmodel($this->justifyRepository->getGridSelection());
29
30 $grid->translator->lang = 'cs';
31 $grid->defaultPerPage = 15;
32 $grid->filterRenderType = \Grido\Components\Filters\Filter::RENDER_INNER;
33 $grid->setRememberState(TRUE);
34
35 $grid->addColumnText('text', 'Text')
36 ->setFilterText()
37 ->setSuggestion();
38 $grid->getColumn('text')->headerPrototype->style['width'] = '50%';
39
40 $column = new \Grido\Components\Columns\Boolean($grid, 'valid', 'Platnost');
41 $column->headerPrototype->style['width'] ='4%';
42 $grid->getColumn('valid')->headerPrototype->class[] = 'center';
43
44
45 $grid->addActionHref('view', 'Zobrazit')
46 ->setIcon('fa fa-eye fa-fw');
47
48 if ($user->isAllowed('catalog', 'edit')) {
49 $grid->addActionHref('edit', 'Editovat')
50 ->setIcon('pencil fa-fw')
51 ->setDisable(function($item){
52 return ($item->id == 1);
53 });
54 }
55
56 if ($user->isInRole('trader')) {
57 $grid->addActionEvent('check', 'Povolit', function($id) {
58 try {
59 $item = $this->justifyRepository->getCheckRecord($id);
60 if ($item->valid == 0) {
61 $this->justifyRepository->updateById($id, array('valid' => 1));
62 $this['grid']->reload();
63 }
64 else {
65 $this['grid']->reload();
66 }
67 } catch (\PDOException $ex) {
68 $this->flashMessage('Chyba při zápisu do DB.', 'error');
69 }
70 })
71 ->setIcon('fa fa-thumbs-o-up fa-fw')
72 ->setDisable(function($item){
73 return ($item->id == 1 OR $item->valid == 1 OR !$this->getUser()->isInRole('trader'));
74 });
75
76 $grid->addActionEvent('uncheck', 'Zakázat', function($id) {
77 try {
78 $item = $this->justifyRepository->getCheckRecord($id);
79 if ($item->valid == 1) {
80 $this->justifyRepository->updateById($id, array('valid' => 0));
81 $this['grid']->reload();
82 }
83 else {
84 $this['grid']->reload();
85 }
86 } catch (\PDOException $ex) {
87 $this->flashMessage('Chyba při zápisu do DB.', 'error');
88 }
89 })
90 ->setIcon('fa fa-thumbs-o-down fa-fw')
91 ->setDisable(function($item){
92 return ($item->id == 1 OR $item->valid == 0 OR !$this->getUser()->isInRole('trader'));
93 });
94 }
95
96
97 $grid->setExport();
98
99 }
100
101
102 public function renderView($id){
103 $item = $this->justifyRepository->findById($id);
104 if (!$item) {
105 $this->redirect('default');
106 }
107
108 $this->template->item = $item;
109 }
110
111
112 public function actionEdit($id){
113
114 $item = $this->justifyRepository->findById($id);
115
116
117 if ($id > 0 && $item === FALSE) {
118 $this->redirect('default');
119 }
120
121 if ($id == 1 && !$this->getUser()->isInRole('sa')) {
122 $this->flashMessage('Nemáte právo editovat položku č. '.$id, 'error');
123 $this->redirect('default');
124 }
125
126 if ($id > 0) {
127 $this['editForm']->setDefaults($item);
128
129 }
130
131
132 $this->template->id = $id;
133 }
134
135 protected function createComponentEditForm() {
136 $session = $this->getSession()->getSection('history');
137
138 if (isset($session->secjustify)) {
139 $backlink = $session->secjustify;
140 } else {
141 $backlink = $this->link('default');
142 }
143
144
145 $form = $this->createEditForm($backlink);
146 $form->onSuccess[] = $this->editFormSubmitted;
147
148 return $form;
149 }
150
151
152 protected function createComponentCreateForm() {
153
154 $session = $this->getSession()->getSection('history');
155
156 if (isset($session->secjustify)) {
157 $backlink = $session->secjustify;
158 } else {
159 $backlink = $this->link('default');
160 }
161
162
163 $form = $this->createEditForm($backlink);
164 $form->onSuccess[] = $this->editFormSubmitted;
165
166 return $form;
167 }
168
169
170 public function editFormSubmitted($form) {
171 $fdata = $form->getValues();
172 $id = $this->getParameter('id');
173
174
175 if ($id) {
176 $this->justifyRepository->updateById($id, $fdata);
177 $this->flashMessage("Záznam byl upraven.", "success");
178 } else {
179 $fdata->valid = 1;
180 $this->justifyRepository->insertData($fdata);
181 $this->flashMessage("Záznam byl vložen.", "success");
182 }
183
184 $session = $this->getSession()->getSection('history');
185
186 if (isset($session->secjustify)) {
187 $backlink = $session->secjustify;
188 } else {
189 $backlink = $this->link('default');
190 }
191
192 $this->redirectUrl($backlink);
193 }
194
195
196
197
198 function createEditForm($backlink){
199
200 $user = $this->getUser();
201 $form = new UI\Form;
202 $form->setRenderer(new BootstrapRenderer());
203
204 $form->getElementPrototype()->class = 'form-horizontal';
205
206
207 $form->addText('text', 'Text:')
208 ->setRequired('Zadejte prosím text')
209 ->setAttribute('class', 'form-control');
210
211 $form->addCheckbox('valid')
212 ->setAttribute('class', 'checkbox');
213
214 $form->addSubmit('saving', 'Uložit a zpět')
215 ->setAttribute('class', 'btn btn-danger');
216
217
218 $form->addSubmit('back')
219 ->setAttribute('class', 'btn btn-outline btn-danger')
220 ->getControlPrototype()
221 ->setName('a')
222 ->href($backlink)
223 ->add(Html::el('i')->class('fa fa-reply'))
224 ->add(Html::el('span', ' Zpět'));
225
226 return $form;
227 }
228
229
230 }
231