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