PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
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
bit-form / v1 / includes / Core / Database / FormEntryMetaModel.php

FormEntryMetaModel.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.0, at v1/includes/Core/Database/FormEntryMetaModel.php

516 lines 21.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 use BitCode\BitForm\Core\Database\Helper;
14 use BitCode\BitForm\Core\Database\Model;
15
16 class FormEntryMetaModel extends Model {
17 protected static $table = 'bitforms_form_entrymeta';
18
19 public function duplicateEntryMeta($data) {
20 $values[] = $data['duplicateID'];
21 $values[] = $data['entryID'];
22 $sql = "INSERT INTO $this->table_name (bitforms_form_entry_id,meta_key,meta_value)"
23 . " SELECT %d as bitforms_form_entry_id,meta_key,meta_value"
24 . " FROM `$this->table_name` WHERE bitforms_form_entry_id = %d";
25 return $this->execute($sql, $values)->getResult();
26 }
27
28 public function update(array $data, array $condition) {
29 $entryID = $condition['bitforms_form_entry_id'];
30 if (empty($entryID)) {
31 return false;
32 }
33 $formEntryMeta = $this->get(
34 array(
35 'meta_key',
36 'meta_value',
37 ),
38 array(
39 'bitforms_form_entry_id' => $entryID,
40 )
41 );
42 $oldEntries = array();
43 foreach ($formEntryMeta as $oldKey => $oldValue) {
44 $oldEntries[$oldValue->meta_key] = $oldValue->meta_value;
45 }
46 $updatedData = array();
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 array(
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 = array();
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) && $status->get_error_code() !== 'result_empty') {
95 return $status;
96 }
97 return $updatedData;
98 }
99
100 public function validQueryCondition($conditions) {
101 foreach ($conditions as $index => $condition) {
102 if (is_object($condition)) {
103 if (empty($condition->field) || empty($condition->logic) || empty($condition->val) && !in_array($condition->val, ['0', 0, 0.0], TRUE)) {
104 unset($conditions[$index]);
105 unset($conditions[$index + 1]);
106 }
107 }
108 }
109 return $conditions;
110 }
111
112 public function sqlQryGenerateByFldCondition($conditions) {
113 $dbHelper = new Helper();
114 $sql = '';
115
116 $dateQuery = $dbHelper->dateQueryList();
117
118 $validCondtions = $this->validQueryCondition($conditions);
119
120 foreach ($validCondtions as $condition) {
121 if (is_object($condition)) {
122 if (is_array($condition->val)) {
123 $value = $dbHelper->arrValueModifyByLogic($condition->logic, $condition->val);
124 } else {
125 $value = $dbHelper->strValueModifyByLogic($condition->logic, $condition->val);
126 }
127
128 $operator = $dbHelper->convertToSqlOperator($condition->logic);
129
130 if (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
148 public function queryRecount($selectedMeta, $groupedCondition, $orderCondition, $all_values) {
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 $all_values = [];
161 $formFieldsNames = [];
162 $metaChecker = 0;
163 $selectedMeta = "`bitforms_form_entry_id` as entry_id,";
164 $selectedMeta .= "e.user_id as '__user_id',";
165 $selectedMeta .= "e.user_ip as '__user_ip',";
166 $selectedMeta .= "e.status as '__entry_status',";
167 $selectedMeta .= "e.user_location as '__user_location',";
168 $selectedMeta .= "e.user_device as '__user_device',";
169 $selectedMeta .= "e.referer as '__referer',";
170 $selectedMeta .= "e.created_at as '__created_at',";
171 $selectedMeta .= "e.updated_at as '__updated_at'";
172
173 if ($fieldCount > 0) {
174 $selectedMeta .= ",";
175 }
176
177 foreach ($formFields as $fieldDetails) {
178 $fieldFormat = $this->getFieldFormat($fieldDetails['key']);
179 $selectedMeta .= "GROUP_CONCAT(
180 CASE
181 `meta_key`
182 WHEN '$fieldFormat' THEN `meta_value`
183 END
184 ) AS '$fieldFormat'";
185 $metaChecker += 1;
186 $all_values[] = $fieldDetails['key'];
187 $all_values[] = $fieldDetails['key'];
188 $formFieldsNames[] = $fieldDetails['key'];
189 if ($metaChecker < $fieldCount) {
190 $selectedMeta .= ",";
191 }
192 ##unused code commented by me##
193 // if ( !empty( $filter['global'] ) ) {
194 // $globalFilterString .= " `" . $fieldDetails['key'] . "` LIKE '%%" . $this->getFieldFormat( $filter['global'] ) . "%%' ";
195 // if ( $metaChecker < $fieldCount ) {
196 // $globalFilterString .= " OR ";
197 // }
198 // $globalFilterValues[] = $filter['global'];
199 // }
200 }
201
202 return [
203 'selected_meta' => $selectedMeta,
204 'form_fields_names' => $formFieldsNames,
205 'all_values' => $all_values,
206 ];
207
208 }
209
210 public function groupedCondition($condition, $all_values, $fieldConditions) {
211 $isFldCondition = false;
212 $formattedCondition = $this->getFormatedCondition($condition);
213 if ($formattedCondition) {
214 $groupedCondition = $formattedCondition['conditions'] . "GROUP BY
215 `bitforms_form_entry_id` ";
216 $all_values = array_merge($all_values, $formattedCondition['values']);
217 } else {
218 $groupedCondition = null;
219 }
220 ##unused code commented by me##
221 //$isRecount = false;
222
223 // if ( !empty( $filter['field'] ) ) {
224 // $isRecount = true;
225 // $filterFieldCount = count( $filter['field'] );
226 // $filterFieldChecker = 0;
227 // if ( $filterFieldCount > 0 ) {
228 // $groupedCondition .= " HAVING ";
229 // }
230 // foreach ( $filter['field'] as $filterFieldKey => $filterFieldDetails ) {
231 // $groupedCondition .= " `$filterFieldDetails->id` ='%%" . $this->getFieldFormat( $filterFieldDetails->value ) . "%%'";
232 // $all_values[] = $filterFieldDetails->value;
233 // if ( $filterFieldChecker < $filterFieldCount ) {
234 // $groupedCondition .= " AND ";
235 // }
236 // }
237 // }
238
239 // if ( !empty( $filter['global'] ) && !empty( $globalFilterString ) && !empty( $globalFilterValues ) ) {
240 // $isRecount = true;
241 // if ( !empty( $filter['field'] ) ) {
242 // $groupedCondition .= " AND (" . $globalFilterString . ") ";
243 // } else {
244 // $groupedCondition .= " HAVING $globalFilterString ";
245 // }
246 // $offset = 0;
247 // $all_values = array_merge( $all_values, $globalFilterValues );
248 // }
249
250 // if ( !empty( $dateBetweenFilter ) && !empty( $dateBetweenFilter->start_date ) && !empty( $dateBetweenFilter->end_date ) ) {
251 // if ( strpos( $groupedCondition, 'HAVING' ) !== false ) {
252 // $groupedCondition .= " AND `__created_at` BETWEEN '" . $dateBetweenFilter->start_date . "' AND '" . $dateBetweenFilter->end_date . "' ";
253 // } else {
254 // $groupedCondition .= " HAVING `__created_at` BETWEEN '" . $dateBetweenFilter->start_date . "' AND '" . $dateBetweenFilter->end_date . "' ";
255 // }
256 // }
257
258 $sqlQryByFldCondtion = $this->sqlQryGenerateByFldCondition($fieldConditions);
259
260 if (!empty($sqlQryByFldCondtion)) {
261 $isFldCondition = true;
262 if (strpos($groupedCondition, 'HAVING') !== false) {
263 $groupedCondition .= " AND (" . $sqlQryByFldCondtion . ") ";
264 } else {
265 $groupedCondition .= " HAVING (" . $sqlQryByFldCondtion . ") ";
266 }
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 $orderCondition = null;
278 if (!empty($sortBy)) {
279 $sortableFieldCount = count($sortBy);
280 $sortableFieldChecker = 0;
281 if ($sortableFieldCount > 0) {
282 $orderCondition .= " ORDER BY ";
283 }
284 $orderList = '';
285 foreach ($sortBy as $sortableFieldKey => $sortableFieldDetails) {
286 // $orderCondition .=" ".$this->getFieldFormat($sortableFieldDetails->id);
287 $sortableFieldChecker += 1;
288 if (in_array($sortableFieldDetails->id, $formFieldsNames)) {
289 $orderList .= " `$sortableFieldDetails->id` ";
290 $orderFollow = $sortableFieldDetails->desc ? ' DESC ' : ' ASC ';
291 $orderList .= " " . $orderFollow;
292 if ($sortableFieldChecker < $sortableFieldCount) {
293 $orderList .= ", ";
294 }
295 }
296 }
297 if (empty($orderList)) {
298 $orderCondition = null;
299 } else {
300 $orderCondition .= $orderList;
301 }
302 }
303 $orderCondition .= empty($orderCondition) ? " ORDER BY `bitforms_form_entry_id` DESC " : ",`bitforms_form_entry_id` DESC ";
304 return $orderCondition;
305 }
306
307 public function getEntryMeta($formFields, $entries, $limit = null, $offset = null, $filter = null, $sortBy = null, $fieldConditions = null) {
308 $entry_table = $this->app_db->prefix . 'bitforms_form_entries';
309 $fieldCount = count($formFields);
310 $getSelectedMetaFldValue = $this->selectedEntryMeta($formFields, $fieldCount);
311
312 $selectedMeta = $getSelectedMetaFldValue['selected_meta'];
313 $formFieldsNames = $getSelectedMetaFldValue['form_fields_names'];
314 $all_values = $getSelectedMetaFldValue['all_values'];
315 $entryIDs = array();
316 $entryCount = count($entries);
317 // $paginateEntry = empty($sortBy) && empty($filter['field']) && empty($filter['global']);
318 // $entries = $paginateEntry ? array_slice($entries, $offset, $limit) : $entries;
319 $paginateEntry = false;
320 foreach ($entries as $entryDetail) {
321 $entryIDs[] = $entryDetail->id;
322 }
323 if (empty($entryIDs)) {
324 return array(
325 'count' => 0,
326 'entries' => [],
327 );
328 }
329 $condition['bitforms_form_entry_id'] = $entryIDs;
330 $group = $this->groupedCondition($condition, $all_values, $fieldConditions);
331 $groupedCondition = $group['groupedCondition'];
332 $all_values = $group['all_values'];
333 $isFldCondition = $group['isFldCondition'];
334 $orderCondition = $this->orderCondition($formFieldsNames, $sortBy);
335
336 $paginate = null;
337 if (!\is_null($limit)) {
338 $limit = \intval($limit);
339 $paginate .= " LIMIT $limit ";
340 }
341 if (!\is_null($offset)) {
342 $offset = \intval($offset);
343 $paginate .= " OFFSET $offset ";
344 }
345 $sql = "SELECT $selectedMeta FROM `$this->table_name` em";
346 $sql .= " INNER JOIN $entry_table e on e.id = em.bitforms_form_entry_id ";
347 $sql .= $groupedCondition . $orderCondition . $paginate;
348 $result = $this->execute($sql, $all_values)->getResult();
349 if (is_wp_error($result)) {
350 return array(
351 'count' => $paginateEntry ? $entryCount : 0,
352 'entries' => [],
353 'error' => $result->get_error_message()
354 );
355 }
356 if ($isFldCondition) {
357 $condition['bitforms_form_entry_id'] = $entryIDs;
358 $group = $this->groupedCondition($condition, $all_values, $fieldConditions);
359 $all_values = $group['all_values'];
360 $entryCount = $this->queryRecount($selectedMeta, $group['groupedCondition'], $orderCondition, $all_values);
361 }
362 $resultedEntries = array(
363 'count' => $entryCount,
364 'entries' => $result,
365 );
366 return $resultedEntries;
367 }
368
369 private function csvInjectionPrevent($value)
370 {
371 $formula= ['=', '-', '+', '@', "\t", "\r"];
372 $valueFilter = preg_replace('/[\]["]/i', '', $value);
373 if (\in_array(substr($value, 0, 1), $formula, true)) {
374 $valueFilter = "'".trim($valueFilter);
375 }
376
377 return $valueFilter;
378 }
379
380
381 public function getExportEntry($formFields, $entries, $formId, $fieldLabels, $fileFormate, $limit = null, $sortBy = null, $sortByField = null) {
382 $entry_table = $this->app_db->prefix . 'bitforms_form_entries';
383 $selectedEntryMeta = "`bitforms_form_entry_id` as entry_id,";
384 $selectedEntryMeta .= "e.user_id as '__user_id',";
385 $selectedEntryMeta .= "e.status as '__entry_status',";
386 $selectedEntryMeta .= "e.user_ip as '__user_ip',";
387 $selectedEntryMeta .= "e.user_location as '__user_location',";
388 $selectedEntryMeta .= "e.user_device as '__user_device',";
389 $selectedEntryMeta .= "e.referer as '__referer',";
390 $selectedEntryMeta .= "e.created_at as '__created_at',";
391 $selectedEntryMeta .= "e.updated_at as '__updated_at',";
392 $metaChecker = 0;
393
394 $entryInfo = ['__user_id', '__user_ip', /* '__user_location', */'__user_device',
395 '__referer', '__created_at', '__updated_at'];
396 $all_values = array();
397 if ($formFields == []) {
398 $data = array(
399 'count' => 0,
400 'entries' => [],
401 );
402 wp_send_json_success($data, 200);
403 }
404 $fieldCount = count($formFields) - count(array_intersect($formFields, $entryInfo));
405 $formFieldsNames = array();
406 foreach ($formFields as $fldKey) {
407 $formFieldsNames[] = $fldKey;
408 if (in_array($fldKey, $entryInfo)) {
409 continue;
410 }
411 $fieldFormat = $this->getFieldFormat($fldKey);
412 $selectedEntryMeta .= "GROUP_CONCAT(
413 CASE
414 `meta_key`
415 WHEN '$fieldFormat' THEN `meta_value`
416 END
417 ) AS '$fieldFormat'";
418 $metaChecker += 1;
419 $all_values[] = $fldKey;
420 $all_values[] = $fldKey;
421 if ($metaChecker < $fieldCount) {
422 $selectedEntryMeta .= ",";
423 }
424 }
425 $entryIDs = array();
426 foreach ($entries as $entryDetail) {
427 $entryIDs[] = $entryDetail->id;
428 }
429 if (empty($entryIDs)) {
430 return array(
431 'count' => 0,
432 'entries' => [],
433 );
434 }
435 $condition['bitforms_form_entry_id'] = $entryIDs;
436 $formattedCondition = $this->getFormatedCondition($condition);
437 $groupedCondition = null;
438 if ($formattedCondition) {
439 $groupedCondition = $formattedCondition['conditions'] . " GROUP BY
440 `bitforms_form_entry_id` ";
441 $all_values = array_merge($all_values, $formattedCondition['values']);
442 }
443 $order = \is_null($sortBy) ? "DESC " : "$sortBy";
444 $orderField = \is_null($sortByField) ? "bitforms_form_entry_id" : "`$sortByField`";
445
446 $orderCondition = "ORDER BY $orderField $order ";
447 if (!\is_null($limit)) {
448 $limitInt = \intval($limit);
449 $limit = " LIMIT $limitInt ";
450 }
451 $sql = "SELECT $selectedEntryMeta FROM `$this->table_name` em";
452 $sql .= " INNER JOIN $entry_table e on e.id = em.bitforms_form_entry_id ";
453 $sql .= $groupedCondition . $orderCondition . $limit;
454
455 $result = $this->execute($sql, $all_values)->getResult();
456 $allData = [];
457 $entry_id = "entry_id";
458 $users = get_users(['fields' => ['ID', 'display_name']]);
459 $userNames = [];
460 foreach ($users as $key => $value) {
461 $userNames[$value->ID] = $value->display_name;
462 }
463 foreach ($result as $key => $value) {
464 foreach ($formFieldsNames as $formFieldName) {
465 $allData[$key]['entry_id'] = preg_replace('/[\]["]/i', '', $value->$entry_id);
466 if ($formFieldName === '__user_id' && intval($value->$formFieldName) > 0) {
467 $allData[$key][$formFieldName] = $userNames[$value->$formFieldName];
468 } elseif ($formFieldName === '__user_ip') {
469 $allData[$key][$formFieldName] = long2ip($value->$formFieldName);
470 } else {
471 $allData[$key][$formFieldName] = preg_replace('/[\]["]/i', '', $value->$formFieldName);
472 }
473 if (is_string($value->$formFieldName) && in_array($fileFormate, ['csv', 'xlsx', 'xls'])) {
474 $allData[$key][$formFieldName] = $this->csvInjectionPrevent($value->$formFieldName);
475 }
476
477
478 }
479 }
480
481 if (is_wp_error($result)) {
482 wp_send_json_error('Internal server error', 500);
483 } else {
484 foreach ($fieldLabels as $field) {
485 foreach ($allData as $index => $entry) {
486 if (array_key_exists($field['key'], $entry) && $field['type'] == 'file-up') {
487 $key = $field['key'];
488 if (empty($entry[$key])) {
489 continue;
490 }
491 $_upload_dir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formId . DIRECTORY_SEPARATOR . $entry['entry_id'];
492 if (is_array(explode(",", $entry[$key]))) {
493 $fileData = array();
494 foreach (explode(",", $entry[$key]) as $file) {
495 $uploadedFile= explode("_", $file);
496 $path = "bitforms/bitforms-file/?formID=$formId&entryID=" . $entry['entry_id'] . "&fileID=$uploadedFile[0]";
497 if (file_exists($_upload_dir . DIRECTORY_SEPARATOR . $uploadedFile[0])) {
498 $fileData[] = site_url($path, null);
499 }
500 }
501 $allData[$index][$key] = implode(',', $fileData);
502 } else {
503 $uploadedFile= explode("_", $entry[$key]);
504 $path = "bitforms/bitforms-file/?formID=$formId&entryID=" . $entry['entry_id'] . "&fileID=" . $uploadedFile[0];
505 if (file_exists($_upload_dir . DIRECTORY_SEPARATOR . $uploadedFile[0])) {
506 $allData[$index][$key] = site_url($path, null);
507 }
508 }
509 }
510 }
511 }
512 wp_send_json_success($allData, 200);
513 }
514 }
515 }
516