| 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 |
* Field dropdowns will truncate at 25 characters by default. |
| 79 |
* |
| 80 |
* @var int|null |
| 81 |
* |
| 82 |
* @since 6.16.2 |
| 83 |
*/ |
| 84 |
protected $truncate; |
| 85 |
|
| 86 |
/** |
| 87 |
* FrmFieldValueSelector constructor |
| 88 |
* |
| 89 |
* @param int|string $field_id |
| 90 |
*/ |
| 91 |
public function __construct( $field_id, $args ) { |
| 92 |
$this->set_html_name( $args ); |
| 93 |
$this->set_value( $args ); |
| 94 |
$this->set_source( $args ); |
| 95 |
$this->set_truncate( $args ); |
| 96 |
|
| 97 |
$this->field_id = (int) $field_id; |
| 98 |
if ( $this->field_id === 0 ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
$this->set_db_row(); |
| 103 |
|
| 104 |
if ( $this->has_db_row() ) { |
| 105 |
$this->set_field_key(); |
| 106 |
$this->set_field_settings(); |
| 107 |
$this->set_options(); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Set the db_row property |
| 113 |
* |
| 114 |
* @since 2.03.05 |
| 115 |
* |
| 116 |
* @return void |
| 117 |
*/ |
| 118 |
private function set_db_row() { |
| 119 |
$where = array( |
| 120 |
'id' => $this->field_id, |
| 121 |
); |
| 122 |
|
| 123 |
$this->db_row = FrmDb::get_row( 'frm_fields', $where ); |
| 124 |
|
| 125 |
if ( ! is_object( $this->db_row ) ) { |
| 126 |
$this->db_row = null; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Set the field_key property |
| 132 |
* |
| 133 |
* @since 2.03.05 |
| 134 |
* |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
private function set_field_key() { |
| 138 |
$this->field_key = $this->db_row->field_key; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Set the field_settings property |
| 143 |
* |
| 144 |
* @since 2.03.05 |
| 145 |
* |
| 146 |
* @return void |
| 147 |
*/ |
| 148 |
protected function set_field_settings() { |
| 149 |
// Leave as null for free version |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Set the options property |
| 154 |
* |
| 155 |
* @since 2.03.05 |
| 156 |
* |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
protected function set_options() { |
| 160 |
$field_obj = FrmFieldFactory::get_field_object( $this->db_row ); |
| 161 |
$this->options = $field_obj->get_options( array() ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Set the html_name property |
| 166 |
* |
| 167 |
* @since 2.03.05 |
| 168 |
* |
| 169 |
* @param array $args |
| 170 |
* |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
protected function set_html_name( $args ) { |
| 174 |
if ( isset( $args['html_name'] ) ) { |
| 175 |
$this->html_name = (string) $args['html_name']; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Set the selected_value property |
| 181 |
* |
| 182 |
* @since 2.03.05 |
| 183 |
* |
| 184 |
* @param array $args |
| 185 |
* |
| 186 |
* @return void |
| 187 |
*/ |
| 188 |
protected function set_value( $args ) { |
| 189 |
if ( isset( $args['value'] ) ) { |
| 190 |
$this->value = (string) $args['value']; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Set the source property |
| 196 |
* |
| 197 |
* @since 2.03.05 |
| 198 |
* |
| 199 |
* @param array $args |
| 200 |
* |
| 201 |
* @return void |
| 202 |
*/ |
| 203 |
protected function set_source( $args ) { |
| 204 |
if ( isset( $args['source'] ) ) { |
| 205 |
$this->source = (string) $args['source']; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* @since 6.16.2 |
| 211 |
* |
| 212 |
* @param array $args |
| 213 |
* |
| 214 |
* @return void |
| 215 |
*/ |
| 216 |
protected function set_truncate( $args ) { |
| 217 |
if ( isset( $args['truncate'] ) && is_numeric( $args['truncate'] ) ) { |
| 218 |
$this->truncate = (int) $args['truncate']; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Check if object has any options |
| 224 |
* |
| 225 |
* @since 2.03.05 |
| 226 |
* |
| 227 |
* @return bool |
| 228 |
*/ |
| 229 |
final protected function has_options() { |
| 230 |
return ! empty( $this->options ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Check if a field is connected to the value selector |
| 235 |
* |
| 236 |
* @since 2.03.05 |
| 237 |
* |
| 238 |
* @return bool |
| 239 |
*/ |
| 240 |
final protected function has_db_row() { |
| 241 |
return $this->db_row !== null; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Display the field value selector (dropdown or text field) |
| 246 |
* |
| 247 |
* @since 2.03.05 |
| 248 |
* |
| 249 |
* @return void |
| 250 |
*/ |
| 251 |
public function display() { |
| 252 |
if ( $this->has_options() ) { |
| 253 |
$this->display_dropdown(); |
| 254 |
} else { |
| 255 |
$this->display_text_box(); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Print the field value text box |
| 261 |
* |
| 262 |
* @since 2.03.05 |
| 263 |
* |
| 264 |
* @return void |
| 265 |
*/ |
| 266 |
public function display_text_box() { |
| 267 |
echo '<input type="text" name="' . esc_attr( $this->html_name ) . '" value="' . esc_attr( trim( $this->value ) ) . '" />'; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Display the field value selector |
| 272 |
* |
| 273 |
* @since 2.03.05 |
| 274 |
* |
| 275 |
* @return void |
| 276 |
*/ |
| 277 |
protected function display_dropdown() { |
| 278 |
echo '<select name="' . esc_attr( $this->html_name ) . '">'; |
| 279 |
echo '<option value="">' . esc_html( $this->blank_option_label ) . '</option>'; |
| 280 |
|
| 281 |
if ( ! empty( $this->options ) ) { |
| 282 |
$truncate = isset( $this->truncate ) ? $this->truncate : 25; |
| 283 |
|
| 284 |
foreach ( $this->options as $key => $value ) { |
| 285 |
if ( $value == '' ) { |
| 286 |
continue; |
| 287 |
} |
| 288 |
|
| 289 |
$option = $this->get_single_field_option( $key, $value ); |
| 290 |
$option->print_single_option( $this->value, $truncate ); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
echo '</select>'; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Get an instance of FrmFieldOption |
| 299 |
* |
| 300 |
* @since 2.03.05 |
| 301 |
* |
| 302 |
* @param string $key |
| 303 |
* @param string $value |
| 304 |
* |
| 305 |
* @return FrmFieldOption |
| 306 |
*/ |
| 307 |
protected function get_single_field_option( $key, $value ) { |
| 308 |
return new FrmFieldOption( $key, $value ); |
| 309 |
} |
| 310 |
} |
| 311 |
|