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