| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Provides Base Model Class |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace BitCode\BitForm\Core\Database; |
| 8 |
|
| 9 |
/** |
| 10 |
* Undocumented class |
| 11 |
*/ |
| 12 |
|
| 13 |
class FormEntryMetaModel extends Model |
| 14 |
{ |
| 15 |
protected static $table = 'bitforms_form_entrymeta'; |
| 16 |
|
| 17 |
public function duplicateEntryMeta($data) |
| 18 |
{ |
| 19 |
$values[] = $data['duplicateID']; |
| 20 |
$values[] = $data['entryID']; |
| 21 |
$sql = "INSERT INTO $this->table_name (bitforms_form_entry_id,meta_key,meta_value)" |
| 22 |
. ' SELECT %d as bitforms_form_entry_id,meta_key,meta_value' |
| 23 |
. " FROM `$this->table_name` WHERE bitforms_form_entry_id = %d"; |
| 24 |
return $this->execute($sql, $values)->getResult(); |
| 25 |
} |
| 26 |
|
| 27 |
public function update(array $data, array $condition) |
| 28 |
{ |
| 29 |
$entryID = $condition['bitforms_form_entry_id']; |
| 30 |
if (empty($entryID)) { |
| 31 |
return false; |
| 32 |
} |
| 33 |
$formEntryMeta = $this->get( |
| 34 |
[ |
| 35 |
'meta_key', |
| 36 |
'meta_value', |
| 37 |
], |
| 38 |
[ |
| 39 |
'bitforms_form_entry_id' => $entryID, |
| 40 |
] |
| 41 |
); |
| 42 |
$oldEntries = []; |
| 43 |
foreach ($formEntryMeta as $oldKey => $oldValue) { |
| 44 |
$oldEntries[$oldValue->meta_key] = $oldValue->meta_value; |
| 45 |
} |
| 46 |
$updatedData = []; |
| 47 |
$oldEntriesKey = array_keys($oldEntries); |
| 48 |
foreach ($data as $upKey => $upValue) { |
| 49 |
$updatedData[$upKey] = is_string($upValue) ? |
| 50 |
$upValue : |
| 51 |
wp_json_encode($upValue); |
| 52 |
if (!\in_array($upKey, $oldEntriesKey)) { |
| 53 |
$this->insert( |
| 54 |
[ |
| 55 |
'bitforms_form_entry_id' => $entryID, |
| 56 |
'meta_key' => $upKey, |
| 57 |
'meta_value' => $updatedData[$upKey], |
| 58 |
] |
| 59 |
); |
| 60 |
unset($data[$upKey]); |
| 61 |
} |
| 62 |
} |
| 63 |
if (empty($data)) { |
| 64 |
return $updatedData; |
| 65 |
} |
| 66 |
$checkCondition = $this->checkCondition($condition); |
| 67 |
if (is_wp_error($checkCondition)) { |
| 68 |
return $checkCondition; |
| 69 |
} |
| 70 |
$case_part = ' '; |
| 71 |
$all_values = []; |
| 72 |
$condition['meta_key'] = array_keys($data); |
| 73 |
foreach ($data as $key => $value) { |
| 74 |
$value = is_string($value) ? $value : wp_json_encode($value); |
| 75 |
$case_part .= " |
| 76 |
WHEN '$key' THEN " . $this->getFieldFormat($value); |
| 77 |
$all_values[] = $value; |
| 78 |
} |
| 79 |
$formattedCondition = $this->getFormatedCondition($condition); |
| 80 |
if ($formattedCondition) { |
| 81 |
$condition_to_check = $formattedCondition['conditions']; |
| 82 |
$all_values = array_merge($all_values, $formattedCondition['values']); |
| 83 |
} else { |
| 84 |
$condition_to_check = null; |
| 85 |
} |
| 86 |
|
| 87 |
$sql = "UPDATE $this->table_name |
| 88 |
SET meta_value = ( CASE meta_key |
| 89 |
$case_part |
| 90 |
END |
| 91 |
)"; |
| 92 |
$sql .= ' ' . $condition_to_check; |
| 93 |
$status = $this->execute($sql, $all_values)->getResult(); |
| 94 |
if (is_wp_error($status) && 'result_empty' !== $status->get_error_code()) { |
| 95 |
return $status; |
| 96 |
} |
| 97 |
return $updatedData; |
| 98 |
} |
| 99 |
|
| 100 |
public function validQueryCondition($conditions) |
| 101 |
{ |
| 102 |
foreach ($conditions as $index => $condition) { |
| 103 |
if (is_object($condition)) { |
| 104 |
if (empty($condition->field) || empty($condition->logic) || empty($condition->val) && !in_array($condition->val, ['0', 0, 0.0], true)) { |
| 105 |
unset($conditions[$index], $conditions[$index + 1]); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
return $conditions; |
| 110 |
} |
| 111 |
|
| 112 |
public function sqlQryGenerateByFldCondition($conditions) |
| 113 |
{ |
| 114 |
$dbHelper = new Helper(); |
| 115 |
$sql = ''; |
| 116 |
|
| 117 |
$dateQuery = $dbHelper->dateQueryList(); |
| 118 |
|
| 119 |
$validCondtions = $this->validQueryCondition($conditions); |
| 120 |
|
| 121 |
foreach ($validCondtions as $condition) { |
| 122 |
if (is_object($condition)) { |
| 123 |
if (is_array($condition->val)) { |
| 124 |
$value = $dbHelper->arrValueModifyByLogic($condition->logic, $condition->val); |
| 125 |
} else { |
| 126 |
$value = $dbHelper->strValueModifyByLogic($condition->logic, $condition->val); |
| 127 |
} |
| 128 |
|
| 129 |
$operator = $dbHelper->convertToSqlOperator($condition->logic); |
| 130 |
|
| 131 |
if (isset($dateQuery[$condition->val])) { |
| 132 |
$sql .= $dbHelper->fieldQueryByDate($condition->field, $operator, $value, $condition->logic); |
| 133 |
} else { |
| 134 |
if (!is_int($value)) { |
| 135 |
$value = "'" . $value . "'"; |
| 136 |
} |
| 137 |
|
| 138 |
$sql .= "`$condition->field` $operator $value"; |
| 139 |
} |
| 140 |
} else { |
| 141 |
$sql .= ' ' . $condition; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return trim($sql); |
| 146 |
} |
| 147 |
|
| 148 |
public function queryRecount($selectedMeta, $groupedCondition, $orderCondition, $all_values) |
| 149 |
{ |
| 150 |
$entry_table = $this->app_db->prefix . 'bitforms_form_entries'; |
| 151 |
$sql = 'SELECT count(*) as count FROM ('; |
| 152 |
$sql .= "SELECT $selectedMeta FROM `$this->table_name` em"; |
| 153 |
$sql .= " INNER JOIN $entry_table e on e.id = em.bitforms_form_entry_id "; |
| 154 |
$sql .= $groupedCondition . $orderCondition; |
| 155 |
$sql .= ') as retrievedData'; |
| 156 |
$countResult = $this->execute($sql, $all_values)->getResult(); |
| 157 |
return $countResult[0]->count; |
| 158 |
} |
| 159 |
|
| 160 |
public function selectedEntryMeta($formFields, $fieldCount) |
| 161 |
{ |
| 162 |
$all_values = []; |
| 163 |
$formFieldsNames = []; |
| 164 |
$metaChecker = 0; |
| 165 |
$selectedMeta = '`bitforms_form_entry_id` as entry_id,'; |
| 166 |
$selectedMeta .= "e.user_id as '__user_id',"; |
| 167 |
$selectedMeta .= "e.user_ip as '__user_ip',"; |
| 168 |
$selectedMeta .= "e.status as '__entry_status',"; |
| 169 |
$selectedMeta .= "e.user_location as '__user_location',"; |
| 170 |
$selectedMeta .= "e.user_device as '__user_device',"; |
| 171 |
$selectedMeta .= "e.referer as '__referer',"; |
| 172 |
$selectedMeta .= "e.created_at as '__created_at',"; |
| 173 |
$selectedMeta .= "e.updated_at as '__updated_at'"; |
| 174 |
|
| 175 |
if ($fieldCount > 0) { |
| 176 |
$selectedMeta .= ','; |
| 177 |
} |
| 178 |
|
| 179 |
foreach ($formFields as $fieldDetails) { |
| 180 |
$fieldFormat = $this->getFieldFormat($fieldDetails['key']); |
| 181 |
$selectedMeta .= "GROUP_CONCAT( |
| 182 |
CASE |
| 183 |
`meta_key` |
| 184 |
WHEN '$fieldFormat' THEN `meta_value` |
| 185 |
END |
| 186 |
) AS '$fieldFormat'"; |
| 187 |
$metaChecker += 1; |
| 188 |
$all_values[] = $fieldDetails['key']; |
| 189 |
$all_values[] = $fieldDetails['key']; |
| 190 |
$formFieldsNames[] = $fieldDetails['key']; |
| 191 |
if ($metaChecker < $fieldCount) { |
| 192 |
$selectedMeta .= ','; |
| 193 |
} |
| 194 |
//#unused code commented by me## |
| 195 |
// if ( !empty( $filter['global'] ) ) { |
| 196 |
// $globalFilterString .= " `" . $fieldDetails['key'] . "` LIKE '%%" . $this->getFieldFormat( $filter['global'] ) . "%%' "; |
| 197 |
// if ( $metaChecker < $fieldCount ) { |
| 198 |
// $globalFilterString .= " OR "; |
| 199 |
// } |
| 200 |
// $globalFilterValues[] = $filter['global']; |
| 201 |
// } |
| 202 |
} |
| 203 |
|
| 204 |
return [ |
| 205 |
'selected_meta' => $selectedMeta, |
| 206 |
'form_fields_names' => $formFieldsNames, |
| 207 |
'all_values' => $all_values, |
| 208 |
]; |
| 209 |
} |
| 210 |
|
| 211 |
public function groupedCondition($condition, $all_values, $fieldConditions) |
| 212 |
{ |
| 213 |
$isFldCondition = false; |
| 214 |
$formattedCondition = $this->getFormatedCondition($condition); |
| 215 |
if ($formattedCondition) { |
| 216 |
$groupedCondition = $formattedCondition['conditions'] . 'GROUP BY |
| 217 |
`bitforms_form_entry_id` '; |
| 218 |
$all_values = array_merge($all_values, $formattedCondition['values']); |
| 219 |
} else { |
| 220 |
$groupedCondition = null; |
| 221 |
} |
| 222 |
//#unused code commented by me## |
| 223 |
//$isRecount = false; |
| 224 |
|
| 225 |
// if ( !empty( $filter['field'] ) ) { |
| 226 |
// $isRecount = true; |
| 227 |
// $filterFieldCount = count( $filter['field'] ); |
| 228 |
// $filterFieldChecker = 0; |
| 229 |
// if ( $filterFieldCount > 0 ) { |
| 230 |
// $groupedCondition .= " HAVING "; |
| 231 |
// } |
| 232 |
// foreach ( $filter['field'] as $filterFieldKey => $filterFieldDetails ) { |
| 233 |
// $groupedCondition .= " `$filterFieldDetails->id` ='%%" . $this->getFieldFormat( $filterFieldDetails->value ) . "%%'"; |
| 234 |
// $all_values[] = $filterFieldDetails->value; |
| 235 |
// if ( $filterFieldChecker < $filterFieldCount ) { |
| 236 |
// $groupedCondition .= " AND "; |
| 237 |
// } |
| 238 |
// } |
| 239 |
// } |
| 240 |
|
| 241 |
// if ( !empty( $filter['global'] ) && !empty( $globalFilterString ) && !empty( $globalFilterValues ) ) { |
| 242 |
// $isRecount = true; |
| 243 |
// if ( !empty( $filter['field'] ) ) { |
| 244 |
// $groupedCondition .= " AND (" . $globalFilterString . ") "; |
| 245 |
// } else { |
| 246 |
// $groupedCondition .= " HAVING $globalFilterString "; |
| 247 |
// } |
| 248 |
// $offset = 0; |
| 249 |
// $all_values = array_merge( $all_values, $globalFilterValues ); |
| 250 |
// } |
| 251 |
|
| 252 |
// if ( !empty( $dateBetweenFilter ) && !empty( $dateBetweenFilter->start_date ) && !empty( $dateBetweenFilter->end_date ) ) { |
| 253 |
// if ( strpos( $groupedCondition, 'HAVING' ) !== false ) { |
| 254 |
// $groupedCondition .= " AND `__created_at` BETWEEN '" . $dateBetweenFilter->start_date . "' AND '" . $dateBetweenFilter->end_date . "' "; |
| 255 |
// } else { |
| 256 |
// $groupedCondition .= " HAVING `__created_at` BETWEEN '" . $dateBetweenFilter->start_date . "' AND '" . $dateBetweenFilter->end_date . "' "; |
| 257 |
// } |
| 258 |
// } |
| 259 |
|
| 260 |
$sqlQryByFldCondtion = $this->sqlQryGenerateByFldCondition($fieldConditions); |
| 261 |
|
| 262 |
if (!empty($sqlQryByFldCondtion)) { |
| 263 |
$isFldCondition = true; |
| 264 |
if (false !== strpos($groupedCondition, 'HAVING')) { |
| 265 |
$groupedCondition .= ' AND (' . $sqlQryByFldCondtion . ') '; |
| 266 |
} else { |
| 267 |
$groupedCondition .= ' HAVING (' . $sqlQryByFldCondtion . ') '; |
| 268 |
} |
| 269 |
} |
| 270 |
return [ |
| 271 |
'groupedCondition' => $groupedCondition, |
| 272 |
'all_values' => $all_values, |
| 273 |
'isFldCondition' => $isFldCondition, |
| 274 |
]; |
| 275 |
} |
| 276 |
|
| 277 |
public function orderCondition($formFieldsNames, $sortBy) |
| 278 |
{ |
| 279 |
$orderCondition = null; |
| 280 |
if (!empty($sortBy)) { |
| 281 |
$sortableFieldCount = count($sortBy); |
| 282 |
$sortableFieldChecker = 0; |
| 283 |
if ($sortableFieldCount > 0) { |
| 284 |
$orderCondition .= ' ORDER BY '; |
| 285 |
} |
| 286 |
$orderList = ''; |
| 287 |
foreach ($sortBy as $sortableFieldKey => $sortableFieldDetails) { |
| 288 |
// $orderCondition .=" ".$this->getFieldFormat($sortableFieldDetails->id); |
| 289 |
$sortableFieldChecker += 1; |
| 290 |
if (in_array($sortableFieldDetails->id, $formFieldsNames)) { |
| 291 |
$orderList .= " `$sortableFieldDetails->id` "; |
| 292 |
$orderFollow = $sortableFieldDetails->desc ? ' DESC ' : ' ASC '; |
| 293 |
$orderList .= ' ' . $orderFollow; |
| 294 |
if ($sortableFieldChecker < $sortableFieldCount) { |
| 295 |
$orderList .= ', '; |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
if (empty($orderList)) { |
| 300 |
$orderCondition = null; |
| 301 |
} else { |
| 302 |
$orderCondition .= $orderList; |
| 303 |
} |
| 304 |
} |
| 305 |
$orderCondition .= empty($orderCondition) ? ' ORDER BY `bitforms_form_entry_id` DESC ' : ',`bitforms_form_entry_id` DESC '; |
| 306 |
return $orderCondition; |
| 307 |
} |
| 308 |
|
| 309 |
public function isEntryMetaExist($conditions) |
| 310 |
{ |
| 311 |
$existMetaData = $this->get( |
| 312 |
[ |
| 313 |
'meta_key', |
| 314 |
'meta_value', |
| 315 |
], |
| 316 |
$conditions |
| 317 |
); |
| 318 |
if (!is_wp_error($existMetaData) && count($existMetaData) > 0) { |
| 319 |
return true; |
| 320 |
} |
| 321 |
return false; |
| 322 |
} |
| 323 |
|
| 324 |
public function getEntryMeta($formFields, $entries, $limit = null, $offset = null, $filter = null, $sortBy = null, $fieldConditions = null, $dateBetweenFilter = null) |
| 325 |
{ |
| 326 |
$entry_table = $this->app_db->prefix . 'bitforms_form_entries'; |
| 327 |
$fieldCount = count($formFields); |
| 328 |
$getSelectedMetaFldValue = $this->selectedEntryMeta($formFields, $fieldCount); |
| 329 |
|
| 330 |
$selectedMeta = $getSelectedMetaFldValue['selected_meta']; |
| 331 |
$formFieldsNames = $getSelectedMetaFldValue['form_fields_names']; |
| 332 |
$all_values = $getSelectedMetaFldValue['all_values']; |
| 333 |
$entryIDs = []; |
| 334 |
$entryCount = count($entries); |
| 335 |
// $paginateEntry = empty($sortBy) && empty($filter['field']) && empty($filter['global']); |
| 336 |
// $entries = $paginateEntry ? array_slice($entries, $offset, $limit) : $entries; |
| 337 |
$paginateEntry = false; |
| 338 |
foreach ($entries as $entryDetail) { |
| 339 |
$entryIDs[] = $entryDetail->id; |
| 340 |
} |
| 341 |
if (empty($entryIDs)) { |
| 342 |
return [ |
| 343 |
'count' => 0, |
| 344 |
'entries' => [], |
| 345 |
]; |
| 346 |
} |
| 347 |
$condition['bitforms_form_entry_id'] = $entryIDs; |
| 348 |
$group = $this->groupedCondition($condition, $all_values, $fieldConditions); |
| 349 |
$groupedCondition = $group['groupedCondition']; |
| 350 |
$all_values = $group['all_values']; |
| 351 |
$isFldCondition = $group['isFldCondition']; |
| 352 |
$orderCondition = $this->orderCondition($formFieldsNames, $sortBy); |
| 353 |
|
| 354 |
$paginate = null; |
| 355 |
if (!\is_null($limit)) { |
| 356 |
$limit = \intval($limit); |
| 357 |
$paginate .= " LIMIT $limit "; |
| 358 |
} |
| 359 |
if (!\is_null($offset)) { |
| 360 |
$offset = \intval($offset); |
| 361 |
$paginate .= " OFFSET $offset "; |
| 362 |
} |
| 363 |
$sql = "SELECT $selectedMeta FROM `$this->table_name` em"; |
| 364 |
$sql .= " INNER JOIN $entry_table e on e.id = em.bitforms_form_entry_id "; |
| 365 |
if ($dateBetweenFilter) { |
| 366 |
$startDate = $dateBetweenFilter->start_date; |
| 367 |
$endDate = $dateBetweenFilter->end_date; |
| 368 |
if ($startDate && $endDate) { |
| 369 |
$sql .= " AND DATE(e.created_at) BETWEEN '$startDate' AND '$endDate' "; |
| 370 |
} elseif ($startDate) { |
| 371 |
$sql .= " AND DATE(e.created_at) >= '$startDate' "; |
| 372 |
} elseif ($endDate) { |
| 373 |
$sql .= " AND DATE(e.created_at) <= '$endDate' "; |
| 374 |
} |
| 375 |
} |
| 376 |
$sql .= $groupedCondition . $orderCondition . $paginate; |
| 377 |
$result = $this->execute($sql, $all_values)->getResult(); |
| 378 |
if (is_wp_error($result)) { |
| 379 |
return [ |
| 380 |
'count' => $paginateEntry ? $entryCount : 0, |
| 381 |
'entries' => [], |
| 382 |
'error' => $result->get_error_message() |
| 383 |
]; |
| 384 |
} |
| 385 |
if ($isFldCondition) { |
| 386 |
$condition['bitforms_form_entry_id'] = $entryIDs; |
| 387 |
$group = $this->groupedCondition($condition, $all_values, $fieldConditions); |
| 388 |
$all_values = $group['all_values']; |
| 389 |
$entryCount = $this->queryRecount($selectedMeta, $group['groupedCondition'], $orderCondition, $all_values); |
| 390 |
} |
| 391 |
$resultedEntries = [ |
| 392 |
'count' => $entryCount, |
| 393 |
'entries' => $result, |
| 394 |
]; |
| 395 |
return $resultedEntries; |
| 396 |
} |
| 397 |
|
| 398 |
private function csvInjectionPrevent($value) |
| 399 |
{ |
| 400 |
$formula = ['=', '-', '+', '@', "\t", "\r"]; |
| 401 |
$valueFilter = preg_replace('/[\]["]/i', '', $value); |
| 402 |
if (\in_array(substr($value, 0, 1), $formula, true)) { |
| 403 |
$valueFilter = "'" . trim($valueFilter); |
| 404 |
} |
| 405 |
|
| 406 |
return $valueFilter; |
| 407 |
} |
| 408 |
|
| 409 |
public function getExportEntry($formFields, $entries, $formId, $fieldLabels, $limit = null, $sortBy = null, $sortByField = null) |
| 410 |
{ |
| 411 |
$entry_table = $this->app_db->prefix . 'bitforms_form_entries'; |
| 412 |
$selectedEntryMeta = '`bitforms_form_entry_id` as entry_id,'; |
| 413 |
$selectedEntryMeta .= "e.user_id as '__user_id',"; |
| 414 |
$selectedEntryMeta .= "e.status as '__entry_status',"; |
| 415 |
$selectedEntryMeta .= "e.user_ip as '__user_ip',"; |
| 416 |
$selectedEntryMeta .= "e.user_location as '__user_location',"; |
| 417 |
$selectedEntryMeta .= "e.user_device as '__user_device',"; |
| 418 |
$selectedEntryMeta .= "e.referer as '__referer',"; |
| 419 |
$selectedEntryMeta .= "e.created_at as '__created_at',"; |
| 420 |
$selectedEntryMeta .= "e.updated_at as '__updated_at',"; |
| 421 |
$metaChecker = 0; |
| 422 |
|
| 423 |
$entryInfo = ['__user_id', '__user_ip', /* '__user_location', */'__user_device', |
| 424 |
'__referer', '__created_at', '__updated_at']; |
| 425 |
$all_values = []; |
| 426 |
if ([] === $formFields) { |
| 427 |
$data = [ |
| 428 |
'count' => 0, |
| 429 |
'entries' => [], |
| 430 |
]; |
| 431 |
wp_send_json_success($data, 200); |
| 432 |
} |
| 433 |
$fieldCount = count($formFields) - count(array_intersect($formFields, $entryInfo)); |
| 434 |
$formFieldsNames = []; |
| 435 |
foreach ($formFields as $fldKey) { |
| 436 |
$formFieldsNames[] = $fldKey; |
| 437 |
if (in_array($fldKey, $entryInfo)) { |
| 438 |
continue; |
| 439 |
} |
| 440 |
$fieldFormat = $this->getFieldFormat($fldKey); |
| 441 |
$selectedEntryMeta .= "GROUP_CONCAT( |
| 442 |
CASE |
| 443 |
`meta_key` |
| 444 |
WHEN '$fieldFormat' THEN `meta_value` |
| 445 |
END |
| 446 |
) AS '$fieldFormat'"; |
| 447 |
$metaChecker += 1; |
| 448 |
$all_values[] = $fldKey; |
| 449 |
$all_values[] = $fldKey; |
| 450 |
if ($metaChecker < $fieldCount) { |
| 451 |
$selectedEntryMeta .= ','; |
| 452 |
} |
| 453 |
} |
| 454 |
$entryIDs = []; |
| 455 |
foreach ($entries as $entryDetail) { |
| 456 |
$entryIDs[] = $entryDetail->id; |
| 457 |
} |
| 458 |
if (empty($entryIDs)) { |
| 459 |
return [ |
| 460 |
'count' => 0, |
| 461 |
'entries' => [], |
| 462 |
]; |
| 463 |
} |
| 464 |
$condition['bitforms_form_entry_id'] = $entryIDs; |
| 465 |
$formattedCondition = $this->getFormatedCondition($condition); |
| 466 |
$groupedCondition = null; |
| 467 |
if ($formattedCondition) { |
| 468 |
$groupedCondition = $formattedCondition['conditions'] . ' GROUP BY |
| 469 |
`bitforms_form_entry_id` '; |
| 470 |
$all_values = array_merge($all_values, $formattedCondition['values']); |
| 471 |
} |
| 472 |
$order = \is_null($sortBy) ? 'DESC ' : "$sortBy"; |
| 473 |
$orderField = \is_null($sortByField) ? 'bitforms_form_entry_id' : "`$sortByField`"; |
| 474 |
|
| 475 |
$orderCondition = "ORDER BY $orderField $order "; |
| 476 |
if (!\is_null($limit)) { |
| 477 |
$limitInt = \intval($limit); |
| 478 |
$limit = " LIMIT $limitInt "; |
| 479 |
} |
| 480 |
$sql = "SELECT $selectedEntryMeta FROM `$this->table_name` em"; |
| 481 |
$sql .= " INNER JOIN $entry_table e on e.id = em.bitforms_form_entry_id "; |
| 482 |
$sql .= $groupedCondition . $orderCondition . $limit; |
| 483 |
|
| 484 |
$result = $this->execute($sql, $all_values)->getResult(); |
| 485 |
$allData = []; |
| 486 |
$entry_id = 'entry_id'; |
| 487 |
$users = get_users(['fields' => ['ID', 'display_name']]); |
| 488 |
$userNames = []; |
| 489 |
foreach ($users as $key => $value) { |
| 490 |
$userNames[$value->ID] = $value->display_name; |
| 491 |
} |
| 492 |
foreach ($result as $key => $value) { |
| 493 |
foreach ($formFieldsNames as $formFieldName) { |
| 494 |
$allData[$key]['entry_id'] = preg_replace('/[\]["]/i', '', $value->$entry_id); |
| 495 |
if ('__user_id' === $formFieldName && intval($value->$formFieldName) > 0) { |
| 496 |
$allData[$key][$formFieldName] = $userNames[$value->$formFieldName]; |
| 497 |
} elseif ('__user_ip' === $formFieldName) { |
| 498 |
$allData[$key][$formFieldName] = long2ip($value->$formFieldName); |
| 499 |
} else { |
| 500 |
$allData[$key][$formFieldName] = preg_replace('/[\]["]/i', '', $value->$formFieldName); |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
if (is_wp_error($result)) { |
| 506 |
wp_send_json_error('Internal server error', 500); |
| 507 |
} else { |
| 508 |
foreach ($fieldLabels as $field) { |
| 509 |
foreach ($allData as $index => $entry) { |
| 510 |
if (array_key_exists($field['key'], $entry) && 'file-up' === $field['type']) { |
| 511 |
$key = $field['key']; |
| 512 |
if (empty($entry[$key])) { |
| 513 |
continue; |
| 514 |
} |
| 515 |
$_upload_dir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formId . DIRECTORY_SEPARATOR . $entry['entry_id']; |
| 516 |
if (is_array(explode(',', $entry[$key]))) { |
| 517 |
$fileData = []; |
| 518 |
foreach (explode(',', $entry[$key]) as $file) { |
| 519 |
$uploadedFile = explode('_', $file); |
| 520 |
$path = "bitforms/bitforms-file/?formID=$formId&entryID=" . $entry['entry_id'] . "&fileID=$uploadedFile[0]"; |
| 521 |
if (file_exists($_upload_dir . DIRECTORY_SEPARATOR . $uploadedFile[0])) { |
| 522 |
$fileData[] = site_url($path, null); |
| 523 |
} |
| 524 |
} |
| 525 |
$allData[$index][$key] = implode(',', $fileData); |
| 526 |
} else { |
| 527 |
$uploadedFile = explode('_', $entry[$key]); |
| 528 |
$path = "bitforms/bitforms-file/?formID=$formId&entryID=" . $entry['entry_id'] . '&fileID=' . $uploadedFile[0]; |
| 529 |
if (file_exists($_upload_dir . DIRECTORY_SEPARATOR . $uploadedFile[0])) { |
| 530 |
$allData[$index][$key] = site_url($path, null); |
| 531 |
} |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
wp_send_json_success($allData, 200); |
| 537 |
} |
| 538 |
} |
| 539 |
} |
| 540 |
|