PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.2
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.2
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 / Modules / Form / Form.php

Form.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 3.6.2, at app/Modules/Form/Form.php

573 lines 17.0 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\Modules\Form;
4
5 use FluentForm\App\Helpers\Helper;
6 use FluentForm\App\Modules\Acl\Acl;
7 use FluentForm\Framework\Foundation\Application;
8
9 class Form
10 {
11 /**
12 * @var \FluentForm\Framework\Request\Request $request
13 */
14 protected $request;
15
16 /**
17 * Set this value when we need predefined default settings.
18 *
19 * @var array $defaultSettings
20 */
21 protected $defaultSettings;
22
23
24 /**
25 * Set this value when we need predefined default notifications.
26 *
27 * @var array $defaultNotifications
28 */
29 protected $defaultNotifications;
30
31 /**
32 * Set this value when we need predefined form fields.
33 *
34 * @var array $formFields
35 */
36 protected $formFields;
37
38 protected $metas = [];
39
40 protected $formType = 'form';
41
42 protected $hasPayment = 0;
43
44 /**
45 * Form constructor.
46 *
47 * @param \FluentForm\Framework\Foundation\Application $application
48 *
49 * @throws \Exception
50 */
51 public function __construct(Application $application)
52 {
53 $this->request = $application->request;
54 $this->model = wpFluent()->table('fluentform_forms');
55 }
56
57 /**
58 * Get all forms from database
59 *
60 * @return void
61 * @throws \Exception
62 */
63 public function index()
64 {
65 $search = $this->request->get('search');
66 $status = $this->request->get('status');
67
68 $query = wpFluent()->table('fluentform_forms')
69 ->orderBy('id', 'DESC');
70
71 if ($status && $status != 'all') {
72 $query->where('status', $status);
73 }
74
75 if ($search) {
76 $query->where(function ($q) use ($search) {
77 $q->where('id', 'LIKE', '%' . $search . '%');
78 $q->orWhere('title', 'LIKE', '%' . $search . '%');
79 });
80 }
81
82 $forms = $query->paginate();
83
84 foreach ($forms['data'] as $form) {
85 $form->preview_url = site_url('?fluentform_pages=1&preview_id=' . $form->id) . '#ff_preview';;
86 $form->edit_url = $this->getAdminPermalink('editor', $form);
87 $form->settings_url = $this->getSettingsUrl($form);
88 $form->entries_url = $this->getAdminPermalink('entries', $form);
89 $form->analytics_url = $this->getAdminPermalink('analytics', $form);
90 $form->total_views = $this->getFormViewCount($form->id);
91 $form->total_views = $this->getFormViewCount($form->id);
92 $form->total_Submissions = $this->getSubmissionCount($form->id);
93 $form->unread_count = $this->getUnreadCount($form->id);
94 $form->conversion = $this->getConversionRate($form);
95 unset($form->form_fields);
96 }
97
98 wp_send_json($forms, 200);
99 }
100
101 private function getFormViewCount($formId)
102 {
103 $hasCount = wpFluent()
104 ->table('fluentform_form_meta')
105 ->where('meta_key', '_total_views')
106 ->where('form_id', $formId)
107 ->first();
108
109 if ($hasCount) {
110 return intval($hasCount->value);
111 }
112
113 return 0;
114 }
115
116 private function getSubmissionCount($formID)
117 {
118 return wpFluent()
119 ->table('fluentform_submissions')
120 ->where('form_id', $formID)
121 ->where('status', '!=', 'trashed')
122 ->count();
123 }
124
125 private function getConversionRate($form)
126 {
127 if (!$form->total_Submissions)
128 return 0;
129
130 if (!$form->total_views)
131 return 0;
132
133 return ceil(($form->total_Submissions / $form->total_views) * 100);
134 }
135
136 /**
137 * Create a form from backend/editor
138 * @return void
139 */
140 public function store()
141 {
142 $type = $this->request->get('type', 'form');
143 $title = $this->request->get('title', 'My New Form');
144 $status = $this->request->get('status', 'published');
145 $createdBy = get_current_user_id();
146
147
148 $now = current_time('mysql');
149
150 $insertData = [
151 'title' => $title,
152 'type' => $type,
153 'status' => $status,
154 'created_by' => $createdBy,
155 'created_at' => $now,
156 'updated_at' => $now
157 ];
158
159 if ($this->formFields) {
160 $insertData['form_fields'] = $this->formFields;
161 }
162
163 if($this->formType) {
164 $insertData['type'] = $this->formType;
165 }
166
167 if($this->hasPayment) {
168 $insertData['has_payment'] = $this->hasPayment;
169 }
170
171
172 $formId = $this->model->insert($insertData);
173
174 // Rename the form name here
175 wpFluent()->table('fluentform_forms')->where('id', $formId)->update(array(
176 'title' => $title . ' (#' . $formId . ')'
177 ));
178
179 if($this->metas && is_array($this->metas)) {
180 foreach ($this->metas as $meta) {
181 $meta['value'] = trim(preg_replace('/\s+/', ' ', $meta['value']));
182
183 wpFluent()->table('fluentform_form_meta')
184 ->insert(array(
185 'form_id' => $formId,
186 'meta_key' => $meta['meta_key'],
187 'value' => $meta['value']
188 ));
189 }
190 } else {
191 // add default form settings now
192 $defaultSettings = $this->defaultSettings ?: $this->getFormsDefaultSettings($formId);
193
194 $defaultSettings = apply_filters('fluentform_create_default_settings', $defaultSettings);
195
196 wpFluent()->table('fluentform_form_meta')
197 ->insert(array(
198 'form_id' => $formId,
199 'meta_key' => 'formSettings',
200 'value' => json_encode($defaultSettings)
201 ));
202
203 if ($this->defaultNotifications) {
204 wpFluent()->table('fluentform_form_meta')
205 ->insert(array(
206 'form_id' => $formId,
207 'meta_key' => 'notifications',
208 'value' => json_encode($this->defaultNotifications)
209 ));
210 }
211 }
212
213 do_action('fluentform_inserted_new_form', $formId, $insertData);
214
215 wp_send_json_success(array(
216 'formId' => $formId,
217 'redirect_url' => admin_url('admin.php?page=fluent_forms&form_id=' . $formId . '&route=editor'),
218 'message' => __('Successfully created a form.', 'fluentform')
219 ), 200);
220 }
221
222 public function getFormsDefaultSettings($formId = false)
223 {
224 $defaultSettings = array(
225 'confirmation' => array(
226 'redirectTo' => 'samePage',
227 'messageToShow' => __('Thank you for your message. We will get in touch with you shortly', 'fluentform'),
228 'customPage' => null,
229 'samePageFormBehavior' => 'hide_form',
230 'customUrl' => null
231 ),
232 'restrictions' => array(
233 'limitNumberOfEntries' => array(
234 'enabled' => false,
235 'numberOfEntries' => null,
236 'period' => 'total',
237 'limitReachedMsg' => 'Maximum number of entries exceeded.'
238 ),
239 'scheduleForm' => array(
240 'enabled' => false,
241 'start' => null,
242 'end' => null,
243 'pendingMsg' => __("Form submission is not started yet.", 'fluentform'),
244 'expiredMsg' => __("Form submission is now closed.", 'fluentform')
245 ),
246 'requireLogin' => array(
247 'enabled' => false,
248 'requireLoginMsg' => 'You must be logged in to submit the form.',
249 ),
250 'denyEmptySubmission' => [
251 'enabled' => false,
252 'message' => __('Sorry, you cannot submit an empty form. Let\'s hear what you wanna say.', 'fluentform'),
253 ]
254 ),
255 'layout' => array(
256 'labelPlacement' => 'top',
257 'helpMessagePlacement' => 'with_label',
258 'errorMessagePlacement' => 'inline',
259 'cssClassName' => '',
260 'asteriskPlacement' => 'asterisk-right'
261 ),
262 'delete_entry_on_submission' => 'no'
263 );
264
265 if ($formId) {
266 $value = $this->getMeta($formId, 'formSettings', true);
267 if ($value) {
268 $defaultSettings = wp_parse_args($value, $defaultSettings);
269 }
270 } else {
271 $globalSettings = get_option('_fluentform_global_form_settings');
272 if (isset($globalSettings['layout'])) {
273 $defaultSettings['layout'] = $globalSettings['layout'];
274 }
275 }
276
277 return $defaultSettings;
278 }
279
280 public function getAdvancedValidationSettings($formId)
281 {
282 $settings = [
283 'status' => false,
284 'type' => 'all',
285 'conditions' => [
286 [
287 'field' => '',
288 'operator' => '=',
289 'value' => ''
290 ]
291 ],
292 'error_message' => '',
293 'validation_type' => 'fail_on_condition_met'
294 ];
295
296 $metaSettings = $this->getMeta($formId, 'advancedValidationSettings', true);
297
298 if($metaSettings && is_array($metaSettings)) {
299 $settings = wp_parse_args($metaSettings, $settings);
300 }
301
302 return $settings;
303 }
304
305 public function getMeta($formId, $metaKey, $isJson = true)
306 {
307 $settingsMeta = wpFluent()->table('fluentform_form_meta')
308 ->where('form_id', $formId)
309 ->where('meta_key', $metaKey)
310 ->first();
311 if ($settingsMeta) {
312 if($isJson) {
313 return \json_decode($settingsMeta->value, true);
314 } else {
315 return $settingsMeta->value;
316 }
317 }
318 return false;
319 }
320
321 public function updateMeta($formId, $metaKey, $metaValue)
322 {
323 $exist = wpFluent()->table('fluentform_form_meta')
324 ->where('form_id', $formId)
325 ->where('meta_key', $metaKey)
326 ->first();
327
328 if(is_array($metaValue) || is_object($metaValue)) {
329 $metaValue = \json_encode($metaValue);
330 }
331
332 if($exist) {
333 return wpFluent()->table('fluentform_form_meta')
334 ->where('id', $exist->id)
335 ->update([
336 'value' => $metaValue
337 ]);
338 }
339
340 return wpFluent()->table('fluentform_form_meta')->insert([
341 'form_id' => $formId,
342 'meta_key' => $metaKey,
343 'value' => $metaValue
344 ]);
345 }
346
347 /**
348 * Find/Read a from from the database
349 * @return void
350 */
351 public function find()
352 {
353 $form = $this->fetchForm($this->request->get('formId'));
354 wp_send_json(['form' => $form, 'metas' => []], 200);
355 }
356
357 /**
358 * Fetch a from from the database
359 * Note: required for ninja-tables
360 * @return mixed
361 */
362 public function fetchForm($formId)
363 {
364 return $this->model->find($formId);
365 }
366
367 /**
368 * Save/update a form from backend/editor
369 * @return void
370 * @throws \WpFluent\Exception
371 */
372 public function update()
373 {
374 $formId = $this->request->get('formId');
375 $title = $this->request->get('title');
376 $status = $this->request->get('status', 'published');
377
378 $this->validate();
379
380 $data = [
381 'title' => $title,
382 'status' => $status,
383 'updated_at' => current_time('mysql')
384 ];
385
386
387 if ($formFields = $this->request->get('formFields')) {
388 $formFields = apply_filters('fluentform_form_fields_update', $formFields, $formId);
389 $data['form_fields'] = $formFields;
390 }
391
392 $this->model->where('id', $formId)->update($data);
393
394 $form = $this->fetchForm($formId);
395
396 if (FormFieldsParser::hasPaymentFields($form)) {
397 $this->model->where('id', $formId)->update([
398 'has_payment' => 1
399 ]);
400 } else if ($form->has_payment) {
401 $this->model->where('id', $formId)->update([
402 'has_payment' => 0
403 ]);
404 }
405
406 wp_send_json([
407 'message' => __('The form is successfully updated.', 'fluentform')
408 ], 200);
409 }
410
411 /**
412 * Delete a from from database
413 * @return void
414 * @throws \WpFluent\Exception
415 */
416 public function delete()
417 {
418 $formId = $this->request->get('formId');
419
420 $this->model->where('id', $formId)->delete();
421
422
423 // Now Let's delete associate items
424 wpFluent()->table('fluentform_submissions')
425 ->where('form_id', $formId)
426 ->delete();
427
428 wpFluent()->table('fluentform_submission_meta')
429 ->where('form_id', $formId)
430 ->delete();
431
432 wpFluent()->table('fluentform_entry_details')
433 ->where('form_id', $formId)
434 ->delete();
435
436 wpFluent()->table('fluentform_form_analytics')
437 ->where('form_id', $formId)
438 ->delete();
439
440 wpFluent()->table('fluentform_logs')
441 ->where('parent_source_id', $formId)
442 ->whereIn('source_type', ['submission_item', 'form_item'])
443 ->delete();
444
445 ob_start();
446 if (defined('FLUENTFORMPRO')) {
447 try {
448 wpFluent()->table('fluentform_order_items')
449 ->where('form_id', $formId)
450 ->delete();
451 wpFluent()->table('fluentform_subscriptions')
452 ->where('form_id', $formId)
453 ->delete();
454 wpFluent()->table('fluentform_transactions')
455 ->where('form_id', $formId)
456 ->delete();
457 } catch (\Exception $exception) {
458
459 }
460 }
461 $errors = ob_get_clean();
462
463 wp_send_json([
464 'message' => __('Successfully deleted the form.', 'fluentform'),
465 'errors' => $errors
466 ], 200);
467 }
468
469 /**
470 * Duplicate a from
471 * @return void
472 * @throws \WpFluent\Exception
473 */
474 public function duplicate()
475 {
476 $formId = absint($this->request->get('formId'));
477 $form = $this->model->where('id', $formId)->first();
478
479 $data = array(
480 'title' => $form->title,
481 'status' => $form->status,
482 'appearance_settings' => $form->appearance_settings,
483 'form_fields' => $form->form_fields,
484 'type' => $form->type,
485 'has_payment' => $form->has_payment,
486 'conditions' => $form->conditions,
487 'created_by' => get_current_user_id(),
488 'created_at' => current_time('mysql'),
489 'updated_at' => current_time('mysql')
490 );
491
492 $newFormId = $this->model->insert($data);
493
494 // Ranme the form name here
495 wpFluent()->table('fluentform_forms')
496 ->where('id', $newFormId)
497 ->update(array(
498 'title' => $form->title . ' (#' . $newFormId . ')'
499 ));
500
501 $formMetas = wpFluent()->table('fluentform_form_meta')
502 ->where('form_id', $formId)
503 ->whereNot('meta_key', ['_total_views'])
504 ->get();
505
506 foreach ($formMetas as $meta) {
507 $metaData = [
508 'meta_key' => $meta->meta_key,
509 'value' => $meta->value,
510 'form_id' => $newFormId
511 ];
512
513 wpFluent()->table('fluentform_form_meta')->insert($metaData);
514 }
515
516 do_action('flentform_form_duplicated', $newFormId);
517
518 wp_send_json([
519 'message' => __('Form has been successfully duplicated.', 'fluentform'),
520 'form_id' => $newFormId,
521 'redirect' => admin_url('admin.php?page=fluent_forms&route=editor&form_id=' . $newFormId)
522 ], 200);
523 }
524
525 /**
526 * Validate a form only by form title
527 * @return void
528 */
529 private function validate()
530 {
531 if (!$this->request->get('title')) {
532 wp_send_json([
533 'title' => 'The title field is required.'
534 ], 422);
535 }
536 }
537
538 private function getAdminPermalink($route, $form)
539 {
540 $baseUrl = admin_url('admin.php?page=fluent_forms');
541 return $baseUrl . '&route=' . $route . '&form_id=' . $form->id;
542 }
543
544 private function getSettingsUrl($form)
545 {
546 $baseUrl = admin_url('admin.php?page=fluent_forms');
547 return $baseUrl . '&form_id=' . $form->id . '&route=settings&sub_route=form_settings#basic_settings';
548 }
549
550 public function getAllForms()
551 {
552 $fields = $this->request->get('fields');
553
554 if ($fields) {
555 $forms = $this->model
556 ->select($fields)
557 ->orderBy('created_at', 'DESC')->get();
558 } else {
559 $forms = $this->model->orderBy('created_at', 'DESC')->get();
560 }
561
562 wp_send_json($forms, 200);
563 }
564
565 private function getUnreadCount($formId)
566 {
567 return wpFluent()->table('fluentform_submissions')
568 ->where('status', 'unread')
569 ->where('form_id', $formId)
570 ->count();
571 }
572 }
573