1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace App\Lib\Forms;
13:
14: use Nette\Application\UI\Form,
15: App\Lib\Statics\Cons,
16: Nette\Forms\Container as FormContainer;
17:
18: 19: 20:
21: class Controls extends Form {
22:
23: 24: 25:
26: const BUTTONS = 'buttons', STATE = 'state', FORM = 'form';
27:
28: 29: 30: 31:
32: protected $state;
33:
34: 35: 36: 37:
38: protected $desc;
39:
40: 41: 42: 43:
44: protected $fields;
45:
46: 47: 48: 49:
50: protected $query;
51:
52: 53: 54: 55:
56: protected $action;
57:
58: 59: 60: 61:
62: private $bar;
63:
64: public function __construct(\Nette\ComponentModel\IContainer $parent = NULL, $name = NULL) {
65: parent::__construct($parent, $name);
66: $this->registerExtensions();
67: }
68:
69: 70: 71: 72:
73: public function addButons($parent, $edit = TRUE) {
74: $this->bar = $this->addContainer(self::BUTTONS);
75: if ($edit === TRUE) {
76: $this->bar = $this->addSave($parent);
77: }
78: $this->bar = $this->addCancel($parent);
79: }
80:
81: 82: 83:
84: public function addState($disabled = FALSE) {
85: if (isset($this->fields[Cons::COLUMN_STATE])) {
86: $state = $this->fields[Cons::COLUMN_STATE];
87: $array = explode(',', $state[Cons::COLUMN_RELATION]);
88: $this->bar = $this->addStateRadioList($state[Cons::COLUMN_FIELD])
89: ->setItems($array, FALSE)
90: ->getSeparatorPrototype()->setName(NULL);
91: ($disabled) ? : $this[$state[Cons::COLUMN_FIELD]]->setDisabled(TRUE);
92: }
93: }
94:
95: 96: 97:
98: public function adddesc($disabled = FALSE) {
99: if (isset($this->fields[Cons::COLUMN_DESC])) {
100: $desc = $this->fields[Cons::COLUMN_DESC];
101: $this->bar = $this->addTextArea($desc[Cons::COLUMN_FIELD], $desc[Cons::COLUMN_NAME])
102: ->addRule(Form::MAX_LENGTH, 'Poznámka je příliš dlouhá', $desc[Cons::COLUMN_LENGHT])
103: ->setAttribute('class', 'desc');
104: ($disabled) ? : $this[$desc[Cons::COLUMN_FIELD]]->setDisabled(TRUE);
105: }
106: }
107:
108: 109: 110: 111: 112: 113: 114:
115: public function addStateRadioList($name, $label = NULL, array $items = NULL) {
116: return $this[$name] = new Controls\StateRadioList($label, $items);
117: }
118:
119: 120: 121: 122:
123: protected function addSave($parent) {
124: $this->addSubmit('save', 'Uložit')
125: ->setAttribute('class', 'default button')
126: ->onClick[] = array($parent, 'formSend');
127: }
128:
129: 130: 131: 132:
133: protected function addCancel($parent) {
134: $this->addSubmit('cancel', 'Zpět')
135: ->setAttribute('class', 'button')
136: ->setValidationScope(array())
137: ->onClick[] = array($parent, 'formCancel');
138: }
139:
140: 141: 142: 143:
144: private function registerExtensions() {
145: 146: 147:
148: FormContainer::extensionMethod('addSelector', function (FormContainer $container, $name, $label, $lenght) {
149: $items = $this->action->getRelation($this->query[Cons::COLUMN_FIELD], $this->query[Cons::COLUMN_RELATION]);
150: $control = new \Nette\Forms\Controls\SelectBox($label, $items);
151: if ($lenght > 1) {
152: $control->setAttribute('size', (int) $lenght);
153: }
154: return $container[$name] = $control;
155: });
156:
157: 158: 159:
160: FormContainer::extensionMethod('addDatePicker', function (FormContainer $container, $name, $label = NULL) {
161: return $container[$name] = new \App\Lib\Forms\Controls\DatePicker($label);
162: });
163:
164: 165: 166: 167: 168: 169:
170: FormContainer::extensionMethod('addCheckboxText', function (FormContainer $container, $name, $caption = NULL) {
171: return $container[$name] = new \App\Lib\Forms\Controls\CheckboxText($caption);
172: });
173:
174: 175: 176: 177: 178: 179: 180:
181: FormContainer::extensionMethod('addRadio', function (FormContainer $container, $name, $label) {
182: $items = explode(',', $this->query[Cons::COLUMN_RELATION]);
183: $container[$name] = new \App\Lib\Forms\Controls\StateRadioList($label, $items);
184: return $container[$name]->setItems($items, FALSE)
185: ->getSeparatorPrototype()->setName(NULL);
186: });
187: }
188:
189: }
190: