1 | <?php |
||
20 | class HtmlForm extends HtmlSemCollection { |
||
21 | |||
22 | use FieldsTrait,FormTrait; |
||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $_fields; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $_validationParams; |
||
32 | |||
33 | public function __construct($identifier, $elements=array()) { |
||
34 | parent::__construct($identifier, "form", "ui form"); |
||
35 | $this->_states=[ State::ERROR,State::SUCCESS,State::WARNING,State::DISABLED ]; |
||
36 | $this->setProperty("name", $this->identifier); |
||
37 | $this->_fields=array (); |
||
38 | $this->addItems($elements); |
||
39 | $this->_validationParams=[]; |
||
40 | } |
||
41 | |||
42 | protected function getForm(){ |
||
43 | return $this; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param string $title |
||
48 | * @param number $niveau |
||
49 | * @param string $dividing |
||
50 | * @return HtmlHeader |
||
51 | */ |
||
52 | public function addHeader($title, $niveau=1, $dividing=true) { |
||
53 | $header=new HtmlHeader("", $niveau, $title); |
||
54 | if ($dividing) |
||
55 | $header->setDividing(); |
||
56 | return $this->addItem($header); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param string $caption |
||
61 | * @return \Ajax\semantic\html\collections\form\HtmlForm |
||
62 | */ |
||
63 | public function addDivider($caption=NULL){ |
||
64 | return $this->addContent(new HtmlDivider("",$caption)); |
||
65 | } |
||
66 | |||
67 | public function addFields($fields=NULL, $label=NULL) { |
||
68 | if (isset($fields)) { |
||
69 | if (!$fields instanceof HtmlFormFields) { |
||
70 | if (!\is_array($fields)) { |
||
71 | $fields=\func_get_args(); |
||
72 | $end=\end($fields); |
||
73 | if (\is_string($end)) { |
||
74 | $label=$end; |
||
75 | \array_pop($fields); |
||
76 | } else |
||
77 | $label=NULL; |
||
78 | } |
||
79 | $this->_fields=\array_merge($this->_fields, $fields); |
||
80 | $fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count(), $fields); |
||
81 | } |
||
82 | if (isset($label)) |
||
83 | $fields=new HtmlFormField("", $fields, $label); |
||
84 | } else { |
||
85 | $fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count()); |
||
86 | } |
||
87 | $this->addItem($fields); |
||
88 | return $fields; |
||
89 | } |
||
90 | |||
91 | public function addItem($item) { |
||
92 | $item=parent::addItem($item); |
||
93 | if (\is_subclass_of($item, HtmlFormField::class) === true) { |
||
94 | $this->_fields[]=$item; |
||
95 | } |
||
96 | return $item; |
||
97 | } |
||
98 | |||
99 | public function getField($index) { |
||
100 | if (\is_string($index)) { |
||
101 | $field=$this->getElementById($index, $this->_fields); |
||
102 | } else { |
||
103 | $field=$this->_fields[$index]; |
||
104 | } |
||
105 | return $field; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * automatically divide fields to be equal width |
||
110 | * @return \Ajax\semantic\html\collections\form\HtmlForm |
||
111 | */ |
||
112 | public function setEqualWidth() { |
||
113 | return $this->addToProperty("class", "equal width"); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Adds a field (alias for addItem) |
||
118 | * @param HtmlFormField $field |
||
119 | * @return \Ajax\common\html\HtmlDoubleElement |
||
120 | */ |
||
121 | public function addField($field) { |
||
122 | return $this->addItem($field); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * |
||
127 | * @param string $identifier |
||
128 | * @param string $content |
||
129 | * @param string $header |
||
130 | * @param string $icon |
||
131 | * @param string $type |
||
132 | * @return \Ajax\semantic\html\collections\HtmlMessage |
||
133 | */ |
||
134 | public function addMessage($identifier, $content, $header=NULL, $icon=NULL, $type=NULL) { |
||
135 | $message=new HtmlMessage($identifier, $content); |
||
136 | if (isset($header)) |
||
137 | $message->addHeader($header); |
||
138 | if (isset($icon)) |
||
139 | $message->setIcon($icon); |
||
140 | if (isset($type)) |
||
141 | $message->setStyle($type); |
||
142 | return $this->addItem($message); |
||
143 | } |
||
144 | |||
145 | |||
146 | |||
147 | public function compile(JsUtils $js=NULL,&$view=NULL){ |
||
148 | if(\sizeof($this->_validationParams)>0) |
||
149 | $this->setProperty("novalidate", ""); |
||
150 | return parent::compile($js,$view); |
||
151 | } |
||
152 | |||
153 | public function run(JsUtils $js) { |
||
154 | $compo=NULL; |
||
155 | foreach ($this->_fields as $field){ |
||
156 | if($field instanceof HtmlFormField){ |
||
157 | $compo=$this->addCompoValidation($js, $compo, $field); |
||
158 | } |
||
159 | } |
||
160 | foreach ($this->content as $field){ |
||
161 | if($field instanceof HtmlFormFields){ |
||
162 | $items=$field->getItems(); |
||
163 | foreach ($items as $_field){ |
||
164 | if($_field instanceof HtmlFormField) |
||
165 | $compo=$this->addCompoValidation($js, $compo, $_field); |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | if(isset($compo)===false){ |
||
170 | return parent::run($js); |
||
171 | } |
||
172 | $this->_runValidationParams($compo,$js); |
||
173 | return $this->_bsComponent; |
||
174 | } |
||
175 | |||
176 | public function addValidationParam($paramName,$paramValue){ |
||
177 | $this->_validationParams[$paramName]=$paramValue; |
||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | public function setValidationParams(array $_validationParams) { |
||
185 | |||
186 | public function getValidationParams() { |
||
187 | return $this->_validationParams; |
||
188 | } |
||
189 | |||
190 | } |