| 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 |
public function report() { |
| 11 |
$reportMdl = new ReportsModel(); |
| 12 |
global $wpdb; |
| 13 |
$tablename = $wpdb->prefix . 'bitforms_reports'; |
| 14 |
|
| 15 |
$latestIds = $wpdb->get_results("SELECT MAX(`id`) as latest_id FROM $tablename GROUP BY `context`"); |
| 16 |
|
| 17 |
$notDeletedIds = array_column($latestIds, 'latest_id'); |
| 18 |
|
| 19 |
if (!is_wp_error($latestIds) && count($notDeletedIds) > 0) { |
| 20 |
$reportMdl->bulkDelete(['id' => $notDeletedIds], 'NOT IN'); |
| 21 |
} |
| 22 |
|
| 23 |
$ipTool = new IpTool(); |
| 24 |
$user_details = $ipTool->getUserDetail(); |
| 25 |
|
| 26 |
$formModel = new FormModel(); |
| 27 |
|
| 28 |
$forms = $formModel->get( |
| 29 |
['id'] |
| 30 |
); |
| 31 |
|
| 32 |
$details = [ |
| 33 |
'report_name' => 'All Entries', |
| 34 |
'hiddenColumns' => [], |
| 35 |
'pageSize' => 10, |
| 36 |
'sortBy' => [], |
| 37 |
'filters' => [], |
| 38 |
'globalFilter' => '', |
| 39 |
'order' => [], |
| 40 |
]; |
| 41 |
|
| 42 |
foreach ($forms as $form) { |
| 43 |
$existReportId = $reportMdl->get( |
| 44 |
[ |
| 45 |
'id', |
| 46 |
], |
| 47 |
[ |
| 48 |
'context' => $form->id, |
| 49 |
] |
| 50 |
); |
| 51 |
if (is_wp_error($existReportId)) { |
| 52 |
$defaultReport = [ |
| 53 |
'type' => 'table', |
| 54 |
'category' => 'form', |
| 55 |
'context' => $form->id, |
| 56 |
'details' => wp_json_encode($details), |
| 57 |
'isDefault' => (bool) 1, |
| 58 |
'user_id' => $user_details['id'], |
| 59 |
'user_ip' => $user_details['ip'], |
| 60 |
'user_device' => $user_details['device'], |
| 61 |
'created_at' => $user_details['time'], |
| 62 |
'updated_at' => $user_details['time'], |
| 63 |
]; |
| 64 |
|
| 65 |
$reportMdl->insert($defaultReport); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|