| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Fallback; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Database\FormModel; |
| 6 |
use BitCode\BitForm\Core\Database\ReportsModel; |
| 7 |
use BitCode\BitForm\Core\Util\IpTool; |
| 8 |
|
| 9 |
class Report |
| 10 |
{ |
| 11 |
public function report() |
| 12 |
{ |
| 13 |
$reportMdl = new ReportsModel(); |
| 14 |
global $wpdb; |
| 15 |
$tablename = $wpdb->prefix . 'bitforms_reports'; |
| 16 |
|
| 17 |
$latestIds = $wpdb->get_results("SELECT MAX(`id`) as latest_id FROM $tablename GROUP BY `context`"); |
| 18 |
|
| 19 |
$notDeletedIds = array_column($latestIds, 'latest_id'); |
| 20 |
|
| 21 |
if (!is_wp_error($latestIds) && count($notDeletedIds) > 0) { |
| 22 |
$reportMdl->bulkDelete(['id' => $notDeletedIds], 'NOT IN'); |
| 23 |
} |
| 24 |
|
| 25 |
$ipTool = new IpTool(); |
| 26 |
$user_details = $ipTool->getUserDetail(); |
| 27 |
|
| 28 |
$formModel = new FormModel(); |
| 29 |
|
| 30 |
$forms = $formModel->get( |
| 31 |
['id'] |
| 32 |
); |
| 33 |
|
| 34 |
$details = [ |
| 35 |
'report_name' => 'All Entries', |
| 36 |
'hiddenColumns' => [], |
| 37 |
'pageSize' => 10, |
| 38 |
'sortBy' => [], |
| 39 |
'filters' => [], |
| 40 |
'globalFilter' => '', |
| 41 |
'order' => [], |
| 42 |
]; |
| 43 |
|
| 44 |
foreach ($forms as $form) { |
| 45 |
$existReportId = $reportMdl->get( |
| 46 |
[ |
| 47 |
'id', |
| 48 |
], |
| 49 |
[ |
| 50 |
'context' => $form->id, |
| 51 |
] |
| 52 |
); |
| 53 |
if (is_wp_error($existReportId)) { |
| 54 |
$defaultReport = [ |
| 55 |
'type' => 'table', |
| 56 |
'category' => 'form', |
| 57 |
'context' => $form->id, |
| 58 |
'details' => wp_json_encode($details), |
| 59 |
'isDefault' => (bool) 1, |
| 60 |
'user_id' => $user_details['id'], |
| 61 |
'user_ip' => $user_details['ip'], |
| 62 |
'user_device' => $user_details['device'], |
| 63 |
'created_at' => $user_details['time'], |
| 64 |
'updated_at' => $user_details['time'], |
| 65 |
]; |
| 66 |
|
| 67 |
$reportMdl->insert($defaultReport); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public function addHiddenColumns() |
| 73 |
{ |
| 74 |
$reportMdl = new ReportsModel(); |
| 75 |
$reports = $reportMdl->get( |
| 76 |
[ |
| 77 |
'id', |
| 78 |
'details', |
| 79 |
], |
| 80 |
[ |
| 81 |
'category' => 'form', |
| 82 |
'type' => 'table', |
| 83 |
] |
| 84 |
); |
| 85 |
|
| 86 |
foreach ($reports as $report) { |
| 87 |
$reportDetails = json_decode($report->details); |
| 88 |
if (is_object($reportDetails) && (!property_exists($reportDetails, 'hiddenColumns') || empty($reportDetails->hiddenColumns))) { |
| 89 |
$reportDetails->hiddenColumns = ['__user_id', '__user_ip', '__referer', '__user_device', '__created_at', '__updated_at']; |
| 90 |
$reportMdl->update( |
| 91 |
[ |
| 92 |
'details' => wp_json_encode($reportDetails), |
| 93 |
], |
| 94 |
[ |
| 95 |
'id' => $report->id, |
| 96 |
] |
| 97 |
); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|