| 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 |
|