| 1 |
<?php namespace FluentForm\App\Modules\Logger; |
| 2 |
|
| 3 |
use FluentForm\App\Databases\Migrations\FormLogs; |
| 4 |
use FluentForm\Framework\Foundation\Application; |
| 5 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 6 |
|
| 7 |
class DataLogger |
| 8 |
{ |
| 9 |
public $app; |
| 10 |
|
| 11 |
public function __construct(Application $app) |
| 12 |
{ |
| 13 |
$this->app = $app; |
| 14 |
} |
| 15 |
|
| 16 |
public function getLogFilters() |
| 17 |
{ |
| 18 |
$statuses = wpFluent()->table('fluentform_logs') |
| 19 |
->select('status') |
| 20 |
->groupBy('status') |
| 21 |
->get(); |
| 22 |
$formattedStatuses = []; |
| 23 |
|
| 24 |
foreach ($statuses as $status) { |
| 25 |
$formattedStatuses[] = $status->status; |
| 26 |
} |
| 27 |
|
| 28 |
$components = wpFluent()->table('fluentform_logs') |
| 29 |
->select('component') |
| 30 |
->groupBy('component') |
| 31 |
->get(); |
| 32 |
|
| 33 |
$formattedComponents = []; |
| 34 |
foreach ($components as $component) { |
| 35 |
$formattedComponents[] = $component->component; |
| 36 |
} |
| 37 |
|
| 38 |
$forms = wpFluent()->table('fluentform_logs') |
| 39 |
->select('fluentform_logs.parent_source_id', 'fluentform_forms.title') |
| 40 |
->groupBy('fluentform_logs.parent_source_id') |
| 41 |
->orderBy('fluentform_logs.parent_source_id', 'DESC') |
| 42 |
->join('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_logs.parent_source_id') |
| 43 |
->get(); |
| 44 |
|
| 45 |
$formattedForms = []; |
| 46 |
|
| 47 |
foreach ($forms as $form) { |
| 48 |
$formattedForms[] = [ |
| 49 |
'form_id' => $form->parent_source_id, |
| 50 |
'title' => $form->title |
| 51 |
];; |
| 52 |
} |
| 53 |
|
| 54 |
wp_send_json_success([ |
| 55 |
'available_statuses' => $formattedStatuses, |
| 56 |
'available_components' => $formattedComponents, |
| 57 |
'available_forms' => $formattedForms |
| 58 |
]); |
| 59 |
} |
| 60 |
|
| 61 |
public function log($data) |
| 62 |
{ |
| 63 |
if (!$data) { |
| 64 |
return; |
| 65 |
} |
| 66 |
$data['created_at'] = current_time('mysql'); |
| 67 |
|
| 68 |
if (!get_option('fluentform_db_fluentform_logs_added')) { |
| 69 |
FormLogs::migrate(); |
| 70 |
} |
| 71 |
|
| 72 |
return wpFluent()->table('fluentform_logs') |
| 73 |
->insert($data); |
| 74 |
} |
| 75 |
|
| 76 |
public function getLogsByEntry($entry_id, $log_type = 'logs', $sourceType = 'submission_item') |
| 77 |
{ |
| 78 |
if($log_type == 'logs') { |
| 79 |
$logs = wpFluent()->table('fluentform_logs') |
| 80 |
->where('source_id', $entry_id) |
| 81 |
->where('source_type', $sourceType) |
| 82 |
->orderBy('id', 'DESC') |
| 83 |
->get(); |
| 84 |
|
| 85 |
$logs = apply_filters('fluentform_entry_logs', $logs, $entry_id); |
| 86 |
} else { |
| 87 |
$logs = wpFluent()->table('ff_scheduled_actions') |
| 88 |
->select([ |
| 89 |
'id', |
| 90 |
'action', |
| 91 |
'status', |
| 92 |
'note', |
| 93 |
'created_at' |
| 94 |
]) |
| 95 |
->where('origin_id', $entry_id) |
| 96 |
->orderBy('id', 'DESC') |
| 97 |
->get(); |
| 98 |
|
| 99 |
$logs = apply_filters('fluentform_entry_api_logs', $logs, $entry_id); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
wp_send_json_success([ |
| 104 |
'logs' => $logs |
| 105 |
], 200); |
| 106 |
} |
| 107 |
|
| 108 |
public function getAllLogs() |
| 109 |
{ |
| 110 |
$limit = intval($_REQUEST['per_page']); |
| 111 |
$pageNumber = intval($_REQUEST['page_number']); |
| 112 |
|
| 113 |
$skip = ($pageNumber - 1) * $limit; |
| 114 |
|
| 115 |
global $wpdb; |
| 116 |
$logsQuery = wpFluent()->table('fluentform_logs') |
| 117 |
->select([ |
| 118 |
'fluentform_logs.*' |
| 119 |
]) |
| 120 |
->select(wpFluent()->raw( $wpdb->prefix.'fluentform_forms.title as form_title' )) |
| 121 |
->join('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_logs.parent_source_id') |
| 122 |
->orderBy('fluentform_logs.id', 'DESC') |
| 123 |
->whereIn('fluentform_logs.source_type', ['submission_item', 'form_item']); |
| 124 |
|
| 125 |
|
| 126 |
if($parentSourceId = ArrayHelper::get($_REQUEST, 'parent_source_id')) { |
| 127 |
$logsQuery = $logsQuery->where('fluentform_logs.parent_source_id', intval($parentSourceId)); |
| 128 |
} |
| 129 |
|
| 130 |
if($status = ArrayHelper::get($_REQUEST, 'status')) { |
| 131 |
$logsQuery = $logsQuery->where('fluentform_logs.status', $status); |
| 132 |
} |
| 133 |
|
| 134 |
if($component = ArrayHelper::get($_REQUEST, 'component')) { |
| 135 |
$logsQuery = $logsQuery->where('fluentform_logs.component', $component); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
$logsQueryMain = $logsQuery; |
| 140 |
|
| 141 |
$logs = $logsQuery->offset($skip) |
| 142 |
->limit($limit) |
| 143 |
->get(); |
| 144 |
|
| 145 |
$logs = apply_filters('fluentform_all_logs', $logs); |
| 146 |
|
| 147 |
$total = $logsQueryMain->count(); |
| 148 |
|
| 149 |
wp_send_json_success([ |
| 150 |
'logs' => $logs, |
| 151 |
'total' => $total |
| 152 |
], 200); |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
public function getApiLogs() |
| 158 |
{ |
| 159 |
$limit = intval($_REQUEST['per_page']); |
| 160 |
$pageNumber = intval($_REQUEST['page_number']); |
| 161 |
|
| 162 |
$skip = ($pageNumber - 1) * $limit; |
| 163 |
global $wpdb; |
| 164 |
$logsQuery = wpFluent()->table('ff_scheduled_actions') |
| 165 |
->select([ |
| 166 |
'ff_scheduled_actions.id', |
| 167 |
'ff_scheduled_actions.action', |
| 168 |
'ff_scheduled_actions.form_id', |
| 169 |
'ff_scheduled_actions.origin_id', |
| 170 |
'ff_scheduled_actions.status', |
| 171 |
'ff_scheduled_actions.note', |
| 172 |
'ff_scheduled_actions.created_at', |
| 173 |
]) |
| 174 |
->select(wpFluent()->raw( $wpdb->prefix.'fluentform_forms.title as form_title' )) |
| 175 |
->join('fluentform_forms', 'fluentform_forms.id', '=', 'ff_scheduled_actions.form_id') |
| 176 |
->orderBy('ff_scheduled_actions.id', 'DESC'); |
| 177 |
|
| 178 |
|
| 179 |
if($formId = ArrayHelper::get($_REQUEST, 'form_id')) { |
| 180 |
$logsQuery = $logsQuery->where('ff_scheduled_actions.form_id', intval($formId)); |
| 181 |
} |
| 182 |
|
| 183 |
if($status = ArrayHelper::get($_REQUEST, 'status')) { |
| 184 |
$logsQuery = $logsQuery->where('ff_scheduled_actions.status', $status); |
| 185 |
} |
| 186 |
|
| 187 |
$logsQueryMain = $logsQuery; |
| 188 |
|
| 189 |
$logs = $logsQuery->offset($skip) |
| 190 |
->limit($limit) |
| 191 |
->get(); |
| 192 |
|
| 193 |
$logs = apply_filters('fluentform_api_all_logs', $logs); |
| 194 |
|
| 195 |
foreach ($logs as $log) { |
| 196 |
$log->submission_url = admin_url('admin.php?page=fluent_forms&route=entries&form_id='.$log->form_id.'#/entries/'.$log->origin_id); |
| 197 |
} |
| 198 |
|
| 199 |
$total = $logsQueryMain->count(); |
| 200 |
|
| 201 |
wp_send_json_success([ |
| 202 |
'logs' => $logs, |
| 203 |
'total' => $total |
| 204 |
], 200); |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
public function deleteLogsByIds($ids = []) |
| 209 |
{ |
| 210 |
if(!$ids) { |
| 211 |
$ids = wp_unslash($_REQUEST['log_ids']); |
| 212 |
} |
| 213 |
|
| 214 |
if(!$ids) { |
| 215 |
wp_send_json_error([ |
| 216 |
'message' => 'No selections found' |
| 217 |
], 423); |
| 218 |
} |
| 219 |
|
| 220 |
wpFluent()->table('fluentform_logs') |
| 221 |
->whereIn('id', $ids) |
| 222 |
->delete(); |
| 223 |
|
| 224 |
wp_send_json_success([ |
| 225 |
'message' => __('Selected logs successfully deleted', 'fluentform') |
| 226 |
], 200); |
| 227 |
} |
| 228 |
|
| 229 |
public function deleteApiLogsByIds($ids = []) |
| 230 |
{ |
| 231 |
if(!$ids) { |
| 232 |
$ids = wp_unslash($_REQUEST['log_ids']); |
| 233 |
} |
| 234 |
|
| 235 |
if(!$ids) { |
| 236 |
wp_send_json_error([ |
| 237 |
'message' => 'No selections found' |
| 238 |
], 423); |
| 239 |
} |
| 240 |
|
| 241 |
wpFluent()->table('ff_scheduled_actions') |
| 242 |
->whereIn('id', $ids) |
| 243 |
->delete(); |
| 244 |
|
| 245 |
wp_send_json_success([ |
| 246 |
'message' => __('Selected logs successfully deleted', 'fluentform') |
| 247 |
], 200); |
| 248 |
} |
| 249 |
} |
| 250 |
|