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

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