| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Models; |
| 4 |
|
| 5 |
use FluentForm\Framework\Support\Arr; |
| 6 |
use FluentForm\App\Models\Traits\PredefinedForms; |
| 7 |
|
| 8 |
class Form extends Model |
| 9 |
{ |
| 10 |
use PredefinedForms; |
| 11 |
|
| 12 |
/** |
| 13 |
* The table associated with the model. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
protected $table = 'fluentform_forms'; |
| 18 |
|
| 19 |
/** |
| 20 |
* A form has many form meta. |
| 21 |
* |
| 22 |
* @return \FluentForm\Framework\Database\Orm\Relations\HasMany |
| 23 |
*/ |
| 24 |
public function formMeta() |
| 25 |
{ |
| 26 |
return $this->hasMany(FormMeta::class, 'form_id', 'id'); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* A form may have one form meta to determine if |
| 31 |
* the form is a regular or conversational one. |
| 32 |
* |
| 33 |
* @return \FluentForm\Framework\Database\Orm\Relations\HasOne |
| 34 |
*/ |
| 35 |
public function conversationalMeta() |
| 36 |
{ |
| 37 |
return $this->hasOne(FormMeta::class, 'form_id', 'id')->where('meta_key', 'is_conversion_form'); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* A form has many submissions. |
| 42 |
* |
| 43 |
* @return \FluentForm\Framework\Database\Orm\Relations\HasMany |
| 44 |
*/ |
| 45 |
public function submissions() |
| 46 |
{ |
| 47 |
return $this->hasMany(Submission::class, 'form_id', 'id'); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* A form has many submission meta. |
| 52 |
* |
| 53 |
* @return \FluentForm\Framework\Database\Orm\Relations\HasMany |
| 54 |
*/ |
| 55 |
public function submissionMeta() |
| 56 |
{ |
| 57 |
return $this->hasMany(SubmissionMeta::class, 'form_id', 'id'); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* A form has many entry details. |
| 62 |
* |
| 63 |
* @return \FluentForm\Framework\Database\Orm\Relations\HasMany |
| 64 |
*/ |
| 65 |
public function entryDetails() |
| 66 |
{ |
| 67 |
return $this->hasMany(EntryDetails::class, 'form_id', 'id'); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* A form has many form analytics. |
| 72 |
* |
| 73 |
* @return \FluentForm\Framework\Database\Orm\Relations\HasMany |
| 74 |
*/ |
| 75 |
public function formAnalytics() |
| 76 |
{ |
| 77 |
return $this->hasMany(FormAnalytics::class, 'form_id', 'id'); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* A form has many logs. |
| 82 |
* |
| 83 |
* @return \FluentForm\Framework\Database\Orm\Relations\HasMany |
| 84 |
*/ |
| 85 |
public function logs() |
| 86 |
{ |
| 87 |
return $this->hasMany(Log::class, 'form_id', 'id'); |
| 88 |
} |
| 89 |
|
| 90 |
public static function prepare($attributes = []) |
| 91 |
{ |
| 92 |
$now = current_time('mysql'); |
| 93 |
|
| 94 |
$data = [ |
| 95 |
'title' => Arr::get($attributes, 'title', 'My New Form'), |
| 96 |
'status' => Arr::get($attributes, 'status', 'published'), |
| 97 |
'appearance_settings' => Arr::get($attributes, 'appearance_settings'), |
| 98 |
'form_fields' => Arr::get($attributes, 'form_fields'), |
| 99 |
'has_payment' => Arr::get($attributes, 'has_payment', 0), |
| 100 |
'type' => Arr::get($attributes, 'type', 'form'), |
| 101 |
'conditions' => Arr::get($attributes, 'conditions'), |
| 102 |
'created_by' => get_current_user_id(), |
| 103 |
'created_at' => $now, |
| 104 |
'updated_at' => $now, |
| 105 |
]; |
| 106 |
|
| 107 |
return apply_filters( |
| 108 |
'fluentform/form_store_attributes', |
| 109 |
array_filter($data) |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
public static function getFormMeta($metaKey, $formId = null) |
| 114 |
{ |
| 115 |
return FormMeta::retrieve($metaKey, $formId); |
| 116 |
} |
| 117 |
|
| 118 |
public static function getFormsDefaultSettings($formId = false) |
| 119 |
{ |
| 120 |
$defaultSettings = [ |
| 121 |
'confirmation' => [ |
| 122 |
'redirectTo' => 'samePage', |
| 123 |
'messageToShow' => __('Thank you for your message. We will get in touch with you shortly', 'fluentform'), |
| 124 |
'customPage' => null, |
| 125 |
'samePageFormBehavior' => 'hide_form', |
| 126 |
'customUrl' => null, |
| 127 |
], |
| 128 |
'restrictions' => [ |
| 129 |
'limitNumberOfEntries' => [ |
| 130 |
'enabled' => false, |
| 131 |
'numberOfEntries' => null, |
| 132 |
'period' => 'total', |
| 133 |
'limitReachedMsg' => __('Maximum number of entries exceeded.', 'fluentform'), |
| 134 |
], |
| 135 |
'scheduleForm' => [ |
| 136 |
'enabled' => false, |
| 137 |
'start' => null, |
| 138 |
'end' => null, |
| 139 |
'selectedDays' => null, |
| 140 |
'pendingMsg' => __('Form submission is not started yet.', 'fluentform'), |
| 141 |
'expiredMsg' => __('Form submission is now closed.', 'fluentform'), |
| 142 |
], |
| 143 |
'requireLogin' => [ |
| 144 |
'enabled' => false, |
| 145 |
'requireLoginMsg' => __('You must be logged in to submit the form.', 'flunetform'), |
| 146 |
], |
| 147 |
'denyEmptySubmission' => [ |
| 148 |
'enabled' => false, |
| 149 |
'message' => __('Sorry, you cannot submit an empty form. Let\'s hear what you wanna say.', 'fluentform'), |
| 150 |
], |
| 151 |
'restrictForm' => [ |
| 152 |
'enabled' => false, |
| 153 |
'fields' => [ |
| 154 |
'ip' => [ |
| 155 |
'status' => false, |
| 156 |
'values' => '', |
| 157 |
'message' => __('Sorry! You can\'t submit a form from your IP address.', 'fluentform'), |
| 158 |
'validation_type' => 'fail_on_condition_met' |
| 159 |
], |
| 160 |
'country' => [ |
| 161 |
'status' => false, |
| 162 |
'values' => [], |
| 163 |
'message' => __('Sorry! You can\'t submit a form the country you are residing.', 'fluentform'), |
| 164 |
'validation_type' => 'fail_on_condition_met' |
| 165 |
], |
| 166 |
'keywords' => [ |
| 167 |
'status' => false, |
| 168 |
'values' => '', |
| 169 |
'message' => __('Sorry! Your submission contains some restricted keywords.', 'fluentform') |
| 170 |
], |
| 171 |
] |
| 172 |
] |
| 173 |
], |
| 174 |
'layout' => [ |
| 175 |
'labelPlacement' => 'top', |
| 176 |
'helpMessagePlacement' => 'with_label', |
| 177 |
'errorMessagePlacement' => 'inline', |
| 178 |
'cssClassName' => '', |
| 179 |
'asteriskPlacement' => 'asterisk-right', |
| 180 |
], |
| 181 |
'delete_entry_on_submission' => 'no', |
| 182 |
'conv_form_per_step_save' => false, |
| 183 |
'conv_form_resume_from_last_step' => false |
| 184 |
]; |
| 185 |
|
| 186 |
if ($formId) { |
| 187 |
$value = static::getFormMeta('formSettings', $formId); |
| 188 |
|
| 189 |
if ($value) { |
| 190 |
$defaultSettings = wp_parse_args($value, $defaultSettings); |
| 191 |
} |
| 192 |
} else { |
| 193 |
$globalSettings = get_option('_fluentform_global_form_settings'); |
| 194 |
|
| 195 |
if (isset($globalSettings['layout'])) { |
| 196 |
$defaultSettings['layout'] = $globalSettings['layout']; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
$defaultSettings = apply_filters( |
| 201 |
'fluentform/forms_default_settings', |
| 202 |
$defaultSettings |
| 203 |
); |
| 204 |
|
| 205 |
$defaultSettings = apply_filters_deprecated( |
| 206 |
'fluentform_create_default_settings', |
| 207 |
[ |
| 208 |
$defaultSettings |
| 209 |
], |
| 210 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 211 |
'fluentform/create_default_settings', |
| 212 |
'Use fluentform/create_default_settings instead of fluentform_create_default_settings.' |
| 213 |
); |
| 214 |
|
| 215 |
return apply_filters( |
| 216 |
'fluentform/create_default_settings', |
| 217 |
$defaultSettings |
| 218 |
); |
| 219 |
} |
| 220 |
|
| 221 |
public static function getAdvancedValidationSettings($formId) |
| 222 |
{ |
| 223 |
$settings = [ |
| 224 |
'status' => false, |
| 225 |
'type' => 'all', |
| 226 |
'conditions' => [ |
| 227 |
[ |
| 228 |
'field' => '', |
| 229 |
'operator' => '=', |
| 230 |
'value' => '', |
| 231 |
], |
| 232 |
], |
| 233 |
'error_message' => '', |
| 234 |
'validation_type' => 'fail_on_condition_met', |
| 235 |
]; |
| 236 |
|
| 237 |
$metaSettings = static::getFormMeta('advancedValidationSettings', $formId); |
| 238 |
|
| 239 |
if ($metaSettings && is_array($metaSettings)) { |
| 240 |
$settings = wp_parse_args($metaSettings, $settings); |
| 241 |
} |
| 242 |
|
| 243 |
return apply_filters( |
| 244 |
'fluentform/advanced_validation_settings', |
| 245 |
$settings |
| 246 |
); |
| 247 |
} |
| 248 |
|
| 249 |
public static function remove($formId) |
| 250 |
{ |
| 251 |
static::where('id', $formId)->delete(); |
| 252 |
|
| 253 |
Submission::where('form_id', $formId)->delete(); |
| 254 |
SubmissionMeta::where('form_id', $formId)->delete(); |
| 255 |
EntryDetails::where('form_id', $formId)->delete(); |
| 256 |
FormMeta::where('form_id', $formId)->delete(); |
| 257 |
FormAnalytics::where('form_id', $formId)->delete(); |
| 258 |
Log::where('parent_source_id', $formId) |
| 259 |
->whereIn('source_type', [ |
| 260 |
'submission_item', 'form_item', 'draft_submission_meta', |
| 261 |
]) |
| 262 |
->delete(); |
| 263 |
|
| 264 |
if (defined('FLUENTFORMPRO')) { |
| 265 |
wpFluent()->table('fluentform_order_items') |
| 266 |
->where('form_id', $formId) |
| 267 |
->delete(); |
| 268 |
|
| 269 |
wpFluent()->table('fluentform_transactions') |
| 270 |
->where('form_id', $formId) |
| 271 |
->delete(); |
| 272 |
|
| 273 |
wpFluent()->table('fluentform_subscriptions') |
| 274 |
->where('form_id', $formId) |
| 275 |
->delete(); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|