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 / Settings / FormSettings.php

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

170 lines 4.8 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\Settings;
4
5 use FluentForm\App\Modules\Acl\Acl;
6 use FluentForm\App\Modules\Form\Form;
7 use FluentForm\Framework\Helpers\ArrayHelper;
8 use FluentForm\Framework\Foundation\Application;
9 use FluentForm\App\Modules\Form\Settings\Validator\Validator;
10
11 class FormSettings
12 {
13 /**
14 * @var \FluentForm\Framework\Request\Request
15 */
16 private $request;
17
18 private $app;
19
20 /**
21 * @var int form ID.
22 */
23 private $formId;
24
25 /**
26 * The settings (fluentform_form_meta) query builder.
27 *
28 * @var \WpFluent\QueryBuilder\QueryBuilderHandler
29 */
30 private $settingsQuery;
31
32 /**
33 * Construct the object
34 * @throws \Exception
35 */
36 public function __construct(Application $application)
37 {
38 $this->app = $application;
39
40 $this->request = $application->request;
41 $this->formId = intval($this->request->get('form_id'));
42
43 $this->settingsQuery = wpFluent()->table('fluentform_form_meta')->where('form_id', $this->formId);
44 }
45
46 /**
47 * Get settings for a particular form by id
48 * @return void
49 */
50 public function index()
51 {
52 $metaKey = sanitize_text_field($this->request->get('meta_key'));
53
54 // We'll always try to get a collection for a given meta key.
55 // Acknowledging that a certain meta key can have multiple
56 // results. The developer using the api knows beforehand
57 // that whether the expected result contains multiple
58 // or one value. The developer will access that way.
59 $query = $this->settingsQuery->where('meta_key', $metaKey);
60
61 $result = $query->get();
62
63 foreach ($result as $item) {
64 $item->value = json_decode($item->value, true);
65 if (isset($item->value['layout']) && !isset($item->value['layout']['asteriskPlacement'])) {
66 $item->value['layout']['asteriskPlacement'] = 'asterisk-right';
67 }
68 }
69
70 wp_send_json_success(['result' => $result], 200);
71 }
72
73
74 public function getGeneralSettingsAjax()
75 {
76 $formId = intval($this->request->get('form_id'));
77 $form = new Form($this->app);
78 $settings = [
79 'generalSettings' => $form->getFormsDefaultSettings($formId),
80 'advancedValidationSettings' => $form->getAdvancedValidationSettings($formId)
81 ];
82
83 wp_send_json_success($settings, 200);
84 }
85
86 public function saveGeneralSettingsAjax()
87 {
88 $formId = intval($this->request->get('form_id'));
89 $form = new Form($this->app);
90 $formSettings = \json_decode($this->request->get('formSettings'), true);
91 $advancedValidationSettings = \json_decode($this->request->get('advancedValidationSettings'), true);
92
93 Validator::validate(
94 'confirmations',
95 ArrayHelper::get($formSettings, 'confirmation', [])
96 );
97
98 $form->updateMeta($formId, 'formSettings', $formSettings);
99 $form->updateMeta($formId, 'advancedValidationSettings', $advancedValidationSettings);
100
101 wp_send_json_success([
102 'message' => __('Settings has been saved.', 'fluentform')
103 ], 200);
104
105 }
106
107 /**
108 * Save settings/meta for a form in database
109 */
110 public function store()
111 {
112 $value = $this->request->get('value', '');
113
114 $valueArray = $value ? json_decode($value, true) : [];
115
116 $key = sanitize_text_field($this->request->get('meta_key'));
117
118 if ($key == 'formSettings') {
119 Validator::validate(
120 'confirmations', ArrayHelper::get(
121 $valueArray, 'confirmation', []
122 )
123 );
124 } else {
125 Validator::validate($key, $valueArray);
126 }
127
128 $data = [
129 'meta_key' => $key,
130 'value' => $value,
131 'form_id' => $this->formId
132 ];
133
134 // If the request has an valid id field it's safe to assume
135 // that the user wants to update an existing settings.
136 // So, we'll proceed to do so by finding it first.
137 $id = intval($this->request->get('id'));
138
139 if ($id) {
140 $settings = $this->settingsQuery->find($id);
141 }
142
143 if (isset($settings)) {
144 $this->settingsQuery->where('id', $settings->id)->update($data);
145 $insertId = $settings->id;
146 } else {
147 $insertId = $this->settingsQuery->insert($data);
148 }
149
150 wp_send_json_success([
151 'message' => __('Settings has been saved.', 'fluentform'),
152 'settings' => json_decode($value, true),
153 'id' => $insertId
154 ], 200);
155 }
156
157 /**
158 * Delete settings/meta from database for a given form
159 * @return void
160 */
161 public function remove()
162 {
163 $id = intval($this->request->get('id'));
164
165 $this->settingsQuery->where('id', $id)->delete();
166
167 wp_send_json([], 200);
168 }
169 }
170