| 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 integer |
| 21 |
*/ |
| 22 |
public $id = 0; |
| 23 |
|
| 24 |
/** |
| 25 |
* The form id |
| 26 |
* |
| 27 |
* @var integer |
| 28 |
*/ |
| 29 |
public $form_id = 0; |
| 30 |
|
| 31 |
/** |
| 32 |
* The user id |
| 33 |
* |
| 34 |
* @var integer |
| 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 = array(); |
| 72 |
|
| 73 |
/** |
| 74 |
* The constructor |
| 75 |
* |
| 76 |
* @param integer $entry_id |
| 77 |
* @param \WeForms_Form $form |
| 78 |
*/ |
| 79 |
function __construct( $entry_id, $form ) { |
| 80 |
$this->id = $entry_id; |
| 81 |
$this->form = $form; |
| 82 |
|
| 83 |
$this->populate_entry_data(); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* |
| 88 |
* Populate the class with data |
| 89 |
* |
| 90 |
* @TODO: Abstract this |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function populate_entry_data() { |
| 95 |
global $wpdb; |
| 96 |
|
| 97 |
// return if we populated the already, ensures single db call |
| 98 |
if ( $this->form_id ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
$values = array(); |
| 103 |
|
| 104 |
$query = "SELECT * FROM {$wpdb->weforms_entries} as entry |
| 105 |
LEFT JOIN {$wpdb->weforms_entrymeta} AS meta ON entry.id = meta.weforms_entry_id |
| 106 |
WHERE entry.id = {$this->id}"; |
| 107 |
|
| 108 |
$results = $wpdb->get_results( $query ); |
| 109 |
|
| 110 |
if ( $results ) { |
| 111 |
$first_row = reset( $results ); |
| 112 |
|
| 113 |
$this->form_id = (int) $first_row->form_id; |
| 114 |
$this->user_id = (int) $first_row->user_id; |
| 115 |
$this->ip_address = long2ip( $first_row->user_ip ); |
| 116 |
$this->device = $first_row->user_device; |
| 117 |
$this->referer = $first_row->referer; |
| 118 |
$this->created = $first_row->created_at; |
| 119 |
|
| 120 |
$this->fields = $this->form->get_field_values(); |
| 121 |
|
| 122 |
foreach ($results as $result) { |
| 123 |
|
| 124 |
if ( array_key_exists( $result->meta_key, $this->fields ) ) { |
| 125 |
|
| 126 |
$field = $this->fields[ $result->meta_key ]; |
| 127 |
$value = $result->meta_value; |
| 128 |
|
| 129 |
if ( $field['type'] == 'textarea_field' ) { |
| 130 |
|
| 131 |
$value = weforms_format_text( $value ); |
| 132 |
|
| 133 |
} elseif ( $field['type'] == 'name_field' ) { |
| 134 |
|
| 135 |
$value = implode( ' ', explode( WeForms::$field_separator, $value ) ); |
| 136 |
|
| 137 |
} elseif ( in_array( $field['type'], array( 'dropdown_field', 'radio_field' ) ) ) { |
| 138 |
|
| 139 |
if ( isset( $field['options'] ) && $field['options'] ) { |
| 140 |
|
| 141 |
if ( isset( $field['options'][ $value ] ) ) { |
| 142 |
$value = $field['options'][ $value ]; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
} elseif ( in_array( $field['type'], array( 'multiple_select', 'checkbox_field' ) ) ) { |
| 147 |
$value = explode( WeForms::$field_separator, $value ); |
| 148 |
$temp_value = $value; |
| 149 |
|
| 150 |
if ( is_array( $value ) && $value ) { |
| 151 |
|
| 152 |
$new_array = array(); |
| 153 |
|
| 154 |
foreach ( $value as $option_key ) { |
| 155 |
if ( is_array( $field['options'] ) && array_key_exists( $option_key, $field['options'] ) ) { |
| 156 |
$new_array[] = $field['options'][ $option_key ]; |
| 157 |
} else { |
| 158 |
$new_array[] = $option_key; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
$value = $new_array; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
} elseif ( in_array( $field['type'], array( 'image_upload', 'file_upload' ) ) ) { |
| 167 |
|
| 168 |
$file_field = ''; |
| 169 |
$value = maybe_unserialize( $value ); |
| 170 |
|
| 171 |
if ( is_array( $value ) && $value ) { |
| 172 |
|
| 173 |
foreach ($value as $attachment_id) { |
| 174 |
|
| 175 |
if ( $field['type'] == 'image_upload' ) { |
| 176 |
$thumb = wp_get_attachment_image( $attachment_id, 'thumbnail' ); |
| 177 |
} else { |
| 178 |
$thumb = get_post_field( 'post_title', $attachment_id ); |
| 179 |
} |
| 180 |
|
| 181 |
$full_size = wp_get_attachment_url( $attachment_id ); |
| 182 |
|
| 183 |
$file_field .= sprintf( '<a href="%s" target="_blank">%s</a> ', $full_size, $thumb ); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
$value = $file_field; |
| 188 |
|
| 189 |
} elseif ( $field['type'] == 'google_map' ) { |
| 190 |
list( $lat, $long ) = explode( ',', $value ); |
| 191 |
|
| 192 |
$value = array( 'lat' => $lat, 'long' => $long ); |
| 193 |
|
| 194 |
} elseif ( $field['type'] == 'address_field' ) { |
| 195 |
|
| 196 |
$field_value = unserialize( $value ); |
| 197 |
|
| 198 |
$serialized_value = array(); |
| 199 |
|
| 200 |
if ( is_array( $field_value ) ) { |
| 201 |
|
| 202 |
foreach ( $field_value as $key => $single_value ) { |
| 203 |
|
| 204 |
$single_value = str_replace( array( "_", "-" ), " ", $key) . ': ' . $single_value; |
| 205 |
$single_value = ucwords( $single_value ); |
| 206 |
$serialized_value[] = $single_value; |
| 207 |
} |
| 208 |
|
| 209 |
$value = implode( "<br> ", $serialized_value ); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
$this->fields[ $result->meta_key ]['value'] = apply_filters( 'weforms_entry_meta_field', $value, $field ); |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Get entry fields |
| 221 |
* |
| 222 |
* @return array |
| 223 |
*/ |
| 224 |
public function get_fields() { |
| 225 |
return $this->fields; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Get entry metadata |
| 230 |
* |
| 231 |
* @return array |
| 232 |
*/ |
| 233 |
public function get_metadata() { |
| 234 |
return array( |
| 235 |
'id' => $this->id, |
| 236 |
'form_id' => $this->form_id, |
| 237 |
'form_title' => $this->form->get_name(), |
| 238 |
'user' => $this->user_id ? get_user_by( 'id', $this->user_id )->display_name : false, |
| 239 |
'ip_address' => $this->ip_address, |
| 240 |
'device' => $this->device, |
| 241 |
'referer' => $this->referer, |
| 242 |
'created' => date_i18n( 'F j, Y g:i a', strtotime( $this->created ) ) |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
} |
| 247 |
|