PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 3.06.06
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v3.06.06
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 3.06.06, at classes/models/FrmFieldValueSelector.php

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