1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace App\Presenters;
13:
14: use Nette\Application\UI\Form,
15: App\Components\ITreeControlFactory,
16: App\Components\IMenuControlFactory,
17: App\Components\ITableControlFactory,
18: App\Lib\Forms\FormFactory,
19: App\Lib\Statics\Cons;
20:
21: 22: 23: 24:
25: class ComponentPresenter extends SecurePresenter {
26:
27:
28: private $menu;
29:
30:
31: private $table;
32:
33:
34:
35:
36:
37: private $tree;
38:
39: public function __construct() {
40: parent::__construct();
41: }
42:
43: 44: 45: 46:
47: public function injectMenu(IMenuControlFactory $factory) {
48: $this->menu = $factory->create();
49: }
50:
51: 52: 53: 54:
55: public function injectTable(ITableControlFactory $factory) {
56: $this->table = $factory->create();
57: }
58:
59: 60: 61: 62:
63: public function injectTree(ITreeControlFactory $factory) {
64: $this->tree = $factory->create();
65: }
66:
67: 68: 69: 70:
71: public function createComponentMenu() {
72: return $this->menu;
73: }
74:
75: 76: 77: 78:
79: public function createComponentTable() {
80: $this->table->params = $this->link('show', array('source' => $this->source));
81: return $this->table;
82: }
83:
84: 85: 86: 87:
88: public function createComponentSubTable() {
89: $this->table->params = array('link' => $this->subrequest());
90: return $this->table;
91: }
92:
93: 94: 95: 96:
97: protected function createComponentTree() {
98: $this->tree->params = $this->link('show', array('source' => $this->source));
99: return $this->tree;
100: }
101:
102: 103: 104: 105:
106: public function createComponentDeleteForm() {
107: $form = new Form;
108: $form->addSubmit('delete', 'Smazat')
109: ->setAttribute('class', 'default button')
110: ->onClick[] = array($this, 'deleteFormSucceeded');
111: $form->addSubmit('cancel', 'Zrušit')
112: ->setAttribute('class', 'button')
113: ->onClick[] = array($this, 'formCancel');
114: $form->addProtection();
115: return $form;
116: }
117:
118: 119: 120:
121: public function deleteFormSucceeded() {
122: $result = $this->action->delete($this->action->data);
123: if (!$result) {
124: $this->flashMessage('Nemáte potřebná oprávnění');
125: } else if ($result == 1) {
126: $this->flashMessage('Záznam byl nenávratně Smazán');
127: } else {
128: $this->flashMessage('Nejprve je nutné smazat všechny záznamy, které se na tento záznam odkazují');
129: }
130: $this->restoreRequest($this->backlink);
131: $this->redirectDefault();
132: }
133:
134: 135: 136: 137:
138: public function createComponentShowForm() {
139: $this->action->params = $this->params;
140: $form = New FormFactory($this->action);
141: $form->addButons($this, FALSE);
142: $form->addState(FALSE);
143: $form->addDesc(FALSE);
144: $form->create(FALSE);
145: return $form;
146: }
147:
148: 149: 150: 151:
152: public function createComponentEditForm() {
153: $this->action->params = $this->params;
154: $form = New FormFactory($this->action);
155: $form->addButons($this, TRUE);
156: $form->addState(TRUE);
157: $form->addDesc(TRUE);
158: $form->create(TRUE);
159: return $form;
160: }
161:
162: 163: 164: 165:
166: public function formSend($button) {
167: $values = $button->form->values;
168: $data = $values['form'];
169: (!isset($values->state)) ? : $data[Cons::COLUMN_STATE] = $values->state;
170: $id = (int) $this->getParameter('id');
171: $finalData = $this->beforeProcess($data);
172: $result = $this->action->update(\App\Lib\Statics\Vars::setNULL($finalData), $id);
173: if ($result === FALSE) {
174: $this->flashMessage('Nemáte potřebná oprávnění');
175: } else if ($result) {
176: if ($result instanceof \Nette\Database\Table\ActiveRow){
177: $this->afterProcess($result[Cons::COLUMN_ID]);
178: }else {
179: $this->afterProcess($id);
180: }
181: ($id) ? $this->flashMessage('Záznam byl upraven.') : $this->flashMessage('Záznam byl přidán.');
182: } else {
183: $this->flashMessage('Záznam se nepodařilo uložit');
184: }
185: $this->restoreRequest($this->backlink);
186: $this->redirect('show', $this->source, ($id) ? $id : $result[Cons::COLUMN_ID]);
187: }
188:
189: 190: 191:
192: public function formCancel() {
193: $this->restoreRequest($this->backlink);
194: $this->redirectDefault();
195: }
196: }
197: