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

266 lines 4.5 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 null
30 *
31 * @since 2.03.05
32 */
33 protected $field_settings = null;
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
72 *
73 * @since 2.03.05
74 */
75 protected $db_row = null;
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 private function set_db_row() {
107 $where = array(
108 'id' => $this->field_id,
109 );
110
111 $this->db_row = FrmDb::get_row( 'frm_fields', $where );
112
113 if ( ! is_object( $this->db_row ) ) {
114 $this->db_row = null;
115 }
116 }
117
118 /**
119 * Set the field_key property
120 *
121 * @since 2.03.05
122 */
123 private function set_field_key() {
124 $this->field_key = $this->db_row->field_key;
125 }
126
127 /**
128 * Set the field_settings property
129 *
130 * @since 2.03.05
131 */
132 protected function set_field_settings() {
133 // Leave as null for free version
134 }
135
136 /**
137 * Set the options property
138 *
139 * @since 2.03.05
140 */
141 protected function set_options() {
142 $field_obj = FrmFieldFactory::get_field_object( $this->db_row );
143 $this->options = $field_obj->get_options( array() );
144 }
145
146 /**
147 * Set the html_name property
148 *
149 * @since 2.03.05
150 *
151 * @param array $args
152 */
153 protected function set_html_name( $args ) {
154 if ( isset( $args['html_name'] ) ) {
155 $this->html_name = (string) $args['html_name'];
156 }
157 }
158
159 /**
160 * Set the selected_value property
161 *
162 * @since 2.03.05
163 *
164 * @param array $args
165 */
166 protected function set_value( $args ) {
167 if ( isset( $args['value'] ) ) {
168 $this->value = (string) $args['value'];
169 }
170 }
171
172 /**
173 * Set the source property
174 *
175 * @since 2.03.05
176 *
177 * @param array $args
178 */
179 protected function set_source( $args ) {
180 if ( isset( $args['source'] ) ) {
181 $this->source = (string) $args['source'];
182 }
183 }
184
185 /**
186 * Check if object has any options
187 *
188 * @since 2.03.05
189 *
190 * @return bool
191 */
192 final protected function has_options() {
193 return ! empty( $this->options );
194 }
195
196 /**
197 * Check if a field is connected to the value selector
198 *
199 * @since 2.03.05
200 *
201 * @return bool
202 */
203 final protected function has_db_row() {
204 return $this->db_row !== null;
205 }
206
207 /**
208 * Display the field value selector (dropdown or text field)
209 *
210 * @since 2.03.05
211 */
212 public function display() {
213 if ( $this->has_options() ) {
214 $this->display_dropdown();
215 } else {
216 $this->display_text_box();
217 }
218 }
219
220 /**
221 * Print the field value text box
222 *
223 * @since 2.03.05
224 */
225 public function display_text_box() {
226 echo '<input type="text" name="' . esc_attr( $this->html_name ) . '" value="' . esc_attr( trim( $this->value ) ) . '" />';
227 }
228
229 /**
230 * Display the field value selector
231 *
232 * @since 2.03.05
233 */
234 protected function display_dropdown() {
235 echo '<select name="' . esc_attr( $this->html_name ) . '">';
236 echo '<option value="">' . esc_attr( $this->blank_option_label ) . '</option>';
237
238 if ( ! empty( $this->options ) ) {
239 foreach ( $this->options as $key => $value ) {
240 if ( $value == '' ) {
241 continue;
242 }
243
244 $option = $this->get_single_field_option( $key, $value );
245 $option->print_single_option( $this->value, 25 );
246 }
247 }
248
249 echo '</select>';
250 }
251
252 /**
253 * Get an instance of FrmFieldOption
254 *
255 * @since 2.03.05
256 *
257 * @param string $key
258 * @param string $value
259 *
260 * @return FrmFieldOption
261 */
262 protected function get_single_field_option( $key, $value ) {
263 return new FrmFieldOption( $key, $value );
264 }
265 }
266