| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Database; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Util\IpTool; |
| 6 |
|
| 7 |
/** |
| 8 |
* Undocumented class |
| 9 |
*/ |
| 10 |
|
| 11 |
class ApiModel extends Model |
| 12 |
{ |
| 13 |
public $_wpdb; |
| 14 |
|
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
global $wpdb; |
| 18 |
$this->_wpdb = $wpdb; |
| 19 |
} |
| 20 |
|
| 21 |
public function getForm() |
| 22 |
{ |
| 23 |
$result = $this->_wpdb->get_results( |
| 24 |
" |
| 25 |
SELECT form_name,id FROM `{$this->_wpdb->prefix}bitforms_form` WHERE `status`=1 order By created_at DESC |
| 26 |
" |
| 27 |
); |
| 28 |
return $result; |
| 29 |
} |
| 30 |
|
| 31 |
public function getField($id) |
| 32 |
{ |
| 33 |
$result = $this->_wpdb->get_results( |
| 34 |
$this->_wpdb->prepare( |
| 35 |
"SELECT form_content,id FROM `{$this->_wpdb->prefix}bitforms_form` WHERE `status`=1 AND `id`=%d", |
| 36 |
$id |
| 37 |
) |
| 38 |
); |
| 39 |
return $result; |
| 40 |
} |
| 41 |
|
| 42 |
public function editEntry($entryID) |
| 43 |
{ |
| 44 |
$result = $this->_wpdb->get_results( |
| 45 |
$this->_wpdb->prepare( |
| 46 |
"SELECT bitforms_form_entry_id,meta_key,meta_value FROM `{$this->_wpdb->prefix}bitforms_form_entrymeta` WHERE `bitforms_form_entry_id`=%d", |
| 47 |
$entryID |
| 48 |
) |
| 49 |
); |
| 50 |
return $result; |
| 51 |
} |
| 52 |
|
| 53 |
public function entryDelete($entryID) |
| 54 |
{ |
| 55 |
$sql = $this->_wpdb->prepare( |
| 56 |
"DELETE FROM `{$this->_wpdb->prefix}bitforms_form_entrymeta` WHERE `bitforms_form_entry_id` = %d", |
| 57 |
$entryID |
| 58 |
); |
| 59 |
$result = $this->_wpdb->query($sql); |
| 60 |
return $result; |
| 61 |
} |
| 62 |
|
| 63 |
public function findRecord($table_name, $column, $value) |
| 64 |
{ |
| 65 |
// Identifiers (table/column) cannot be parameterized with wpdb placeholders. |
| 66 |
// Sanitize identifiers to prevent SQL injection through dynamic identifiers. |
| 67 |
$safeTable = preg_replace('/[^A-Za-z0-9_]/', '', (string) $table_name); |
| 68 |
$safeColumn = preg_replace('/[^A-Za-z0-9_]/', '', (string) $column); |
| 69 |
$table_name = $this->_wpdb->prefix . $safeTable; |
| 70 |
$value = is_scalar($value) ? $value : ''; // Ensure value is scalar for placeholder. |
| 71 |
if ('' === $safeTable || '' === $safeColumn) { |
| 72 |
return []; |
| 73 |
} |
| 74 |
|
| 75 |
$sql = $this->_wpdb->prepare( |
| 76 |
'SELECT `%1$s` FROM `%2$s` WHERE `%1$s`=%3$s', |
| 77 |
$safeColumn, |
| 78 |
$table_name, |
| 79 |
$value |
| 80 |
); |
| 81 |
return $this->_wpdb->get_results($sql); |
| 82 |
} |
| 83 |
|
| 84 |
public function noteCreate($formID, $entryID, $note_details) |
| 85 |
{ |
| 86 |
$ipTool = new IpTool(); |
| 87 |
$user_details = $ipTool->getUserDetail(); |
| 88 |
$result = $this->_wpdb->insert( |
| 89 |
"{$this->_wpdb->prefix}bitforms_form_entry_relatedinfo", |
| 90 |
[ |
| 91 |
'info_type' => 'note', |
| 92 |
'info_details' => $note_details, |
| 93 |
'form_id' => $formID, |
| 94 |
'entry_id' => $entryID, |
| 95 |
'user_id' => $user_details['id'], |
| 96 |
'user_ip' => $user_details['ip'], |
| 97 |
'created_at' => $user_details['time'], |
| 98 |
] |
| 99 |
); |
| 100 |
return $result; |
| 101 |
} |
| 102 |
|
| 103 |
public function noteList() |
| 104 |
{ |
| 105 |
$result = $this->_wpdb->get_results("SELECT * FROM `{$this->_wpdb->prefix}bitforms_form_entry_relatedinfo` WHERE `status`=1"); |
| 106 |
return $result; |
| 107 |
} |
| 108 |
|
| 109 |
public function getWorkFlow($formID) |
| 110 |
{ |
| 111 |
$result = $this->_wpdb->get_results( |
| 112 |
$this->_wpdb->prepare( |
| 113 |
"SELECT workflow_name,id FROM `{$this->_wpdb->prefix}bitforms_workflows` WHERE `form_id` = %d", |
| 114 |
$formID |
| 115 |
) |
| 116 |
); |
| 117 |
return $result; |
| 118 |
} |
| 119 |
|
| 120 |
public function noteUpdate($noteID, $note_details) |
| 121 |
{ |
| 122 |
$data = ['info_details' => $note_details]; |
| 123 |
$result = $this->_wpdb->update( |
| 124 |
"{$this->_wpdb->prefix}bitforms_form_entry_relatedinfo", |
| 125 |
$data, |
| 126 |
[ |
| 127 |
'id' => $noteID, |
| 128 |
] |
| 129 |
); |
| 130 |
return $result; |
| 131 |
} |
| 132 |
|
| 133 |
public function noteDelete($noteID) |
| 134 |
{ |
| 135 |
$sql = $this->_wpdb->prepare( |
| 136 |
"DELETE FROM `{$this->_wpdb->prefix}bitforms_form_entry_relatedinfo` WHERE `id` = %d", |
| 137 |
$noteID |
| 138 |
); |
| 139 |
$result = $this->_wpdb->query($sql); |
| 140 |
return $result; |
| 141 |
} |
| 142 |
|
| 143 |
public function get_form_value($entryID) |
| 144 |
{ |
| 145 |
$sql = $this->_wpdb->prepare( |
| 146 |
"SELECT `meta_key`,`meta_value` FROM `{$this->_wpdb->prefix}bitforms_form_entrymeta` WHERE bitforms_form_entry_id=%d", |
| 147 |
$entryID |
| 148 |
); |
| 149 |
$result = $this->_wpdb->get_results($sql); |
| 150 |
return $result; |
| 151 |
} |
| 152 |
|
| 153 |
public function logUpdate($updateValue, $logID) |
| 154 |
{ |
| 155 |
if (empty($logID)) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
$sql = $this->_wpdb->prepare( |
| 159 |
"UPDATE `{$this->_wpdb->prefix}bitforms_form_entry_log` SET content=%s WHERE id=%d", |
| 160 |
$updateValue, |
| 161 |
$logID |
| 162 |
); |
| 163 |
$result = $this->_wpdb->get_results($sql); |
| 164 |
return $result; |
| 165 |
} |
| 166 |
|
| 167 |
public function getFormId($formID) |
| 168 |
{ |
| 169 |
$sql = $this->_wpdb->prepare( |
| 170 |
"SELECT form_id FROM `{$this->_wpdb->prefix}bitforms_form_entries` WHERE id=%d", |
| 171 |
$formID |
| 172 |
); |
| 173 |
$result = $this->_wpdb->get_results($sql); |
| 174 |
return $result; |
| 175 |
} |
| 176 |
|
| 177 |
public function getOnSubmitWorkflow($formID) |
| 178 |
{ |
| 179 |
$sql = $this->_wpdb->prepare( |
| 180 |
"SELECT `id`, `workflow_name`, `workflow_type`, `workflow_run`, `workflow_behaviour`, `workflow_status` FROM `{$this->_wpdb->prefix}bitforms_workflows` WHERE `form_id`=%d AND `workflow_type`='onsubmit' ORDER BY id DESC", |
| 181 |
$formID |
| 182 |
); |
| 183 |
$result = $this->_wpdb->get_results($sql); |
| 184 |
return $result; |
| 185 |
} |
| 186 |
} |
| 187 |
|