| 1 |
<?php namespace FluentForm\App\Modules\Logger; |
| 2 |
|
| 3 |
use FluentForm\App\Databases\Migrations\FormLogs; |
| 4 |
use FluentForm\App\Modules\Form\FormDataParser; |
| 5 |
use FluentForm\App\Modules\Form\FormFieldsParser; |
| 6 |
use FluentForm\Framework\Foundation\Application; |
| 7 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 8 |
|
| 9 |
class DataLogger |
| 10 |
{ |
| 11 |
public $app; |
| 12 |
|
| 13 |
public function __construct(Application $app) |
| 14 |
{ |
| 15 |
$this->app = $app; |
| 16 |
} |
| 17 |
|
| 18 |
public function getLogFilters() |
| 19 |
{ |
| 20 |
$statuses = wpFluent()->table('fluentform_logs') |
| 21 |
->select('status') |
| 22 |
->groupBy('status') |
| 23 |
->get(); |
| 24 |
$formattedStatuses = []; |
| 25 |
|
| 26 |
foreach ($statuses as $status) { |
| 27 |
$formattedStatuses[] = $status->status; |
| 28 |
} |
| 29 |
|
| 30 |
$apis = wpFluent()->table('ff_scheduled_actions') |
| 31 |
->select('status') |
| 32 |
->groupBy('status') |
| 33 |
->get(); |
| 34 |
|
| 35 |
$apiStatuses = []; |
| 36 |
foreach ($apis as $api) { |
| 37 |
$apiStatuses[] = $api->status; |
| 38 |
} |
| 39 |
|
| 40 |
$components = wpFluent()->table('fluentform_logs') |
| 41 |
->select('component') |
| 42 |
->groupBy('component') |
| 43 |
->get(); |
| 44 |
|
| 45 |
$formattedComponents = []; |
| 46 |
foreach ($components as $component) { |
| 47 |
$formattedComponents[] = $component->component; |
| 48 |
} |
| 49 |
|
| 50 |
$forms = wpFluent()->table('fluentform_logs') |
| 51 |
->select('fluentform_logs.parent_source_id', 'fluentform_forms.title') |
| 52 |
->groupBy('fluentform_logs.parent_source_id') |
| 53 |
->orderBy('fluentform_logs.parent_source_id', 'DESC') |
| 54 |
->join('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_logs.parent_source_id') |
| 55 |
->get(); |
| 56 |
|
| 57 |
$formattedForms = []; |
| 58 |
|
| 59 |
foreach ($forms as $form) { |
| 60 |
$formattedForms[] = [ |
| 61 |
'form_id' => $form->parent_source_id, |
| 62 |
'title' => $form->title |
| 63 |
];; |
| 64 |
} |
| 65 |
|
| 66 |
wp_send_json_success([ |
| 67 |
'available_statuses' => $formattedStatuses, |
| 68 |
'available_components' => $formattedComponents, |
| 69 |
'available_forms' => $formattedForms, |
| 70 |
'api_statuses' => $apiStatuses |
| 71 |
]); |
| 72 |
} |
| 73 |
|
| 74 |
public function log($data) |
| 75 |
{ |
| 76 |
if (!$data) { |
| 77 |
return; |
| 78 |
} |
| 79 |
$data['created_at'] = current_time('mysql'); |
| 80 |
|
| 81 |
if (!get_option('fluentform_db_fluentform_logs_added')) { |
| 82 |
FormLogs::migrate(); |
| 83 |
} |
| 84 |
|
| 85 |
return wpFluent()->table('fluentform_logs') |
| 86 |
->insert($data); |
| 87 |
} |
| 88 |
|
| 89 |
public function getLogsByEntry($entry_id, $log_type = 'logs', $sourceType = 'submission_item') |
| 90 |
{ |
| 91 |
if ($log_type == 'logs') { |
| 92 |
$logs = wpFluent()->table('fluentform_logs') |
| 93 |
->where('source_id', $entry_id) |
| 94 |
->where('source_type', $sourceType) |
| 95 |
->orderBy('id', 'DESC') |
| 96 |
->get(); |
| 97 |
|
| 98 |
$logs = apply_filters('fluentform_entry_logs', $logs, $entry_id); |
| 99 |
} else { |
| 100 |
$logs = wpFluent()->table('ff_scheduled_actions') |
| 101 |
->select([ |
| 102 |
'id', |
| 103 |
'action', |
| 104 |
'status', |
| 105 |
'note', |
| 106 |
'created_at' |
| 107 |
]) |
| 108 |
->where('origin_id', $entry_id) |
| 109 |
->orderBy('id', 'DESC') |
| 110 |
->get(); |
| 111 |
|
| 112 |
$logs = apply_filters('fluentform_entry_api_logs', $logs, $entry_id); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
wp_send_json_success([ |
| 117 |
'logs' => $logs |
| 118 |
], 200); |
| 119 |
} |
| 120 |
|
| 121 |
public function getAllLogs() |
| 122 |
{ |
| 123 |
$limit = intval($_REQUEST['per_page']); |
| 124 |
$pageNumber = intval($_REQUEST['page_number']); |
| 125 |
|
| 126 |
$skip = ($pageNumber - 1) * $limit; |
| 127 |
|
| 128 |
global $wpdb; |
| 129 |
$logsQuery = wpFluent()->table('fluentform_logs') |
| 130 |
->select([ |
| 131 |
'fluentform_logs.*' |
| 132 |
]) |
| 133 |
->select(wpFluent()->raw($wpdb->prefix . 'fluentform_forms.title as form_title')) |
| 134 |
->select(wpFluent()->raw($wpdb->prefix . 'fluentform_logs.parent_source_id as form_id')) |
| 135 |
->select(wpFluent()->raw($wpdb->prefix . 'fluentform_logs.source_id as entry_id')) |
| 136 |
->leftJoin('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_logs.parent_source_id') |
| 137 |
->orderBy('fluentform_logs.id', 'DESC'); |
| 138 |
// ->whereIn('fluentform_logs.source_type', ['submission_item', 'form_item']); |
| 139 |
|
| 140 |
|
| 141 |
if ($parentSourceId = ArrayHelper::get($_REQUEST, 'parent_source_id')) { |
| 142 |
$logsQuery = $logsQuery->where('fluentform_logs.parent_source_id', intval($parentSourceId)); |
| 143 |
} |
| 144 |
|
| 145 |
if ($status = ArrayHelper::get($_REQUEST, 'status')) { |
| 146 |
$logsQuery = $logsQuery->where('fluentform_logs.status', sanitize_text_field($status)); |
| 147 |
} |
| 148 |
|
| 149 |
if ($component = ArrayHelper::get($_REQUEST, 'component')) { |
| 150 |
$logsQuery = $logsQuery->where('fluentform_logs.component', sanitize_text_field($component)); |
| 151 |
} |
| 152 |
|
| 153 |
if ($formId = ArrayHelper::get($_REQUEST, 'form_id')) { |
| 154 |
$logsQuery = $logsQuery->where('fluentform_forms.id', intval($formId)); |
| 155 |
} |
| 156 |
|
| 157 |
$logsQueryMain = $logsQuery; |
| 158 |
|
| 159 |
$logs = $logsQuery->offset($skip) |
| 160 |
->limit($limit) |
| 161 |
->get(); |
| 162 |
|
| 163 |
foreach ($logs as $log) { |
| 164 |
if($log->source_type == 'submission_item' && $log->entry_id) { |
| 165 |
$log->submission_url = admin_url('admin.php?page=fluent_forms&route=entries&form_id=' . $log->form_id . '#/entries/' . $log->entry_id); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
$logs = apply_filters('fluentform_all_logs', $logs); |
| 170 |
|
| 171 |
$total = $logsQueryMain->count(); |
| 172 |
|
| 173 |
wp_send_json_success([ |
| 174 |
'logs' => $logs, |
| 175 |
'total' => $total |
| 176 |
], 200); |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
public function getApiLogs() |
| 182 |
{ |
| 183 |
$limit = intval($_REQUEST['per_page']); |
| 184 |
$pageNumber = intval($_REQUEST['page_number']); |
| 185 |
|
| 186 |
$skip = ($pageNumber - 1) * $limit; |
| 187 |
global $wpdb; |
| 188 |
$logsQuery = wpFluent()->table('ff_scheduled_actions') |
| 189 |
->select([ |
| 190 |
'ff_scheduled_actions.id', |
| 191 |
'ff_scheduled_actions.action', |
| 192 |
'ff_scheduled_actions.form_id', |
| 193 |
'ff_scheduled_actions.origin_id', |
| 194 |
'ff_scheduled_actions.status', |
| 195 |
'ff_scheduled_actions.note', |
| 196 |
'ff_scheduled_actions.created_at', |
| 197 |
]) |
| 198 |
->select(wpFluent()->raw($wpdb->prefix . 'fluentform_forms.title as form_title')) |
| 199 |
->join('fluentform_forms', 'fluentform_forms.id', '=', 'ff_scheduled_actions.form_id') |
| 200 |
->orderBy('ff_scheduled_actions.id', 'DESC'); |
| 201 |
|
| 202 |
|
| 203 |
if ($formId = ArrayHelper::get($_REQUEST, 'form_id')) { |
| 204 |
$logsQuery = $logsQuery->where('ff_scheduled_actions.form_id', intval($formId)); |
| 205 |
} |
| 206 |
|
| 207 |
if ($status = ArrayHelper::get($_REQUEST, 'status')) { |
| 208 |
$logsQuery = $logsQuery->where('ff_scheduled_actions.status', $status); |
| 209 |
} |
| 210 |
|
| 211 |
if ($component = ArrayHelper::get($_REQUEST, 'component')) { |
| 212 |
$logsQuery = $logsQuery->where('ff_scheduled_actions.action', $component); |
| 213 |
} |
| 214 |
|
| 215 |
$logsQueryMain = $logsQuery; |
| 216 |
|
| 217 |
$logs = $logsQuery->offset($skip) |
| 218 |
->limit($limit) |
| 219 |
->get(); |
| 220 |
|
| 221 |
$logs = apply_filters('fluentform_api_all_logs', $logs); |
| 222 |
|
| 223 |
foreach ($logs as $log) { |
| 224 |
$log->submission_url = admin_url('admin.php?page=fluent_forms&route=entries&form_id=' . $log->form_id . '#/entries/' . $log->origin_id); |
| 225 |
} |
| 226 |
|
| 227 |
$total = $logsQueryMain->count(); |
| 228 |
|
| 229 |
wp_send_json_success([ |
| 230 |
'logs' => $logs, |
| 231 |
'total' => $total |
| 232 |
], 200); |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
public function deleteLogsByIds($ids = []) |
| 237 |
{ |
| 238 |
if (!$ids) { |
| 239 |
$ids = wp_unslash($_REQUEST['log_ids']); |
| 240 |
} |
| 241 |
|
| 242 |
if (!$ids) { |
| 243 |
wp_send_json_error([ |
| 244 |
'message' => 'No selections found' |
| 245 |
], 423); |
| 246 |
} |
| 247 |
|
| 248 |
wpFluent()->table('fluentform_logs') |
| 249 |
->whereIn('id', $ids) |
| 250 |
->delete(); |
| 251 |
|
| 252 |
wp_send_json_success([ |
| 253 |
'message' => __('Selected log(s) successfully deleted', 'fluentform') |
| 254 |
], 200); |
| 255 |
} |
| 256 |
|
| 257 |
public function deleteApiLogsByIds($ids = []) |
| 258 |
{ |
| 259 |
if (!$ids) { |
| 260 |
$ids = wp_unslash($_REQUEST['log_ids']); |
| 261 |
} |
| 262 |
|
| 263 |
if (!$ids) { |
| 264 |
wp_send_json_error([ |
| 265 |
'message' => 'No selections found' |
| 266 |
], 423); |
| 267 |
} |
| 268 |
|
| 269 |
wpFluent()->table('ff_scheduled_actions') |
| 270 |
->whereIn('id', $ids) |
| 271 |
->delete(); |
| 272 |
|
| 273 |
wp_send_json_success([ |
| 274 |
'message' => __('Selected log(s) successfully deleted', 'fluentform') |
| 275 |
], 200); |
| 276 |
} |
| 277 |
|
| 278 |
public function retryApiAction() |
| 279 |
{ |
| 280 |
$logId = $this->app->request->get('log_id'); |
| 281 |
$actionFeed = wpFluent()->table('ff_scheduled_actions') |
| 282 |
->find($logId); |
| 283 |
|
| 284 |
if (!$actionFeed) { |
| 285 |
wp_send_json_error([ |
| 286 |
'message' => 'API log does not exist' |
| 287 |
], 423); |
| 288 |
} |
| 289 |
|
| 290 |
if (!$actionFeed->status == 'success') { |
| 291 |
wp_send_json_error([ |
| 292 |
'message' => 'API log already in success mode' |
| 293 |
], 423); |
| 294 |
} |
| 295 |
|
| 296 |
$form = wpFluent()->table('fluentform_forms')->find($actionFeed->form_id); |
| 297 |
|
| 298 |
$feed = maybe_unserialize($actionFeed->data); |
| 299 |
$feed['scheduled_action_id'] = $actionFeed->id; |
| 300 |
|
| 301 |
$submission = wpFluent()->table('fluentform_submissions')->find($actionFeed->origin_id); |
| 302 |
$entry = $this->getEntry($submission, $form); |
| 303 |
$formData = json_decode($submission->response, true); |
| 304 |
|
| 305 |
wpFluent()->table($this->table) |
| 306 |
->where('id', $actionFeed->id) |
| 307 |
->update([ |
| 308 |
'status' => 'manual_retry', |
| 309 |
'retry_count' => $actionFeed->retry_count + 1, |
| 310 |
'updated_at' => current_time('mysql') |
| 311 |
]); |
| 312 |
|
| 313 |
do_action($actionFeed->action, $feed, $formData, $entry, $form); |
| 314 |
|
| 315 |
/* |
| 316 |
* Hopefully it's done |
| 317 |
*/ |
| 318 |
$actionFeed = wpFluent()->table('ff_scheduled_actions') |
| 319 |
->find($logId); |
| 320 |
|
| 321 |
wp_send_json_success([ |
| 322 |
'message' => 'Retry completed', |
| 323 |
'feed' => $actionFeed |
| 324 |
], 200); |
| 325 |
} |
| 326 |
|
| 327 |
private function getEntry($submission, $form) |
| 328 |
{ |
| 329 |
$formInputs = FormFieldsParser::getEntryInputs($form, ['admin_label', 'raw']); |
| 330 |
return FormDataParser::parseFormEntry($submission, $form, $formInputs); |
| 331 |
} |
| 332 |
|
| 333 |
public function getApiLogFilters() |
| 334 |
{ |
| 335 |
$apis = wpFluent()->table('ff_scheduled_actions') |
| 336 |
->select('status') |
| 337 |
->groupBy('status') |
| 338 |
->get(); |
| 339 |
|
| 340 |
$apiStatuses = []; |
| 341 |
foreach ($apis as $api) { |
| 342 |
$apiStatuses[] = $api->status; |
| 343 |
} |
| 344 |
|
| 345 |
$components = wpFluent()->table('ff_scheduled_actions') |
| 346 |
->select('action') |
| 347 |
->groupBy('action') |
| 348 |
->get(); |
| 349 |
|
| 350 |
$formattedComponents = []; |
| 351 |
foreach ($components as $component) { |
| 352 |
$formattedComponents[] = $component->action; |
| 353 |
} |
| 354 |
|
| 355 |
$forms = wpFluent()->table('ff_scheduled_actions') |
| 356 |
->select('ff_scheduled_actions.form_id', 'fluentform_forms.title') |
| 357 |
->groupBy('ff_scheduled_actions.form_id') |
| 358 |
->orderBy('ff_scheduled_actions.form_id', 'DESC') |
| 359 |
->join('fluentform_forms', 'fluentform_forms.id', '=', 'ff_scheduled_actions.form_id') |
| 360 |
->get(); |
| 361 |
|
| 362 |
$formattedForms = []; |
| 363 |
|
| 364 |
foreach ($forms as $form) { |
| 365 |
$formattedForms[] = [ |
| 366 |
'form_id' => $form->form_id, |
| 367 |
'title' => $form->title |
| 368 |
];; |
| 369 |
} |
| 370 |
|
| 371 |
wp_send_json_success([ |
| 372 |
'available_components' => $formattedComponents, |
| 373 |
'available_forms' => $formattedForms, |
| 374 |
'api_statuses' => $apiStatuses |
| 375 |
]); |
| 376 |
} |
| 377 |
} |
| 378 |
|