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

570 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 $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
420 // Now Let's delete associate items
421 wpFluent()->table('fluentform_submissions')
422 ->where('form_id', $formId)
423 ->delete();
424
425 wpFluent()->table('fluentform_submission_meta')
426 ->where('form_id', $formId)
427 ->delete();
428
429 wpFluent()->table('fluentform_entry_details')
430 ->where('form_id', $formId)
431 ->delete();
432
433 wpFluent()->table('fluentform_form_analytics')
434 ->where('form_id', $formId)
435 ->delete();
436
437 wpFluent()->table('fluentform_logs')
438 ->where('parent_source_id', $formId)
439 ->whereIn('source_type', ['submission_item', 'form_item'])
440 ->delete();
441
442 ob_start();
443 if (defined('FLUENTFORMPRO')) {
444 try {
445 wpFluent()->table('fluentform_order_items')
446 ->where('form_id', $formId)
447 ->delete();
448 wpFluent()->table('fluentform_subscriptions')
449 ->where('form_id', $formId)
450 ->delete();
451 wpFluent()->table('fluentform_transactions')
452 ->where('form_id', $formId)
453 ->delete();
454 } catch (\Exception $exception) {
455
456 }
457 }
458 $errors = ob_get_clean();
459
460 wp_send_json([
461 'message' => __('Successfully deleted the form.', 'fluentform'),
462 'errors' => $errors
463 ], 200);
464 }
465
466 /**
467 * Duplicate a from
468 * @return void
469 * @throws \WpFluent\Exception
470 */
471 public function duplicate()
472 {
473 $formId = absint($this->request->get('formId'));
474 $form = $this->model->where('id', $formId)->first();
475
476 $data = array(
477 'title' => $form->title,
478 'status' => $form->status,
479 'appearance_settings' => $form->appearance_settings,
480 'form_fields' => $form->form_fields,
481 'type' => $form->type,
482 'has_payment' => $form->has_payment,
483 'conditions' => $form->conditions,
484 'created_by' => get_current_user_id(),
485 'created_at' => current_time('mysql'),
486 'updated_at' => current_time('mysql')
487 );
488
489 $newFormId = $this->model->insert($data);
490
491 // Ranme the form name here
492 wpFluent()->table('fluentform_forms')
493 ->where('id', $newFormId)
494 ->update(array(
495 'title' => $form->title . ' (#' . $newFormId . ')'
496 ));
497
498 $formMetas = wpFluent()->table('fluentform_form_meta')
499 ->where('form_id', $formId)
500 ->whereNot('meta_key', ['_total_views'])
501 ->get();
502
503 foreach ($formMetas as $meta) {
504 $metaData = [
505 'meta_key' => $meta->meta_key,
506 'value' => $meta->value,
507 'form_id' => $newFormId
508 ];
509
510 wpFluent()->table('fluentform_form_meta')->insert($metaData);
511 }
512
513 do_action('flentform_form_duplicated', $newFormId);
514
515 wp_send_json([
516 'message' => __('Form has been successfully duplicated.', 'fluentform'),
517 'form_id' => $newFormId,
518 'redirect' => admin_url('admin.php?page=fluent_forms&route=editor&form_id=' . $newFormId)
519 ], 200);
520 }
521
522 /**
523 * Validate a form only by form title
524 * @return void
525 */
526 private function validate()
527 {
528 if (!$this->request->get('title')) {
529 wp_send_json([
530 'title' => 'The title field is required.'
531 ], 422);
532 }
533 }
534
535 private function getAdminPermalink($route, $form)
536 {
537 $baseUrl = admin_url('admin.php?page=fluent_forms');
538 return $baseUrl . '&route=' . $route . '&form_id=' . $form->id;
539 }
540
541 private function getSettingsUrl($form)
542 {
543 $baseUrl = admin_url('admin.php?page=fluent_forms');
544 return $baseUrl . '&form_id=' . $form->id . '&route=settings&sub_route=form_settings#basic_settings';
545 }
546
547 public function getAllForms()
548 {
549 $fields = $this->request->get('fields');
550
551 if ($fields) {
552 $forms = $this->model
553 ->select($fields)
554 ->orderBy('created_at', 'DESC')->get();
555 } else {
556 $forms = $this->model->orderBy('created_at', 'DESC')->get();
557 }
558
559 wp_send_json($forms, 200);
560 }
561
562 private function getUnreadCount($formId)
563 {
564 return wpFluent()->table('fluentform_submissions')
565 ->where('status', 'unread')
566 ->where('form_id', $formId)
567 ->count();
568 }
569 }
570