| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Single Form Entry Class |
| 5 |
* |
| 6 |
* @since 1.1.0 |
| 7 |
*/ |
| 8 |
class WeForms_Form_Entry { |
| 9 |
|
| 10 |
/** |
| 11 |
* The form object |
| 12 |
* |
| 13 |
* @var \WeForms_Form |
| 14 |
*/ |
| 15 |
private $form; |
| 16 |
|
| 17 |
/** |
| 18 |
* Entry id |
| 19 |
* |
| 20 |
* @var int |
| 21 |
*/ |
| 22 |
public $id = 0; |
| 23 |
|
| 24 |
/** |
| 25 |
* The form id |
| 26 |
* |
| 27 |
* @var int |
| 28 |
*/ |
| 29 |
public $form_id = 0; |
| 30 |
|
| 31 |
/** |
| 32 |
* The user id |
| 33 |
* |
| 34 |
* @var int |
| 35 |
*/ |
| 36 |
public $user_id = 0; |
| 37 |
|
| 38 |
/** |
| 39 |
* IP Address |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
public $ip_address = '127.0.0.1'; |
| 44 |
|
| 45 |
/** |
| 46 |
* The device of the user |
| 47 |
* |
| 48 |
* @var string |
| 49 |
*/ |
| 50 |
public $device = ''; |
| 51 |
|
| 52 |
/** |
| 53 |
* Referer URL |
| 54 |
* |
| 55 |
* @var string |
| 56 |
*/ |
| 57 |
public $referer = ''; |
| 58 |
|
| 59 |
/** |
| 60 |
* Entry creation date |
| 61 |
* |
| 62 |
* @var string |
| 63 |
*/ |
| 64 |
public $created = '0000-00-00 00:00:00'; |
| 65 |
|
| 66 |
/** |
| 67 |
* Form fields |
| 68 |
* |
| 69 |
* @var array |
| 70 |
*/ |
| 71 |
public $fields = []; |
| 72 |
|
| 73 |
/** |
| 74 |
* Form fields raw data |
| 75 |
* |
| 76 |
* @var array |
| 77 |
*/ |
| 78 |
public $raw_fields = []; |
| 79 |
|
| 80 |
/** |
| 81 |
* The constructor |
| 82 |
* |
| 83 |
* @param int $entry_id |
| 84 |
* @param \WeForms_Form $form |
| 85 |
*/ |
| 86 |
public function __construct( $entry_id, $form ) { |
| 87 |
$this->id = $entry_id; |
| 88 |
$this->form = $form; |
| 89 |
|
| 90 |
$this->populate_entry_data(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Populate the class with data |
| 95 |
* |
| 96 |
* @TODO: Abstract this |
| 97 |
* |
| 98 |
* @return void |
| 99 |
*/ |
| 100 |
public function populate_entry_data() { |
| 101 |
global $wpdb; |
| 102 |
|
| 103 |
// return if we populated the already, ensures single db call |
| 104 |
if ( $this->form_id ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
$grid_css_added = false; |
| 109 |
$grid_css = '<style>.wpufTable {display: table; width: 100%; } .wpufTableRow {display: table-row; } .wpufTableRow:nth-child(even) {background-color: #f5f5f5; } .wpufTableHeading {background-color: #eee; display: table-header-group; font-weight: bold; } .wpufTableCell, .wpufTableHead {border: none; display: table-cell; padding: 3px 10px; } .wpufTableFoot {background-color: #eee; display: table-footer-group; font-weight: bold; } .wpufTableBody {display: table-row-group; }</style>'; |
| 110 |
|
| 111 |
$values = []; |
| 112 |
|
| 113 |
$query = "SELECT * FROM {$wpdb->weforms_entries} as entry |
| 114 |
LEFT JOIN {$wpdb->weforms_entrymeta} AS meta ON entry.id = meta.weforms_entry_id |
| 115 |
WHERE entry.id = {$this->id}"; |
| 116 |
|
| 117 |
$results = $wpdb->get_results( $query ); |
| 118 |
|
| 119 |
if ( $results ) { |
| 120 |
$first_row = reset( $results ); |
| 121 |
|
| 122 |
$this->form_id = (int) $first_row->form_id; |
| 123 |
$this->user_id = (int) $first_row->user_id; |
| 124 |
$this->ip_address = long2ip( $first_row->user_ip ); |
| 125 |
$this->device = $first_row->user_device; |
| 126 |
$this->referer = $first_row->referer; |
| 127 |
$this->created = $first_row->created_at; |
| 128 |
|
| 129 |
$this->fields = $this->form->get_field_values(); |
| 130 |
$this->raw_fields = $this->fields; |
| 131 |
|
| 132 |
foreach ( $results as $result ) { |
| 133 |
if ( array_key_exists( $result->meta_key, $this->fields ) ) { |
| 134 |
$field = $this->fields[ $result->meta_key ]; |
| 135 |
$value = $result->meta_value; |
| 136 |
|
| 137 |
$this->raw_fields[ $result->meta_key ]['value'] = $value; |
| 138 |
|
| 139 |
if ( $field['type'] == 'textarea_field' ) { |
| 140 |
$value = weforms_format_text( $value ); |
| 141 |
} elseif ( $field['type'] == 'name_field' ) { |
| 142 |
$value = implode( ' ', explode( WeForms::$field_separator, $value ) ); |
| 143 |
} elseif ( in_array( $field['type'], [ 'dropdown_field', 'radio_field' ] ) ) { |
| 144 |
if ( isset( $field['options'] ) && $field['options'] ) { |
| 145 |
if ( isset( $field['options'][ $value ] ) ) { |
| 146 |
$value = $field['options'][ $value ]; |
| 147 |
} |
| 148 |
} |
| 149 |
} elseif ( in_array( $field['type'], [ 'multiple_select', 'checkbox_field' ] ) ) { |
| 150 |
$value = explode( WeForms::$field_separator, $value ); |
| 151 |
$temp_value = $value; |
| 152 |
|
| 153 |
if ( is_array( $value ) && $value ) { |
| 154 |
$new_array = []; |
| 155 |
|
| 156 |
foreach ( $value as $option_key ) { |
| 157 |
if ( is_array( $field['options'] ) && array_key_exists( $option_key, $field['options'] ) ) { |
| 158 |
$new_array[] = $field['options'][ $option_key ]; |
| 159 |
} else { |
| 160 |
$new_array[] = $option_key; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
$value = $new_array; |
| 165 |
} |
| 166 |
} elseif ( in_array( $field['type'], [ 'image_upload', 'file_upload' ] ) ) { |
| 167 |
$file_field = ''; |
| 168 |
$value = maybe_unserialize( $value ); |
| 169 |
|
| 170 |
if ( is_array( $value ) && $value ) { |
| 171 |
foreach ( $value as $attachment_id ) { |
| 172 |
if ( $field['type'] == 'image_upload' ) { |
| 173 |
$thumb = wp_get_attachment_image( $attachment_id, 'thumbnail' ); |
| 174 |
} else { |
| 175 |
$thumb = get_post_field( 'post_title', $attachment_id ); |
| 176 |
} |
| 177 |
|
| 178 |
$full_size = wp_get_attachment_url( $attachment_id ); |
| 179 |
|
| 180 |
$file_field .= sprintf( '<a href="%s" target="_blank">%s</a> ', $full_size, $thumb ); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
$value = $file_field; |
| 185 |
} elseif ( $field['type'] == 'google_map' ) { |
| 186 |
list( $address, $lat, $long ) = explode( '||', $value ); |
| 187 |
|
| 188 |
$value = [ |
| 189 |
'address' => $address, |
| 190 |
'lat' => trim( $lat ), |
| 191 |
'long' => trim( $long ), |
| 192 |
]; |
| 193 |
} elseif ( $field['type'] == 'multiple_product' ) { |
| 194 |
$field_value = unserialize( $value ); |
| 195 |
|
| 196 |
$serialized_value = []; |
| 197 |
|
| 198 |
if ( is_array( $field_value ) ) { |
| 199 |
foreach ( $field_value as $key => $sfv ) { |
| 200 |
if ( is_array( $sfv ) ) { |
| 201 |
$v = []; |
| 202 |
|
| 203 |
foreach ( $sfv as $key => $sv ) { |
| 204 |
$sv = str_replace( [ '_', '-' ], ' ', $key ) . ': ' . $sv; |
| 205 |
$sv = ucwords( $sv ); |
| 206 |
$v[] = $sv; |
| 207 |
} |
| 208 |
|
| 209 |
$serialized_value[] = implode( '<br> ', $v ); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
$value = implode( '<br> <br> ', $serialized_value ); |
| 214 |
} |
| 215 |
} elseif ( $field['type'] == 'checkbox_grid' ) { |
| 216 |
$entry_value = unserialize( $value ); |
| 217 |
|
| 218 |
if ( $entry_value ) { |
| 219 |
$return = ''; |
| 220 |
$check = ''; |
| 221 |
|
| 222 |
if ( !$grid_css_added ) { |
| 223 |
$return = $grid_css; |
| 224 |
$grid_css_added = true; |
| 225 |
} |
| 226 |
|
| 227 |
$new_val = []; |
| 228 |
|
| 229 |
foreach ( $entry_value as $key => $option_value ) { |
| 230 |
$new_val[ $key ] = $option_value; |
| 231 |
} |
| 232 |
|
| 233 |
if ( $field['grid_rows'] && count( $field['grid_rows'] ) > 0 && $field['grid_columns'] && count( $field['grid_columns'] ) > 0 ) { |
| 234 |
$return .= '<div class="wpufTable"> |
| 235 |
<div class="wpufTableHeading"> |
| 236 |
<div class="wpufTableRow"> |
| 237 |
<div class="wpufTableHead"> </div>'; |
| 238 |
|
| 239 |
foreach ( $field['grid_columns'] as $column ) { |
| 240 |
$return .= '<div class="wpufTableHead">' . $column . '</div>'; |
| 241 |
} |
| 242 |
|
| 243 |
$return .= '</div> |
| 244 |
</div> |
| 245 |
<div class="wpufTableBody">'; |
| 246 |
|
| 247 |
foreach ( $field['grid_rows'] as $row_key => $row_value ) { |
| 248 |
$return .= '<div class="wpufTableRow"> |
| 249 |
<div class="wpufTableHead">' . $row_value . '</div>'; |
| 250 |
|
| 251 |
foreach ( $field['grid_columns'] as $column_key => $column_value ) { |
| 252 |
if ( isset( $new_val[ $row_key ] ) ) { |
| 253 |
$check = ( in_array( $column_value, $new_val[ $row_key ] ) ) ? 'checked ' : ''; |
| 254 |
} |
| 255 |
|
| 256 |
$return .= '<div class="wpufTableCell"> |
| 257 |
<label class="wpuf-radio-inline"> |
| 258 |
<input |
| 259 |
name="' . $field['name'] . '[' . $row_key . '][]" |
| 260 |
class="wpuf_' . $field['name'] . '_' . $this->form_id . '" |
| 261 |
type="checkbox" |
| 262 |
value="' . esc_attr( $column_value ) . '"' |
| 263 |
. $check . 'disabled |
| 264 |
/> |
| 265 |
</label> |
| 266 |
</div>'; |
| 267 |
} |
| 268 |
|
| 269 |
$return .= '</div>'; |
| 270 |
} |
| 271 |
|
| 272 |
$return .= '</div> |
| 273 |
</div>'; |
| 274 |
} |
| 275 |
|
| 276 |
$value = $return; |
| 277 |
} |
| 278 |
} elseif ( $field['type'] == 'multiple_choice_grid' ) { |
| 279 |
$entry_value = unserialize( $value ); |
| 280 |
|
| 281 |
if ( $entry_value ) { |
| 282 |
$return = ''; |
| 283 |
$check = ''; |
| 284 |
|
| 285 |
if ( !$grid_css_added ) { |
| 286 |
$return = $grid_css; |
| 287 |
$grid_css_added = true; |
| 288 |
} |
| 289 |
|
| 290 |
$new_val = []; |
| 291 |
|
| 292 |
foreach ( $entry_value as $key => $option_value ) { |
| 293 |
$new_val[ $key ] = $option_value; |
| 294 |
} |
| 295 |
|
| 296 |
if ( $field['grid_rows'] && count( $field['grid_rows'] ) > 0 && $field['grid_columns'] && count( $field['grid_columns'] ) > 0 ) { |
| 297 |
$return .= '<div class="wpufTable"> |
| 298 |
<div class="wpufTableHeading"> |
| 299 |
<div class="wpufTableRow"> |
| 300 |
<div class="wpufTableHead"> </div>'; |
| 301 |
|
| 302 |
foreach ( $field['grid_columns'] as $column ) { |
| 303 |
$return .= '<div class="wpufTableHead">' . $column . '</div>'; |
| 304 |
} |
| 305 |
|
| 306 |
$return .= '</div> |
| 307 |
</div> |
| 308 |
<div class="wpufTableBody">'; |
| 309 |
|
| 310 |
foreach ( $field['grid_rows'] as $row_key => $row_value ) { |
| 311 |
$return .= '<div class="wpufTableRow"> |
| 312 |
<div class="wpufTableHead">' . $row_value . '</div>'; |
| 313 |
|
| 314 |
foreach ( $field['grid_columns'] as $column_key => $column_value ) { |
| 315 |
if ( isset( $new_val[ $row_key ] ) ) { |
| 316 |
$check = ( $new_val[ $row_key ] == $column_value ) ? 'checked ' : ''; |
| 317 |
} |
| 318 |
|
| 319 |
$return .= '<div class="wpufTableCell"> |
| 320 |
<label class="wpuf-radio-inline"> |
| 321 |
<input |
| 322 |
name="' . $field['name'] . '[' . $row_key . ']" |
| 323 |
class="wpuf_' . $field['name'] . '_' . $this->form_id . '" |
| 324 |
type="radio" |
| 325 |
value="' . esc_attr( $column_value ) . '"' |
| 326 |
. $check . 'disabled |
| 327 |
/> |
| 328 |
</label> |
| 329 |
</div>'; |
| 330 |
} |
| 331 |
|
| 332 |
$return .= '</div>'; |
| 333 |
} |
| 334 |
|
| 335 |
$return .= '</div> |
| 336 |
</div>'; |
| 337 |
} |
| 338 |
|
| 339 |
$value = $return; |
| 340 |
} |
| 341 |
} elseif ( $field['type'] == 'address_field' || is_serialized( $value ) ) { |
| 342 |
$field_value = unserialize( $value ); |
| 343 |
|
| 344 |
$serialized_value = []; |
| 345 |
|
| 346 |
if ( is_array( $field_value ) ) { |
| 347 |
foreach ( $field_value as $key => $sfv ) { |
| 348 |
$sfv = str_replace( [ '_', '-' ], ' ', $key ) . ': ' . $sfv; |
| 349 |
$sfv = ucwords( $sfv ); |
| 350 |
$serialized_value[] = $sfv; |
| 351 |
} |
| 352 |
|
| 353 |
$value = implode( '<br> ', $serialized_value ); |
| 354 |
} |
| 355 |
} elseif ( $field['type'] == 'signature_field' ) { |
| 356 |
$url = content_url() . $value; |
| 357 |
$value = $url; |
| 358 |
|
| 359 |
if ( isset( $_REQUEST['action'] ) != 'weforms_pdf_download' ) { |
| 360 |
$value = sprintf( '<img src="%s">', $url ); |
| 361 |
$value .= sprintf( '<a style="margin-left: -200px" href="%s">Download</a>', $url ); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
$this->fields[ $result->meta_key ]['value'] = apply_filters( 'weforms_entry_meta_field', $value, $field ); |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Get entry fields |
| 373 |
* |
| 374 |
* @return array |
| 375 |
*/ |
| 376 |
public function get_fields() { |
| 377 |
return $this->fields; |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Get entry fields |
| 382 |
* |
| 383 |
* @return array |
| 384 |
*/ |
| 385 |
public function get_raw_fields() { |
| 386 |
return $this->raw_fields; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Get entry metadata |
| 391 |
* |
| 392 |
* @return array |
| 393 |
*/ |
| 394 |
public function get_metadata() { |
| 395 |
return [ |
| 396 |
'id' => $this->id, |
| 397 |
'form_id' => $this->form_id, |
| 398 |
'form_title' => $this->form->get_name(), |
| 399 |
'user' => $this->user_id ? get_user_by( 'id', $this->user_id )->display_name : false, |
| 400 |
'ip_address' => $this->ip_address, |
| 401 |
'device' => $this->device, |
| 402 |
'referer' => $this->referer, |
| 403 |
'created' => date_i18n( 'F j, Y g:i a', strtotime( $this->created ) ), |
| 404 |
]; |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* Get entry metadata |
| 409 |
* |
| 410 |
* @return array |
| 411 |
*/ |
| 412 |
public function get_payment_data() { |
| 413 |
global $wpdb; |
| 414 |
|
| 415 |
if ( !class_exists( 'WeForms_Payment' ) ) { |
| 416 |
return; |
| 417 |
} |
| 418 |
|
| 419 |
return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}weforms_payments WHERE entry_id = {$this->id} " ); |
| 420 |
} |
| 421 |
} |
| 422 |
|