PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.12
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.12
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmFieldValueSelector.php

FrmFieldValueSelector.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.12, at classes/models/FrmFieldValueSelector.php

286 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 /**
7 * A class for the field value selector
8 * Used in field conditional logic, action conditional logic, Mailchimp action, etc.
9 *
10 * @since 2.03.05
11 */
12 class FrmFieldValueSelector {
13
14 /**
15 * @var int
16 *
17 * @since 2.03.05
18 */
19 protected $field_id = 0;
20
21 /**
22 * @var string
23 *
24 * @since 2.03.05
25 */
26 protected $field_key = '';
27
28 /**
29 * @var FrmProFieldSettings|null
30 *
31 * @since 2.03.05
32 */
33 protected $field_settings;
34
35 /**
36 * @var array
37 *
38 * @since 2.03.05
39 */
40 protected $options = array();
41
42 /**
43 * @var string
44 *
45 * @since 2.03.05
46 */
47 protected $html_name = '';
48
49 /**
50 * @var string
51 *
52 * @since 2.03.05
53 */
54 protected $value = '';
55
56 /**
57 * @var string
58 *
59 * @since 2.03.05
60 */
61 protected $source = 'unknown';
62
63 /**
64 * @var string
65 *
66 * @since 2.03.05
67 */
68 protected $blank_option_label = '';
69
70 /**
71 * @var object|null
72 *
73 * @since 2.03.05
74 */
75 protected $db_row;
76
77 /**
78 * FrmFieldValueSelector constructor
79 *
80 * @param int|string $field_id
81 */
82 public function __construct( $field_id, $args ) {
83 $this->set_html_name( $args );
84 $this->set_value( $args );
85 $this->set_source( $args );
86
87 $this->field_id = (int) $field_id;
88 if ( $this->field_id === 0 ) {
89 return;
90 }
91
92 $this->set_db_row();
93
94 if ( $this->has_db_row() ) {
95 $this->set_field_key();
96 $this->set_field_settings();
97 $this->set_options();
98 }
99 }
100
101 /**
102 * Set the db_row property
103 *
104 * @since 2.03.05
105 *
106 * @return void
107 */
108 private function set_db_row() {
109 $where = array(
110 'id' => $this->field_id,
111 );
112
113 $this->db_row = FrmDb::get_row( 'frm_fields', $where );
114
115 if ( ! is_object( $this->db_row ) ) {
116 $this->db_row = null;
117 }
118 }
119
120 /**
121 * Set the field_key property
122 *
123 * @since 2.03.05
124 *
125 * @return void
126 */
127 private function set_field_key() {
128 $this->field_key = $this->db_row->field_key;
129 }
130
131 /**
132 * Set the field_settings property
133 *
134 * @since 2.03.05
135 *
136 * @return void
137 */
138 protected function set_field_settings() {
139 // Leave as null for free version
140 }
141
142 /**
143 * Set the options property
144 *
145 * @since 2.03.05
146 *
147 * @return void
148 */
149 protected function set_options() {
150 $field_obj = FrmFieldFactory::get_field_object( $this->db_row );
151 $this->options = $field_obj->get_options( array() );
152 }
153
154 /**
155 * Set the html_name property
156 *
157 * @since 2.03.05
158 *
159 * @param array $args
160 *
161 * @return void
162 */
163 protected function set_html_name( $args ) {
164 if ( isset( $args['html_name'] ) ) {
165 $this->html_name = (string) $args['html_name'];
166 }
167 }
168
169 /**
170 * Set the selected_value property
171 *
172 * @since 2.03.05
173 *
174 * @param array $args
175 *
176 * @return void
177 */
178 protected function set_value( $args ) {
179 if ( isset( $args['value'] ) ) {
180 $this->value = (string) $args['value'];
181 }
182 }
183
184 /**
185 * Set the source property
186 *
187 * @since 2.03.05
188 *
189 * @param array $args
190 *
191 * @return void
192 */
193 protected function set_source( $args ) {
194 if ( isset( $args['source'] ) ) {
195 $this->source = (string) $args['source'];
196 }
197 }
198
199 /**
200 * Check if object has any options
201 *
202 * @since 2.03.05
203 *
204 * @return bool
205 */
206 final protected function has_options() {
207 return ! empty( $this->options );
208 }
209
210 /**
211 * Check if a field is connected to the value selector
212 *
213 * @since 2.03.05
214 *
215 * @return bool
216 */
217 final protected function has_db_row() {
218 return $this->db_row !== null;
219 }
220
221 /**
222 * Display the field value selector (dropdown or text field)
223 *
224 * @since 2.03.05
225 *
226 * @return void
227 */
228 public function display() {
229 if ( $this->has_options() ) {
230 $this->display_dropdown();
231 } else {
232 $this->display_text_box();
233 }
234 }
235
236 /**
237 * Print the field value text box
238 *
239 * @since 2.03.05
240 *
241 * @return void
242 */
243 public function display_text_box() {
244 echo '<input type="text" name="' . esc_attr( $this->html_name ) . '" value="' . esc_attr( trim( $this->value ) ) . '" />';
245 }
246
247 /**
248 * Display the field value selector
249 *
250 * @since 2.03.05
251 *
252 * @return void
253 */
254 protected function display_dropdown() {
255 echo '<select name="' . esc_attr( $this->html_name ) . '">';
256 echo '<option value="">' . esc_html( $this->blank_option_label ) . '</option>';
257
258 if ( ! empty( $this->options ) ) {
259 foreach ( $this->options as $key => $value ) {
260 if ( $value == '' ) {
261 continue;
262 }
263
264 $option = $this->get_single_field_option( $key, $value );
265 $option->print_single_option( $this->value, 25 );
266 }
267 }
268
269 echo '</select>';
270 }
271
272 /**
273 * Get an instance of FrmFieldOption
274 *
275 * @since 2.03.05
276 *
277 * @param string $key
278 * @param string $value
279 *
280 * @return FrmFieldOption
281 */
282 protected function get_single_field_option( $key, $value ) {
283 return new FrmFieldOption( $key, $value );
284 }
285 }
286