| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Models; |
| 4 |
|
| 5 |
use FluentForm\Framework\Support\Arr; |
| 6 |
|
| 7 |
// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- This class uses custom meta table (fluentform_form_meta), not WordPress postmeta |
| 8 |
class FormMeta extends Model |
| 9 |
{ |
| 10 |
/** |
| 11 |
* The table associated with the model. |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
protected $table = 'fluentform_form_meta'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Indicates if the model should be timestamped. |
| 19 |
* |
| 20 |
* @var bool |
| 21 |
*/ |
| 22 |
public $timestamps = false; |
| 23 |
|
| 24 |
/** |
| 25 |
* A formMeta is owned by a form. |
| 26 |
* |
| 27 |
* @return \FluentForm\Framework\Database\Orm\Relations\BelongsTo |
| 28 |
*/ |
| 29 |
public function form() |
| 30 |
{ |
| 31 |
return $this->belongsTo(Form::class, 'form_id', 'id'); |
| 32 |
} |
| 33 |
|
| 34 |
public static function prepare($attributes, $predefinedForm) |
| 35 |
{ |
| 36 |
$formMeta = []; |
| 37 |
|
| 38 |
$formMeta[] = [ |
| 39 |
'meta_key' => 'formSettings', |
| 40 |
'value' => json_encode(Form::getFormsDefaultSettings()), |
| 41 |
]; |
| 42 |
|
| 43 |
$formMeta[] = [ |
| 44 |
'meta_key' => 'template_name', |
| 45 |
'value' => Arr::get($attributes, 'predefined', Arr::get($attributes, 'type', '')), |
| 46 |
]; |
| 47 |
|
| 48 |
if (isset($predefinedForm['notifications'])) { |
| 49 |
$formMeta[] = [ |
| 50 |
'meta_key' => 'notifications', |
| 51 |
'value' => json_encode($predefinedForm['notifications']), |
| 52 |
]; |
| 53 |
} |
| 54 |
|
| 55 |
if (Arr::get($attributes, 'predefined') == 'conversational') { |
| 56 |
$formMeta[] = [ |
| 57 |
'meta_key' => 'is_conversion_form', |
| 58 |
'value' => 'yes', |
| 59 |
]; |
| 60 |
} |
| 61 |
|
| 62 |
return $formMeta; |
| 63 |
} |
| 64 |
|
| 65 |
public static function retrieve($key, $formId = null, $default = null) |
| 66 |
{ |
| 67 |
$meta = static::when($formId, function ($q) use ($formId) { |
| 68 |
return $q->where('form_id', $formId); |
| 69 |
}) |
| 70 |
->where('meta_key', $key) |
| 71 |
->first(); |
| 72 |
|
| 73 |
if ($meta && isset($meta->value)) { |
| 74 |
$value = json_decode($meta->value, true); |
| 75 |
if (JSON_ERROR_NONE == json_last_error()) { |
| 76 |
return $value; |
| 77 |
} |
| 78 |
return $meta->value; |
| 79 |
} |
| 80 |
|
| 81 |
return $default; |
| 82 |
} |
| 83 |
|
| 84 |
public static function store(Form $form, $formMeta) |
| 85 |
{ |
| 86 |
foreach ($formMeta as $meta) { |
| 87 |
$metaValue = $meta['value']; |
| 88 |
|
| 89 |
if (is_array($metaValue) || is_object($metaValue)) { |
| 90 |
$metaValue = wp_json_encode($metaValue); |
| 91 |
} |
| 92 |
|
| 93 |
$meta['value'] = trim(preg_replace('/\s+/', ' ', (string) $metaValue)); |
| 94 |
|
| 95 |
$form->formMeta()->create([ |
| 96 |
'meta_key' => $meta['meta_key'], |
| 97 |
'value' => $meta['value'], |
| 98 |
]); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
public static function persist($formId, $metaKey, $metaValue) |
| 103 |
{ |
| 104 |
if (is_array($metaValue) || is_object($metaValue)) { |
| 105 |
$metaValue = json_encode($metaValue); |
| 106 |
} |
| 107 |
|
| 108 |
return static::updateOrCreate( |
| 109 |
['form_id' => $formId, 'meta_key' => $metaKey], |
| 110 |
['value' => $metaValue] |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
public static function remove($formId, $metaKey) |
| 115 |
{ |
| 116 |
static::where('form_id', $formId) |
| 117 |
->where('meta_key', $metaKey) |
| 118 |
->delete(); |
| 119 |
} |
| 120 |
} |
| 121 |
|