1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace App\Components;
13:
14: use App\Lib\Forms\FormFactory,
15: Nette\Application\UI\Form;
16:
17:
18: 19: 20:
21: class FormControl extends Component {
22: private $form ;
23:
24: public function create() {
25: $form = New FormFactory($this->action->table);
26: $form->addButons($this->form, $this->param);
27: $form->addState();
28: $form->create();
29:
30:
31:
32: $row = $this->action->data;
33: if (!$row) {
34: $this->error('Záznam nenalezen');
35: } else if ($row instanceof \Nette\Database\Table\ActiveRow) {
36: $form['form']->setDefaults($row);
37: }
38:
39:
40:
41:
42:
43:
44: return $form;
45: }
46:
47:
48: 49: 50:
51: public function render() {
52: $this->controlTemplate = __DIR__ . '/templates/FormControl.latte';
53: $this->template->foot = $this->action->jidash;
54: parent::render();
55: }
56:
57: 58: 59: 60:
61: public function formSend($button) {
62: $this->updateData($button->form->values, (int) $this->getParameter('id'));
63: }
64:
65: public function editform() {
66: dump('edit');
67: }
68:
69: 70: 71:
72: public function deleteFormSucceeded() {
73: $this->action->data->delete($this->getParameter('id'));
74: $this->flashMessage('Záznam byl smazán');
75: $this->redirectDefault();
76: }
77:
78: 79: 80:
81: public function formCancel(\Nette\Forms\Controls\SubmitButton $button) {
82: dump($button);
83: $this->redirectDefault();
84: }
85:
86: 87: 88: 89: 90:
91: protected function updateData($values, $id = NULL) {
92: if ($id) {
93: $this->data->update($id, $values);
94: $this->flashMessage('Záznam byl upraven.');
95: } else {
96: $this->data->insert($values);
97: $this->flashMessage('Záznam byl přidán.');
98: }
99: $this->redirectDefault();
100: }
101:
102: 103: 104:
105: private function redirectDefault() {
106: $this->presenter->redirect('default', $this->action->source);
107: }
108: }
109: