1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace App\Lib\Forms;
13:
14: use Nette,
15: Nette\Application\UI\Form,
16: App\Model\Action,
17: App\Lib\Statics\Cons,
18: App\Lib\Statics\Vars;
19:
20: 21: 22:
23: class FormFactory extends Controls {
24:
25: 26: 27: 28: 29: 30:
31: public function __construct(Action $action, Nette\ComponentModel\IContainer $parent = NULL, $name = NULL) {
32: parent::__construct($parent, $name);
33: $this->fields = $action->table;
34: $this->action = $action;
35: }
36:
37: 38: 39:
40: public function create($disabled = FALSE) {
41: $form = $this->addContainer(self::FORM);
42: foreach ($this->fields as $field) {
43: $this->query = $field;
44: ($disabled) ? : $this->query[Cons::COLUMN_READONLY] = TRUE;
45: if ($this->query[Cons::COLUMN_FIELD] === Cons::COLUMN_PASSWORD) {
46: (!$disabled)? : $form = $this->Password($disabled);
47: } else if ($this->query[Cons::COLUMN_FIELD] === Cons::COLUMN_STATE) {
48: $form = $this->state($disabled);
49: } else if ($this->query[Cons::COLUMN_FIELD] === Cons::COLUMN_DESC) {
50: $form = $this->desc($disabled);
51: } else {
52: $form = $this->createField();
53: }
54: }
55: }
56:
57: 58: 59: 60:
61: public function createField() {
62: $field = $this->field(
63: $this->query[Cons::TABLE_TABLE . '_' . Cons::TABLE_ELEMENTS . '_' . Cons::COLUMN_ID],
64: $this->query[Cons::COLUMN_FIELD],
65: $this->query[Cons::COLUMN_NAME],
66: $this->query[Cons::COLUMN_LENGHT]
67: );
68: (!isset($this->query[Cons::COLUMN_CLASS])) ? : $field->setAttribute('class', $this->query[Cons::COLUMN_CLASS] . ' tooltip');
69: (!Vars::trueFalse($this->query[Cons::COLUMN_REQUIRED])) ? : $field->setRequired("Pole %label je povinné");
70: (!Vars::trueFalse($this->query[Cons::COLUMN_READONLY])) ? : $field->setDisabled(TRUE);
71: (!isset($this->query[Cons::COLUMN_DESC])) ? : $this->tooltip($field);
72: return $field;
73: }
74:
75: 76: 77:
78: private function tooltip($field) {
79: return $field->setAttribute('title', $this->query[Cons::COLUMN_DESC]);
80: }
81:
82: 83: 84:
85: private function Password() {
86: $pass = $this->createField();
87: $pass->addCondition(Form::FILLED, TRUE)
88: ->addRule(Form::MIN_LENGTH, 'Heslo musí mít alespoň %d znaky', 8)
89: ->addRule(Form::PATTERN, 'Musí obsahovat číslici', '.*[0-9].*');
90:
91: $this[self::FORM]->addPassword('password_verify', 'Heslo pro kontrolu', $this->query[Cons::COLUMN_LENGHT])
92:
93: ->addRule(Form::EQUAL, 'Hesla se neshodují', $pass)
94: ->setAttribute('class', $this->query[Cons::COLUMN_CLASS] . ' tooltip')
95: ->setAttribute('title', 'Zadejte prosím heslo ještě jednou pro kontrolu')
96: ->setOmitted(TRUE);
97: }
98:
99: 100: 101:
102: private function state($disabled) {
103: $this->state = $this->query;
104: ($disabled) ? : $this->state[Cons::COLUMN_READONLY] = TRUE;
105: }
106:
107: 108: 109:
110: private function desc($disabled) {
111: $this->desc = $this->query;
112: ($disabled) ? : $this->desc[Cons::COLUMN_READONLY] = TRUE;
113: }
114: 115: 116: 117: 118:
119: private function setLenght($field) {
120: $field->addRule(Form::MAX_LENGTH, "Maximální délka pole %label je %d znaků", $this->query['lenght']);
121: }
122:
123: 124: 125: 126: 127: 128: 129: 130: 131:
132: private function field($type, $field, $label, $lenght) {
133: $method = 'add' . $type;
134: try {
135: if ($lenght !== '1') {
136: return $this[self::FORM]->{$method}($field, $label, $lenght);
137: } else {
138: return $this[self::FORM]->{$method}($field, $label);
139: }
140: } catch (Nette\MemberAccessException $e) {
141: unset($e);
142: throw new Nette\InvalidArgumentException("Neplatný typ pole formuláře $type");
143: }
144: }
145:
146: }
147: