PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.21
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.21
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 3.6.66 All 195 releases
fluentform / app / Modules / Form / Form.php

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

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