Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/fluentform/app/Modules/Entries/Entries.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
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 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is just passing URL parameter to frontend for display
200 + 'update_status' => isset($_REQUEST['update_status']) ? sanitize_text_field(wp_unslash($_REQUEST['update_status'])) : '',
201 + 'address_fields' => array_keys(FormFieldsParser::getAddressFields($form)),
202 + ];
203 +
204 + $data = apply_filters_deprecated(
205 + 'fluent_form_entries_vars',
206 + [
207 + $data,
208 + $form
209 + ],
210 + FLUENTFORM_FRAMEWORK_UPGRADE,
211 + 'fluentform/entries_vars',
212 + 'Use fluentform/entries_vars instead of fluent_form_entries_vars.'
213 + );
214 +
215 + $fluentFormEntriesVars = apply_filters('fluentform/entries_vars', $data, $form);
216 +
217 + wp_localize_script(
218 + 'fluentform_form_entries',
219 + 'fluent_form_entries_vars',
220 + $fluentFormEntriesVars
221 + );
222 +
223 + wpFluentForm('view')->render('admin.form.entries', [
224 + 'form_id' => $form_id,
225 + 'has_pdf' => defined('FLUENTFORM_PDF_VERSION') ? 'true' : 'false',
226 + ]);
227 + }
228 +
229 + public function getEntriesGroup()
230 + {
231 + $formId = intval($this->request->get('form_id'));
232 + $counts = $this->groupCount($formId);
233 + wp_send_json_success([
234 + 'counts' => $counts,
235 + ], 200);
236 + }
237 +
238 + public function _getEntries(
239 + $formId,
240 + $currentPage,
241 + $perPage,
242 + $sortBy,
243 + $entryType,
244 + $search,
245 + $wheres = []
246 + ) {
247 + $this->formId = $formId;
248 + $this->per_page = $perPage;
249 + $this->sort_by = $sortBy;
250 + $this->page_number = $currentPage;
251 + $this->search = $search;
252 + $this->wheres = $wheres;
253 +
254 + if ('favorite' == $entryType) {
255 + $this->is_favourite = true;
256 + } elseif ('all' != $entryType && $entryType) {
257 + $this->status = $entryType;
258 + }
259 +
260 + $dateRange = $this->request->get('date_range');
261 + if ($dateRange) {
262 + $this->startDate = $dateRange[0];
263 + $this->endDate = $dateRange[1];
264 + }
265 +
266 + $form = $this->formModel->find($formId);
267 + $formMeta = $this->getFormInputsAndLabels($form);
268 + $formLabels = $formMeta['labels'];
269 +
270 + $formLabels = apply_filters_deprecated(
271 + 'fluentfoform_entry_lists_labels',
272 + [
273 + $formLabels,
274 + $form
275 + ],
276 + FLUENTFORM_FRAMEWORK_UPGRADE,
277 + 'fluentform/entry_lists_labels',
278 + 'Use fluentform/entry_lists_labels instead of fluentfoform_entry_lists_labels.'
279 + );
280 +
281 + $formLabels = apply_filters('fluentform/entry_lists_labels', $formLabels, $form);
282 + $submissions = $this->getResponses();
283 + $submissions['data'] = FormDataParser::parseFormEntries($submissions['data'], $form);
284 +
285 + return compact('submissions', 'formLabels');
286 + }
287 +
288 + public function getEntries()
289 + {
290 + if (!defined('FLUENTFORM_RENDERING_ENTRIES')) {
291 + define('FLUENTFORM_RENDERING_ENTRIES', true);
292 + }
293 +
294 + $wheres = [];
295 +
296 + if ($paymentStatuses = $this->request->get('payment_statuses')) {
297 + if (is_array($paymentStatuses)) {
298 + $wheres[] = ['payment_status', $paymentStatuses];
299 + }
300 + }
301 +
302 + $entries = $this->_getEntries(
303 + intval($this->request->get('form_id')),
304 + intval($this->request->get('current_page', 1)),
305 + intval($this->request->get('per_page', 10)),
306 + Helper::sanitizeOrderValue($this->request->get('sort_by', 'DESC')),
307 + sanitize_text_field($this->request->get('entry_type', 'all')),
308 + sanitize_text_field($this->request->get('search')),
309 + $wheres
310 + );
311 +
312 + $entriesFormLabels = $entries['formLabels'];
313 + $formId = $this->request->get('form_id');
314 +
315 + $entriesFormLabels = apply_filters_deprecated(
316 + 'fluentform_all_entry_labels',
317 + [
318 + $entriesFormLabels,
319 + $formId
320 + ],
321 + FLUENTFORM_FRAMEWORK_UPGRADE,
322 + 'fluentform/all_entry_labels',
323 + 'Use fluentform/all_entry_labels instead of fluentform_all_entry_labels.'
324 + );
325 +
326 + $labels = apply_filters('fluentform/all_entry_labels', $entriesFormLabels, $formId);
327 +
328 + $form = $this->formModel->find($this->request->get('form_id'));
329 +
330 + if ($form->has_payment) {
331 + $entriesFormLabels = apply_filters_deprecated(
332 + 'fluentform_all_entry_labels_with_payment',
333 + [
334 + $entriesFormLabels,
335 + false,
336 + $form
337 + ],
338 + FLUENTFORM_FRAMEWORK_UPGRADE,
339 + 'fluentform/all_entry_labels_with_payment',
340 + 'Use fluentform/all_entry_labels_with_payment instead of fluentform_all_entry_labels_with_payment.'
341 + );
342 +
343 + $labels = apply_filters('fluentform/all_entry_labels_with_payment', $entriesFormLabels, false, $form);
344 + }
345 + $formId = $this->request->get('form_id');
346 + $visible_columns = Helper::getFormMeta($formId, '_visible_columns', null);
347 + $columns_order = Helper::getFormMeta($formId, '_columns_order', null);
348 +
349 + $entries['submissions'] = apply_filters_deprecated(
350 + 'fluentform_all_entries',
351 + [
352 + $entries['submissions']
353 + ],
354 + FLUENTFORM_FRAMEWORK_UPGRADE,
355 + 'fluentform/all_entries',
356 + 'Use fluentform/all_entries instead of fluentform_all_entries.'
357 + );
358 +
359 + wp_send_json_success([
360 + 'submissions' => apply_filters('fluentform/all_entries', $entries['submissions']),
361 + 'labels' => $labels,
362 + 'visible_columns' => $visible_columns,
363 + 'columns_order' => $columns_order,
364 + ], 200);
365 + }
366 +
367 + public function _getEntry()
368 + {
369 + $this->formId = intval($this->request->get('form_id'));
370 +
371 + $entryId = intval($this->request->get('entry_id'));
372 +
373 + $entry_type = sanitize_key($this->request->get('entry_type', 'all'));
374 +
375 + if ('favorite' === $entry_type) {
376 + $this->is_favourite = true;
377 + } elseif ('all' !== $entry_type) {
378 + $this->status = $entry_type;
379 + }
380 +
381 + $this->sort_by = Helper::sanitizeOrderValue($this->request->get('sort_by', 'ASC'));
382 +
383 + $this->search = sanitize_text_field($this->request->get('search'));
384 +
385 + $submission = $this->getResponse($entryId);
386 +
387 + if (!$submission) {
388 + wp_send_json_error([
389 + 'message' => 'No Entry found.',
390 + ], 422);
391 + }
392 +
393 + $form = $this->formModel->find($this->formId);
394 +
395 + $autoRead = apply_filters_deprecated(
396 + 'fluentform_auto_read',
397 + [
398 + true,
399 + $form
400 + ],
401 + FLUENTFORM_FRAMEWORK_UPGRADE,
402 + 'fluentform/auto_read',
403 + 'Use fluentform/auto_read instead of fluentform_auto_read.'
404 + );
405 +
406 + if ('unread' == $submission->status && apply_filters('fluentform/auto_read', $autoRead, $form)) {
407 + wpFluent()->table('fluentform_submissions')
408 + ->where('id', $entryId)
409 + ->update([
410 + 'status' => 'read',
411 + ]);
412 +
413 + $submission->status = 'read';
414 + }
415 +
416 + $formMeta = $this->getFormInputsAndLabels($form);
417 +
418 + $submission = FormDataParser::parseFormEntry($submission, $form, $formMeta['inputs'], true);
419 +
420 + if ($submission->user_id) {
421 + $user = get_user_by('ID', $submission->user_id);
422 + $user_data = [
423 + 'name' => $user->display_name,
424 + 'email' => $user->user_email,
425 + 'ID' => $user->ID,
426 + 'permalink' => get_edit_user_link($user->ID),
427 + ];
428 + $submission->user = $user_data;
429 + }
430 +
431 + $submission = apply_filters_deprecated(
432 + 'fluentform_single_response_data',
433 + [
434 + $submission,
435 + $this->formId
436 + ],
437 + FLUENTFORM_FRAMEWORK_UPGRADE,
438 + 'fluentform/find_submission',
439 + 'Use fluentform/find_submission instead of fluentform_single_response_data.'
440 + );
441 +
442 + $submission = apply_filters('fluentform/find_submission', $submission, $this->formId);
443 +
444 + $fields = $formMeta['inputs'];
445 + $fields = apply_filters_deprecated(
446 + 'fluentform_single_response_input_fields',
447 + [
448 + $fields,
449 + $this->formId
450 + ],
451 + FLUENTFORM_FRAMEWORK_UPGRADE,
452 + 'fluentform/single_response_input_fields',
453 + 'Use fluentform/single_response_input_fields instead of fluentform_single_response_input_fields.'
454 + );
455 +
456 + $fields = apply_filters(
457 + 'fluentform/single_response_input_fields',
458 + $fields,
459 + $this->formId
460 + );
461 + $labels = $formMeta['labels'];
462 + $labels = apply_filters_deprecated(
463 + 'fluentform_single_response_input_labels',
464 + [
465 + $labels,
466 + $this->formId
467 + ],
468 + FLUENTFORM_FRAMEWORK_UPGRADE,
469 + 'fluentform/single_response_input_labels',
470 + 'Use fluentform/single_response_input_labels instead of fluentform_single_response_input_labels.'
471 + );
472 +
473 + $labels = apply_filters(
474 + 'fluentform/single_response_input_labels',
475 + $labels,
476 + $this->formId
477 + );
478 +
479 + $order_data = false;
480 +
481 + if ($submission->payment_status || $submission->payment_total || 'subscription' === $submission->payment_type) {
482 + $order_data = apply_filters_deprecated(
483 + 'fluentform_submission_order_data',
484 + [
485 + $order_data,
486 + $submission,
487 + $form
488 + ],
489 + FLUENTFORM_FRAMEWORK_UPGRADE,
490 + 'fluentform/submission_order_data',
491 + 'Use fluentform/submission_order_data instead of fluentform_submission_order_data.'
492 + );
493 +
494 + $order_data = apply_filters(
495 + 'fluentform/submission_order_data',
496 + $order_data,
497 + $submission,
498 + $form
499 + );
500 +
501 + $labels = apply_filters_deprecated(
502 + 'fluentform_submission_entry_labels_with_payment',
503 + [
504 + $labels,
505 + $submission,
506 + $form
507 + ],
508 + FLUENTFORM_FRAMEWORK_UPGRADE,
509 + 'fluentform/submission_entry_labels_with_payment',
510 + 'Use fluentform/submission_entry_labels_with_payment instead of fluentform_submission_entry_labels_with_payment.'
511 + );
512 +
513 + $labels = apply_filters(
514 + 'fluentform/submission_entry_labels_with_payment',
515 + $labels,
516 + $submission,
517 + $form
518 + );
519 + }
520 +
521 + $nextSubmissionId = $this->getNextResponse($entryId);
522 +
523 + $previousSubmissionId = $this->getPrevResponse($entryId);
524 +
525 + return [
526 + 'submission' => $submission,
527 + 'next' => $nextSubmissionId,
528 + 'prev' => $previousSubmissionId,
529 + 'labels' => $labels,
530 + 'fields' => $fields,
531 + 'order_data' => $order_data,
532 + ];
533 + }
534 +
535 + public function getEntry()
536 + {
537 + if (!defined('FLUENTFORM_RENDERING_ENTRY')) {
538 + define('FLUENTFORM_RENDERING_ENTRY', true);
539 + }
540 +
541 + $entryData = $this->_getEntry();
542 +
543 + $widgets = apply_filters_deprecated(
544 + 'fluentform_single_entry_widgets',
545 + [
546 + [],
547 + $entryData
548 + ],
549 + FLUENTFORM_FRAMEWORK_UPGRADE,
550 + 'fluentform/single_entry_widgets',
551 + 'Use fluentform/single_entry_widgets instead of fluentform_single_entry_widgets.'
552 + );
553 + $entryData['widgets'] = apply_filters('fluentform/single_entry_widgets', $widgets, $entryData);
554 +
555 + $cards = apply_filters_deprecated(
556 + 'fluentform_single_entry_cards',
557 + [
558 + [],
559 + $entryData
560 + ],
561 + FLUENTFORM_FRAMEWORK_UPGRADE,
562 + 'fluentform/single_entry_cards',
563 + 'Use fluentform/single_entry_cards instead of fluentform_single_entry_cards.'
564 + );
565 + $entryData['extraCards'] = apply_filters('fluentform/single_entry_cards', $cards, $entryData);
566 +
567 + wp_send_json_success($entryData, 200);
568 + }
569 +
570 + /**
571 + * @param $form
572 + * @param array $with
573 + *
574 + * @return array
575 + *
576 + * @todo: Implement Caching mechanism so we don't have to parse these things for every request
577 + */
578 + public function getFormInputsAndLabels($form, $with = ['admin_label', 'raw'])
579 + {
580 + $formInputs = FormFieldsParser::getEntryInputs($form, $with);
581 + $inputLabels = FormFieldsParser::getAdminLabels($form, $formInputs);
582 + return [
583 + 'inputs' => $formInputs,
584 + 'labels' => $inputLabels,
585 + ];
586 + }
587 +
588 + public function getNotes()
589 + {
590 + $formId = intval($this->request->get('form_id'));
591 + $entry_id = intval($this->request->get('entry_id'));
592 + $apiLog = 'yes' == sanitize_text_field($this->request->get('api_log'));
593 +
594 + $metaKeys = ['_notes'];
595 +
596 + if ($apiLog) {
597 + $metaKeys[] = 'api_log';
598 + }
599 +
600 + $notes = $this->responseMetaModel
601 + ->where('form_id', $formId)
602 + ->where('response_id', $entry_id)
603 + ->whereIn('meta_key', $metaKeys)
604 + ->orderBy('id', 'DESC')
605 + ->get();
606 +
607 + foreach ($notes as $note) {
608 + if ($note->user_id) {
609 + $note->pemalink = get_edit_user_link($note->user_id);
610 + $user = get_user_by('ID', $note->user_id);
611 + if ($user) {
612 + $note->created_by = $user->display_name;
613 + } else {
614 + $note->created_by = __('Fluent Forms Bot', 'fluentform');
615 + }
616 + } else {
617 + $note->pemalink = false;
618 + }
619 + }
620 +
621 + $notes = apply_filters_deprecated(
622 + 'fluentform_entry_notes',
623 + [
624 + $notes,
625 + $entry_id,
626 + $formId
627 + ],
628 + FLUENTFORM_FRAMEWORK_UPGRADE,
629 + 'fluentform/entry_notes',
630 + 'Use fluentform/entry_notes instead of fluentform_entry_notes.'
631 + );
632 +
633 + $notes = apply_filters('fluentform/entry_notes', $notes, $entry_id, $formId);
634 +
635 + wp_send_json_success([
636 + 'notes' => $notes,
637 + ], 200);
638 + }
639 +
640 + public function addNote()
641 + {
642 + $entryId = intval($this->request->get('entry_id'));
643 + $formId = intval($this->request->get('form_id'));
644 + $note = $this->request->get('note');
645 + $note_content = sanitize_textarea_field($note['content']);
646 + $note_status = sanitize_text_field($note['status']);
647 + $user = get_user_by('ID', get_current_user_id());
648 +
649 + $response_note = [
650 + 'response_id' => $entryId,
651 + 'form_id' => $formId,
652 + 'meta_key' => '_notes',
653 + 'value' => $note_content,
654 + 'status' => $note_status,
655 + 'user_id' => $user->ID,
656 + 'name' => $user->display_name,
657 + 'created_at' => current_time('mysql'),
658 + 'updated_at' => current_time('mysql'),
659 + ];
660 +
661 + $response_note = apply_filters_deprecated(
662 + 'fluentform_add_response_note',
663 + [
664 + $response_note
665 + ],
666 + FLUENTFORM_FRAMEWORK_UPGRADE,
667 + 'fluentform/add_response_note',
668 + 'Use fluentform/add_response_note instead of fluentform_add_response_note.'
669 + );
670 +
671 + $response_note = apply_filters('fluentform/add_response_note', $response_note);
672 +
673 + $insertId = $this->responseMetaModel->insertGetId($response_note);
674 +
675 + $added_note = $this->responseMetaModel->find($insertId);
676 +
677 + do_action_deprecated(
678 + 'fluentform_new_response_note_added',
679 + [
680 + $insertId,
681 + $added_note
682 + ],
683 + FLUENTFORM_FRAMEWORK_UPGRADE,
684 + 'fluentform/new_response_note_added',
685 + 'Use fluentform/new_response_note_added instead of fluentform_new_response_note_added.'
686 + );
687 +
688 + do_action('fluentform/new_response_note_added', $insertId, $added_note);
689 +
690 + wp_send_json_success([
691 + 'message' => __('Note has been successfully added', 'fluentform'),
692 + 'note' => $added_note,
693 + 'insert_id' => $insertId,
694 + ], 200);
695 + }
696 +
697 + public function changeEntryStatus()
698 + {
699 + $formId = intval($this->request->get('form_id'));
700 + $entryId = intval($this->request->get('entry_id'));
701 + $newStatus = sanitize_text_field($this->request->get('status'));
702 +
703 + $this->responseModel
704 + ->where('form_id', $formId)
705 + ->where('id', $entryId)
706 + ->update(['status' => $newStatus]);
707 +
708 + // Line 708 - Use sprintf with placeholder
709 + /* translators: %s is the new status */
710 + $message = sprintf(__('Item has been marked as %s', 'fluentform'), $newStatus);
711 +
712 + wp_send_json_success([
713 + 'message' => $message,
714 + 'status' => $newStatus,
715 + ], 200);
716 + }
717 +
718 + public function updateEntryDiffs($entryId, $formId, $formData)
719 + {
720 + wpFluent()->table('fluentform_entry_details')
721 + ->where('submission_id', $entryId)
722 + ->where('form_id', $formId)
723 + ->whereIn('field_name', array_keys($formData))
724 + ->delete();
725 +
726 + $entryItems = [];
727 + foreach ($formData as $dataKey => $dataValue) {
728 + if (!$dataValue) {
729 + continue;
730 + }
731 +
732 + if (is_array($dataValue)) {
733 + foreach ($dataValue as $subKey => $subValue) {
734 + $entryItems[] = [
735 + 'form_id' => $formId,
736 + 'submission_id' => $entryId,
737 + 'field_name' => $dataKey,
738 + 'sub_field_name' => $subKey,
739 + 'field_value' => maybe_serialize($subValue),
740 + ];
741 + }
742 + } else {
743 + $entryItems[] = [
744 + 'form_id' => $formId,
745 + 'submission_id' => $entryId,
746 + 'field_name' => $dataKey,
747 + 'sub_field_name' => '',
748 + 'field_value' => $dataValue,
749 + ];
750 + }
751 + }
752 +
753 + foreach ($entryItems as $entryItem) {
754 + wpFluent()->table('fluentform_entry_details')->insert($entryItem);
755 + }
756 +
757 + return true;
758 + }
759 +
760 + public function getUsers()
761 + {
762 + // if (!current_user_can('list_users')) {
763 + // wp_send_json_error([
764 + // 'message' => __('Sorry, You do not have permission to list users', 'fluentform')
765 + // ]);
766 + // }
767 +
768 + $search = sanitize_text_field($this->request->get('search'));
769 +
770 + $users = get_users([
771 + 'search' => "*{$search}*",
772 + 'number' => 50,
773 + ]);
774 +
775 + $formattedUsers = [];
776 +
777 + foreach ($users as $user) {
778 + $formattedUsers[] = [
779 + 'ID' => $user->ID,
780 + 'label' => $user->display_name . ' - ' . $user->user_email,
781 + ];
782 + }
783 +
784 + wp_send_json_success([
785 + 'users' => $formattedUsers,
786 + ]);
787 + }
788 +
789 + public function changeEntryUser()
790 + {
791 + $userId = intval($this->request->get('user_id'));
792 + $submissionId = intval($this->request->get('submission_id'));
793 +
794 + if (!$userId || !$submissionId) {
795 + wp_send_json_error([
796 + 'message' => __('Submission ID and User ID is required', 'fluentform'),
797 + ], 423);
798 + }
799 +
800 + $submission = fluentFormApi('submissions')->find($submissionId);
801 +
802 + $user = get_user_by('ID', $userId);
803 +
804 + if (!$submission || $submission->user_id == $userId || !$user) {
805 + wp_send_json_error([
806 + 'message' => __('Invalid Request', 'fluentform'),
807 + ], 423);
808 + }
809 +
810 + wpFluent()->table('fluentform_submissions')
811 + ->where('id', $submission->id)
812 + ->update([
813 + 'user_id' => $userId,
814 + 'updated_at' => current_time('mysql'),
815 + ]);
816 +
817 + if (defined('FLUENTFORMPRO')) {
818 + // let's update the corresponding user IDs for transactions and
819 + wpFluent()->table('fluentform_transactions')
820 + ->where('submission_id', $submission->id)
821 + ->update([
822 + 'user_id' => $userId,
823 + 'updated_at' => current_time('mysql'),
824 + ]);
825 + }
826 +
827 + $logData = [
828 + 'parent_source_id' => $submission->form_id,
829 + 'source_type' => 'submission_item',
830 + 'source_id' => $submission->id,
831 + 'component' => 'General',
832 + 'status' => 'info',
833 + 'title' => 'Associate user has been changed from ' . $submission->user_id . ' to ' . $userId,
834 + ];
835 +
836 + do_action('fluentform/log_data', $logData);
837 +
838 + do_action_deprecated(
839 + 'fluentform_submission_user_changed',
840 + [
841 + $submission,
842 + $user
843 + ],
844 + FLUENTFORM_FRAMEWORK_UPGRADE,
845 + 'fluentform/submission_user_changed',
846 + 'Use fluentform/submission_user_changed instead of fluentform_submission_user_changed.'
847 + );
848 +
849 + do_action('fluentform/submission_user_changed', $submission, $user);
850 +
851 + wp_send_json_success([
852 + 'message' => __('Selected user has been successfully assigned to this submission', 'fluentform'),
853 + 'user' => [
854 + 'name' => $user->display_name,
855 + 'email' => $user->user_email,
856 + 'ID' => $user->ID,
857 + 'permalink' => get_edit_user_link($user->ID),
858 + ],
859 + 'user_id' => $userId,
860 + ]);
861 + }
862 +
863 + public function getAvailableForms()
864 + {
865 + $forms = wpFluent()->table('fluentform_forms')
866 + ->select(['id', 'title'])
867 + ->orderBy('id', 'DESC')
868 + ->get();
869 +
870 + $formattedForms = [];
871 + foreach ($forms as $form) {
872 + $formattedForms[] = [
873 + 'id' => $form->id,
874 + 'title' => $form->title,
875 + ];
876 + }
877 +
878 + return $formattedForms;
879 + }
880 + }
881 +