PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.6
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.6
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Modules / Entries / Entries.php

Entries.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 4.3.6, at app/Modules/Entries/Entries.php

907 lines 29.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Modules\Entries;
4
5 use FluentForm\App\Helpers\Helper;
6 use FluentForm\App\Modules\Form\FormDataParser;
7 use FluentForm\App\Modules\Form\FormFieldsParser;
8 use FluentForm\Framework\Helpers\ArrayHelper;
9 use FluentForm\View;
10
11 class Entries extends EntryQuery
12 {
13 /**
14 * The form response model.
15 *
16 * @var \WpFluent\QueryBuilder\QueryBuilderHandler $responseMetaModel
17 */
18 protected $responseMetaModel;
19
20 /**
21 * Entries constructor.
22 *
23 * @throws \Exception
24 */
25 public function __construct()
26 {
27 parent::__construct();
28
29 $this->responseMetaModel = wpFluent()->table('fluentform_submission_meta');
30 }
31
32 public function getAllFormEntries()
33 {
34 $formId = intval($this->request->get('form_id'));
35
36
37 $limit = $this->request->get('per_page', 10);
38 $page = $this->request->get('page', 1);
39 $offset = ($page - 1) * $limit;
40
41 $search = $this->request->get('search');
42 $status = $this->request->get('entry_status');
43
44 $query = wpFluent()->table('fluentform_submissions')
45 ->select([
46 'fluentform_submissions.id',
47 'fluentform_submissions.form_id',
48 'fluentform_submissions.status',
49 'fluentform_submissions.created_at',
50 'fluentform_submissions.browser',
51 'fluentform_submissions.currency',
52 'fluentform_submissions.total_paid',
53 'fluentform_forms.title',
54 ])
55 ->join('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_submissions.form_id')
56 ->orderBy('fluentform_submissions.id', 'DESC')
57 ->limit($limit)
58 ->offset($offset);
59
60 if ($formId) {
61 $query->where('fluentform_submissions.form_id', $formId);
62 }
63
64 if ($status) {
65 $query->where('fluentform_submissions.status', $status);
66 } else {
67 $query->where('fluentform_submissions.status', '!=', 'trashed');
68 }
69
70
71 $dateRange = $this->request->get('date_range');
72 if ($dateRange) {
73 $query->where('fluentform_submissions.created_at', '>=', $dateRange[0] . ' 00:00:01');
74 $query->where('fluentform_submissions.created_at', '<=', $dateRange[1] . ' 23:59:59');
75 }
76
77 if ($search) {
78 $query->where('fluentform_submissions.response', 'LIKE', '%' . $search . '%');
79 $query->orWhere('fluentform_forms.title', 'LIKE', '%' . $search . '%');
80 $query->orWhere('fluentform_submissions.id', 'LIKE', '%' . $search . '%');
81 }
82
83 $total = $query->count();
84 $entries = $query->get();
85 foreach ($entries as $entry) {
86 $entry->entry_url = admin_url('admin.php?page=fluent_forms&route=entries&form_id=' . $entry->form_id . '#/entries/' . $entry->id);
87 $entry->human_date = human_time_diff(strtotime($entry->created_at), strtotime(current_time('mysql')));
88 }
89 wp_send_json_success([
90 'entries' => $entries,
91 'total' => $total,
92 'last_page' => ceil($total / $limit),
93 'available_forms' => $this->getAvailableForms()
94 ]);
95 }
96
97 public function getEntriesReport()
98 {
99 $from = date('Y-m-d H:i:s', strtotime('-30 days'));
100 $to = date('Y-m-d H:i:s', strtotime('+1 days'));
101
102 $ranges = $this->request->get('date_range', []);
103
104 if (!empty($ranges[0])) {
105 $from = $ranges[0];
106 }
107
108 if (!empty($ranges[1])) {
109 $time = strtotime($ranges[1]) + 24 * 60 * 60;
110 $to = date('Y-m-d H:i:s', $time);
111
112 }
113
114
115 $period = new \DatePeriod(new \DateTime($from), new \DateInterval('P1D'), new \DateTime($to));
116
117 $range = [];
118
119 foreach ($period as $date) {
120 $range[$date->format('Y-m-d')] = 0;
121 }
122
123 $itemsQuery = wpFluent()->table('fluentform_submissions')->select([
124 wpFluent()->raw('DATE(created_at) AS date'),
125 wpFluent()->raw('COUNT(id) AS count'),
126 ])
127 ->whereBetween('created_at', $from, $to)
128 ->groupBy('date')
129 ->orderBy('date', 'ASC');
130
131 $formId = $this->request->get('form_id');
132
133 if ($formId) {
134 $itemsQuery = $itemsQuery->where('form_id', $formId);
135 }
136
137 $items = $itemsQuery->get();
138 foreach ($items as $item) {
139 $range[$item->date] = $item->count; //Filling value in the array
140 }
141
142 wp_send_json_success([
143 'stats' => $range
144 ]);
145
146 }
147
148 public function renderEntries($form_id)
149 {
150 wp_enqueue_script('fluentform_form_entries');
151
152 $forms = wpFluent()
153 ->table('fluentform_forms')
154 ->select(['id', 'title'])
155 ->orderBy('id', 'DESC')
156 ->get();
157
158 $emailNotifications = wpFluent()
159 ->table('fluentform_form_meta')
160 ->where('form_id', $form_id)
161 ->where('meta_key', 'notifications')
162 ->get();
163
164 $formattedNotification = [];
165
166 foreach ($emailNotifications as $notification) {
167 $value = \json_decode($notification->value, true);
168 $formattedNotification[] = [
169 'id' => $notification->id,
170 'name' => ArrayHelper::get($value, 'name')
171 ];
172 }
173
174 $form = wpFluent()->table('fluentform_forms')->find($form_id);
175
176
177 $app = wpFluentForm();
178
179 $fluentFormEntriesVars = apply_filters('fluent_form_entries_vars', [
180 'all_forms_url' => admin_url('admin.php?page=fluent_forms'),
181 'forms' => $forms,
182 'form_id' => $form->id,
183 'enabled_auto_delete' => Helper::isEntryAutoDeleteEnabled($form_id),
184 'current_form_title' => $form->title,
185 'entry_statuses' => Helper::getEntryStatuses($form_id),
186 'entries_url_base' => admin_url('admin.php?page=fluent_forms&route=entries&form_id='),
187 'no_found_text' => __('Sorry! No entries found. All your entries will be shown here once you start getting form submissions', 'fluentform'),
188 'has_pro' => defined('FLUENTFORMPRO'),
189 'printStyles' => [fluentformMix('css/settings_global.css')],
190 'email_notifications' => $formattedNotification,
191 'available_countries' => $app->load(
192 $app->appPath('Services/FormBuilder/CountryNames.php')
193 ),
194 'upgrade_url' => fluentform_upgrade_url()
195 ], $form);
196
197 wp_localize_script(
198 'fluentform_form_entries', 'fluent_form_entries_vars', $fluentFormEntriesVars
199 );
200
201 View::render('admin.form.entries', [
202 'form_id' => $form_id,
203 'has_pdf' => defined('FLUENTFORM_PDF_VERSION') ? 'true' : 'false'
204 ]);
205 }
206
207 public function getEntriesGroup()
208 {
209 $formId = intval($this->request->get('form_id'));
210 $counts = $this->groupCount($formId);
211 wp_send_json_success([
212 'counts' => $counts
213 ], 200);
214 }
215
216 public function _getEntries(
217 $formId,
218 $currentPage,
219 $perPage,
220 $sortBy,
221 $entryType,
222 $search,
223 $wheres = []
224 )
225 {
226 $this->formId = $formId;
227 $this->per_page = $perPage;
228 $this->sort_by = $sortBy;
229 $this->page_number = $currentPage;
230 $this->search = $search;
231 $this->wheres = $wheres;
232
233 if ($entryType == 'favorite') {
234 $this->is_favourite = true;
235 } elseif ($entryType != 'all' && $entryType) {
236 $this->status = $entryType;
237 }
238
239 $dateRange = $this->request->get('date_range');
240 if ($dateRange) {
241 $this->startDate = $dateRange[0];
242 $this->endDate = $dateRange[1];
243 }
244
245 $form = $this->formModel->find($formId);
246 $formMeta = $this->getFormInputsAndLabels($form);
247 $formLabels = $formMeta['labels'];
248 $formLabels = apply_filters('fluentfoform_entry_lists_labels', $formLabels, $form);
249 $submissions = $this->getResponses();
250 $submissions['data'] = FormDataParser::parseFormEntries($submissions['data'], $form);
251
252 return compact('submissions', 'formLabels');
253 }
254
255 public function getEntries()
256 {
257 if (!defined('FLUENTFORM_RENDERING_ENTRIES')) {
258 define('FLUENTFORM_RENDERING_ENTRIES', true);
259 }
260
261 $wheres = [];
262
263 if ($paymentStatuses = $this->request->get('payment_statuses')) {
264 if (is_array($paymentStatuses)) {
265 $wheres[] = ['payment_status', $paymentStatuses];
266 }
267 }
268
269 $entries = $this->_getEntries(
270 intval($this->request->get('form_id')),
271 intval($this->request->get('current_page', 1)),
272 intval($this->request->get('per_page', 10)),
273 sanitize_text_field($this->request->get('sort_by', 'DESC')),
274 sanitize_text_field($this->request->get('entry_type', 'all')),
275 sanitize_text_field($this->request->get('search')),
276 $wheres
277 );
278
279
280 $labels = apply_filters(
281 'fluentform_all_entry_labels', $entries['formLabels'], $this->request->get('form_id')
282 );
283
284 $form = $this->formModel->find($this->request->get('form_id'));
285
286 if ($form->has_payment) {
287 $labels = apply_filters(
288 'fluentform_all_entry_labels_with_payment', $entries['formLabels'], false, $form
289 );
290 }
291 $formId = $this->request->get('form_id');
292 $visible_columns = Helper::getFormMeta($formId, '_visible_columns', NULL);
293 $columns_order = Helper::getFormMeta($formId, '_columns_order', NULL);
294
295 wp_send_json_success([
296 'submissions' => apply_filters('fluentform_all_entries', $entries['submissions']),
297 'labels' => $labels,
298 'visible_columns' => $visible_columns,
299 'columns_order' => $columns_order
300 ], 200);
301 }
302
303 public function _getEntry()
304 {
305 $this->formId = intval($this->request->get('form_id'));
306
307 $entryId = intval($this->request->get('entry_id'));
308
309 $entry_type = sanitize_key($this->request->get('entry_type', 'all'));
310
311 if ($entry_type === 'favorite') {
312 $this->is_favourite = true;
313 } elseif ($entry_type !== 'all') {
314 $this->status = $entry_type;
315 }
316
317 $this->sort_by = sanitize_text_field($this->request->get('sort_by', 'ASC'));
318
319 $this->search = sanitize_text_field($this->request->get('search'));
320
321 $submission = $this->getResponse($entryId);
322
323 if (!$submission) {
324 wp_send_json_error([
325 'message' => 'No Entry found.'
326 ], 422);
327 }
328
329 $form = $this->formModel->find($this->formId);
330
331 if ($submission->status == 'unread' && apply_filters('fluentform_auto_read', true, $form)) {
332 wpFluent()->table('fluentform_submissions')
333 ->where('id', $entryId)
334 ->update([
335 'status' => 'read'
336 ]);
337
338 $submission->status = 'read';
339 }
340
341 $formMeta = $this->getFormInputsAndLabels($form);
342
343 $submission = FormDataParser::parseFormEntry($submission, $form, $formMeta['inputs'], true);
344
345 if ($submission->user_id) {
346 $user = get_user_by('ID', $submission->user_id);
347 $user_data = [
348 'name' => $user->display_name,
349 'email' => $user->user_email,
350 'ID' => $user->ID,
351 'permalink' => get_edit_user_link($user->ID)
352 ];
353 $submission->user = $user_data;
354 }
355
356 $submission = apply_filters('fluentform_single_response_data', $submission, $this->formId);
357
358 $fields = apply_filters(
359 'fluentform_single_response_input_fields', $formMeta['inputs'], $this->formId
360 );
361
362 $labels = apply_filters(
363 'fluentform_single_response_input_labels', $formMeta['labels'], $this->formId
364 );
365
366 $order_data = false;
367
368 if ($submission->payment_status || $submission->payment_total || $submission->payment_type === 'subscription') {
369 $order_data = apply_filters(
370 'fluentform_submission_order_data', false, $submission, $form
371 );
372
373 $labels = apply_filters(
374 'fluentform_submission_entry_labels_with_payment', $labels, $submission, $form
375 );
376 }
377
378 $nextSubmissionId = $this->getNextResponse($entryId);
379
380 $previousSubmissionId = $this->getPrevResponse($entryId);
381
382 return [
383 'submission' => $submission,
384 'next' => $nextSubmissionId,
385 'prev' => $previousSubmissionId,
386 'labels' => $labels,
387 'fields' => $fields,
388 'order_data' => $order_data
389 ];
390 }
391
392 public function getEntry()
393 {
394 $entryData = $this->_getEntry();
395
396 $entryData['widgets'] = apply_filters('fluentform_single_entry_widgets', [], $entryData);
397 $entryData['extraCards'] = apply_filters('fluentform_single_entry_cards', [], $entryData);
398
399 wp_send_json_success($entryData, 200);
400 }
401
402 /**
403 * @param $form
404 * @param array $with
405 *
406 * @return array
407 * @todo: Implement Caching mechanism so we don't have to parse these things for every request
408 */
409 public function getFormInputsAndLabels($form, $with = ['admin_label', 'raw'])
410 {
411 $formInputs = FormFieldsParser::getEntryInputs($form, $with);
412 $inputLabels = FormFieldsParser::getAdminLabels($form, $formInputs);
413 return [
414 'inputs' => $formInputs,
415 'labels' => $inputLabels
416 ];
417 }
418
419 public function getNotes()
420 {
421 $formId = intval($this->request->get('form_id'));
422 $entry_id = intval($this->request->get('entry_id'));
423 $apiLog = sanitize_text_field($this->request->get('api_log')) == 'yes';
424
425 $metaKeys = ['_notes'];
426
427 if ($apiLog) {
428 $metaKeys[] = 'api_log';
429 }
430
431 $notes = $this->responseMetaModel
432 ->where('form_id', $formId)
433 ->where('response_id', $entry_id)
434 ->whereIn('meta_key', $metaKeys)
435 ->orderBy('id', 'DESC')
436 ->get();
437
438 foreach ($notes as $note) {
439 if ($note->user_id) {
440 $note->pemalink = get_edit_user_link($note->user_id);
441 $user = get_user_by('ID', $note->user_id);
442 if ($user) {
443 $note->created_by = $user->display_name;
444 } else {
445 $note->created_by = __('Fluent Forms Bot', 'fluentform');
446 }
447 } else {
448 $note->pemalink = false;
449 }
450 }
451
452 $notes = apply_filters('fluentform_entry_notes', $notes, $entry_id, $formId);
453
454 wp_send_json_success([
455 'notes' => $notes
456 ], 200);
457 }
458
459 public function addNote()
460 {
461 $entryId = intval($this->request->get('entry_id'));
462 $formId = intval($this->request->get('form_id'));
463 $note = $this->request->get('note');
464 $note_content = sanitize_textarea_field($note['content']);
465 $note_status = sanitize_text_field($note['status']);
466 $user = get_user_by('ID', get_current_user_id());
467
468 $response_note = [
469 'response_id' => $entryId,
470 'form_id' => $formId,
471 'meta_key' => '_notes',
472 'value' => $note_content,
473 'status' => $note_status,
474 'user_id' => $user->ID,
475 'name' => $user->display_name,
476 'created_at' => current_time('mysql'),
477 'updated_at' => current_time('mysql')
478 ];
479
480 $response_note = apply_filters('fluentform_add_response_note', $response_note);
481
482 $insertId = $this->responseMetaModel->insert($response_note);
483
484 $added_note = $this->responseMetaModel->find($insertId);
485
486 do_action('fluentform_new_response_note_added', $insertId, $added_note);
487
488 wp_send_json_success([
489 'message' => __('Note has been successfully added', 'fluentform'),
490 'note' => $added_note,
491 'insert_id' => $insertId
492 ], 200);
493 }
494
495 public function changeEntryStatus()
496 {
497 $formId = intval($this->request->get('form_id'));
498 $entryId = intval($this->request->get('entry_id'));
499 $newStatus = sanitize_text_field($this->request->get('status'));
500
501 $this->responseModel
502 ->where('form_id', $formId)
503 ->where('id', $entryId)
504 ->update(['status' => $newStatus]);
505
506 wp_send_json_success([
507 'message' => __('Item has been marked as ' . $newStatus, 'fluentform'),
508 'status' => $newStatus
509 ], 200);
510 }
511
512 public function deleteEntry()
513 {
514 $formId = intval($this->request->get('form_id'));
515 $entryId = intval($this->request->get('entry_id'));
516 $newStatus = sanitize_text_field($this->request->get('status'));
517
518 $this->deleteEntryById($entryId, $formId);
519
520 wp_send_json_success([
521 'message' => __('Item Successfully deleted', 'fluentform'),
522 'status' => $newStatus
523 ], 200);
524 }
525
526 public function deleteEntryById($entryId, $formId = false)
527 {
528 do_action('fluentform_before_entry_deleted', $entryId, $formId);
529
530 $disableAttachmentDelete = apply_filters('fluentform_disable_attachment_delete', false, $formId);
531 if (defined('FLUENTFORMPRO') && $formId && !$disableAttachmentDelete) {
532 if (is_numeric($formId)) {
533 $form = wpFluent()->table('fluentform_forms')->find($formId);
534 } else {
535 $form = $formId;
536 }
537 $deletableFiles = $this->getSubmissionAttachments($entryId, $form);
538 if ($deletableFiles) {
539 foreach ($deletableFiles as $eachFile) {
540 $file = wp_upload_dir()['basedir'] . FLUENTFORM_UPLOAD_DIR . '/' . basename($eachFile);
541 if (is_readable($file) && !is_dir($file)) {
542 @unlink($file);
543 }
544 }
545 }
546 }
547
548 wpFluent()->table('fluentform_submissions')
549 ->where('id', $entryId)
550 ->delete();
551 wpFluent()->table('fluentform_submission_meta')
552 ->where('response_id', $entryId)
553 ->delete();
554
555 wpFluent()->table('fluentform_logs')
556 ->where('source_id', $entryId)
557 ->where('source_type', 'submission_item')
558 ->delete();
559
560 wpFluent()->table('fluentform_entry_details')
561 ->where('submission_id', $entryId)
562 ->delete();
563
564 ob_start();
565 if (defined('FLUENTFORMPRO')) {
566 try {
567 wpFluent()->table('fluentform_order_items')
568 ->where('submission_id', $entryId)
569 ->delete();
570
571
572 wpFluent()->table('fluentform_transactions')
573 ->where('submission_id', $entryId)
574 ->delete();
575
576 wpFluent()->table('fluentform_subscriptions')
577 ->where('submission_id', $entryId)
578 ->delete();
579
580 wpFluent()->table('ff_scheduled_actions')
581 ->where('origin_id', $entryId)
582 ->where('type', 'submission_action')
583 ->delete();
584
585 } catch (\Exception $exception) {
586 // ...
587 }
588 }
589
590
591 $errors = ob_get_clean();
592
593
594 do_action('fluentform_after_entry_deleted', $entryId, $formId);
595
596 return true;
597 }
598
599 private function getSubmissionAttachments($submissionId, $form)
600 {
601 $fields = FormFieldsParser::getAttachmentInputFields($form, ['element', 'attributes']);
602
603 $deletableFiles = [];
604 if ($fields) {
605 $submission = wpFluent()->table('fluentform_submissions')
606 ->where('id', $submissionId)
607 ->first();
608
609 $data = json_decode($submission->response, true);
610
611 foreach ($fields as $field) {
612 if (!empty($data[$field['attributes']['name']])) {
613 $files = $data[$field['attributes']['name']];
614 if (is_array($files)) {
615 $deletableFiles = array_merge($deletableFiles, $files);
616 } else {
617 $deletableFiles = $files;
618 }
619
620 }
621 }
622 }
623
624 return $deletableFiles;
625 }
626
627 public function favoriteChange()
628 {
629 $formId = intval($this->request->get('form_id'));
630 $entryId = intval($this->request->get('entry_id'));
631 $newStatus = intval($this->request->get('is_favourite'));
632 if ($newStatus) {
633 $message = __('Item has been added to favorites', 'fluentform');
634 } else {
635 $message = __('Item has been removed from favorites', 'fluentform');
636 }
637 $this->responseModel
638 ->where('form_id', $formId)
639 ->where('id', $entryId)
640 ->update(['is_favourite' => $newStatus]);
641
642 wp_send_json_success([
643 'message' => $message,
644 'is_favourite' => $newStatus
645 ], 200);
646 }
647
648 public function handleBulkAction()
649 {
650 $formId = intval($this->request->get('form_id'));
651 $entries = fluentFormSanitizer($this->request->get('entries', []));
652
653 $actionType = sanitize_text_field($this->request->get('action_type'));
654
655 // check if it's status change or not
656 $statuses = Helper::getEntryStatuses($formId);
657
658 if (!$formId || !count($entries)) {
659 wp_send_json_error([
660 'message' => __('Please select entries first', 'fluentform')
661 ], 400);
662 }
663
664 $bulkQuery = wpFluent()->table('fluentform_submissions')
665 ->where('form_id', $formId)
666 ->whereIn('id', $entries);
667
668 if (isset($statuses[$actionType])) {
669 // it's status change
670 $bulkQuery->update([
671 'status' => $actionType,
672 'updated_at' => current_time('mysql')
673 ]);
674
675 wp_send_json_success([
676 'message' => 'Selected entries successfully marked as ' . $statuses[$actionType]
677 ], 200);
678 }
679
680 // now other action handler
681 if ($actionType == 'other.delete_permanently') {
682 $form = wpFluent()->table('fluentform_forms')->find($formId);
683 foreach ($entries as $entryId) {
684 $this->deleteEntryById($entryId, $form);
685 }
686 $message = __('Selected entries successfully deleted', 'fluentform');
687
688 } elseif ($actionType == 'other.make_favorite') {
689 $bulkQuery->update([
690 'is_favourite' => 1
691 ]);
692 $message = __('Selected entries successfully marked as favorites', 'fluentform');
693 } elseif ($actionType == 'other.unmark_favorite') {
694 $bulkQuery->update([
695 'is_favourite' => 0
696 ]);
697 $message = __('Selected entries successfully removed from favorites', 'fluentform');
698 }
699
700 wp_send_json_success([
701 'message' => $message
702 ], 200);
703 }
704
705 public function recordEntryDetails($entryId, $formId, $data)
706 {
707 $formData = ArrayHelper::except($data, [
708 '__fluent_form_embded_post_id',
709 '_fluentform_' . $formId . '_fluentformnonce',
710 '_wp_http_referer',
711 'g-recaptcha-response',
712 'h-captcha-response',
713 '__stripe_payment_method_id',
714 '__ff_all_applied_coupons',
715 '__entry_intermediate_hash'
716 ]);
717
718 $entryItems = [];
719 foreach ($formData as $dataKey => $dataValue) {
720 if (empty($dataValue)) {
721 continue;
722 }
723
724 if (is_array($dataValue) || is_object($dataValue)) {
725 foreach ($dataValue as $subKey => $subValue) {
726 if (empty($subValue)) {
727 continue;
728 }
729 $entryItems[] = [
730 'form_id' => $formId,
731 'submission_id' => $entryId,
732 'field_name' => trim($dataKey),
733 'sub_field_name' => $subKey,
734 'field_value' => maybe_serialize($subValue)
735 ];
736 }
737 } else {
738 $entryItems[] = [
739 'form_id' => $formId,
740 'submission_id' => $entryId,
741 'field_name' => trim($dataKey),
742 'sub_field_name' => '',
743 'field_value' => $dataValue
744 ];
745 }
746 }
747
748 foreach ($entryItems as $entryItem) {
749 wpFluent()->table('fluentform_entry_details')->insert($entryItem);
750 }
751
752 return true;
753 }
754
755 public function updateEntryDiffs($entryId, $formId, $formData)
756 {
757 wpFluent()->table('fluentform_entry_details')
758 ->where('submission_id', $entryId)
759 ->where('form_id', $formId)
760 ->whereIn('field_name', array_keys($formData))
761 ->delete();
762
763 $entryItems = [];
764 foreach ($formData as $dataKey => $dataValue) {
765 if (!$dataValue) {
766 continue;
767 }
768
769 if (is_array($dataValue)) {
770 foreach ($dataValue as $subKey => $subValue) {
771 $entryItems[] = [
772 'form_id' => $formId,
773 'submission_id' => $entryId,
774 'field_name' => $dataKey,
775 'sub_field_name' => $subKey,
776 'field_value' => maybe_serialize($subValue)
777 ];
778 }
779 } else {
780 $entryItems[] = [
781 'form_id' => $formId,
782 'submission_id' => $entryId,
783 'field_name' => $dataKey,
784 'sub_field_name' => '',
785 'field_value' => $dataValue
786 ];
787 }
788 }
789
790 foreach ($entryItems as $entryItem) {
791 wpFluent()->table('fluentform_entry_details')->insert($entryItem);
792 }
793
794 return true;
795 }
796
797 public function getUsers()
798 {
799 // if (!current_user_can('list_users')) {
800 // wp_send_json_error([
801 // 'message' => __('Sorry, You do not have permission to list users', 'fluentform')
802 // ]);
803 // }
804
805 $search = sanitize_text_field($this->request->get('search'));
806
807 $users = get_users([
808 'search' => "*{$search}*",
809 'number' => 50
810 ]);
811
812 $formattedUsers = [];
813
814 foreach ($users as $user) {
815 $formattedUsers[] = [
816 'ID' => $user->ID,
817 'label' => $user->display_name . ' - ' . $user->user_email
818 ];
819 }
820
821 wp_send_json_success([
822 'users' => $formattedUsers
823 ]);
824 }
825
826 public function changeEntryUser()
827 {
828 $userId = intval($this->request->get('user_id'));
829 $submissionId = intval($this->request->get('submission_id'));
830
831 if (!$userId || !$submissionId) {
832 wp_send_json_error([
833 'message' => __('Submission ID and User ID is required', 'fluentform')
834 ], 423);
835 }
836
837 $submission = fluentFormApi('submissions')->find($submissionId);
838
839 $user = get_user_by('ID', $userId);
840
841 if (!$submission || $submission->user_id == $userId || !$user) {
842 wp_send_json_error([
843 'message' => __('Invalid Request', 'fluentform')
844 ], 423);
845 }
846
847 wpFluent()->table('fluentform_submissions')
848 ->where('id', $submission->id)
849 ->update([
850 'user_id' => $userId,
851 'updated_at' => current_time('mysql')
852 ]);
853
854 if (defined('FLUENTFORMPRO')) {
855 // let's update the corresponding user IDs for transactions and
856 wpFluent()->table('fluentform_transactions')
857 ->where('submission_id', $submission->id)
858 ->update([
859 'user_id' => $userId,
860 'updated_at' => current_time('mysql')
861 ]);
862 }
863
864 do_action('ff_log_data', [
865 'parent_source_id' => $submission->form_id,
866 'source_type' => 'submission_item',
867 'source_id' => $submission->id,
868 'component' => 'General',
869 'status' => 'info',
870 'title' => 'Associate user has been changed from '.$submission->user_id .' to '.$userId
871 ]);
872
873 do_action('fluentform_submission_user_changed', $submission, $user);
874
875 wp_send_json_success([
876 'message' => __('Selected user has been successfully assigned to this submission', 'fuentform'),
877 'user' => [
878 'name' => $user->display_name,
879 'email' => $user->user_email,
880 'ID' => $user->ID,
881 'permalink' => get_edit_user_link($user->ID)
882 ],
883 'user_id' => $userId
884 ]);
885 }
886
887 public function getAvailableForms()
888 {
889
890 $forms = wpFluent()->table('fluentform_forms')
891 ->select(array('id', 'title'))
892 ->orderBy('id', 'DESC')
893 ->get();
894
895 $formattedForms = [];
896 foreach ($forms as $form) {
897 $formattedForms[] = [
898 'id' => $form->id,
899 'title' => $form->title
900 ];
901 }
902
903 return $formattedForms;
904
905 }
906 }
907