| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Database; |
| 4 |
|
| 5 |
use WP_Error; |
| 6 |
|
| 7 |
/** |
| 8 |
* Undocumented class |
| 9 |
*/ |
| 10 |
|
| 11 |
class FormEntryLogModel extends Model |
| 12 |
{ |
| 13 |
protected static $table = 'bitforms_form_log_details'; |
| 14 |
|
| 15 |
public function geLogHistory($form_id, $entry_id) |
| 16 |
{ |
| 17 |
$sql = "SELECT * FROM `{$this->app_db->prefix}bitforms_form_entry_log` where form_entry_id=%d AND form_id=%d order By created_at DESC"; |
| 18 |
$logs = $this->execute($sql, [$entry_id, $form_id])->getResult(); |
| 19 |
$response = ['success' => true, 'data' => [], 'integrations' => []]; |
| 20 |
$ids = []; |
| 21 |
foreach ($logs as $log) { |
| 22 |
$ids[] = (int) $log->id; |
| 23 |
} |
| 24 |
if (isset($logs->errors['result_empty'])) { |
| 25 |
wp_send_json($response); |
| 26 |
} else { |
| 27 |
$allLogId = preg_replace('/"/', '', implode(',', $ids)); |
| 28 |
$sql2 = "SELECT * FROM `{$this->app_db->prefix}bitforms_form_log_details` WHERE `log_id` IN ($allLogId)"; |
| 29 |
$integrations = $this->execute($sql2)->getResult(); |
| 30 |
|
| 31 |
if (isset($integrations->errors['result_empty'])) { |
| 32 |
$integrations = []; |
| 33 |
} else { |
| 34 |
foreach ($logs as $key => $log) { |
| 35 |
foreach ($integrations as $integration) { |
| 36 |
if ($integration->log_id === $log->id) { |
| 37 |
$logs[$key]->integration = true; |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
wp_send_json(['success' => true, 'data' => $logs, 'integrations' => $integrations]); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public function entryLogCheck($entry_id, $integ_id) |
| 48 |
{ |
| 49 |
if (is_null($entry_id)) { |
| 50 |
return new WP_Error('empty_data', __('Form data is empty', 'bit-form')); |
| 51 |
} |
| 52 |
$sql = $this->app_db->prepare( |
| 53 |
"SELECT api_type, response_obj FROM `{$this->app_db->prefix}bitforms_form_entry_log` as el JOIN `{$this->app_db->prefix}bitforms_form_log_details` as ld ON ld.log_id = el.id WHERE form_entry_id = %d AND integration_id = %d AND ld.response_type = 'success' ORDER BY el.id DESC LIMIT 1", |
| 54 |
$entry_id, |
| 55 |
$integ_id |
| 56 |
); |
| 57 |
|
| 58 |
return $this->app_db->get_results($sql); |
| 59 |
} |
| 60 |
|
| 61 |
public function form_log_insert($data = []) |
| 62 |
{ |
| 63 |
if (is_null($data)) { |
| 64 |
return new WP_Error('empty_data', __('Form data is empty', 'bit-form')); |
| 65 |
} |
| 66 |
$result = $this->app_db->insert( |
| 67 |
"{$this->app_db->prefix}bitforms_form_entry_log", |
| 68 |
$data |
| 69 |
); |
| 70 |
return $this->getResult($result); |
| 71 |
} |
| 72 |
|
| 73 |
public function log_history_insert($data = []) |
| 74 |
{ |
| 75 |
if (is_null($data)) { |
| 76 |
return new WP_Error('empty_data', __('Form data is empty', 'bit-form')); |
| 77 |
} |
| 78 |
$result = $this->app_db->insert( |
| 79 |
"{$this->app_db->prefix}bitforms_form_log_details", |
| 80 |
$data |
| 81 |
); |
| 82 |
return $this->getResult($result); |
| 83 |
} |
| 84 |
|
| 85 |
public function get_form_value($form_id = '') |
| 86 |
{ |
| 87 |
$sql = $this->app_db->prepare( |
| 88 |
"SELECT `meta_key`,`meta_value` FROM `{$this->app_db->prefix}bitforms_form_entrymeta` WHERE bitforms_form_entry_id=%d", |
| 89 |
$form_id |
| 90 |
); |
| 91 |
return $this->execute($sql)->getResult(); |
| 92 |
} |
| 93 |
|
| 94 |
public function logUpdate($updateValue, $logID) |
| 95 |
{ |
| 96 |
if (empty($logID)) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
$sql = $this->app_db->prepare( |
| 100 |
"UPDATE `{$this->app_db->prefix}bitforms_form_entry_log` SET content=%s WHERE id=%d", |
| 101 |
$updateValue, |
| 102 |
$logID |
| 103 |
); |
| 104 |
return $this->execute($sql)->getResult(); |
| 105 |
} |
| 106 |
} |
| 107 |
|