PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 5.2.9
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v5.2.9
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 5.2.9, at app/Modules/Entries/Entries.php

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