@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | // used by display strategies |
95 | 95 | // assign incoming values to properties |
96 | 96 | foreach ($options_array as $key => $value) { |
97 | - $key = '_' . $key; |
|
97 | + $key = '_'.$key; |
|
98 | 98 | if (property_exists($this, $key) && empty($this->{$key})) { |
99 | 99 | $this->{$key} = $value; |
100 | 100 | if ($key === '_subsections' && ! is_array($value)) { |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | */ |
135 | 135 | public function ensure_construct_finalized_called() |
136 | 136 | { |
137 | - if (! $this->_construction_finalized) { |
|
137 | + if ( ! $this->_construction_finalized) { |
|
138 | 138 | $this->_construct_finalize($this->_parent_section, $this->_name); |
139 | 139 | } |
140 | 140 | } |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | */ |
198 | 198 | protected function _set_default_html_id_if_empty() |
199 | 199 | { |
200 | - if (! $this->_html_id) { |
|
200 | + if ( ! $this->_html_id) { |
|
201 | 201 | if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
202 | 202 | $this->_html_id = $this->_parent_section->html_id() |
203 | 203 | . '-' |
@@ -256,7 +256,7 @@ discard block |
||
256 | 256 | public function html_id($add_pound_sign = false) |
257 | 257 | { |
258 | 258 | $this->_set_default_html_id_if_empty(); |
259 | - return $add_pound_sign ? '#' . $this->_html_id : $this->_html_id; |
|
259 | + return $add_pound_sign ? '#'.$this->_html_id : $this->_html_id; |
|
260 | 260 | } |
261 | 261 | |
262 | 262 | |
@@ -326,7 +326,7 @@ discard block |
||
326 | 326 | */ |
327 | 327 | public function other_html_attributes() |
328 | 328 | { |
329 | - return ! empty($this->_other_html_attributes) ? ' ' . $this->_other_html_attributes : ''; |
|
329 | + return ! empty($this->_other_html_attributes) ? ' '.$this->_other_html_attributes : ''; |
|
330 | 330 | } |
331 | 331 | |
332 | 332 | |
@@ -339,7 +339,7 @@ discard block |
||
339 | 339 | */ |
340 | 340 | public function name() |
341 | 341 | { |
342 | - if (! $this->_construction_finalized) { |
|
342 | + if ( ! $this->_construction_finalized) { |
|
343 | 343 | throw new EE_Error(sprintf(esc_html__( |
344 | 344 | 'You cannot use the form section\s name until _construct_finalize has been called on it (when we set the name). It was called on a form section of type \'s\'', |
345 | 345 | 'event_espresso' |
@@ -372,18 +372,18 @@ discard block |
||
372 | 372 | */ |
373 | 373 | public function form_open($action = '', $method = '', $other_attributes = '') |
374 | 374 | { |
375 | - if (! empty($action)) { |
|
375 | + if ( ! empty($action)) { |
|
376 | 376 | $this->set_action($action); |
377 | 377 | } |
378 | - if (! empty($method)) { |
|
378 | + if ( ! empty($method)) { |
|
379 | 379 | $this->set_method($method); |
380 | 380 | } |
381 | - $html = EEH_HTML::nl(1, 'form') . '<form'; |
|
382 | - $html .= $this->html_id() !== '' ? ' id="' . $this->get_html_id_for_form($this->html_id()) . '"' : ''; |
|
383 | - $html .= ' action="' . esc_url_raw($this->action()) . '"'; |
|
384 | - $html .= ' method="' . $this->method() . '"'; |
|
385 | - $html .= ' name="' . $this->name() . '"'; |
|
386 | - $html .= ' ' . $other_attributes . '>'; |
|
381 | + $html = EEH_HTML::nl(1, 'form').'<form'; |
|
382 | + $html .= $this->html_id() !== '' ? ' id="'.$this->get_html_id_for_form($this->html_id()).'"' : ''; |
|
383 | + $html .= ' action="'.esc_url_raw($this->action()).'"'; |
|
384 | + $html .= ' method="'.$this->method().'"'; |
|
385 | + $html .= ' name="'.$this->name().'"'; |
|
386 | + $html .= ' '.$other_attributes.'>'; |
|
387 | 387 | return $html; |
388 | 388 | } |
389 | 389 | |
@@ -401,7 +401,7 @@ discard block |
||
401 | 401 | $strlen = strlen($html_id); |
402 | 402 | $html_id = strpos($html_id, '-form') === $strlen - 5 || strpos($html_id, '-frm') === $strlen - 4 |
403 | 403 | ? $html_id |
404 | - : $html_id . '-frm'; |
|
404 | + : $html_id.'-frm'; |
|
405 | 405 | return $html_id; |
406 | 406 | } |
407 | 407 |
@@ -14,484 +14,484 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class EE_Form_Section_Base |
16 | 16 | { |
17 | - /** |
|
18 | - * the URL the form is submitted to |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $_action = ''; |
|
23 | - |
|
24 | - /** |
|
25 | - * POST (default) or GET |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $_method = ''; |
|
30 | - |
|
31 | - /** |
|
32 | - * html_id and html_name are derived from this by default |
|
33 | - * |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - protected $_name = ''; |
|
37 | - |
|
38 | - /** |
|
39 | - * $_html_id |
|
40 | - * @var string |
|
41 | - */ |
|
42 | - protected $_html_id = ''; |
|
43 | - |
|
44 | - /** |
|
45 | - * $_html_class |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - protected $_html_class = ''; |
|
49 | - |
|
50 | - /** |
|
51 | - * $_html_style |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - protected $_html_style = ''; |
|
55 | - |
|
56 | - /** |
|
57 | - * $_other_html_attributes |
|
58 | - * @var string |
|
59 | - */ |
|
60 | - protected $_other_html_attributes = ''; |
|
61 | - |
|
62 | - /** |
|
63 | - * The form section of which this form section is a part |
|
64 | - * |
|
65 | - * @var EE_Form_Section_Proper |
|
66 | - */ |
|
67 | - protected $_parent_section; |
|
68 | - |
|
69 | - /** |
|
70 | - * flag indicating that _construct_finalize has been called. |
|
71 | - * If it has not been called and we try to use functions which require it, we call it |
|
72 | - * with no parameters. But normally, _construct_finalize should be called by the instantiating class |
|
73 | - * |
|
74 | - * @var bool |
|
75 | - */ |
|
76 | - protected $_construction_finalized = false; |
|
77 | - |
|
78 | - /** |
|
79 | - * Strategy for parsing the form HTML upon display |
|
80 | - * |
|
81 | - * @var FormHtmlFilter |
|
82 | - */ |
|
83 | - protected $_form_html_filter; |
|
84 | - |
|
85 | - |
|
86 | - /** |
|
87 | - * @param array $options_array { |
|
88 | - * @type $name string the name for this form section, if you want to explicitly define it |
|
89 | - * } |
|
90 | - * @throws InvalidDataTypeException |
|
91 | - */ |
|
92 | - public function __construct($options_array = array()) |
|
93 | - { |
|
94 | - // used by display strategies |
|
95 | - // assign incoming values to properties |
|
96 | - foreach ($options_array as $key => $value) { |
|
97 | - $key = '_' . $key; |
|
98 | - if (property_exists($this, $key) && empty($this->{$key})) { |
|
99 | - $this->{$key} = $value; |
|
100 | - if ($key === '_subsections' && ! is_array($value)) { |
|
101 | - throw new InvalidDataTypeException($key, $value, 'array'); |
|
102 | - } |
|
103 | - } |
|
104 | - } |
|
105 | - // set parser which allows the form section's rendered HTML to be filtered |
|
106 | - if (isset($options_array['form_html_filter']) && $options_array['form_html_filter'] instanceof FormHtmlFilter) { |
|
107 | - $this->_form_html_filter = $options_array['form_html_filter']; |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * @param $parent_form_section |
|
115 | - * @param $name |
|
116 | - */ |
|
117 | - protected function _construct_finalize($parent_form_section, $name) |
|
118 | - { |
|
119 | - $this->_construction_finalized = true; |
|
120 | - $this->_parent_section = $parent_form_section; |
|
121 | - if ($name !== null) { |
|
122 | - $this->_name = $name; |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * make sure construction finalized was called, otherwise children might not be ready |
|
130 | - * |
|
131 | - * @return void |
|
132 | - */ |
|
133 | - public function ensure_construct_finalized_called() |
|
134 | - { |
|
135 | - if (! $this->_construction_finalized) { |
|
136 | - $this->_construct_finalize($this->_parent_section, $this->_name); |
|
137 | - } |
|
138 | - } |
|
139 | - |
|
140 | - |
|
141 | - |
|
142 | - /** |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function action() |
|
146 | - { |
|
147 | - return $this->_action; |
|
148 | - } |
|
149 | - |
|
150 | - |
|
151 | - |
|
152 | - /** |
|
153 | - * @param string $action |
|
154 | - */ |
|
155 | - public function set_action($action) |
|
156 | - { |
|
157 | - $this->_action = $action; |
|
158 | - } |
|
159 | - |
|
160 | - |
|
161 | - |
|
162 | - /** |
|
163 | - * @return string |
|
164 | - */ |
|
165 | - public function method() |
|
166 | - { |
|
167 | - return ! empty($this->_method) ? $this->_method : 'POST'; |
|
168 | - } |
|
169 | - |
|
170 | - |
|
171 | - |
|
172 | - /** |
|
173 | - * @param string $method |
|
174 | - */ |
|
175 | - public function set_method($method) |
|
176 | - { |
|
177 | - switch ($method) { |
|
178 | - case 'get': |
|
179 | - case 'GET': |
|
180 | - $this->_method = 'GET'; |
|
181 | - break; |
|
182 | - default: |
|
183 | - $this->_method = 'POST'; |
|
184 | - } |
|
185 | - } |
|
186 | - |
|
187 | - |
|
188 | - |
|
189 | - /** |
|
190 | - * Sets the html_id to its default value, if none was specified in the constructor. |
|
191 | - * Calculation involves using the name and the parent's html id |
|
192 | - * return void |
|
193 | - * |
|
194 | - * @throws \EE_Error |
|
195 | - */ |
|
196 | - protected function _set_default_html_id_if_empty() |
|
197 | - { |
|
198 | - if (! $this->_html_id) { |
|
199 | - if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
200 | - $this->_html_id = $this->_parent_section->html_id() |
|
201 | - . '-' |
|
202 | - . $this->_prep_name_for_html_id($this->name()); |
|
203 | - } else { |
|
204 | - $this->_html_id = $this->_prep_name_for_html_id($this->name()); |
|
205 | - } |
|
206 | - } |
|
207 | - } |
|
208 | - |
|
209 | - |
|
210 | - |
|
211 | - /** |
|
212 | - * _prep_name_for_html_id |
|
213 | - * |
|
214 | - * @param $name |
|
215 | - * @return string |
|
216 | - */ |
|
217 | - private function _prep_name_for_html_id($name) |
|
218 | - { |
|
219 | - return sanitize_key(str_replace(array(' ', ' ', '_'), '-', $name)); |
|
220 | - } |
|
17 | + /** |
|
18 | + * the URL the form is submitted to |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $_action = ''; |
|
23 | + |
|
24 | + /** |
|
25 | + * POST (default) or GET |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $_method = ''; |
|
30 | + |
|
31 | + /** |
|
32 | + * html_id and html_name are derived from this by default |
|
33 | + * |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + protected $_name = ''; |
|
37 | + |
|
38 | + /** |
|
39 | + * $_html_id |
|
40 | + * @var string |
|
41 | + */ |
|
42 | + protected $_html_id = ''; |
|
43 | + |
|
44 | + /** |
|
45 | + * $_html_class |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + protected $_html_class = ''; |
|
49 | + |
|
50 | + /** |
|
51 | + * $_html_style |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + protected $_html_style = ''; |
|
55 | + |
|
56 | + /** |
|
57 | + * $_other_html_attributes |
|
58 | + * @var string |
|
59 | + */ |
|
60 | + protected $_other_html_attributes = ''; |
|
61 | + |
|
62 | + /** |
|
63 | + * The form section of which this form section is a part |
|
64 | + * |
|
65 | + * @var EE_Form_Section_Proper |
|
66 | + */ |
|
67 | + protected $_parent_section; |
|
68 | + |
|
69 | + /** |
|
70 | + * flag indicating that _construct_finalize has been called. |
|
71 | + * If it has not been called and we try to use functions which require it, we call it |
|
72 | + * with no parameters. But normally, _construct_finalize should be called by the instantiating class |
|
73 | + * |
|
74 | + * @var bool |
|
75 | + */ |
|
76 | + protected $_construction_finalized = false; |
|
77 | + |
|
78 | + /** |
|
79 | + * Strategy for parsing the form HTML upon display |
|
80 | + * |
|
81 | + * @var FormHtmlFilter |
|
82 | + */ |
|
83 | + protected $_form_html_filter; |
|
84 | + |
|
85 | + |
|
86 | + /** |
|
87 | + * @param array $options_array { |
|
88 | + * @type $name string the name for this form section, if you want to explicitly define it |
|
89 | + * } |
|
90 | + * @throws InvalidDataTypeException |
|
91 | + */ |
|
92 | + public function __construct($options_array = array()) |
|
93 | + { |
|
94 | + // used by display strategies |
|
95 | + // assign incoming values to properties |
|
96 | + foreach ($options_array as $key => $value) { |
|
97 | + $key = '_' . $key; |
|
98 | + if (property_exists($this, $key) && empty($this->{$key})) { |
|
99 | + $this->{$key} = $value; |
|
100 | + if ($key === '_subsections' && ! is_array($value)) { |
|
101 | + throw new InvalidDataTypeException($key, $value, 'array'); |
|
102 | + } |
|
103 | + } |
|
104 | + } |
|
105 | + // set parser which allows the form section's rendered HTML to be filtered |
|
106 | + if (isset($options_array['form_html_filter']) && $options_array['form_html_filter'] instanceof FormHtmlFilter) { |
|
107 | + $this->_form_html_filter = $options_array['form_html_filter']; |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * @param $parent_form_section |
|
115 | + * @param $name |
|
116 | + */ |
|
117 | + protected function _construct_finalize($parent_form_section, $name) |
|
118 | + { |
|
119 | + $this->_construction_finalized = true; |
|
120 | + $this->_parent_section = $parent_form_section; |
|
121 | + if ($name !== null) { |
|
122 | + $this->_name = $name; |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * make sure construction finalized was called, otherwise children might not be ready |
|
130 | + * |
|
131 | + * @return void |
|
132 | + */ |
|
133 | + public function ensure_construct_finalized_called() |
|
134 | + { |
|
135 | + if (! $this->_construction_finalized) { |
|
136 | + $this->_construct_finalize($this->_parent_section, $this->_name); |
|
137 | + } |
|
138 | + } |
|
139 | + |
|
140 | + |
|
141 | + |
|
142 | + /** |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function action() |
|
146 | + { |
|
147 | + return $this->_action; |
|
148 | + } |
|
149 | + |
|
150 | + |
|
151 | + |
|
152 | + /** |
|
153 | + * @param string $action |
|
154 | + */ |
|
155 | + public function set_action($action) |
|
156 | + { |
|
157 | + $this->_action = $action; |
|
158 | + } |
|
159 | + |
|
160 | + |
|
161 | + |
|
162 | + /** |
|
163 | + * @return string |
|
164 | + */ |
|
165 | + public function method() |
|
166 | + { |
|
167 | + return ! empty($this->_method) ? $this->_method : 'POST'; |
|
168 | + } |
|
169 | + |
|
170 | + |
|
171 | + |
|
172 | + /** |
|
173 | + * @param string $method |
|
174 | + */ |
|
175 | + public function set_method($method) |
|
176 | + { |
|
177 | + switch ($method) { |
|
178 | + case 'get': |
|
179 | + case 'GET': |
|
180 | + $this->_method = 'GET'; |
|
181 | + break; |
|
182 | + default: |
|
183 | + $this->_method = 'POST'; |
|
184 | + } |
|
185 | + } |
|
186 | + |
|
187 | + |
|
188 | + |
|
189 | + /** |
|
190 | + * Sets the html_id to its default value, if none was specified in the constructor. |
|
191 | + * Calculation involves using the name and the parent's html id |
|
192 | + * return void |
|
193 | + * |
|
194 | + * @throws \EE_Error |
|
195 | + */ |
|
196 | + protected function _set_default_html_id_if_empty() |
|
197 | + { |
|
198 | + if (! $this->_html_id) { |
|
199 | + if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
200 | + $this->_html_id = $this->_parent_section->html_id() |
|
201 | + . '-' |
|
202 | + . $this->_prep_name_for_html_id($this->name()); |
|
203 | + } else { |
|
204 | + $this->_html_id = $this->_prep_name_for_html_id($this->name()); |
|
205 | + } |
|
206 | + } |
|
207 | + } |
|
208 | + |
|
209 | + |
|
210 | + |
|
211 | + /** |
|
212 | + * _prep_name_for_html_id |
|
213 | + * |
|
214 | + * @param $name |
|
215 | + * @return string |
|
216 | + */ |
|
217 | + private function _prep_name_for_html_id($name) |
|
218 | + { |
|
219 | + return sanitize_key(str_replace(array(' ', ' ', '_'), '-', $name)); |
|
220 | + } |
|
221 | 221 | |
222 | 222 | |
223 | 223 | |
224 | - /** |
|
225 | - * Returns the HTML, JS, and CSS necessary to display this form section on a page. |
|
226 | - * Note however, it's recommended that you instead call enqueue_js on the "wp_enqueue_scripts" action, |
|
227 | - * and call get_html when you want to output the html. Calling get_html_and_js after |
|
228 | - * "wp_enqueue_scripts" has already fired seems to work for now, but is contrary |
|
229 | - * to the instructions on https://developer.wordpress.org/reference/functions/wp_enqueue_script/ |
|
230 | - * and so might stop working anytime. |
|
231 | - * |
|
232 | - * @return string |
|
233 | - */ |
|
234 | - public function get_html_and_js() |
|
235 | - { |
|
236 | - return $this->get_html(); |
|
237 | - } |
|
224 | + /** |
|
225 | + * Returns the HTML, JS, and CSS necessary to display this form section on a page. |
|
226 | + * Note however, it's recommended that you instead call enqueue_js on the "wp_enqueue_scripts" action, |
|
227 | + * and call get_html when you want to output the html. Calling get_html_and_js after |
|
228 | + * "wp_enqueue_scripts" has already fired seems to work for now, but is contrary |
|
229 | + * to the instructions on https://developer.wordpress.org/reference/functions/wp_enqueue_script/ |
|
230 | + * and so might stop working anytime. |
|
231 | + * |
|
232 | + * @return string |
|
233 | + */ |
|
234 | + public function get_html_and_js() |
|
235 | + { |
|
236 | + return $this->get_html(); |
|
237 | + } |
|
238 | 238 | |
239 | 239 | |
240 | - |
|
241 | - /** |
|
242 | - * Gets the HTML for displaying this form section |
|
243 | - * |
|
244 | - * @return string |
|
245 | - */ |
|
246 | - abstract public function get_html(); |
|
247 | - |
|
248 | - |
|
249 | - /** |
|
250 | - * @param bool $add_pound_sign |
|
251 | - * @return string |
|
252 | - * @throws EE_Error |
|
253 | - */ |
|
254 | - public function html_id($add_pound_sign = false) |
|
255 | - { |
|
256 | - $this->_set_default_html_id_if_empty(); |
|
257 | - return $add_pound_sign ? '#' . $this->_html_id : $this->_html_id; |
|
258 | - } |
|
259 | - |
|
260 | - |
|
261 | - |
|
262 | - /** |
|
263 | - * @return string |
|
264 | - */ |
|
265 | - public function html_class() |
|
266 | - { |
|
267 | - return $this->_html_class; |
|
268 | - } |
|
269 | - |
|
270 | - |
|
271 | - |
|
272 | - /** |
|
273 | - * @return string |
|
274 | - */ |
|
275 | - public function html_style() |
|
276 | - { |
|
277 | - return $this->_html_style; |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - |
|
282 | - /** |
|
283 | - * @param mixed $html_class |
|
284 | - */ |
|
285 | - public function set_html_class($html_class) |
|
286 | - { |
|
287 | - $this->_html_class = $html_class; |
|
288 | - } |
|
289 | - |
|
290 | - |
|
291 | - |
|
292 | - /** |
|
293 | - * @param mixed $html_id |
|
294 | - */ |
|
295 | - public function set_html_id($html_id) |
|
296 | - { |
|
297 | - $this->_html_id = $html_id; |
|
298 | - } |
|
299 | - |
|
300 | - |
|
301 | - |
|
302 | - /** |
|
303 | - * @param mixed $html_style |
|
304 | - */ |
|
305 | - public function set_html_style($html_style) |
|
306 | - { |
|
307 | - $this->_html_style = $html_style; |
|
308 | - } |
|
309 | - |
|
310 | - |
|
311 | - |
|
312 | - /** |
|
313 | - * @param string $other_html_attributes |
|
314 | - */ |
|
315 | - public function set_other_html_attributes($other_html_attributes) |
|
316 | - { |
|
317 | - $this->_other_html_attributes = $other_html_attributes ?? ''; |
|
318 | - } |
|
319 | - |
|
320 | - |
|
321 | - |
|
322 | - /** |
|
323 | - * @return string |
|
324 | - */ |
|
325 | - public function other_html_attributes() |
|
326 | - { |
|
327 | - return ! empty($this->_other_html_attributes) ? ' ' . $this->_other_html_attributes : ''; |
|
328 | - } |
|
329 | - |
|
330 | - |
|
331 | - |
|
332 | - /** |
|
333 | - * Gets the name of the form section. This is not the same as the HTML name. |
|
334 | - * |
|
335 | - * @throws EE_Error |
|
336 | - * @return string |
|
337 | - */ |
|
338 | - public function name() |
|
339 | - { |
|
340 | - if (! $this->_construction_finalized) { |
|
341 | - throw new EE_Error(sprintf(esc_html__( |
|
342 | - 'You cannot use the form section\s name until _construct_finalize has been called on it (when we set the name). It was called on a form section of type \'s\'', |
|
343 | - 'event_espresso' |
|
344 | - ), get_class($this))); |
|
345 | - } |
|
346 | - return $this->_name; |
|
347 | - } |
|
348 | - |
|
349 | - |
|
350 | - |
|
351 | - /** |
|
352 | - * Gets the parent section |
|
353 | - * |
|
354 | - * @return EE_Form_Section_Proper |
|
355 | - */ |
|
356 | - public function parent_section() |
|
357 | - { |
|
358 | - return $this->_parent_section; |
|
359 | - } |
|
360 | - |
|
361 | - |
|
362 | - /** |
|
363 | - * returns HTML for generating the opening form HTML tag (<form>) |
|
364 | - * |
|
365 | - * @param string $action the URL the form is submitted to |
|
366 | - * @param string $method POST (default) or GET |
|
367 | - * @param string $other_attributes anything else added to the form open tag, MUST BE VALID HTML |
|
368 | - * @return string |
|
369 | - * @throws EE_Error |
|
370 | - */ |
|
371 | - public function form_open($action = '', $method = '', $other_attributes = '') |
|
372 | - { |
|
373 | - if (! empty($action)) { |
|
374 | - $this->set_action($action); |
|
375 | - } |
|
376 | - if (! empty($method)) { |
|
377 | - $this->set_method($method); |
|
378 | - } |
|
379 | - $html = EEH_HTML::nl(1, 'form') . '<form'; |
|
380 | - $html .= $this->html_id() !== '' ? ' id="' . $this->get_html_id_for_form($this->html_id()) . '"' : ''; |
|
381 | - $html .= ' action="' . esc_url_raw($this->action()) . '"'; |
|
382 | - $html .= ' method="' . $this->method() . '"'; |
|
383 | - $html .= ' name="' . $this->name() . '"'; |
|
384 | - $html .= ' ' . $other_attributes . '>'; |
|
385 | - return $html; |
|
386 | - } |
|
387 | - |
|
388 | - |
|
389 | - |
|
390 | - /** |
|
391 | - * ensures that html id for form either ends in "-form" or "-frm" |
|
392 | - * so that id doesn't conflict/collide with other elements |
|
393 | - * |
|
394 | - * @param string $html_id |
|
395 | - * @return string |
|
396 | - */ |
|
397 | - protected function get_html_id_for_form($html_id) |
|
398 | - { |
|
399 | - $strlen = strlen($html_id); |
|
400 | - $html_id = strpos($html_id, '-form') === $strlen - 5 || strpos($html_id, '-frm') === $strlen - 4 |
|
401 | - ? $html_id |
|
402 | - : $html_id . '-frm'; |
|
403 | - return $html_id; |
|
404 | - } |
|
405 | - |
|
406 | - |
|
407 | - /** |
|
408 | - * returns HTML for generating the closing form HTML tag (</form>) |
|
409 | - * |
|
410 | - * @return string |
|
411 | - * @throws EE_Error |
|
412 | - */ |
|
413 | - public function form_close() |
|
414 | - { |
|
415 | - return EEH_HTML::nl(-1, 'form') |
|
416 | - . '</form>' |
|
417 | - . EEH_HTML::nl() |
|
418 | - . '<!-- end of ee-' |
|
419 | - . $this->html_id() |
|
420 | - . '-form -->' |
|
421 | - . EEH_HTML::nl(); |
|
422 | - } |
|
423 | - |
|
424 | - |
|
425 | - |
|
426 | - /** |
|
427 | - * enqueues JS (and CSS) for the form (ie immediately call wp_enqueue_script and |
|
428 | - * wp_enqueue_style; the scripts could have optionally been registered earlier) |
|
429 | - * Default does nothing, but child classes can override |
|
430 | - * |
|
431 | - * @return void |
|
432 | - */ |
|
433 | - public function enqueue_js() |
|
434 | - { |
|
435 | - // defaults to enqueue NO js or css |
|
436 | - } |
|
437 | - |
|
438 | - |
|
439 | - |
|
440 | - /** |
|
441 | - * Adds any extra data needed by js. Eventually we'll call wp_localize_script |
|
442 | - * with it, and it will be on each form section's 'other_data' property. |
|
443 | - * By default nothing is added, but child classes can extend this method to add something. |
|
444 | - * Eg, if you have an input that will cause a modal dialog to appear, |
|
445 | - * here you could add an entry like 'modal_dialog_inputs' to this array |
|
446 | - * to map between the input's html ID and the modal dialogue's ID, so that |
|
447 | - * your JS code will know where to find the modal dialog when the input is pressed. |
|
448 | - * Eg $form_other_js_data['modal_dialog_inputs']['some-input-id']='modal-dialog-id'; |
|
449 | - * |
|
450 | - * @param array $form_other_js_data |
|
451 | - * @return array |
|
452 | - */ |
|
453 | - public function get_other_js_data($form_other_js_data = array()) |
|
454 | - { |
|
455 | - return $form_other_js_data; |
|
456 | - } |
|
457 | - |
|
458 | - |
|
459 | - |
|
460 | - /** |
|
461 | - * This isn't just the name of an input, it's a path pointing to an input. The |
|
462 | - * path is similar to a folder path: slash (/) means to descend into a subsection, |
|
463 | - * dot-dot-slash (../) means to ascend into the parent section. |
|
464 | - * After a series of slashes and dot-dot-slashes, there should be the name of an input, |
|
465 | - * which will be returned. |
|
466 | - * Eg, if you want the related input to be conditional on a sibling input name 'foobar' |
|
467 | - * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name |
|
468 | - * 'baz', use '../baz'. If you want it to be conditional on a cousin input, |
|
469 | - * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'. |
|
470 | - * Etc |
|
471 | - * |
|
472 | - * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false |
|
473 | - * @return EE_Form_Section_Base |
|
474 | - */ |
|
475 | - public function find_section_from_path($form_section_path) |
|
476 | - { |
|
477 | - if (strpos($form_section_path, '/') === 0) { |
|
478 | - $form_section_path = substr($form_section_path, strlen('/')); |
|
479 | - } |
|
480 | - if (empty($form_section_path)) { |
|
481 | - return $this; |
|
482 | - } |
|
483 | - if (strpos($form_section_path, '../') === 0) { |
|
484 | - $parent = $this->parent_section(); |
|
485 | - $form_section_path = substr($form_section_path, strlen('../')); |
|
486 | - if ($parent instanceof EE_Form_Section_Base) { |
|
487 | - return $parent->find_section_from_path($form_section_path); |
|
488 | - } |
|
489 | - if (empty($form_section_path)) { |
|
490 | - return $this; |
|
491 | - } |
|
492 | - } |
|
493 | - // couldn't find it using simple parent following |
|
494 | - return null; |
|
495 | - } |
|
240 | + |
|
241 | + /** |
|
242 | + * Gets the HTML for displaying this form section |
|
243 | + * |
|
244 | + * @return string |
|
245 | + */ |
|
246 | + abstract public function get_html(); |
|
247 | + |
|
248 | + |
|
249 | + /** |
|
250 | + * @param bool $add_pound_sign |
|
251 | + * @return string |
|
252 | + * @throws EE_Error |
|
253 | + */ |
|
254 | + public function html_id($add_pound_sign = false) |
|
255 | + { |
|
256 | + $this->_set_default_html_id_if_empty(); |
|
257 | + return $add_pound_sign ? '#' . $this->_html_id : $this->_html_id; |
|
258 | + } |
|
259 | + |
|
260 | + |
|
261 | + |
|
262 | + /** |
|
263 | + * @return string |
|
264 | + */ |
|
265 | + public function html_class() |
|
266 | + { |
|
267 | + return $this->_html_class; |
|
268 | + } |
|
269 | + |
|
270 | + |
|
271 | + |
|
272 | + /** |
|
273 | + * @return string |
|
274 | + */ |
|
275 | + public function html_style() |
|
276 | + { |
|
277 | + return $this->_html_style; |
|
278 | + } |
|
279 | + |
|
280 | + |
|
281 | + |
|
282 | + /** |
|
283 | + * @param mixed $html_class |
|
284 | + */ |
|
285 | + public function set_html_class($html_class) |
|
286 | + { |
|
287 | + $this->_html_class = $html_class; |
|
288 | + } |
|
289 | + |
|
290 | + |
|
291 | + |
|
292 | + /** |
|
293 | + * @param mixed $html_id |
|
294 | + */ |
|
295 | + public function set_html_id($html_id) |
|
296 | + { |
|
297 | + $this->_html_id = $html_id; |
|
298 | + } |
|
299 | + |
|
300 | + |
|
301 | + |
|
302 | + /** |
|
303 | + * @param mixed $html_style |
|
304 | + */ |
|
305 | + public function set_html_style($html_style) |
|
306 | + { |
|
307 | + $this->_html_style = $html_style; |
|
308 | + } |
|
309 | + |
|
310 | + |
|
311 | + |
|
312 | + /** |
|
313 | + * @param string $other_html_attributes |
|
314 | + */ |
|
315 | + public function set_other_html_attributes($other_html_attributes) |
|
316 | + { |
|
317 | + $this->_other_html_attributes = $other_html_attributes ?? ''; |
|
318 | + } |
|
319 | + |
|
320 | + |
|
321 | + |
|
322 | + /** |
|
323 | + * @return string |
|
324 | + */ |
|
325 | + public function other_html_attributes() |
|
326 | + { |
|
327 | + return ! empty($this->_other_html_attributes) ? ' ' . $this->_other_html_attributes : ''; |
|
328 | + } |
|
329 | + |
|
330 | + |
|
331 | + |
|
332 | + /** |
|
333 | + * Gets the name of the form section. This is not the same as the HTML name. |
|
334 | + * |
|
335 | + * @throws EE_Error |
|
336 | + * @return string |
|
337 | + */ |
|
338 | + public function name() |
|
339 | + { |
|
340 | + if (! $this->_construction_finalized) { |
|
341 | + throw new EE_Error(sprintf(esc_html__( |
|
342 | + 'You cannot use the form section\s name until _construct_finalize has been called on it (when we set the name). It was called on a form section of type \'s\'', |
|
343 | + 'event_espresso' |
|
344 | + ), get_class($this))); |
|
345 | + } |
|
346 | + return $this->_name; |
|
347 | + } |
|
348 | + |
|
349 | + |
|
350 | + |
|
351 | + /** |
|
352 | + * Gets the parent section |
|
353 | + * |
|
354 | + * @return EE_Form_Section_Proper |
|
355 | + */ |
|
356 | + public function parent_section() |
|
357 | + { |
|
358 | + return $this->_parent_section; |
|
359 | + } |
|
360 | + |
|
361 | + |
|
362 | + /** |
|
363 | + * returns HTML for generating the opening form HTML tag (<form>) |
|
364 | + * |
|
365 | + * @param string $action the URL the form is submitted to |
|
366 | + * @param string $method POST (default) or GET |
|
367 | + * @param string $other_attributes anything else added to the form open tag, MUST BE VALID HTML |
|
368 | + * @return string |
|
369 | + * @throws EE_Error |
|
370 | + */ |
|
371 | + public function form_open($action = '', $method = '', $other_attributes = '') |
|
372 | + { |
|
373 | + if (! empty($action)) { |
|
374 | + $this->set_action($action); |
|
375 | + } |
|
376 | + if (! empty($method)) { |
|
377 | + $this->set_method($method); |
|
378 | + } |
|
379 | + $html = EEH_HTML::nl(1, 'form') . '<form'; |
|
380 | + $html .= $this->html_id() !== '' ? ' id="' . $this->get_html_id_for_form($this->html_id()) . '"' : ''; |
|
381 | + $html .= ' action="' . esc_url_raw($this->action()) . '"'; |
|
382 | + $html .= ' method="' . $this->method() . '"'; |
|
383 | + $html .= ' name="' . $this->name() . '"'; |
|
384 | + $html .= ' ' . $other_attributes . '>'; |
|
385 | + return $html; |
|
386 | + } |
|
387 | + |
|
388 | + |
|
389 | + |
|
390 | + /** |
|
391 | + * ensures that html id for form either ends in "-form" or "-frm" |
|
392 | + * so that id doesn't conflict/collide with other elements |
|
393 | + * |
|
394 | + * @param string $html_id |
|
395 | + * @return string |
|
396 | + */ |
|
397 | + protected function get_html_id_for_form($html_id) |
|
398 | + { |
|
399 | + $strlen = strlen($html_id); |
|
400 | + $html_id = strpos($html_id, '-form') === $strlen - 5 || strpos($html_id, '-frm') === $strlen - 4 |
|
401 | + ? $html_id |
|
402 | + : $html_id . '-frm'; |
|
403 | + return $html_id; |
|
404 | + } |
|
405 | + |
|
406 | + |
|
407 | + /** |
|
408 | + * returns HTML for generating the closing form HTML tag (</form>) |
|
409 | + * |
|
410 | + * @return string |
|
411 | + * @throws EE_Error |
|
412 | + */ |
|
413 | + public function form_close() |
|
414 | + { |
|
415 | + return EEH_HTML::nl(-1, 'form') |
|
416 | + . '</form>' |
|
417 | + . EEH_HTML::nl() |
|
418 | + . '<!-- end of ee-' |
|
419 | + . $this->html_id() |
|
420 | + . '-form -->' |
|
421 | + . EEH_HTML::nl(); |
|
422 | + } |
|
423 | + |
|
424 | + |
|
425 | + |
|
426 | + /** |
|
427 | + * enqueues JS (and CSS) for the form (ie immediately call wp_enqueue_script and |
|
428 | + * wp_enqueue_style; the scripts could have optionally been registered earlier) |
|
429 | + * Default does nothing, but child classes can override |
|
430 | + * |
|
431 | + * @return void |
|
432 | + */ |
|
433 | + public function enqueue_js() |
|
434 | + { |
|
435 | + // defaults to enqueue NO js or css |
|
436 | + } |
|
437 | + |
|
438 | + |
|
439 | + |
|
440 | + /** |
|
441 | + * Adds any extra data needed by js. Eventually we'll call wp_localize_script |
|
442 | + * with it, and it will be on each form section's 'other_data' property. |
|
443 | + * By default nothing is added, but child classes can extend this method to add something. |
|
444 | + * Eg, if you have an input that will cause a modal dialog to appear, |
|
445 | + * here you could add an entry like 'modal_dialog_inputs' to this array |
|
446 | + * to map between the input's html ID and the modal dialogue's ID, so that |
|
447 | + * your JS code will know where to find the modal dialog when the input is pressed. |
|
448 | + * Eg $form_other_js_data['modal_dialog_inputs']['some-input-id']='modal-dialog-id'; |
|
449 | + * |
|
450 | + * @param array $form_other_js_data |
|
451 | + * @return array |
|
452 | + */ |
|
453 | + public function get_other_js_data($form_other_js_data = array()) |
|
454 | + { |
|
455 | + return $form_other_js_data; |
|
456 | + } |
|
457 | + |
|
458 | + |
|
459 | + |
|
460 | + /** |
|
461 | + * This isn't just the name of an input, it's a path pointing to an input. The |
|
462 | + * path is similar to a folder path: slash (/) means to descend into a subsection, |
|
463 | + * dot-dot-slash (../) means to ascend into the parent section. |
|
464 | + * After a series of slashes and dot-dot-slashes, there should be the name of an input, |
|
465 | + * which will be returned. |
|
466 | + * Eg, if you want the related input to be conditional on a sibling input name 'foobar' |
|
467 | + * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name |
|
468 | + * 'baz', use '../baz'. If you want it to be conditional on a cousin input, |
|
469 | + * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'. |
|
470 | + * Etc |
|
471 | + * |
|
472 | + * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false |
|
473 | + * @return EE_Form_Section_Base |
|
474 | + */ |
|
475 | + public function find_section_from_path($form_section_path) |
|
476 | + { |
|
477 | + if (strpos($form_section_path, '/') === 0) { |
|
478 | + $form_section_path = substr($form_section_path, strlen('/')); |
|
479 | + } |
|
480 | + if (empty($form_section_path)) { |
|
481 | + return $this; |
|
482 | + } |
|
483 | + if (strpos($form_section_path, '../') === 0) { |
|
484 | + $parent = $this->parent_section(); |
|
485 | + $form_section_path = substr($form_section_path, strlen('../')); |
|
486 | + if ($parent instanceof EE_Form_Section_Base) { |
|
487 | + return $parent->find_section_from_path($form_section_path); |
|
488 | + } |
|
489 | + if (empty($form_section_path)) { |
|
490 | + return $this; |
|
491 | + } |
|
492 | + } |
|
493 | + // couldn't find it using simple parent following |
|
494 | + return null; |
|
495 | + } |
|
496 | 496 | } |
497 | 497 | // End of file EE_Form_Section_Base.form.php |
@@ -14,1259 +14,1259 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class EE_Form_Input_Base extends EE_Form_Section_Validatable |
16 | 16 | { |
17 | - /** |
|
18 | - * the input's name attribute |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $_html_name; |
|
23 | - |
|
24 | - /** |
|
25 | - * id for the html label tag |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $_html_label_id; |
|
30 | - |
|
31 | - /** |
|
32 | - * class for teh html label tag |
|
33 | - * |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - protected $_html_label_class; |
|
37 | - |
|
38 | - /** |
|
39 | - * style for teh html label tag |
|
40 | - * |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - protected $_html_label_style; |
|
44 | - |
|
45 | - /** |
|
46 | - * text to be placed in the html label |
|
47 | - * |
|
48 | - * @var string |
|
49 | - */ |
|
50 | - protected $_html_label_text; |
|
51 | - |
|
52 | - /** |
|
53 | - * the full html label. If used, all other html_label_* properties are invalid |
|
54 | - * |
|
55 | - * @var string |
|
56 | - */ |
|
57 | - protected $_html_label; |
|
58 | - |
|
59 | - /** |
|
60 | - * HTML to use for help text (normally placed below form input), in a span which normally |
|
61 | - * has a class of 'description' |
|
62 | - * |
|
63 | - * @var string |
|
64 | - */ |
|
65 | - protected $_html_help_text; |
|
66 | - |
|
67 | - /** |
|
68 | - * CSS classes for displaying the help span |
|
69 | - * |
|
70 | - * @var string |
|
71 | - */ |
|
72 | - protected $_html_help_class = 'description'; |
|
73 | - |
|
74 | - /** |
|
75 | - * CSS to put in the style attribute on the help span |
|
76 | - * |
|
77 | - * @var string |
|
78 | - */ |
|
79 | - protected $_html_help_style; |
|
80 | - |
|
81 | - /** |
|
82 | - * Stores whether or not this input's response is required. |
|
83 | - * Because certain styling elements may also want to know that this |
|
84 | - * input is required etc. |
|
85 | - * |
|
86 | - * @var boolean |
|
87 | - */ |
|
88 | - protected $_required; |
|
89 | - |
|
90 | - /** |
|
91 | - * css class added to required inputs |
|
92 | - * |
|
93 | - * @var string |
|
94 | - */ |
|
95 | - protected $_required_css_class = 'ee-required'; |
|
96 | - |
|
97 | - /** |
|
98 | - * css styles applied to button type inputs |
|
99 | - * |
|
100 | - * @var string |
|
101 | - */ |
|
102 | - protected $_button_css_attributes; |
|
103 | - |
|
104 | - /** |
|
105 | - * The raw post data submitted for this |
|
106 | - * Generally unsafe for usage in client code |
|
107 | - * |
|
108 | - * @var mixed string or array |
|
109 | - */ |
|
110 | - protected $_raw_value; |
|
111 | - |
|
112 | - /** |
|
113 | - * Value normalized according to the input's normalization strategy. |
|
114 | - * The normalization strategy dictates whether this is a string, int, float, |
|
115 | - * boolean, or array of any of those. |
|
116 | - * |
|
117 | - * @var mixed |
|
118 | - */ |
|
119 | - protected $_normalized_value; |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * Normalized default value either initially set on the input, or provided by calling |
|
124 | - * set_default(). |
|
125 | - * @var mixed |
|
126 | - */ |
|
127 | - protected $_default; |
|
128 | - |
|
129 | - /** |
|
130 | - * Strategy used for displaying this field. |
|
131 | - * Child classes must use _get_display_strategy to access it. |
|
132 | - * |
|
133 | - * @var EE_Display_Strategy_Base |
|
134 | - */ |
|
135 | - private $_display_strategy; |
|
136 | - |
|
137 | - /** |
|
138 | - * Gets all the validation strategies used on this field |
|
139 | - * |
|
140 | - * @var EE_Validation_Strategy_Base[] |
|
141 | - */ |
|
142 | - private $_validation_strategies = array(); |
|
143 | - |
|
144 | - /** |
|
145 | - * The normalization strategy for this field |
|
146 | - * |
|
147 | - * @var EE_Normalization_Strategy_Base |
|
148 | - */ |
|
149 | - private $_normalization_strategy; |
|
150 | - |
|
151 | - /** |
|
152 | - * Strategy for removing sensitive data after we're done with the form input |
|
153 | - * |
|
154 | - * @var EE_Sensitive_Data_Removal_Base |
|
155 | - */ |
|
156 | - protected $_sensitive_data_removal_strategy; |
|
157 | - |
|
158 | - /** |
|
159 | - * Whether this input has been disabled or not. |
|
160 | - * If it's disabled while rendering, an extra hidden input is added that indicates it has been knowingly disabled. |
|
161 | - * (Client-side code that wants to dynamically disable it must also add this hidden input). |
|
162 | - * When the form is submitted, if the input is disabled in the PHP form section, then input is ignored. |
|
163 | - * If the input is missing from the request data but the hidden input indicating the input is disabled, then the input is again ignored. |
|
164 | - * |
|
165 | - * @var boolean |
|
166 | - */ |
|
167 | - protected $disabled = false; |
|
168 | - |
|
169 | - |
|
170 | - |
|
171 | - /** |
|
172 | - * @param array $input_args { |
|
173 | - * @type string $html_name the html name for the input |
|
174 | - * @type string $html_label_id the id attribute to give to the html label tag |
|
175 | - * @type string $html_label_class the class attribute to give to the html label tag |
|
176 | - * @type string $html_label_style the style attribute to give ot teh label tag |
|
177 | - * @type string $html_label_text the text to put in the label tag |
|
178 | - * @type string $html_label the full html label. If used, |
|
179 | - * all other html_label_* args are invalid |
|
180 | - * @type string $html_help_text text to put in help element |
|
181 | - * @type string $html_help_style style attribute to give to teh help element |
|
182 | - * @type string $html_help_class class attribute to give to the help element |
|
183 | - * @type string $default default value NORMALIZED (eg, if providing the default |
|
184 | - * for a Yes_No_Input, you should provide TRUE or FALSE, not '1' or '0') |
|
185 | - * @type EE_Display_Strategy_Base $display strategy |
|
186 | - * @type EE_Normalization_Strategy_Base $normalization_strategy |
|
187 | - * @type EE_Validation_Strategy_Base[] $validation_strategies |
|
188 | - * @type boolean $ignore_input special argument which can be used to avoid adding any validation strategies, |
|
189 | - * and sets the normalization strategy to the Null normalization. This is good |
|
190 | - * when you want the input to be totally ignored server-side (like when using |
|
191 | - * React.js form inputs) |
|
192 | - * } |
|
193 | - */ |
|
194 | - public function __construct($input_args = array()) |
|
195 | - { |
|
196 | - $input_args = (array) apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this); |
|
197 | - // the following properties must be cast as arrays |
|
198 | - if (isset($input_args['validation_strategies'])) { |
|
199 | - foreach ((array) $input_args['validation_strategies'] as $validation_strategy) { |
|
200 | - if ($validation_strategy instanceof EE_Validation_Strategy_Base && empty($input_args['ignore_input'])) { |
|
201 | - $this->_validation_strategies[ get_class($validation_strategy) ] = $validation_strategy; |
|
202 | - } |
|
203 | - } |
|
204 | - unset($input_args['validation_strategies']); |
|
205 | - } |
|
206 | - if (isset($input_args['ignore_input'])) { |
|
207 | - $this->_validation_strategies = array(); |
|
208 | - } |
|
209 | - // loop thru incoming options |
|
210 | - foreach ($input_args as $key => $value) { |
|
211 | - // add underscore to $key to match property names |
|
212 | - $_key = '_' . $key; |
|
213 | - if (property_exists($this, $_key)) { |
|
214 | - $this->{$_key} = $value; |
|
215 | - } |
|
216 | - } |
|
217 | - // ensure that "required" is set correctly |
|
218 | - $this->set_required( |
|
219 | - $this->_required, |
|
220 | - $input_args['required_validation_error_message'] ?? null |
|
221 | - ); |
|
222 | - // $this->_html_name_specified = isset( $input_args['html_name'] ) ? TRUE : FALSE; |
|
223 | - $this->_display_strategy->_construct_finalize($this); |
|
224 | - foreach ($this->_validation_strategies as $validation_strategy) { |
|
225 | - $validation_strategy->_construct_finalize($this); |
|
226 | - } |
|
227 | - if (isset($input_args['ignore_input'])) { |
|
228 | - $this->_normalization_strategy = new EE_Null_Normalization(); |
|
229 | - } |
|
230 | - if (! $this->_normalization_strategy) { |
|
231 | - $this->_normalization_strategy = new EE_Text_Normalization(); |
|
232 | - } |
|
233 | - $this->_normalization_strategy->_construct_finalize($this); |
|
234 | - // at least we can use the normalization strategy to populate the default |
|
235 | - if (isset($input_args['default'])) { |
|
236 | - $this->set_default($input_args['default']); |
|
237 | - unset($input_args['default']); |
|
238 | - } |
|
239 | - if (! $this->_sensitive_data_removal_strategy) { |
|
240 | - $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal(); |
|
241 | - } |
|
242 | - $this->_sensitive_data_removal_strategy->_construct_finalize($this); |
|
243 | - parent::__construct($input_args); |
|
244 | - } |
|
245 | - |
|
246 | - |
|
247 | - |
|
248 | - /** |
|
249 | - * Sets the html_name to its default value, if none was specified in teh constructor. |
|
250 | - * Calculation involves using the name and the parent's html_name |
|
251 | - * |
|
252 | - * @throws EE_Error |
|
253 | - */ |
|
254 | - protected function _set_default_html_name_if_empty() |
|
255 | - { |
|
256 | - if (! $this->_html_name) { |
|
257 | - $this->_html_name = $this->name(); |
|
258 | - if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
259 | - $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]"; |
|
260 | - } |
|
261 | - } |
|
262 | - } |
|
263 | - |
|
264 | - |
|
265 | - |
|
266 | - /** |
|
267 | - * @param $parent_form_section |
|
268 | - * @param $name |
|
269 | - * @throws EE_Error |
|
270 | - */ |
|
271 | - public function _construct_finalize($parent_form_section, $name) |
|
272 | - { |
|
273 | - parent::_construct_finalize($parent_form_section, $name); |
|
274 | - if ($this->_html_label === null && $this->_html_label_text === null) { |
|
275 | - $this->_html_label_text = ucwords(str_replace("_", " ", $name)); |
|
276 | - } |
|
277 | - do_action('AHEE__EE_Form_Input_Base___construct_finalize__end', $this, $parent_form_section, $name); |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - |
|
282 | - /** |
|
283 | - * Returns the strategy for displaying this form input. If none is set, throws an exception. |
|
284 | - * |
|
285 | - * @return EE_Display_Strategy_Base |
|
286 | - * @throws EE_Error |
|
287 | - */ |
|
288 | - protected function _get_display_strategy() |
|
289 | - { |
|
290 | - $this->ensure_construct_finalized_called(); |
|
291 | - if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
292 | - throw new EE_Error( |
|
293 | - sprintf( |
|
294 | - esc_html__( |
|
295 | - "Cannot get display strategy for form input with name %s and id %s, because it has not been set in the constructor", |
|
296 | - "event_espresso" |
|
297 | - ), |
|
298 | - $this->html_name(), |
|
299 | - $this->html_id() |
|
300 | - ) |
|
301 | - ); |
|
302 | - } else { |
|
303 | - return $this->_display_strategy; |
|
304 | - } |
|
305 | - } |
|
306 | - |
|
307 | - |
|
308 | - |
|
309 | - /** |
|
310 | - * Sets the display strategy. |
|
311 | - * |
|
312 | - * @param EE_Display_Strategy_Base $strategy |
|
313 | - */ |
|
314 | - protected function _set_display_strategy(EE_Display_Strategy_Base $strategy) |
|
315 | - { |
|
316 | - $this->_display_strategy = $strategy; |
|
317 | - } |
|
318 | - |
|
319 | - |
|
320 | - |
|
321 | - /** |
|
322 | - * Sets the sanitization strategy |
|
323 | - * |
|
324 | - * @param EE_Normalization_Strategy_Base $strategy |
|
325 | - */ |
|
326 | - protected function _set_normalization_strategy(EE_Normalization_Strategy_Base $strategy) |
|
327 | - { |
|
328 | - $this->_normalization_strategy = $strategy; |
|
329 | - } |
|
330 | - |
|
331 | - |
|
332 | - |
|
333 | - /** |
|
334 | - * Gets sensitive_data_removal_strategy |
|
335 | - * |
|
336 | - * @return EE_Sensitive_Data_Removal_Base |
|
337 | - */ |
|
338 | - public function get_sensitive_data_removal_strategy() |
|
339 | - { |
|
340 | - return $this->_sensitive_data_removal_strategy; |
|
341 | - } |
|
342 | - |
|
343 | - |
|
344 | - |
|
345 | - /** |
|
346 | - * Sets sensitive_data_removal_strategy |
|
347 | - * |
|
348 | - * @param EE_Sensitive_Data_Removal_Base $sensitive_data_removal_strategy |
|
349 | - * @return void |
|
350 | - */ |
|
351 | - public function set_sensitive_data_removal_strategy($sensitive_data_removal_strategy) |
|
352 | - { |
|
353 | - $this->_sensitive_data_removal_strategy = $sensitive_data_removal_strategy; |
|
354 | - } |
|
355 | - |
|
356 | - |
|
357 | - |
|
358 | - /** |
|
359 | - * Gets the display strategy for this input |
|
360 | - * |
|
361 | - * @return EE_Display_Strategy_Base |
|
362 | - */ |
|
363 | - public function get_display_strategy() |
|
364 | - { |
|
365 | - return $this->_display_strategy; |
|
366 | - } |
|
367 | - |
|
368 | - |
|
369 | - |
|
370 | - /** |
|
371 | - * Overwrites the display strategy |
|
372 | - * |
|
373 | - * @param EE_Display_Strategy_Base $display_strategy |
|
374 | - */ |
|
375 | - public function set_display_strategy($display_strategy) |
|
376 | - { |
|
377 | - $this->_display_strategy = $display_strategy; |
|
378 | - $this->_display_strategy->_construct_finalize($this); |
|
379 | - } |
|
380 | - |
|
381 | - |
|
382 | - |
|
383 | - /** |
|
384 | - * Gets the normalization strategy set on this input |
|
385 | - * |
|
386 | - * @return EE_Normalization_Strategy_Base |
|
387 | - */ |
|
388 | - public function get_normalization_strategy() |
|
389 | - { |
|
390 | - return $this->_normalization_strategy; |
|
391 | - } |
|
392 | - |
|
393 | - |
|
394 | - |
|
395 | - /** |
|
396 | - * Overwrites the normalization strategy |
|
397 | - * |
|
398 | - * @param EE_Normalization_Strategy_Base $normalization_strategy |
|
399 | - */ |
|
400 | - public function set_normalization_strategy($normalization_strategy) |
|
401 | - { |
|
402 | - $this->_normalization_strategy = $normalization_strategy; |
|
403 | - $this->_normalization_strategy->_construct_finalize($this); |
|
404 | - } |
|
405 | - |
|
406 | - |
|
407 | - |
|
408 | - /** |
|
409 | - * Returns all teh validation strategies which apply to this field, numerically indexed |
|
410 | - * |
|
411 | - * @return EE_Validation_Strategy_Base[] |
|
412 | - */ |
|
413 | - public function get_validation_strategies() |
|
414 | - { |
|
415 | - return $this->_validation_strategies; |
|
416 | - } |
|
417 | - |
|
418 | - |
|
419 | - |
|
420 | - /** |
|
421 | - * Adds this strategy to the field so it will be used in both JS validation and server-side validation |
|
422 | - * |
|
423 | - * @param EE_Validation_Strategy_Base $validation_strategy |
|
424 | - * @return void |
|
425 | - */ |
|
426 | - protected function _add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
427 | - { |
|
428 | - $validation_strategy->_construct_finalize($this); |
|
429 | - $this->_validation_strategies[] = $validation_strategy; |
|
430 | - } |
|
431 | - |
|
432 | - |
|
433 | - |
|
434 | - /** |
|
435 | - * Adds a new validation strategy onto the form input |
|
436 | - * |
|
437 | - * @param EE_Validation_Strategy_Base $validation_strategy |
|
438 | - * @return void |
|
439 | - */ |
|
440 | - public function add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
441 | - { |
|
442 | - $this->_add_validation_strategy($validation_strategy); |
|
443 | - } |
|
444 | - |
|
445 | - |
|
446 | - |
|
447 | - /** |
|
448 | - * The classname of the validation strategy to remove |
|
449 | - * |
|
450 | - * @param string $validation_strategy_classname |
|
451 | - */ |
|
452 | - public function remove_validation_strategy($validation_strategy_classname) |
|
453 | - { |
|
454 | - foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
455 | - if ( |
|
456 | - $validation_strategy instanceof $validation_strategy_classname |
|
457 | - || is_subclass_of($validation_strategy, $validation_strategy_classname) |
|
458 | - ) { |
|
459 | - unset($this->_validation_strategies[ $key ]); |
|
460 | - } |
|
461 | - } |
|
462 | - } |
|
463 | - |
|
464 | - |
|
465 | - |
|
466 | - /** |
|
467 | - * returns true if input employs any of the validation strategy defined by the supplied array of classnames |
|
468 | - * |
|
469 | - * @param array $validation_strategy_classnames |
|
470 | - * @return bool |
|
471 | - */ |
|
472 | - public function has_validation_strategy($validation_strategy_classnames) |
|
473 | - { |
|
474 | - $validation_strategy_classnames = is_array($validation_strategy_classnames) |
|
475 | - ? $validation_strategy_classnames |
|
476 | - : array($validation_strategy_classnames); |
|
477 | - foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
478 | - if (in_array($key, $validation_strategy_classnames)) { |
|
479 | - return true; |
|
480 | - } |
|
481 | - } |
|
482 | - return false; |
|
483 | - } |
|
484 | - |
|
485 | - |
|
486 | - |
|
487 | - /** |
|
488 | - * Gets the HTML |
|
489 | - * |
|
490 | - * @return string |
|
491 | - */ |
|
492 | - public function get_html() |
|
493 | - { |
|
494 | - return $this->_parent_section->get_html_for_input($this); |
|
495 | - } |
|
496 | - |
|
497 | - |
|
498 | - |
|
499 | - /** |
|
500 | - * Gets the HTML for the input itself (no label or errors) according to the |
|
501 | - * input's display strategy |
|
502 | - * Makes sure the JS and CSS are enqueued for it |
|
503 | - * |
|
504 | - * @return string |
|
505 | - * @throws EE_Error |
|
506 | - */ |
|
507 | - public function get_html_for_input() |
|
508 | - { |
|
509 | - return $this->_form_html_filter |
|
510 | - ? $this->_form_html_filter->filterHtml( |
|
511 | - $this->_get_display_strategy()->display(), |
|
512 | - $this |
|
513 | - ) |
|
514 | - : $this->_get_display_strategy()->display(); |
|
515 | - } |
|
516 | - |
|
517 | - |
|
518 | - |
|
519 | - /** |
|
520 | - * @return string |
|
521 | - */ |
|
522 | - public function html_other_attributes() |
|
523 | - { |
|
524 | - EE_Error::doing_it_wrong( |
|
525 | - __METHOD__, |
|
526 | - sprintf( |
|
527 | - esc_html__( |
|
528 | - 'This method is no longer in use. You should replace it by %s', |
|
529 | - 'event_espresso' |
|
530 | - ), |
|
531 | - 'EE_Form_Section_Base::other_html_attributes()' |
|
532 | - ), |
|
533 | - '4.10.2.p' |
|
534 | - ); |
|
535 | - |
|
536 | - return $this->other_html_attributes(); |
|
537 | - } |
|
538 | - |
|
539 | - |
|
540 | - |
|
541 | - /** |
|
542 | - * @param string $html_other_attributes |
|
543 | - */ |
|
544 | - public function set_html_other_attributes($html_other_attributes) |
|
545 | - { |
|
546 | - EE_Error::doing_it_wrong( |
|
547 | - __METHOD__, |
|
548 | - sprintf( |
|
549 | - esc_html__( |
|
550 | - 'This method is no longer in use. You should replace it by %s', |
|
551 | - 'event_espresso' |
|
552 | - ), |
|
553 | - 'EE_Form_Section_Base::set_other_html_attributes()' |
|
554 | - ), |
|
555 | - '4.10.2.p' |
|
556 | - ); |
|
557 | - |
|
558 | - $this->set_other_html_attributes($html_other_attributes); |
|
559 | - } |
|
560 | - |
|
561 | - |
|
562 | - |
|
563 | - /** |
|
564 | - * Gets the HTML for displaying the label for this form input |
|
565 | - * according to the form section's layout strategy |
|
566 | - * |
|
567 | - * @return string |
|
568 | - */ |
|
569 | - public function get_html_for_label() |
|
570 | - { |
|
571 | - return $this->_parent_section->get_layout_strategy()->display_label($this); |
|
572 | - } |
|
573 | - |
|
574 | - |
|
575 | - |
|
576 | - /** |
|
577 | - * Gets the HTML for displaying the errors section for this form input |
|
578 | - * according to the form section's layout strategy |
|
579 | - * |
|
580 | - * @return string |
|
581 | - */ |
|
582 | - public function get_html_for_errors() |
|
583 | - { |
|
584 | - return $this->_parent_section->get_layout_strategy()->display_errors($this); |
|
585 | - } |
|
586 | - |
|
587 | - |
|
588 | - |
|
589 | - /** |
|
590 | - * Gets the HTML for displaying the help text for this form input |
|
591 | - * according to the form section's layout strategy |
|
592 | - * |
|
593 | - * @return string |
|
594 | - */ |
|
595 | - public function get_html_for_help() |
|
596 | - { |
|
597 | - return $this->_parent_section->get_layout_strategy()->display_help_text($this); |
|
598 | - } |
|
599 | - |
|
600 | - |
|
601 | - |
|
602 | - /** |
|
603 | - * Validates the input's sanitized value (assumes _sanitize() has already been called) |
|
604 | - * and returns whether or not the form input's submitted value is value |
|
605 | - * |
|
606 | - * @return boolean |
|
607 | - */ |
|
608 | - protected function _validate() |
|
609 | - { |
|
610 | - if ($this->isDisabled()) { |
|
611 | - return true; |
|
612 | - } |
|
613 | - foreach ($this->_validation_strategies as $validation_strategy) { |
|
614 | - if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
615 | - try { |
|
616 | - $validation_strategy->validate($this->normalized_value()); |
|
617 | - } catch (EE_Validation_Error $e) { |
|
618 | - $this->add_validation_error($e); |
|
619 | - } |
|
620 | - } |
|
621 | - } |
|
622 | - if ($this->get_validation_errors()) { |
|
623 | - return false; |
|
624 | - } else { |
|
625 | - return true; |
|
626 | - } |
|
627 | - } |
|
628 | - |
|
629 | - |
|
630 | - |
|
631 | - /** |
|
632 | - * Performs basic sanitization on this value. But what sanitization can be performed anyways? |
|
633 | - * This value MIGHT be allowed to have tags, so we can't really remove them. |
|
634 | - * |
|
635 | - * @param string $value |
|
636 | - * @return null|string |
|
637 | - */ |
|
638 | - protected function _sanitize($value) |
|
639 | - { |
|
640 | - return $value !== null ? stripslashes(html_entity_decode(trim($value))) : null; |
|
641 | - } |
|
642 | - |
|
643 | - |
|
644 | - |
|
645 | - /** |
|
646 | - * Picks out the form value that relates to this form input, |
|
647 | - * and stores it as the sanitized value on the form input, and sets the normalized value. |
|
648 | - * Returns whether or not any validation errors occurred |
|
649 | - * |
|
650 | - * @param array $req_data |
|
651 | - * @return boolean whether or not there was an error |
|
652 | - * @throws EE_Error |
|
653 | - */ |
|
654 | - protected function _normalize($req_data) |
|
655 | - { |
|
656 | - // any existing validation errors don't apply so clear them |
|
657 | - $this->_validation_errors = array(); |
|
658 | - // if the input is disabled, ignore whatever input was sent in |
|
659 | - if ($this->isDisabled()) { |
|
660 | - $this->_set_raw_value(null); |
|
661 | - $this->_set_normalized_value($this->get_default()); |
|
662 | - return false; |
|
663 | - } |
|
664 | - try { |
|
665 | - $raw_input = $this->find_form_data_for_this_section($req_data); |
|
666 | - // super simple sanitization for now |
|
667 | - if (is_array($raw_input)) { |
|
668 | - $raw_value = array(); |
|
669 | - foreach ($raw_input as $key => $value) { |
|
670 | - $raw_value[ $key ] = $this->_sanitize($value); |
|
671 | - } |
|
672 | - $this->_set_raw_value($raw_value); |
|
673 | - } else { |
|
674 | - $this->_set_raw_value($this->_sanitize($raw_input)); |
|
675 | - } |
|
676 | - // we want to mostly leave the input alone in case we need to re-display it to the user |
|
677 | - $this->_set_normalized_value($this->_normalization_strategy->normalize($this->raw_value())); |
|
678 | - return false; |
|
679 | - } catch (EE_Validation_Error $e) { |
|
680 | - $this->add_validation_error($e); |
|
681 | - return true; |
|
682 | - } |
|
683 | - } |
|
684 | - |
|
685 | - |
|
686 | - /** |
|
687 | - * @return string |
|
688 | - * @throws EE_Error |
|
689 | - */ |
|
690 | - public function html_name() |
|
691 | - { |
|
692 | - $this->_set_default_html_name_if_empty(); |
|
693 | - return $this->_html_name ?? ''; |
|
694 | - } |
|
695 | - |
|
696 | - |
|
697 | - /** |
|
698 | - * @return string |
|
699 | - * @throws EE_Error |
|
700 | - */ |
|
701 | - public function html_label_id() |
|
702 | - { |
|
703 | - return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id() . '-lbl'; |
|
704 | - } |
|
705 | - |
|
706 | - |
|
707 | - |
|
708 | - /** |
|
709 | - * @return string |
|
710 | - */ |
|
711 | - public function html_label_class() |
|
712 | - { |
|
713 | - return $this->_html_label_class ?? ''; |
|
714 | - } |
|
715 | - |
|
716 | - |
|
717 | - |
|
718 | - /** |
|
719 | - * @return string |
|
720 | - */ |
|
721 | - public function html_label_style() |
|
722 | - { |
|
723 | - return $this->_html_label_style ?? ''; |
|
724 | - } |
|
725 | - |
|
726 | - |
|
727 | - |
|
728 | - /** |
|
729 | - * @return string |
|
730 | - */ |
|
731 | - public function html_label_text() |
|
732 | - { |
|
733 | - return $this->_html_label_text ?? ''; |
|
734 | - } |
|
735 | - |
|
736 | - |
|
737 | - |
|
738 | - /** |
|
739 | - * @return string |
|
740 | - */ |
|
741 | - public function html_help_text() |
|
742 | - { |
|
743 | - return $this->_html_help_text ?? ''; |
|
744 | - } |
|
745 | - |
|
746 | - |
|
747 | - |
|
748 | - /** |
|
749 | - * @return string |
|
750 | - */ |
|
751 | - public function html_help_class() |
|
752 | - { |
|
753 | - return $this->_html_help_class ?? ''; |
|
754 | - } |
|
755 | - |
|
756 | - |
|
757 | - |
|
758 | - /** |
|
759 | - * @return string |
|
760 | - */ |
|
761 | - public function html_help_style() |
|
762 | - { |
|
763 | - return $this->_html_style ?? ''; |
|
764 | - } |
|
765 | - |
|
766 | - |
|
767 | - |
|
768 | - /** |
|
769 | - * returns the raw, UNSAFE, input, almost exactly as the user submitted it. |
|
770 | - * Please note that almost all client code should instead use the normalized_value; |
|
771 | - * or possibly raw_value_in_form (which prepares the string for displaying in an HTML attribute on a tag, |
|
772 | - * mostly by escaping quotes) |
|
773 | - * Note, we do not store the exact original value sent in the user's request because |
|
774 | - * it may have malicious content, and we MIGHT want to store the form input in a transient or something... |
|
775 | - * in which case, we would have stored the malicious content to our database. |
|
776 | - * |
|
777 | - * @return string |
|
778 | - */ |
|
779 | - public function raw_value() |
|
780 | - { |
|
781 | - return $this->_raw_value; |
|
782 | - } |
|
783 | - |
|
784 | - |
|
785 | - |
|
786 | - /** |
|
787 | - * Returns a string safe to usage in form inputs when displaying, because |
|
788 | - * it escapes all html entities |
|
789 | - * |
|
790 | - * @return string |
|
791 | - */ |
|
792 | - public function raw_value_in_form() |
|
793 | - { |
|
794 | - return htmlentities($this->raw_value(), ENT_QUOTES, 'UTF-8'); |
|
795 | - } |
|
796 | - |
|
797 | - |
|
798 | - |
|
799 | - /** |
|
800 | - * returns the value after it's been sanitized, and then converted into it's proper type |
|
801 | - * in PHP. Eg, a string, an int, an array, |
|
802 | - * |
|
803 | - * @return mixed |
|
804 | - */ |
|
805 | - public function normalized_value() |
|
806 | - { |
|
807 | - return $this->_normalized_value; |
|
808 | - } |
|
809 | - |
|
810 | - |
|
811 | - |
|
812 | - /** |
|
813 | - * Returns the normalized value is a presentable way. By default this is just |
|
814 | - * the normalized value by itself, but it can be overridden for when that's not |
|
815 | - * the best thing to display |
|
816 | - * |
|
817 | - * @return string |
|
818 | - */ |
|
819 | - public function pretty_value() |
|
820 | - { |
|
821 | - return $this->_normalized_value; |
|
822 | - } |
|
823 | - |
|
824 | - |
|
825 | - |
|
826 | - /** |
|
827 | - * When generating the JS for the jquery validation rules like<br> |
|
828 | - * <code>$( "#myform" ).validate({ |
|
829 | - * rules: { |
|
830 | - * password: "required", |
|
831 | - * password_again: { |
|
832 | - * equalTo: "#password" |
|
833 | - * } |
|
834 | - * } |
|
835 | - * });</code> |
|
836 | - * if this field had the name 'password_again', it should return |
|
837 | - * <br><code>password_again: { |
|
838 | - * equalTo: "#password" |
|
839 | - * }</code> |
|
840 | - * |
|
841 | - * @return array |
|
842 | - */ |
|
843 | - public function get_jquery_validation_rules() |
|
844 | - { |
|
845 | - $jquery_validation_js = array(); |
|
846 | - $jquery_validation_rules = array(); |
|
847 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
848 | - $jquery_validation_rules = array_replace_recursive( |
|
849 | - $jquery_validation_rules, |
|
850 | - $validation_strategy->get_jquery_validation_rule_array() |
|
851 | - ); |
|
852 | - } |
|
853 | - if (! empty($jquery_validation_rules)) { |
|
854 | - foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) { |
|
855 | - $jquery_validation_js[ $html_id_with_pound_sign ] = $jquery_validation_rules; |
|
856 | - } |
|
857 | - } |
|
858 | - return $jquery_validation_js; |
|
859 | - } |
|
860 | - |
|
861 | - |
|
862 | - |
|
863 | - /** |
|
864 | - * Sets the input's default value for use in displaying in the form. Note: value should be |
|
865 | - * normalized (Eg, if providing a default of ra Yes_NO_Input you would provide TRUE or FALSE, not '1' or '0') |
|
866 | - * |
|
867 | - * @param mixed $value |
|
868 | - * @return void |
|
869 | - */ |
|
870 | - public function set_default($value) |
|
871 | - { |
|
872 | - $this->_default = $value; |
|
873 | - $this->_set_normalized_value($value); |
|
874 | - $this->_set_raw_value($value); |
|
875 | - } |
|
876 | - |
|
877 | - |
|
878 | - |
|
879 | - /** |
|
880 | - * Sets the normalized value on this input |
|
881 | - * |
|
882 | - * @param mixed $value |
|
883 | - */ |
|
884 | - protected function _set_normalized_value($value) |
|
885 | - { |
|
886 | - $this->_normalized_value = $value; |
|
887 | - } |
|
888 | - |
|
889 | - |
|
890 | - |
|
891 | - /** |
|
892 | - * Sets the raw value on this input (ie, exactly as the user submitted it) |
|
893 | - * |
|
894 | - * @param mixed $value |
|
895 | - */ |
|
896 | - protected function _set_raw_value($value) |
|
897 | - { |
|
898 | - $this->_raw_value = $this->_normalization_strategy->unnormalize($value); |
|
899 | - } |
|
900 | - |
|
901 | - |
|
902 | - |
|
903 | - /** |
|
904 | - * Sets the HTML label text after it has already been defined |
|
905 | - * |
|
906 | - * @param string $label |
|
907 | - * @return void |
|
908 | - */ |
|
909 | - public function set_html_label_text($label) |
|
910 | - { |
|
911 | - $this->_html_label_text = $label; |
|
912 | - } |
|
913 | - |
|
914 | - |
|
915 | - |
|
916 | - /** |
|
917 | - * Sets whether or not this field is required, and adjusts the validation strategy. |
|
918 | - * If you want to use the EE_Conditionally_Required_Validation_Strategy, |
|
919 | - * please add it as a validation strategy using add_validation_strategy as normal |
|
920 | - * |
|
921 | - * @param boolean $required boolean |
|
922 | - * @param null $required_text |
|
923 | - */ |
|
924 | - public function set_required($required = true, $required_text = null) |
|
925 | - { |
|
926 | - $required = filter_var($required, FILTER_VALIDATE_BOOLEAN); |
|
927 | - // whether $required is a string or a boolean, we want to add a required validation strategy |
|
928 | - if ($required) { |
|
929 | - $this->_add_validation_strategy(new EE_Required_Validation_Strategy($required_text)); |
|
930 | - } else { |
|
931 | - $this->remove_validation_strategy('EE_Required_Validation_Strategy'); |
|
932 | - } |
|
933 | - $this->_required = $required; |
|
934 | - } |
|
935 | - |
|
936 | - |
|
937 | - |
|
938 | - /** |
|
939 | - * Returns whether or not this field is required |
|
940 | - * |
|
941 | - * @return boolean |
|
942 | - */ |
|
943 | - public function required() |
|
944 | - { |
|
945 | - return $this->_required; |
|
946 | - } |
|
947 | - |
|
948 | - |
|
949 | - |
|
950 | - /** |
|
951 | - * @param string $required_css_class |
|
952 | - */ |
|
953 | - public function set_required_css_class($required_css_class) |
|
954 | - { |
|
955 | - $this->_required_css_class = $required_css_class; |
|
956 | - } |
|
957 | - |
|
958 | - |
|
959 | - |
|
960 | - /** |
|
961 | - * @return string |
|
962 | - */ |
|
963 | - public function required_css_class() |
|
964 | - { |
|
965 | - return $this->_required_css_class; |
|
966 | - } |
|
967 | - |
|
968 | - |
|
969 | - |
|
970 | - /** |
|
971 | - * @param bool $add_required |
|
972 | - * @return string |
|
973 | - */ |
|
974 | - public function html_class($add_required = false) |
|
975 | - { |
|
976 | - return $add_required && $this->required() |
|
977 | - ? $this->required_css_class() . ' ' . $this->_html_class |
|
978 | - : $this->_html_class; |
|
979 | - } |
|
980 | - |
|
981 | - |
|
982 | - /** |
|
983 | - * Sets the help text, in case |
|
984 | - * |
|
985 | - * @param string $text |
|
986 | - */ |
|
987 | - public function set_html_help_text($text) |
|
988 | - { |
|
989 | - $this->_html_help_text = $text; |
|
990 | - } |
|
991 | - |
|
992 | - |
|
993 | - |
|
994 | - /** |
|
995 | - * Uses the sensitive data removal strategy to remove the sensitive data from this |
|
996 | - * input. If there is any kind of sensitive data removal on this input, we clear |
|
997 | - * out the raw value completely |
|
998 | - * |
|
999 | - * @return void |
|
1000 | - */ |
|
1001 | - public function clean_sensitive_data() |
|
1002 | - { |
|
1003 | - // if we do ANY kind of sensitive data removal on this, then just clear out the raw value |
|
1004 | - // if we need more logic than this we'll make a strategy for it |
|
1005 | - if ( |
|
1006 | - $this->_sensitive_data_removal_strategy |
|
1007 | - && ! $this->_sensitive_data_removal_strategy instanceof EE_No_Sensitive_Data_Removal |
|
1008 | - ) { |
|
1009 | - $this->_set_raw_value(null); |
|
1010 | - } |
|
1011 | - // and clean the normalized value according to the appropriate strategy |
|
1012 | - $this->_set_normalized_value( |
|
1013 | - $this->get_sensitive_data_removal_strategy()->remove_sensitive_data( |
|
1014 | - $this->_normalized_value |
|
1015 | - ) |
|
1016 | - ); |
|
1017 | - } |
|
1018 | - |
|
1019 | - |
|
1020 | - |
|
1021 | - /** |
|
1022 | - * @param bool $primary |
|
1023 | - * @param string $button_size |
|
1024 | - * @param string $other_attributes |
|
1025 | - */ |
|
1026 | - public function set_button_css_attributes($primary = true, $button_size = '', $other_attributes = '') |
|
1027 | - { |
|
1028 | - $button_css_attributes = 'button'; |
|
1029 | - $button_css_attributes .= $primary === true ? ' button-primary' : ' button-secondary'; |
|
1030 | - switch ($button_size) { |
|
1031 | - case 'xs': |
|
1032 | - case 'extra-small': |
|
1033 | - $button_css_attributes .= ' button-xs'; |
|
1034 | - break; |
|
1035 | - case 'sm': |
|
1036 | - case 'small': |
|
1037 | - $button_css_attributes .= ' button-sm'; |
|
1038 | - break; |
|
1039 | - case 'lg': |
|
1040 | - case 'large': |
|
1041 | - $button_css_attributes .= ' button-lg'; |
|
1042 | - break; |
|
1043 | - case 'block': |
|
1044 | - $button_css_attributes .= ' button-block'; |
|
1045 | - break; |
|
1046 | - case 'md': |
|
1047 | - case 'medium': |
|
1048 | - default: |
|
1049 | - $button_css_attributes .= ''; |
|
1050 | - } |
|
1051 | - $this->_button_css_attributes .= ! empty($other_attributes) |
|
1052 | - ? $button_css_attributes . ' ' . $other_attributes |
|
1053 | - : $button_css_attributes; |
|
1054 | - } |
|
1055 | - |
|
1056 | - |
|
1057 | - |
|
1058 | - /** |
|
1059 | - * @return string |
|
1060 | - */ |
|
1061 | - public function button_css_attributes() |
|
1062 | - { |
|
1063 | - if (empty($this->_button_css_attributes)) { |
|
1064 | - $this->set_button_css_attributes(); |
|
1065 | - } |
|
1066 | - return $this->_button_css_attributes; |
|
1067 | - } |
|
1068 | - |
|
1069 | - |
|
1070 | - |
|
1071 | - /** |
|
1072 | - * find_form_data_for_this_section |
|
1073 | - * using this section's name and its parents, finds the value of the form data that corresponds to it. |
|
1074 | - * For example, if this form section's HTML name is my_form[subform][form_input_1], |
|
1075 | - * then it's value should be in request at request['my_form']['subform']['form_input_1']. |
|
1076 | - * (If that doesn't exist, we also check for this subsection's name |
|
1077 | - * at the TOP LEVEL of the request data. Eg request['form_input_1'].) |
|
1078 | - * This function finds its value in the form. |
|
1079 | - * |
|
1080 | - * @param array $req_data |
|
1081 | - * @return mixed whatever the raw value of this form section is in the request data |
|
1082 | - * @throws EE_Error |
|
1083 | - */ |
|
1084 | - public function find_form_data_for_this_section($req_data) |
|
1085 | - { |
|
1086 | - $name_parts = $this->getInputNameParts(); |
|
1087 | - // now get the value for the input |
|
1088 | - $value = $this->findRequestForSectionUsingNameParts($name_parts, $req_data); |
|
1089 | - // check if this thing's name is at the TOP level of the request data |
|
1090 | - if ($value === null && isset($req_data[ $this->name() ])) { |
|
1091 | - $value = $req_data[ $this->name() ]; |
|
1092 | - } |
|
1093 | - return $value; |
|
1094 | - } |
|
1095 | - |
|
1096 | - |
|
1097 | - /** |
|
1098 | - * If this input's name is something like "foo[bar][baz]" |
|
1099 | - * returns an array like `array('foo','bar',baz')` |
|
1100 | - * |
|
1101 | - * @return array |
|
1102 | - * @throws EE_Error |
|
1103 | - */ |
|
1104 | - protected function getInputNameParts() |
|
1105 | - { |
|
1106 | - // break up the html name by "[]" |
|
1107 | - if (strpos($this->html_name(), '[') !== false) { |
|
1108 | - $before_any_brackets = substr($this->html_name(), 0, strpos($this->html_name(), '[')); |
|
1109 | - } else { |
|
1110 | - $before_any_brackets = $this->html_name(); |
|
1111 | - } |
|
1112 | - // grab all of the segments |
|
1113 | - preg_match_all('~\[([^]]*)\]~', $this->html_name(), $matches); |
|
1114 | - if (isset($matches[1]) && is_array($matches[1])) { |
|
1115 | - $name_parts = $matches[1]; |
|
1116 | - array_unshift($name_parts, $before_any_brackets); |
|
1117 | - } else { |
|
1118 | - $name_parts = array($before_any_brackets); |
|
1119 | - } |
|
1120 | - return $name_parts; |
|
1121 | - } |
|
1122 | - |
|
1123 | - |
|
1124 | - |
|
1125 | - /** |
|
1126 | - * @param array $html_name_parts |
|
1127 | - * @param array $req_data |
|
1128 | - * @return array | NULL |
|
1129 | - */ |
|
1130 | - public function findRequestForSectionUsingNameParts($html_name_parts, $req_data) |
|
1131 | - { |
|
1132 | - $first_part_to_consider = array_shift($html_name_parts); |
|
1133 | - if (isset($req_data[ $first_part_to_consider ])) { |
|
1134 | - if (empty($html_name_parts)) { |
|
1135 | - return $req_data[ $first_part_to_consider ]; |
|
1136 | - } else { |
|
1137 | - return $this->findRequestForSectionUsingNameParts( |
|
1138 | - $html_name_parts, |
|
1139 | - $req_data[ $first_part_to_consider ] |
|
1140 | - ); |
|
1141 | - } |
|
1142 | - } else { |
|
1143 | - return null; |
|
1144 | - } |
|
1145 | - } |
|
1146 | - |
|
1147 | - |
|
1148 | - |
|
1149 | - /** |
|
1150 | - * Checks if this form input's data is in the request data |
|
1151 | - * |
|
1152 | - * @param array $req_data |
|
1153 | - * @return boolean |
|
1154 | - * @throws EE_Error |
|
1155 | - */ |
|
1156 | - public function form_data_present_in($req_data = null) |
|
1157 | - { |
|
1158 | - if ($req_data === null) { |
|
1159 | - /** @var RequestInterface $request */ |
|
1160 | - $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
1161 | - $req_data = $request->postParams(); |
|
1162 | - } |
|
1163 | - $checked_value = $this->find_form_data_for_this_section($req_data); |
|
1164 | - if ($checked_value !== null) { |
|
1165 | - return true; |
|
1166 | - } else { |
|
1167 | - return false; |
|
1168 | - } |
|
1169 | - } |
|
1170 | - |
|
1171 | - |
|
1172 | - |
|
1173 | - /** |
|
1174 | - * Overrides parent to add js data from validation and display strategies |
|
1175 | - * |
|
1176 | - * @param array $form_other_js_data |
|
1177 | - * @return array |
|
1178 | - */ |
|
1179 | - public function get_other_js_data($form_other_js_data = array()) |
|
1180 | - { |
|
1181 | - return $this->get_other_js_data_from_strategies($form_other_js_data); |
|
1182 | - } |
|
1183 | - |
|
1184 | - |
|
1185 | - |
|
1186 | - /** |
|
1187 | - * Gets other JS data for localization from this input's strategies, like |
|
1188 | - * the validation strategies and the display strategy |
|
1189 | - * |
|
1190 | - * @param array $form_other_js_data |
|
1191 | - * @return array |
|
1192 | - */ |
|
1193 | - public function get_other_js_data_from_strategies($form_other_js_data = array()) |
|
1194 | - { |
|
1195 | - $form_other_js_data = $this->get_display_strategy()->get_other_js_data($form_other_js_data); |
|
1196 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1197 | - $form_other_js_data = $validation_strategy->get_other_js_data($form_other_js_data); |
|
1198 | - } |
|
1199 | - return $form_other_js_data; |
|
1200 | - } |
|
1201 | - |
|
1202 | - |
|
1203 | - |
|
1204 | - /** |
|
1205 | - * Override parent because we want to give our strategies an opportunity to enqueue some js and css |
|
1206 | - * |
|
1207 | - * @return void |
|
1208 | - */ |
|
1209 | - public function enqueue_js() |
|
1210 | - { |
|
1211 | - // ask our display strategy and validation strategies if they have js to enqueue |
|
1212 | - $this->enqueue_js_from_strategies(); |
|
1213 | - } |
|
1214 | - |
|
1215 | - |
|
1216 | - |
|
1217 | - /** |
|
1218 | - * Tells strategies when its ok to enqueue their js and css |
|
1219 | - * |
|
1220 | - * @return void |
|
1221 | - */ |
|
1222 | - public function enqueue_js_from_strategies() |
|
1223 | - { |
|
1224 | - $this->get_display_strategy()->enqueue_js(); |
|
1225 | - foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1226 | - $validation_strategy->enqueue_js(); |
|
1227 | - } |
|
1228 | - } |
|
1229 | - |
|
1230 | - |
|
1231 | - |
|
1232 | - /** |
|
1233 | - * Gets the default value set on the input (not the current value, which may have been |
|
1234 | - * changed because of a form submission). If no default was set, this us null. |
|
1235 | - * @return mixed |
|
1236 | - */ |
|
1237 | - public function get_default() |
|
1238 | - { |
|
1239 | - return $this->_default; |
|
1240 | - } |
|
1241 | - |
|
1242 | - |
|
1243 | - |
|
1244 | - /** |
|
1245 | - * Makes this input disabled. That means it will have the HTML attribute 'disabled="disabled"', |
|
1246 | - * and server-side if any input was received it will be ignored |
|
1247 | - */ |
|
1248 | - public function disable($disable = true) |
|
1249 | - { |
|
1250 | - $disabled_attribute = ' disabled="disabled"'; |
|
1251 | - $this->disabled = filter_var($disable, FILTER_VALIDATE_BOOLEAN); |
|
1252 | - if ($this->disabled) { |
|
1253 | - if (strpos($this->_other_html_attributes, $disabled_attribute) === false) { |
|
1254 | - $this->_other_html_attributes .= $disabled_attribute; |
|
1255 | - } |
|
1256 | - $this->_set_normalized_value($this->get_default()); |
|
1257 | - } else { |
|
1258 | - $this->_other_html_attributes = str_replace($disabled_attribute, '', $this->_other_html_attributes); |
|
1259 | - } |
|
1260 | - } |
|
1261 | - |
|
1262 | - |
|
1263 | - |
|
1264 | - /** |
|
1265 | - * Returns whether or not this input is currently disabled. |
|
1266 | - * @return bool |
|
1267 | - */ |
|
1268 | - public function isDisabled() |
|
1269 | - { |
|
1270 | - return $this->disabled; |
|
1271 | - } |
|
17 | + /** |
|
18 | + * the input's name attribute |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $_html_name; |
|
23 | + |
|
24 | + /** |
|
25 | + * id for the html label tag |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $_html_label_id; |
|
30 | + |
|
31 | + /** |
|
32 | + * class for teh html label tag |
|
33 | + * |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + protected $_html_label_class; |
|
37 | + |
|
38 | + /** |
|
39 | + * style for teh html label tag |
|
40 | + * |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + protected $_html_label_style; |
|
44 | + |
|
45 | + /** |
|
46 | + * text to be placed in the html label |
|
47 | + * |
|
48 | + * @var string |
|
49 | + */ |
|
50 | + protected $_html_label_text; |
|
51 | + |
|
52 | + /** |
|
53 | + * the full html label. If used, all other html_label_* properties are invalid |
|
54 | + * |
|
55 | + * @var string |
|
56 | + */ |
|
57 | + protected $_html_label; |
|
58 | + |
|
59 | + /** |
|
60 | + * HTML to use for help text (normally placed below form input), in a span which normally |
|
61 | + * has a class of 'description' |
|
62 | + * |
|
63 | + * @var string |
|
64 | + */ |
|
65 | + protected $_html_help_text; |
|
66 | + |
|
67 | + /** |
|
68 | + * CSS classes for displaying the help span |
|
69 | + * |
|
70 | + * @var string |
|
71 | + */ |
|
72 | + protected $_html_help_class = 'description'; |
|
73 | + |
|
74 | + /** |
|
75 | + * CSS to put in the style attribute on the help span |
|
76 | + * |
|
77 | + * @var string |
|
78 | + */ |
|
79 | + protected $_html_help_style; |
|
80 | + |
|
81 | + /** |
|
82 | + * Stores whether or not this input's response is required. |
|
83 | + * Because certain styling elements may also want to know that this |
|
84 | + * input is required etc. |
|
85 | + * |
|
86 | + * @var boolean |
|
87 | + */ |
|
88 | + protected $_required; |
|
89 | + |
|
90 | + /** |
|
91 | + * css class added to required inputs |
|
92 | + * |
|
93 | + * @var string |
|
94 | + */ |
|
95 | + protected $_required_css_class = 'ee-required'; |
|
96 | + |
|
97 | + /** |
|
98 | + * css styles applied to button type inputs |
|
99 | + * |
|
100 | + * @var string |
|
101 | + */ |
|
102 | + protected $_button_css_attributes; |
|
103 | + |
|
104 | + /** |
|
105 | + * The raw post data submitted for this |
|
106 | + * Generally unsafe for usage in client code |
|
107 | + * |
|
108 | + * @var mixed string or array |
|
109 | + */ |
|
110 | + protected $_raw_value; |
|
111 | + |
|
112 | + /** |
|
113 | + * Value normalized according to the input's normalization strategy. |
|
114 | + * The normalization strategy dictates whether this is a string, int, float, |
|
115 | + * boolean, or array of any of those. |
|
116 | + * |
|
117 | + * @var mixed |
|
118 | + */ |
|
119 | + protected $_normalized_value; |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * Normalized default value either initially set on the input, or provided by calling |
|
124 | + * set_default(). |
|
125 | + * @var mixed |
|
126 | + */ |
|
127 | + protected $_default; |
|
128 | + |
|
129 | + /** |
|
130 | + * Strategy used for displaying this field. |
|
131 | + * Child classes must use _get_display_strategy to access it. |
|
132 | + * |
|
133 | + * @var EE_Display_Strategy_Base |
|
134 | + */ |
|
135 | + private $_display_strategy; |
|
136 | + |
|
137 | + /** |
|
138 | + * Gets all the validation strategies used on this field |
|
139 | + * |
|
140 | + * @var EE_Validation_Strategy_Base[] |
|
141 | + */ |
|
142 | + private $_validation_strategies = array(); |
|
143 | + |
|
144 | + /** |
|
145 | + * The normalization strategy for this field |
|
146 | + * |
|
147 | + * @var EE_Normalization_Strategy_Base |
|
148 | + */ |
|
149 | + private $_normalization_strategy; |
|
150 | + |
|
151 | + /** |
|
152 | + * Strategy for removing sensitive data after we're done with the form input |
|
153 | + * |
|
154 | + * @var EE_Sensitive_Data_Removal_Base |
|
155 | + */ |
|
156 | + protected $_sensitive_data_removal_strategy; |
|
157 | + |
|
158 | + /** |
|
159 | + * Whether this input has been disabled or not. |
|
160 | + * If it's disabled while rendering, an extra hidden input is added that indicates it has been knowingly disabled. |
|
161 | + * (Client-side code that wants to dynamically disable it must also add this hidden input). |
|
162 | + * When the form is submitted, if the input is disabled in the PHP form section, then input is ignored. |
|
163 | + * If the input is missing from the request data but the hidden input indicating the input is disabled, then the input is again ignored. |
|
164 | + * |
|
165 | + * @var boolean |
|
166 | + */ |
|
167 | + protected $disabled = false; |
|
168 | + |
|
169 | + |
|
170 | + |
|
171 | + /** |
|
172 | + * @param array $input_args { |
|
173 | + * @type string $html_name the html name for the input |
|
174 | + * @type string $html_label_id the id attribute to give to the html label tag |
|
175 | + * @type string $html_label_class the class attribute to give to the html label tag |
|
176 | + * @type string $html_label_style the style attribute to give ot teh label tag |
|
177 | + * @type string $html_label_text the text to put in the label tag |
|
178 | + * @type string $html_label the full html label. If used, |
|
179 | + * all other html_label_* args are invalid |
|
180 | + * @type string $html_help_text text to put in help element |
|
181 | + * @type string $html_help_style style attribute to give to teh help element |
|
182 | + * @type string $html_help_class class attribute to give to the help element |
|
183 | + * @type string $default default value NORMALIZED (eg, if providing the default |
|
184 | + * for a Yes_No_Input, you should provide TRUE or FALSE, not '1' or '0') |
|
185 | + * @type EE_Display_Strategy_Base $display strategy |
|
186 | + * @type EE_Normalization_Strategy_Base $normalization_strategy |
|
187 | + * @type EE_Validation_Strategy_Base[] $validation_strategies |
|
188 | + * @type boolean $ignore_input special argument which can be used to avoid adding any validation strategies, |
|
189 | + * and sets the normalization strategy to the Null normalization. This is good |
|
190 | + * when you want the input to be totally ignored server-side (like when using |
|
191 | + * React.js form inputs) |
|
192 | + * } |
|
193 | + */ |
|
194 | + public function __construct($input_args = array()) |
|
195 | + { |
|
196 | + $input_args = (array) apply_filters('FHEE__EE_Form_Input_Base___construct__input_args', $input_args, $this); |
|
197 | + // the following properties must be cast as arrays |
|
198 | + if (isset($input_args['validation_strategies'])) { |
|
199 | + foreach ((array) $input_args['validation_strategies'] as $validation_strategy) { |
|
200 | + if ($validation_strategy instanceof EE_Validation_Strategy_Base && empty($input_args['ignore_input'])) { |
|
201 | + $this->_validation_strategies[ get_class($validation_strategy) ] = $validation_strategy; |
|
202 | + } |
|
203 | + } |
|
204 | + unset($input_args['validation_strategies']); |
|
205 | + } |
|
206 | + if (isset($input_args['ignore_input'])) { |
|
207 | + $this->_validation_strategies = array(); |
|
208 | + } |
|
209 | + // loop thru incoming options |
|
210 | + foreach ($input_args as $key => $value) { |
|
211 | + // add underscore to $key to match property names |
|
212 | + $_key = '_' . $key; |
|
213 | + if (property_exists($this, $_key)) { |
|
214 | + $this->{$_key} = $value; |
|
215 | + } |
|
216 | + } |
|
217 | + // ensure that "required" is set correctly |
|
218 | + $this->set_required( |
|
219 | + $this->_required, |
|
220 | + $input_args['required_validation_error_message'] ?? null |
|
221 | + ); |
|
222 | + // $this->_html_name_specified = isset( $input_args['html_name'] ) ? TRUE : FALSE; |
|
223 | + $this->_display_strategy->_construct_finalize($this); |
|
224 | + foreach ($this->_validation_strategies as $validation_strategy) { |
|
225 | + $validation_strategy->_construct_finalize($this); |
|
226 | + } |
|
227 | + if (isset($input_args['ignore_input'])) { |
|
228 | + $this->_normalization_strategy = new EE_Null_Normalization(); |
|
229 | + } |
|
230 | + if (! $this->_normalization_strategy) { |
|
231 | + $this->_normalization_strategy = new EE_Text_Normalization(); |
|
232 | + } |
|
233 | + $this->_normalization_strategy->_construct_finalize($this); |
|
234 | + // at least we can use the normalization strategy to populate the default |
|
235 | + if (isset($input_args['default'])) { |
|
236 | + $this->set_default($input_args['default']); |
|
237 | + unset($input_args['default']); |
|
238 | + } |
|
239 | + if (! $this->_sensitive_data_removal_strategy) { |
|
240 | + $this->_sensitive_data_removal_strategy = new EE_No_Sensitive_Data_Removal(); |
|
241 | + } |
|
242 | + $this->_sensitive_data_removal_strategy->_construct_finalize($this); |
|
243 | + parent::__construct($input_args); |
|
244 | + } |
|
245 | + |
|
246 | + |
|
247 | + |
|
248 | + /** |
|
249 | + * Sets the html_name to its default value, if none was specified in teh constructor. |
|
250 | + * Calculation involves using the name and the parent's html_name |
|
251 | + * |
|
252 | + * @throws EE_Error |
|
253 | + */ |
|
254 | + protected function _set_default_html_name_if_empty() |
|
255 | + { |
|
256 | + if (! $this->_html_name) { |
|
257 | + $this->_html_name = $this->name(); |
|
258 | + if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) { |
|
259 | + $this->_html_name = $this->_parent_section->html_name_prefix() . "[{$this->name()}]"; |
|
260 | + } |
|
261 | + } |
|
262 | + } |
|
263 | + |
|
264 | + |
|
265 | + |
|
266 | + /** |
|
267 | + * @param $parent_form_section |
|
268 | + * @param $name |
|
269 | + * @throws EE_Error |
|
270 | + */ |
|
271 | + public function _construct_finalize($parent_form_section, $name) |
|
272 | + { |
|
273 | + parent::_construct_finalize($parent_form_section, $name); |
|
274 | + if ($this->_html_label === null && $this->_html_label_text === null) { |
|
275 | + $this->_html_label_text = ucwords(str_replace("_", " ", $name)); |
|
276 | + } |
|
277 | + do_action('AHEE__EE_Form_Input_Base___construct_finalize__end', $this, $parent_form_section, $name); |
|
278 | + } |
|
279 | + |
|
280 | + |
|
281 | + |
|
282 | + /** |
|
283 | + * Returns the strategy for displaying this form input. If none is set, throws an exception. |
|
284 | + * |
|
285 | + * @return EE_Display_Strategy_Base |
|
286 | + * @throws EE_Error |
|
287 | + */ |
|
288 | + protected function _get_display_strategy() |
|
289 | + { |
|
290 | + $this->ensure_construct_finalized_called(); |
|
291 | + if (! $this->_display_strategy || ! $this->_display_strategy instanceof EE_Display_Strategy_Base) { |
|
292 | + throw new EE_Error( |
|
293 | + sprintf( |
|
294 | + esc_html__( |
|
295 | + "Cannot get display strategy for form input with name %s and id %s, because it has not been set in the constructor", |
|
296 | + "event_espresso" |
|
297 | + ), |
|
298 | + $this->html_name(), |
|
299 | + $this->html_id() |
|
300 | + ) |
|
301 | + ); |
|
302 | + } else { |
|
303 | + return $this->_display_strategy; |
|
304 | + } |
|
305 | + } |
|
306 | + |
|
307 | + |
|
308 | + |
|
309 | + /** |
|
310 | + * Sets the display strategy. |
|
311 | + * |
|
312 | + * @param EE_Display_Strategy_Base $strategy |
|
313 | + */ |
|
314 | + protected function _set_display_strategy(EE_Display_Strategy_Base $strategy) |
|
315 | + { |
|
316 | + $this->_display_strategy = $strategy; |
|
317 | + } |
|
318 | + |
|
319 | + |
|
320 | + |
|
321 | + /** |
|
322 | + * Sets the sanitization strategy |
|
323 | + * |
|
324 | + * @param EE_Normalization_Strategy_Base $strategy |
|
325 | + */ |
|
326 | + protected function _set_normalization_strategy(EE_Normalization_Strategy_Base $strategy) |
|
327 | + { |
|
328 | + $this->_normalization_strategy = $strategy; |
|
329 | + } |
|
330 | + |
|
331 | + |
|
332 | + |
|
333 | + /** |
|
334 | + * Gets sensitive_data_removal_strategy |
|
335 | + * |
|
336 | + * @return EE_Sensitive_Data_Removal_Base |
|
337 | + */ |
|
338 | + public function get_sensitive_data_removal_strategy() |
|
339 | + { |
|
340 | + return $this->_sensitive_data_removal_strategy; |
|
341 | + } |
|
342 | + |
|
343 | + |
|
344 | + |
|
345 | + /** |
|
346 | + * Sets sensitive_data_removal_strategy |
|
347 | + * |
|
348 | + * @param EE_Sensitive_Data_Removal_Base $sensitive_data_removal_strategy |
|
349 | + * @return void |
|
350 | + */ |
|
351 | + public function set_sensitive_data_removal_strategy($sensitive_data_removal_strategy) |
|
352 | + { |
|
353 | + $this->_sensitive_data_removal_strategy = $sensitive_data_removal_strategy; |
|
354 | + } |
|
355 | + |
|
356 | + |
|
357 | + |
|
358 | + /** |
|
359 | + * Gets the display strategy for this input |
|
360 | + * |
|
361 | + * @return EE_Display_Strategy_Base |
|
362 | + */ |
|
363 | + public function get_display_strategy() |
|
364 | + { |
|
365 | + return $this->_display_strategy; |
|
366 | + } |
|
367 | + |
|
368 | + |
|
369 | + |
|
370 | + /** |
|
371 | + * Overwrites the display strategy |
|
372 | + * |
|
373 | + * @param EE_Display_Strategy_Base $display_strategy |
|
374 | + */ |
|
375 | + public function set_display_strategy($display_strategy) |
|
376 | + { |
|
377 | + $this->_display_strategy = $display_strategy; |
|
378 | + $this->_display_strategy->_construct_finalize($this); |
|
379 | + } |
|
380 | + |
|
381 | + |
|
382 | + |
|
383 | + /** |
|
384 | + * Gets the normalization strategy set on this input |
|
385 | + * |
|
386 | + * @return EE_Normalization_Strategy_Base |
|
387 | + */ |
|
388 | + public function get_normalization_strategy() |
|
389 | + { |
|
390 | + return $this->_normalization_strategy; |
|
391 | + } |
|
392 | + |
|
393 | + |
|
394 | + |
|
395 | + /** |
|
396 | + * Overwrites the normalization strategy |
|
397 | + * |
|
398 | + * @param EE_Normalization_Strategy_Base $normalization_strategy |
|
399 | + */ |
|
400 | + public function set_normalization_strategy($normalization_strategy) |
|
401 | + { |
|
402 | + $this->_normalization_strategy = $normalization_strategy; |
|
403 | + $this->_normalization_strategy->_construct_finalize($this); |
|
404 | + } |
|
405 | + |
|
406 | + |
|
407 | + |
|
408 | + /** |
|
409 | + * Returns all teh validation strategies which apply to this field, numerically indexed |
|
410 | + * |
|
411 | + * @return EE_Validation_Strategy_Base[] |
|
412 | + */ |
|
413 | + public function get_validation_strategies() |
|
414 | + { |
|
415 | + return $this->_validation_strategies; |
|
416 | + } |
|
417 | + |
|
418 | + |
|
419 | + |
|
420 | + /** |
|
421 | + * Adds this strategy to the field so it will be used in both JS validation and server-side validation |
|
422 | + * |
|
423 | + * @param EE_Validation_Strategy_Base $validation_strategy |
|
424 | + * @return void |
|
425 | + */ |
|
426 | + protected function _add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
427 | + { |
|
428 | + $validation_strategy->_construct_finalize($this); |
|
429 | + $this->_validation_strategies[] = $validation_strategy; |
|
430 | + } |
|
431 | + |
|
432 | + |
|
433 | + |
|
434 | + /** |
|
435 | + * Adds a new validation strategy onto the form input |
|
436 | + * |
|
437 | + * @param EE_Validation_Strategy_Base $validation_strategy |
|
438 | + * @return void |
|
439 | + */ |
|
440 | + public function add_validation_strategy(EE_Validation_Strategy_Base $validation_strategy) |
|
441 | + { |
|
442 | + $this->_add_validation_strategy($validation_strategy); |
|
443 | + } |
|
444 | + |
|
445 | + |
|
446 | + |
|
447 | + /** |
|
448 | + * The classname of the validation strategy to remove |
|
449 | + * |
|
450 | + * @param string $validation_strategy_classname |
|
451 | + */ |
|
452 | + public function remove_validation_strategy($validation_strategy_classname) |
|
453 | + { |
|
454 | + foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
455 | + if ( |
|
456 | + $validation_strategy instanceof $validation_strategy_classname |
|
457 | + || is_subclass_of($validation_strategy, $validation_strategy_classname) |
|
458 | + ) { |
|
459 | + unset($this->_validation_strategies[ $key ]); |
|
460 | + } |
|
461 | + } |
|
462 | + } |
|
463 | + |
|
464 | + |
|
465 | + |
|
466 | + /** |
|
467 | + * returns true if input employs any of the validation strategy defined by the supplied array of classnames |
|
468 | + * |
|
469 | + * @param array $validation_strategy_classnames |
|
470 | + * @return bool |
|
471 | + */ |
|
472 | + public function has_validation_strategy($validation_strategy_classnames) |
|
473 | + { |
|
474 | + $validation_strategy_classnames = is_array($validation_strategy_classnames) |
|
475 | + ? $validation_strategy_classnames |
|
476 | + : array($validation_strategy_classnames); |
|
477 | + foreach ($this->_validation_strategies as $key => $validation_strategy) { |
|
478 | + if (in_array($key, $validation_strategy_classnames)) { |
|
479 | + return true; |
|
480 | + } |
|
481 | + } |
|
482 | + return false; |
|
483 | + } |
|
484 | + |
|
485 | + |
|
486 | + |
|
487 | + /** |
|
488 | + * Gets the HTML |
|
489 | + * |
|
490 | + * @return string |
|
491 | + */ |
|
492 | + public function get_html() |
|
493 | + { |
|
494 | + return $this->_parent_section->get_html_for_input($this); |
|
495 | + } |
|
496 | + |
|
497 | + |
|
498 | + |
|
499 | + /** |
|
500 | + * Gets the HTML for the input itself (no label or errors) according to the |
|
501 | + * input's display strategy |
|
502 | + * Makes sure the JS and CSS are enqueued for it |
|
503 | + * |
|
504 | + * @return string |
|
505 | + * @throws EE_Error |
|
506 | + */ |
|
507 | + public function get_html_for_input() |
|
508 | + { |
|
509 | + return $this->_form_html_filter |
|
510 | + ? $this->_form_html_filter->filterHtml( |
|
511 | + $this->_get_display_strategy()->display(), |
|
512 | + $this |
|
513 | + ) |
|
514 | + : $this->_get_display_strategy()->display(); |
|
515 | + } |
|
516 | + |
|
517 | + |
|
518 | + |
|
519 | + /** |
|
520 | + * @return string |
|
521 | + */ |
|
522 | + public function html_other_attributes() |
|
523 | + { |
|
524 | + EE_Error::doing_it_wrong( |
|
525 | + __METHOD__, |
|
526 | + sprintf( |
|
527 | + esc_html__( |
|
528 | + 'This method is no longer in use. You should replace it by %s', |
|
529 | + 'event_espresso' |
|
530 | + ), |
|
531 | + 'EE_Form_Section_Base::other_html_attributes()' |
|
532 | + ), |
|
533 | + '4.10.2.p' |
|
534 | + ); |
|
535 | + |
|
536 | + return $this->other_html_attributes(); |
|
537 | + } |
|
538 | + |
|
539 | + |
|
540 | + |
|
541 | + /** |
|
542 | + * @param string $html_other_attributes |
|
543 | + */ |
|
544 | + public function set_html_other_attributes($html_other_attributes) |
|
545 | + { |
|
546 | + EE_Error::doing_it_wrong( |
|
547 | + __METHOD__, |
|
548 | + sprintf( |
|
549 | + esc_html__( |
|
550 | + 'This method is no longer in use. You should replace it by %s', |
|
551 | + 'event_espresso' |
|
552 | + ), |
|
553 | + 'EE_Form_Section_Base::set_other_html_attributes()' |
|
554 | + ), |
|
555 | + '4.10.2.p' |
|
556 | + ); |
|
557 | + |
|
558 | + $this->set_other_html_attributes($html_other_attributes); |
|
559 | + } |
|
560 | + |
|
561 | + |
|
562 | + |
|
563 | + /** |
|
564 | + * Gets the HTML for displaying the label for this form input |
|
565 | + * according to the form section's layout strategy |
|
566 | + * |
|
567 | + * @return string |
|
568 | + */ |
|
569 | + public function get_html_for_label() |
|
570 | + { |
|
571 | + return $this->_parent_section->get_layout_strategy()->display_label($this); |
|
572 | + } |
|
573 | + |
|
574 | + |
|
575 | + |
|
576 | + /** |
|
577 | + * Gets the HTML for displaying the errors section for this form input |
|
578 | + * according to the form section's layout strategy |
|
579 | + * |
|
580 | + * @return string |
|
581 | + */ |
|
582 | + public function get_html_for_errors() |
|
583 | + { |
|
584 | + return $this->_parent_section->get_layout_strategy()->display_errors($this); |
|
585 | + } |
|
586 | + |
|
587 | + |
|
588 | + |
|
589 | + /** |
|
590 | + * Gets the HTML for displaying the help text for this form input |
|
591 | + * according to the form section's layout strategy |
|
592 | + * |
|
593 | + * @return string |
|
594 | + */ |
|
595 | + public function get_html_for_help() |
|
596 | + { |
|
597 | + return $this->_parent_section->get_layout_strategy()->display_help_text($this); |
|
598 | + } |
|
599 | + |
|
600 | + |
|
601 | + |
|
602 | + /** |
|
603 | + * Validates the input's sanitized value (assumes _sanitize() has already been called) |
|
604 | + * and returns whether or not the form input's submitted value is value |
|
605 | + * |
|
606 | + * @return boolean |
|
607 | + */ |
|
608 | + protected function _validate() |
|
609 | + { |
|
610 | + if ($this->isDisabled()) { |
|
611 | + return true; |
|
612 | + } |
|
613 | + foreach ($this->_validation_strategies as $validation_strategy) { |
|
614 | + if ($validation_strategy instanceof EE_Validation_Strategy_Base) { |
|
615 | + try { |
|
616 | + $validation_strategy->validate($this->normalized_value()); |
|
617 | + } catch (EE_Validation_Error $e) { |
|
618 | + $this->add_validation_error($e); |
|
619 | + } |
|
620 | + } |
|
621 | + } |
|
622 | + if ($this->get_validation_errors()) { |
|
623 | + return false; |
|
624 | + } else { |
|
625 | + return true; |
|
626 | + } |
|
627 | + } |
|
628 | + |
|
629 | + |
|
630 | + |
|
631 | + /** |
|
632 | + * Performs basic sanitization on this value. But what sanitization can be performed anyways? |
|
633 | + * This value MIGHT be allowed to have tags, so we can't really remove them. |
|
634 | + * |
|
635 | + * @param string $value |
|
636 | + * @return null|string |
|
637 | + */ |
|
638 | + protected function _sanitize($value) |
|
639 | + { |
|
640 | + return $value !== null ? stripslashes(html_entity_decode(trim($value))) : null; |
|
641 | + } |
|
642 | + |
|
643 | + |
|
644 | + |
|
645 | + /** |
|
646 | + * Picks out the form value that relates to this form input, |
|
647 | + * and stores it as the sanitized value on the form input, and sets the normalized value. |
|
648 | + * Returns whether or not any validation errors occurred |
|
649 | + * |
|
650 | + * @param array $req_data |
|
651 | + * @return boolean whether or not there was an error |
|
652 | + * @throws EE_Error |
|
653 | + */ |
|
654 | + protected function _normalize($req_data) |
|
655 | + { |
|
656 | + // any existing validation errors don't apply so clear them |
|
657 | + $this->_validation_errors = array(); |
|
658 | + // if the input is disabled, ignore whatever input was sent in |
|
659 | + if ($this->isDisabled()) { |
|
660 | + $this->_set_raw_value(null); |
|
661 | + $this->_set_normalized_value($this->get_default()); |
|
662 | + return false; |
|
663 | + } |
|
664 | + try { |
|
665 | + $raw_input = $this->find_form_data_for_this_section($req_data); |
|
666 | + // super simple sanitization for now |
|
667 | + if (is_array($raw_input)) { |
|
668 | + $raw_value = array(); |
|
669 | + foreach ($raw_input as $key => $value) { |
|
670 | + $raw_value[ $key ] = $this->_sanitize($value); |
|
671 | + } |
|
672 | + $this->_set_raw_value($raw_value); |
|
673 | + } else { |
|
674 | + $this->_set_raw_value($this->_sanitize($raw_input)); |
|
675 | + } |
|
676 | + // we want to mostly leave the input alone in case we need to re-display it to the user |
|
677 | + $this->_set_normalized_value($this->_normalization_strategy->normalize($this->raw_value())); |
|
678 | + return false; |
|
679 | + } catch (EE_Validation_Error $e) { |
|
680 | + $this->add_validation_error($e); |
|
681 | + return true; |
|
682 | + } |
|
683 | + } |
|
684 | + |
|
685 | + |
|
686 | + /** |
|
687 | + * @return string |
|
688 | + * @throws EE_Error |
|
689 | + */ |
|
690 | + public function html_name() |
|
691 | + { |
|
692 | + $this->_set_default_html_name_if_empty(); |
|
693 | + return $this->_html_name ?? ''; |
|
694 | + } |
|
695 | + |
|
696 | + |
|
697 | + /** |
|
698 | + * @return string |
|
699 | + * @throws EE_Error |
|
700 | + */ |
|
701 | + public function html_label_id() |
|
702 | + { |
|
703 | + return ! empty($this->_html_label_id) ? $this->_html_label_id : $this->html_id() . '-lbl'; |
|
704 | + } |
|
705 | + |
|
706 | + |
|
707 | + |
|
708 | + /** |
|
709 | + * @return string |
|
710 | + */ |
|
711 | + public function html_label_class() |
|
712 | + { |
|
713 | + return $this->_html_label_class ?? ''; |
|
714 | + } |
|
715 | + |
|
716 | + |
|
717 | + |
|
718 | + /** |
|
719 | + * @return string |
|
720 | + */ |
|
721 | + public function html_label_style() |
|
722 | + { |
|
723 | + return $this->_html_label_style ?? ''; |
|
724 | + } |
|
725 | + |
|
726 | + |
|
727 | + |
|
728 | + /** |
|
729 | + * @return string |
|
730 | + */ |
|
731 | + public function html_label_text() |
|
732 | + { |
|
733 | + return $this->_html_label_text ?? ''; |
|
734 | + } |
|
735 | + |
|
736 | + |
|
737 | + |
|
738 | + /** |
|
739 | + * @return string |
|
740 | + */ |
|
741 | + public function html_help_text() |
|
742 | + { |
|
743 | + return $this->_html_help_text ?? ''; |
|
744 | + } |
|
745 | + |
|
746 | + |
|
747 | + |
|
748 | + /** |
|
749 | + * @return string |
|
750 | + */ |
|
751 | + public function html_help_class() |
|
752 | + { |
|
753 | + return $this->_html_help_class ?? ''; |
|
754 | + } |
|
755 | + |
|
756 | + |
|
757 | + |
|
758 | + /** |
|
759 | + * @return string |
|
760 | + */ |
|
761 | + public function html_help_style() |
|
762 | + { |
|
763 | + return $this->_html_style ?? ''; |
|
764 | + } |
|
765 | + |
|
766 | + |
|
767 | + |
|
768 | + /** |
|
769 | + * returns the raw, UNSAFE, input, almost exactly as the user submitted it. |
|
770 | + * Please note that almost all client code should instead use the normalized_value; |
|
771 | + * or possibly raw_value_in_form (which prepares the string for displaying in an HTML attribute on a tag, |
|
772 | + * mostly by escaping quotes) |
|
773 | + * Note, we do not store the exact original value sent in the user's request because |
|
774 | + * it may have malicious content, and we MIGHT want to store the form input in a transient or something... |
|
775 | + * in which case, we would have stored the malicious content to our database. |
|
776 | + * |
|
777 | + * @return string |
|
778 | + */ |
|
779 | + public function raw_value() |
|
780 | + { |
|
781 | + return $this->_raw_value; |
|
782 | + } |
|
783 | + |
|
784 | + |
|
785 | + |
|
786 | + /** |
|
787 | + * Returns a string safe to usage in form inputs when displaying, because |
|
788 | + * it escapes all html entities |
|
789 | + * |
|
790 | + * @return string |
|
791 | + */ |
|
792 | + public function raw_value_in_form() |
|
793 | + { |
|
794 | + return htmlentities($this->raw_value(), ENT_QUOTES, 'UTF-8'); |
|
795 | + } |
|
796 | + |
|
797 | + |
|
798 | + |
|
799 | + /** |
|
800 | + * returns the value after it's been sanitized, and then converted into it's proper type |
|
801 | + * in PHP. Eg, a string, an int, an array, |
|
802 | + * |
|
803 | + * @return mixed |
|
804 | + */ |
|
805 | + public function normalized_value() |
|
806 | + { |
|
807 | + return $this->_normalized_value; |
|
808 | + } |
|
809 | + |
|
810 | + |
|
811 | + |
|
812 | + /** |
|
813 | + * Returns the normalized value is a presentable way. By default this is just |
|
814 | + * the normalized value by itself, but it can be overridden for when that's not |
|
815 | + * the best thing to display |
|
816 | + * |
|
817 | + * @return string |
|
818 | + */ |
|
819 | + public function pretty_value() |
|
820 | + { |
|
821 | + return $this->_normalized_value; |
|
822 | + } |
|
823 | + |
|
824 | + |
|
825 | + |
|
826 | + /** |
|
827 | + * When generating the JS for the jquery validation rules like<br> |
|
828 | + * <code>$( "#myform" ).validate({ |
|
829 | + * rules: { |
|
830 | + * password: "required", |
|
831 | + * password_again: { |
|
832 | + * equalTo: "#password" |
|
833 | + * } |
|
834 | + * } |
|
835 | + * });</code> |
|
836 | + * if this field had the name 'password_again', it should return |
|
837 | + * <br><code>password_again: { |
|
838 | + * equalTo: "#password" |
|
839 | + * }</code> |
|
840 | + * |
|
841 | + * @return array |
|
842 | + */ |
|
843 | + public function get_jquery_validation_rules() |
|
844 | + { |
|
845 | + $jquery_validation_js = array(); |
|
846 | + $jquery_validation_rules = array(); |
|
847 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
848 | + $jquery_validation_rules = array_replace_recursive( |
|
849 | + $jquery_validation_rules, |
|
850 | + $validation_strategy->get_jquery_validation_rule_array() |
|
851 | + ); |
|
852 | + } |
|
853 | + if (! empty($jquery_validation_rules)) { |
|
854 | + foreach ($this->get_display_strategy()->get_html_input_ids(true) as $html_id_with_pound_sign) { |
|
855 | + $jquery_validation_js[ $html_id_with_pound_sign ] = $jquery_validation_rules; |
|
856 | + } |
|
857 | + } |
|
858 | + return $jquery_validation_js; |
|
859 | + } |
|
860 | + |
|
861 | + |
|
862 | + |
|
863 | + /** |
|
864 | + * Sets the input's default value for use in displaying in the form. Note: value should be |
|
865 | + * normalized (Eg, if providing a default of ra Yes_NO_Input you would provide TRUE or FALSE, not '1' or '0') |
|
866 | + * |
|
867 | + * @param mixed $value |
|
868 | + * @return void |
|
869 | + */ |
|
870 | + public function set_default($value) |
|
871 | + { |
|
872 | + $this->_default = $value; |
|
873 | + $this->_set_normalized_value($value); |
|
874 | + $this->_set_raw_value($value); |
|
875 | + } |
|
876 | + |
|
877 | + |
|
878 | + |
|
879 | + /** |
|
880 | + * Sets the normalized value on this input |
|
881 | + * |
|
882 | + * @param mixed $value |
|
883 | + */ |
|
884 | + protected function _set_normalized_value($value) |
|
885 | + { |
|
886 | + $this->_normalized_value = $value; |
|
887 | + } |
|
888 | + |
|
889 | + |
|
890 | + |
|
891 | + /** |
|
892 | + * Sets the raw value on this input (ie, exactly as the user submitted it) |
|
893 | + * |
|
894 | + * @param mixed $value |
|
895 | + */ |
|
896 | + protected function _set_raw_value($value) |
|
897 | + { |
|
898 | + $this->_raw_value = $this->_normalization_strategy->unnormalize($value); |
|
899 | + } |
|
900 | + |
|
901 | + |
|
902 | + |
|
903 | + /** |
|
904 | + * Sets the HTML label text after it has already been defined |
|
905 | + * |
|
906 | + * @param string $label |
|
907 | + * @return void |
|
908 | + */ |
|
909 | + public function set_html_label_text($label) |
|
910 | + { |
|
911 | + $this->_html_label_text = $label; |
|
912 | + } |
|
913 | + |
|
914 | + |
|
915 | + |
|
916 | + /** |
|
917 | + * Sets whether or not this field is required, and adjusts the validation strategy. |
|
918 | + * If you want to use the EE_Conditionally_Required_Validation_Strategy, |
|
919 | + * please add it as a validation strategy using add_validation_strategy as normal |
|
920 | + * |
|
921 | + * @param boolean $required boolean |
|
922 | + * @param null $required_text |
|
923 | + */ |
|
924 | + public function set_required($required = true, $required_text = null) |
|
925 | + { |
|
926 | + $required = filter_var($required, FILTER_VALIDATE_BOOLEAN); |
|
927 | + // whether $required is a string or a boolean, we want to add a required validation strategy |
|
928 | + if ($required) { |
|
929 | + $this->_add_validation_strategy(new EE_Required_Validation_Strategy($required_text)); |
|
930 | + } else { |
|
931 | + $this->remove_validation_strategy('EE_Required_Validation_Strategy'); |
|
932 | + } |
|
933 | + $this->_required = $required; |
|
934 | + } |
|
935 | + |
|
936 | + |
|
937 | + |
|
938 | + /** |
|
939 | + * Returns whether or not this field is required |
|
940 | + * |
|
941 | + * @return boolean |
|
942 | + */ |
|
943 | + public function required() |
|
944 | + { |
|
945 | + return $this->_required; |
|
946 | + } |
|
947 | + |
|
948 | + |
|
949 | + |
|
950 | + /** |
|
951 | + * @param string $required_css_class |
|
952 | + */ |
|
953 | + public function set_required_css_class($required_css_class) |
|
954 | + { |
|
955 | + $this->_required_css_class = $required_css_class; |
|
956 | + } |
|
957 | + |
|
958 | + |
|
959 | + |
|
960 | + /** |
|
961 | + * @return string |
|
962 | + */ |
|
963 | + public function required_css_class() |
|
964 | + { |
|
965 | + return $this->_required_css_class; |
|
966 | + } |
|
967 | + |
|
968 | + |
|
969 | + |
|
970 | + /** |
|
971 | + * @param bool $add_required |
|
972 | + * @return string |
|
973 | + */ |
|
974 | + public function html_class($add_required = false) |
|
975 | + { |
|
976 | + return $add_required && $this->required() |
|
977 | + ? $this->required_css_class() . ' ' . $this->_html_class |
|
978 | + : $this->_html_class; |
|
979 | + } |
|
980 | + |
|
981 | + |
|
982 | + /** |
|
983 | + * Sets the help text, in case |
|
984 | + * |
|
985 | + * @param string $text |
|
986 | + */ |
|
987 | + public function set_html_help_text($text) |
|
988 | + { |
|
989 | + $this->_html_help_text = $text; |
|
990 | + } |
|
991 | + |
|
992 | + |
|
993 | + |
|
994 | + /** |
|
995 | + * Uses the sensitive data removal strategy to remove the sensitive data from this |
|
996 | + * input. If there is any kind of sensitive data removal on this input, we clear |
|
997 | + * out the raw value completely |
|
998 | + * |
|
999 | + * @return void |
|
1000 | + */ |
|
1001 | + public function clean_sensitive_data() |
|
1002 | + { |
|
1003 | + // if we do ANY kind of sensitive data removal on this, then just clear out the raw value |
|
1004 | + // if we need more logic than this we'll make a strategy for it |
|
1005 | + if ( |
|
1006 | + $this->_sensitive_data_removal_strategy |
|
1007 | + && ! $this->_sensitive_data_removal_strategy instanceof EE_No_Sensitive_Data_Removal |
|
1008 | + ) { |
|
1009 | + $this->_set_raw_value(null); |
|
1010 | + } |
|
1011 | + // and clean the normalized value according to the appropriate strategy |
|
1012 | + $this->_set_normalized_value( |
|
1013 | + $this->get_sensitive_data_removal_strategy()->remove_sensitive_data( |
|
1014 | + $this->_normalized_value |
|
1015 | + ) |
|
1016 | + ); |
|
1017 | + } |
|
1018 | + |
|
1019 | + |
|
1020 | + |
|
1021 | + /** |
|
1022 | + * @param bool $primary |
|
1023 | + * @param string $button_size |
|
1024 | + * @param string $other_attributes |
|
1025 | + */ |
|
1026 | + public function set_button_css_attributes($primary = true, $button_size = '', $other_attributes = '') |
|
1027 | + { |
|
1028 | + $button_css_attributes = 'button'; |
|
1029 | + $button_css_attributes .= $primary === true ? ' button-primary' : ' button-secondary'; |
|
1030 | + switch ($button_size) { |
|
1031 | + case 'xs': |
|
1032 | + case 'extra-small': |
|
1033 | + $button_css_attributes .= ' button-xs'; |
|
1034 | + break; |
|
1035 | + case 'sm': |
|
1036 | + case 'small': |
|
1037 | + $button_css_attributes .= ' button-sm'; |
|
1038 | + break; |
|
1039 | + case 'lg': |
|
1040 | + case 'large': |
|
1041 | + $button_css_attributes .= ' button-lg'; |
|
1042 | + break; |
|
1043 | + case 'block': |
|
1044 | + $button_css_attributes .= ' button-block'; |
|
1045 | + break; |
|
1046 | + case 'md': |
|
1047 | + case 'medium': |
|
1048 | + default: |
|
1049 | + $button_css_attributes .= ''; |
|
1050 | + } |
|
1051 | + $this->_button_css_attributes .= ! empty($other_attributes) |
|
1052 | + ? $button_css_attributes . ' ' . $other_attributes |
|
1053 | + : $button_css_attributes; |
|
1054 | + } |
|
1055 | + |
|
1056 | + |
|
1057 | + |
|
1058 | + /** |
|
1059 | + * @return string |
|
1060 | + */ |
|
1061 | + public function button_css_attributes() |
|
1062 | + { |
|
1063 | + if (empty($this->_button_css_attributes)) { |
|
1064 | + $this->set_button_css_attributes(); |
|
1065 | + } |
|
1066 | + return $this->_button_css_attributes; |
|
1067 | + } |
|
1068 | + |
|
1069 | + |
|
1070 | + |
|
1071 | + /** |
|
1072 | + * find_form_data_for_this_section |
|
1073 | + * using this section's name and its parents, finds the value of the form data that corresponds to it. |
|
1074 | + * For example, if this form section's HTML name is my_form[subform][form_input_1], |
|
1075 | + * then it's value should be in request at request['my_form']['subform']['form_input_1']. |
|
1076 | + * (If that doesn't exist, we also check for this subsection's name |
|
1077 | + * at the TOP LEVEL of the request data. Eg request['form_input_1'].) |
|
1078 | + * This function finds its value in the form. |
|
1079 | + * |
|
1080 | + * @param array $req_data |
|
1081 | + * @return mixed whatever the raw value of this form section is in the request data |
|
1082 | + * @throws EE_Error |
|
1083 | + */ |
|
1084 | + public function find_form_data_for_this_section($req_data) |
|
1085 | + { |
|
1086 | + $name_parts = $this->getInputNameParts(); |
|
1087 | + // now get the value for the input |
|
1088 | + $value = $this->findRequestForSectionUsingNameParts($name_parts, $req_data); |
|
1089 | + // check if this thing's name is at the TOP level of the request data |
|
1090 | + if ($value === null && isset($req_data[ $this->name() ])) { |
|
1091 | + $value = $req_data[ $this->name() ]; |
|
1092 | + } |
|
1093 | + return $value; |
|
1094 | + } |
|
1095 | + |
|
1096 | + |
|
1097 | + /** |
|
1098 | + * If this input's name is something like "foo[bar][baz]" |
|
1099 | + * returns an array like `array('foo','bar',baz')` |
|
1100 | + * |
|
1101 | + * @return array |
|
1102 | + * @throws EE_Error |
|
1103 | + */ |
|
1104 | + protected function getInputNameParts() |
|
1105 | + { |
|
1106 | + // break up the html name by "[]" |
|
1107 | + if (strpos($this->html_name(), '[') !== false) { |
|
1108 | + $before_any_brackets = substr($this->html_name(), 0, strpos($this->html_name(), '[')); |
|
1109 | + } else { |
|
1110 | + $before_any_brackets = $this->html_name(); |
|
1111 | + } |
|
1112 | + // grab all of the segments |
|
1113 | + preg_match_all('~\[([^]]*)\]~', $this->html_name(), $matches); |
|
1114 | + if (isset($matches[1]) && is_array($matches[1])) { |
|
1115 | + $name_parts = $matches[1]; |
|
1116 | + array_unshift($name_parts, $before_any_brackets); |
|
1117 | + } else { |
|
1118 | + $name_parts = array($before_any_brackets); |
|
1119 | + } |
|
1120 | + return $name_parts; |
|
1121 | + } |
|
1122 | + |
|
1123 | + |
|
1124 | + |
|
1125 | + /** |
|
1126 | + * @param array $html_name_parts |
|
1127 | + * @param array $req_data |
|
1128 | + * @return array | NULL |
|
1129 | + */ |
|
1130 | + public function findRequestForSectionUsingNameParts($html_name_parts, $req_data) |
|
1131 | + { |
|
1132 | + $first_part_to_consider = array_shift($html_name_parts); |
|
1133 | + if (isset($req_data[ $first_part_to_consider ])) { |
|
1134 | + if (empty($html_name_parts)) { |
|
1135 | + return $req_data[ $first_part_to_consider ]; |
|
1136 | + } else { |
|
1137 | + return $this->findRequestForSectionUsingNameParts( |
|
1138 | + $html_name_parts, |
|
1139 | + $req_data[ $first_part_to_consider ] |
|
1140 | + ); |
|
1141 | + } |
|
1142 | + } else { |
|
1143 | + return null; |
|
1144 | + } |
|
1145 | + } |
|
1146 | + |
|
1147 | + |
|
1148 | + |
|
1149 | + /** |
|
1150 | + * Checks if this form input's data is in the request data |
|
1151 | + * |
|
1152 | + * @param array $req_data |
|
1153 | + * @return boolean |
|
1154 | + * @throws EE_Error |
|
1155 | + */ |
|
1156 | + public function form_data_present_in($req_data = null) |
|
1157 | + { |
|
1158 | + if ($req_data === null) { |
|
1159 | + /** @var RequestInterface $request */ |
|
1160 | + $request = LoaderFactory::getLoader()->getShared(RequestInterface::class); |
|
1161 | + $req_data = $request->postParams(); |
|
1162 | + } |
|
1163 | + $checked_value = $this->find_form_data_for_this_section($req_data); |
|
1164 | + if ($checked_value !== null) { |
|
1165 | + return true; |
|
1166 | + } else { |
|
1167 | + return false; |
|
1168 | + } |
|
1169 | + } |
|
1170 | + |
|
1171 | + |
|
1172 | + |
|
1173 | + /** |
|
1174 | + * Overrides parent to add js data from validation and display strategies |
|
1175 | + * |
|
1176 | + * @param array $form_other_js_data |
|
1177 | + * @return array |
|
1178 | + */ |
|
1179 | + public function get_other_js_data($form_other_js_data = array()) |
|
1180 | + { |
|
1181 | + return $this->get_other_js_data_from_strategies($form_other_js_data); |
|
1182 | + } |
|
1183 | + |
|
1184 | + |
|
1185 | + |
|
1186 | + /** |
|
1187 | + * Gets other JS data for localization from this input's strategies, like |
|
1188 | + * the validation strategies and the display strategy |
|
1189 | + * |
|
1190 | + * @param array $form_other_js_data |
|
1191 | + * @return array |
|
1192 | + */ |
|
1193 | + public function get_other_js_data_from_strategies($form_other_js_data = array()) |
|
1194 | + { |
|
1195 | + $form_other_js_data = $this->get_display_strategy()->get_other_js_data($form_other_js_data); |
|
1196 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1197 | + $form_other_js_data = $validation_strategy->get_other_js_data($form_other_js_data); |
|
1198 | + } |
|
1199 | + return $form_other_js_data; |
|
1200 | + } |
|
1201 | + |
|
1202 | + |
|
1203 | + |
|
1204 | + /** |
|
1205 | + * Override parent because we want to give our strategies an opportunity to enqueue some js and css |
|
1206 | + * |
|
1207 | + * @return void |
|
1208 | + */ |
|
1209 | + public function enqueue_js() |
|
1210 | + { |
|
1211 | + // ask our display strategy and validation strategies if they have js to enqueue |
|
1212 | + $this->enqueue_js_from_strategies(); |
|
1213 | + } |
|
1214 | + |
|
1215 | + |
|
1216 | + |
|
1217 | + /** |
|
1218 | + * Tells strategies when its ok to enqueue their js and css |
|
1219 | + * |
|
1220 | + * @return void |
|
1221 | + */ |
|
1222 | + public function enqueue_js_from_strategies() |
|
1223 | + { |
|
1224 | + $this->get_display_strategy()->enqueue_js(); |
|
1225 | + foreach ($this->get_validation_strategies() as $validation_strategy) { |
|
1226 | + $validation_strategy->enqueue_js(); |
|
1227 | + } |
|
1228 | + } |
|
1229 | + |
|
1230 | + |
|
1231 | + |
|
1232 | + /** |
|
1233 | + * Gets the default value set on the input (not the current value, which may have been |
|
1234 | + * changed because of a form submission). If no default was set, this us null. |
|
1235 | + * @return mixed |
|
1236 | + */ |
|
1237 | + public function get_default() |
|
1238 | + { |
|
1239 | + return $this->_default; |
|
1240 | + } |
|
1241 | + |
|
1242 | + |
|
1243 | + |
|
1244 | + /** |
|
1245 | + * Makes this input disabled. That means it will have the HTML attribute 'disabled="disabled"', |
|
1246 | + * and server-side if any input was received it will be ignored |
|
1247 | + */ |
|
1248 | + public function disable($disable = true) |
|
1249 | + { |
|
1250 | + $disabled_attribute = ' disabled="disabled"'; |
|
1251 | + $this->disabled = filter_var($disable, FILTER_VALIDATE_BOOLEAN); |
|
1252 | + if ($this->disabled) { |
|
1253 | + if (strpos($this->_other_html_attributes, $disabled_attribute) === false) { |
|
1254 | + $this->_other_html_attributes .= $disabled_attribute; |
|
1255 | + } |
|
1256 | + $this->_set_normalized_value($this->get_default()); |
|
1257 | + } else { |
|
1258 | + $this->_other_html_attributes = str_replace($disabled_attribute, '', $this->_other_html_attributes); |
|
1259 | + } |
|
1260 | + } |
|
1261 | + |
|
1262 | + |
|
1263 | + |
|
1264 | + /** |
|
1265 | + * Returns whether or not this input is currently disabled. |
|
1266 | + * @return bool |
|
1267 | + */ |
|
1268 | + public function isDisabled() |
|
1269 | + { |
|
1270 | + return $this->disabled; |
|
1271 | + } |
|
1272 | 1272 | } |