PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.13
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.13
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 3.6.66 All 195 releases
fluentform / app / Modules / Entries / Entries.php

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

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