PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.62
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.62
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.62, at app/Modules/Form/Form.php

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