PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.7
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.7
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 / Models / SubmissionMeta.php

SubmissionMeta.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.7, at app/Models/SubmissionMeta.php

98 lines 2.7 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\Models;
4
5 use FluentForm\App\Helpers\Helper;
6
7 // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- This class uses custom meta table (fluentform_submission_meta), not WordPress postmeta
8 class SubmissionMeta extends Model
9 {
10 /**
11 * The table associated with the model.
12 *
13 * @var string
14 */
15 protected $table = 'fluentform_submission_meta';
16
17 /**
18 * A formMeta is owned by a form.
19 *
20 * @return \FluentForm\Framework\Database\Orm\Relations\BelongsTo
21 */
22 public function form()
23 {
24 return $this->belongsTo(Form::class, 'form_id', 'id');
25 }
26
27 /**
28 * A formMeta is owned by a submission.
29 *
30 * @return \FluentForm\Framework\Database\Orm\Relations\BelongsTo
31 */
32 public function submission()
33 {
34 return $this->belongsTo(Submission::class, 'response_id', 'id');
35 }
36
37
38 public static function retrieve($key, $submissionId = null, $default = null)
39 {
40 $meta = static::when($submissionId, function ($q) use ($submissionId) {
41 return $q->where('response_id', $submissionId);
42 })
43 ->where('meta_key', $key)
44 ->first();
45
46 if ($meta && isset($meta->value)) {
47 return Helper::safeUnserialize($meta->value);
48 }
49
50 return $default;
51 }
52
53 public static function persist($submissionId, $metaKey, $metaValue, $formId = null)
54 {
55 $metaValue = maybe_serialize($metaValue);
56 if (!$formId) {
57 $formId = Submission::select('form_id')->where('id', $submissionId)->value('form_id');
58 }
59 return static::updateOrCreate(
60 ['response_id' => $submissionId, 'meta_key' => $metaKey],
61 [
62 'value' => $metaValue,
63 'form_id' => $formId,
64 ]
65 );
66 }
67
68 public static function persistArray($submissionId, $metaKey, $metaValue, $formId = null)
69 {
70 if (!$formId) {
71 $formId = Submission::select('form_id')->where('id', $submissionId)->value('form_id');
72 }
73
74 // Try to fetch an existing record.
75 $record = static::where('response_id', $submissionId)
76 ->where('meta_key', $metaKey)
77 ->first();
78
79 if ($record) {
80 $values = json_decode($record->value);
81 $values[] = $metaValue;
82 $record->value = json_encode($values);
83 $record->save();
84 } else {
85 $metaValue = json_encode([$metaValue]);
86
87 $record = static::create([
88 'response_id' => $submissionId,
89 'meta_key' => $metaKey,
90 'value' => $metaValue,
91 'form_id' => $formId,
92 ]);
93 }
94
95 return $record;
96 }
97 }
98