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

577 lines 17.2 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 /**
345 * Find/Read a from from the database
346 * @return void
347 */
348 public function find()
349 {
350 $form = $this->fetchForm($this->request->get('formId'));
351 wp_send_json(['form' => $form, 'metas' => []], 200);
352 }
353
354 /**
355 * Fetch a from from the database
356 * Note: required for ninja-tables
357 * @return mixed
358 */
359 public function fetchForm($formId)
360 {
361 return $this->model->find($formId);
362 }
363
364 /**
365 * Save/update a form from backend/editor
366 * @return void
367 * @throws \WpFluent\Exception
368 */
369 public function update()
370 {
371 $formId = $this->request->get('formId');
372 $title = $this->request->get('title');
373 $status = $this->request->get('status', 'published');
374
375 $this->validate();
376
377 $data = [
378 'title' => $title,
379 'status' => $status,
380 'updated_at' => current_time('mysql')
381 ];
382
383
384 if ($formFields = $this->request->get('formFields')) {
385 $formFields = apply_filters('fluentform_form_fields_update', $formFields, $formId);
386 $data['form_fields'] = $formFields;
387 }
388
389 $this->model->where('id', $formId)->update($data);
390
391 $form = $this->fetchForm($formId);
392
393 if (FormFieldsParser::hasPaymentFields($form)) {
394 $this->model->where('id', $formId)->update([
395 'has_payment' => 1
396 ]);
397 } else if ($form->has_payment) {
398 $this->model->where('id', $formId)->update([
399 'has_payment' => 0
400 ]);
401 }
402
403 wp_send_json([
404 'message' => __('The form is successfully updated.', 'fluentform')
405 ], 200);
406 }
407
408 /**
409 * Delete a from from database
410 * @return void
411 * @throws \WpFluent\Exception
412 */
413 public function delete()
414 {
415 $formId = $this->request->get('formId');
416
417 $this->model->where('id', $formId)->delete();
418
419 $maybeErrors = $this->deleteFormAssests($formId);
420
421
422 wp_send_json([
423 'message' => __('Successfully deleted the form.', 'fluentform'),
424 'errors' => $maybeErrors
425 ], 200);
426 }
427
428
429 protected function deleteFormAssests($formId)
430 {
431 // Now Let's delete associate items
432 wpFluent()->table('fluentform_submissions')
433 ->where('form_id', $formId)
434 ->delete();
435
436 wpFluent()->table('fluentform_submission_meta')
437 ->where('form_id', $formId)
438 ->delete();
439
440 wpFluent()->table('fluentform_entry_details')
441 ->where('form_id', $formId)
442 ->delete();
443
444 wpFluent()->table('fluentform_form_analytics')
445 ->where('form_id', $formId)
446 ->delete();
447
448 wpFluent()->table('fluentform_logs')
449 ->where('parent_source_id', $formId)
450 ->whereIn('source_type', ['submission_item', 'form_item'])
451 ->delete();
452
453 ob_start();
454 if (defined('FLUENTFORMPRO')) {
455 try {
456 wpFluent()->table('fluentform_order_items')
457 ->where('form_id', $formId)
458 ->delete();
459 wpFluent()->table('fluentform_subscriptions')
460 ->where('form_id', $formId)
461 ->delete();
462 wpFluent()->table('fluentform_transactions')
463 ->where('form_id', $formId)
464 ->delete();
465 } catch (\Exception $exception) {
466
467 }
468 }
469 $errors = ob_get_clean();
470 return $errors;
471 }
472
473 /**
474 * Duplicate a from
475 * @return void
476 * @throws \WpFluent\Exception
477 */
478 public function duplicate()
479 {
480 $formId = absint($this->request->get('formId'));
481 $form = $this->model->where('id', $formId)->first();
482
483 $data = array(
484 'title' => $form->title,
485 'status' => $form->status,
486 'appearance_settings' => $form->appearance_settings,
487 'form_fields' => $form->form_fields,
488 'type' => $form->type,
489 'has_payment' => $form->has_payment,
490 'conditions' => $form->conditions,
491 'created_by' => get_current_user_id(),
492 'created_at' => current_time('mysql'),
493 'updated_at' => current_time('mysql')
494 );
495
496 $newFormId = $this->model->insert($data);
497
498 // Ranme the form name here
499 wpFluent()->table('fluentform_forms')
500 ->where('id', $newFormId)
501 ->update(array(
502 'title' => $form->title . ' (#' . $newFormId . ')'
503 ));
504
505 $formMetas = wpFluent()->table('fluentform_form_meta')
506 ->where('form_id', $formId)
507 ->whereNot('meta_key', ['_total_views'])
508 ->get();
509
510 foreach ($formMetas as $meta) {
511 $metaData = [
512 'meta_key' => $meta->meta_key,
513 'value' => $meta->value,
514 'form_id' => $newFormId
515 ];
516
517 wpFluent()->table('fluentform_form_meta')->insert($metaData);
518 }
519
520 do_action('flentform_form_duplicated', $newFormId);
521
522 wp_send_json([
523 'message' => __('Form has been successfully duplicated.', 'fluentform'),
524 'form_id' => $newFormId,
525 'redirect' => admin_url('admin.php?page=fluent_forms&route=editor&form_id=' . $newFormId)
526 ], 200);
527 }
528
529 /**
530 * Validate a form only by form title
531 * @return void
532 */
533 private function validate()
534 {
535 if (!$this->request->get('title')) {
536 wp_send_json([
537 'title' => 'The title field is required.'
538 ], 422);
539 }
540 }
541
542 private function getAdminPermalink($route, $form)
543 {
544 $baseUrl = admin_url('admin.php?page=fluent_forms');
545 return $baseUrl . '&route=' . $route . '&form_id=' . $form->id;
546 }
547
548 private function getSettingsUrl($form)
549 {
550 $baseUrl = admin_url('admin.php?page=fluent_forms');
551 return $baseUrl . '&form_id=' . $form->id . '&route=settings&sub_route=form_settings#basic_settings';
552 }
553
554 public function getAllForms()
555 {
556 $fields = $this->request->get('fields');
557
558 if ($fields) {
559 $forms = $this->model
560 ->select($fields)
561 ->orderBy('created_at', 'DESC')->get();
562 } else {
563 $forms = $this->model->orderBy('created_at', 'DESC')->get();
564 }
565
566 wp_send_json($forms, 200);
567 }
568
569 private function getUnreadCount($formId)
570 {
571 return wpFluent()->table('fluentform_submissions')
572 ->where('status', 'unread')
573 ->where('form_id', $formId)
574 ->count();
575 }
576 }
577