PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 1.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v1.2
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Form / FormManager.php

FormManager.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 1.2, at includes/Core/Form/FormManager.php

513 lines 20.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Get set Form,fields
5 */
6
7 namespace BitCode\BitForm\Core\Form;
8
9 /**
10 * FrontendFormManager class
11 */
12
13 use WP_Error;
14 use BitCode\BitForm\Core\Util\IpTool;
15 use BitCode\BitForm\Core\Util\FileHandler;
16 use BitCode\BitForm\Core\Database\FormModel;
17 use BitCode\BitForm\Core\Database\FormEntryModel;
18 use BitCode\BitForm\Core\WorkFlow\WorkFlowRunHelper;
19 use BitCode\BitForm\Core\Database\FormEntryMetaModel;
20 use BitCode\BitForm\Core\Form\Validator\FormFieldValidator;
21 use BitCode\BitForm\Core\Database\FormEntryLogModel;
22 use BitCode\BitForm\Core\Form\ApiResponse;
23 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
24
25 class FormManager
26 {
27 protected static $form;
28 protected $formModel;
29 protected $form_id;
30 private $_has_upload;
31 private $_field_label;
32 private $_fields;
33 public function __construct($form_id)
34 {
35 $this->form_id = $form_id;
36 $this->formModel = new FormModel();
37
38 static::$form = $this->formModel->get(
39 [
40 "id",
41 "form_content",
42 "form_name",
43 'created_at',
44 'views',
45 'entries',
46 'status'
47 ],
48 array(
49 'id' => $form_id
50 )
51 );
52 }
53
54 public function isExist()
55 {
56 return (!static::$form || is_wp_error(static::$form)) ? false : true;
57 }
58
59 public function checkStatus()
60 {
61 return static::$form[0]->status === '1' ? true : false;
62 }
63
64 public function getFormContentWithValue($defaultValues)
65 {
66 $form_content = \json_decode(static::$form[0]->form_content);
67 if (!is_array($defaultValues)) {
68 return $form_content;
69 }
70 foreach ($form_content->fields as $fieldKey => $fieldDetails) {
71 // $field_name = empty($fieldDetails->lbl) ? null : \preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\!]/', '_', $fieldDetails->lbl);
72 if ((isset($fieldDetails->mul) || $fieldDetails->typ === 'check') && isset($defaultValues[$fieldKey])) {
73 if (is_array($defaultValues[$fieldKey])) {
74 $fieldDetails->val =
75 wp_json_encode(
76 array_map('sanitize_text_field', $defaultValues[$fieldKey])
77 );
78 } else {
79 $fieldDetails->val = sanitize_text_field($defaultValues[$fieldKey]);
80 }
81 } elseif (isset($defaultValues[$fieldKey])) {
82 $fieldDetails->val = is_string($defaultValues[$fieldKey]) ?
83 sanitize_text_field($defaultValues[$fieldKey]) :
84 sanitize_text_field($defaultValues[$fieldKey][count($defaultValues[$fieldKey]) - 1]);
85 }
86 }
87 return $form_content;
88 }
89
90 public function getFormContent()
91 {
92 return json_decode(static::$form[0]->form_content);
93 }
94
95 public function getFormName()
96 {
97 return static::$form[0]->form_name;
98 }
99
100 public function getFields()
101 {
102 if (!is_null($this->_fields)) {
103 return $this->_fields;
104 }
105 $form_content = \json_decode(static::$form[0]->form_content);
106 $layout = $form_content->layout;
107 $fields = $form_content->fields;
108 $field_details = array();
109 foreach ($fields as $key => $field) {
110 if ($field->typ === 'recaptcha') {
111 continue;
112 }
113 // $field_name = empty($field->lbl) ? null : \preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\\!]/', '_', $field->lbl);
114 $field_type = $field->typ;
115 $field_details[$key]['label'] = empty($field->lbl) ? null : $field->lbl;
116 $field_details[$key]['type'] = $field_type;
117 $field_details[$key]['key'] = $key;
118 if (isset($field->mul)) {
119 $field_details[$key]['mul'] = $field->mul;
120 }
121 if ($field_type === 'file-up' && isset($field->exts)) {
122 $field_details[$key]['valid']['type'] = $field->exts;
123 }
124 if (isset($field->valid) && !is_null($field->valid)) {
125 if (isset($field->valid->req)) {
126 $field_details[$key]['valid']['req'] = $field->valid->req;
127 }
128 if (isset($field->valid->reqMsg)) {
129 $field_details[$key]['valid']['reqMsg'] = $field->valid->reqMsg;
130 }
131 if (isset($field->valid->typMsg)) {
132 $field_details[$key]['valid']['typMsg'] = $field->valid->typMsg;
133 }
134 }
135 }
136 if ($this->isGCLIDEnabled()) {
137 $field_details['GCLID']['name'] = 'GCLID';
138 $field_details['GCLID']['adminLbl'] = 'GCLID';
139 $field_details['GCLID']['key'] = 'GCLID';
140 $field_details['GCLID']['type'] = 'hidden';
141 }
142 $this->_fields = $field_details;
143 return $field_details;
144 }
145 public function getFieldsKey()
146 {
147 $form_content = \json_decode(static::$form[0]->form_content);
148 $fields = $form_content->fields;
149 $field_details = array();
150 foreach ($fields as $key => $field) {
151 if ($field->typ === 'recaptcha') {
152 continue;
153 }
154 // $field_name = empty($field->lbl) ? null : \preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\\!]/', '_', $field->lbl);
155 $field_details[$key] = $key;
156 }
157 if ($this->isGCLIDEnabled()) {
158 $field_details['GCLID'] = 'GCLID';
159 }
160 return $field_details;
161 }
162
163 public function getFieldLabel()
164 {
165 if (!is_null($this->_field_label)) {
166 return $this->_field_label;
167 }
168 $form_content = \json_decode(static::$form[0]->form_content);
169 $layout = $form_content->layout;
170 $fields = $form_content->fields;
171 $field_details = array();
172 $fieldCounter = 0;
173 foreach ($fields as $key => $field) {
174 if ($field->typ === 'recaptcha') {
175 continue;
176 }
177 // $field_name = empty($field->lbl) ? null : preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\\!]/', '_', $field->lbl);
178 $field_details[$fieldCounter]['name'] = empty($field->lbl) ? null : $field->lbl;
179 $field_details[$fieldCounter]['adminLbl'] = empty($field->adminLbl) ? $field_details[$fieldCounter]['name'] : $field->adminLbl;
180 $field_details[$fieldCounter]['key'] = $key;
181 $field_details[$fieldCounter]['type'] = $field->typ;
182 $fieldCounter += 1;
183 }
184 if ($this->isGCLIDEnabled()) {
185 $field_details[$fieldCounter]['name'] = 'GCLID';
186 $field_details[$fieldCounter]['adminLbl'] = 'GCLID';
187 $field_details[$fieldCounter]['key'] = 'GCLID';
188 $field_details[$fieldCounter]['type'] = 'hidden';
189 }
190 $this->_field_label = $field_details;
191 return $field_details;
192 }
193
194 public function getUploadFields()
195 {
196 if (!is_null($this->_has_upload)) {
197 return $this->_has_upload;
198 }
199 $upload_fields = array();
200 $form_field_details = $this->getFields();
201 foreach ($form_field_details as $field_name => $__field_detail) {
202 if (isset($__field_detail['type']) && $__field_detail['type'] === 'file-up') {
203 $upload_fields[] = $field_name;
204 }
205 }
206 $this->_has_upload = $upload_fields;
207 return $upload_fields;
208 }
209
210 public function saveFormEntry($submitted_data)
211 {
212 unset($submitted_data[$this->getFormIdentifier()]);
213 $formEntryModel = new FormEntryModel();
214 $formEntryLogModel = new FormEntryLogModel();
215 $key = null;
216 $ipTool = new IpTool();
217 $user_details = $ipTool->getUserDetail();
218 $entry_id = $formEntryModel->insert(
219 array(
220 'form_id' => $this->form_id,
221 'user_id' => $user_details['id'],
222 'user_ip' => $user_details['ip'],
223 'user_device' => $user_details['device'],
224 'referer' => $user_details['page'],
225 'status' => 0,
226 'created_at' => $user_details['time']
227 )
228 );
229 $log_id = null;
230 if ($entry_id) {
231 $log_id = $formEntryLogModel->form_log_insert(
232 array(
233 "user_id" => $user_details['id'],
234 "action_type" => "create",
235 "log_type" => "entry",
236 "ip" => $user_details['ip'],
237 "form_entry_id" => $entry_id,
238 "content" => $key,
239 "form_id" => $this->form_id,
240 "created_at" => $user_details['time'],
241 )
242 );
243 }
244 if ($entry_id) {
245 $entryMeta = new FormEntryMetaModel();
246 $submitted_fields = $this->getFormContentWithValue($submitted_data)->fields;
247 $file_fields = $this->getUploadFields();
248 $fileHandler = new FileHandler;
249 foreach ($_FILES as $file_name => $file_details) {
250 if ($file_fields && in_array($file_name, $file_fields)) {
251 $filePath = $fileHandler->moveUploadedFiles($file_details, $this->form_id, $entry_id);
252 if (!empty($filePath)) {
253 $submitted_data[$file_name] = $filePath;
254 }
255 }
256 }
257 $workFlowRunHelper = new WorkFlowRunHelper($this->form_id);
258 $workFlowreturnedOnSubmit = $workFlowRunHelper->executeOnSubmit(
259 'create',
260 $submitted_fields,
261 $submitted_data,
262 $entry_id,
263 $log_id
264 );
265 if (!empty($workFlowreturnedOnSubmit['fields'])) {
266 $submitted_data = $workFlowreturnedOnSubmit['fields'];
267 unset($workFlowreturnedOnSubmit['fields']);
268 }
269 foreach ($submitted_data as $key => $value) {
270 $entryMeta->insert(
271 array(
272 'bitforms_form_entry_id' => $entry_id,
273 'meta_key' => $key,
274 'meta_value' => is_string($submitted_data[$key]) ?
275 $submitted_data[$key] :
276 wp_json_encode($submitted_data[$key])
277 )
278 );
279 }
280 if (isset($workFlowreturnedOnSubmit['cronNotOk'])) {
281 die();
282 }
283 return $workFlowreturnedOnSubmit;
284 } else {
285 return false;
286 }
287 }
288
289 public function updateFormEntry($updatedValue, $formID, $entryID)
290 {
291 $formEntryModel = new FormEntryModel();
292 $formEntryLogModel = new FormEntryLogModel();
293 $formOldData = $formEntryLogModel->get_form_value($entryID);
294 $key = null;
295 $entryMeta = new FormEntryMetaModel();
296 $ipTool = new IpTool();
297 $user_details = $ipTool->getUserDetail();
298 $form_fields = $this->getFields();
299 $field_map = [];
300 foreach ($formOldData as $index => $data) {
301 foreach ($form_fields as $field_key => $field) {
302 if ($data->meta_key == $field['key']) {
303 $field_map[$field_key] = $field['key'];
304 }
305 }
306 }
307 $formFieldValidator = new FormFieldValidator($form_fields, $updatedValue, $_FILES);
308 $validateField = $formFieldValidator->validate('edit', $formID);
309 if (!$validateField) {
310 $errorMessage = count($formFieldValidator->getMessage()) > 0 ?
311 $formFieldValidator->getMessage() : __("Internal error occured!!!", 'bitform');
312 return new WP_Error('validation_error', $errorMessage);
313 }
314 $formEntry = $formEntryModel->update(
315 array(
316 "user_id" => $user_details['id'],
317 "user_ip" => $user_details['ip'],
318 "user_device" => $user_details['device'],
319 "status" => 0,
320 "updated_at" => $user_details['time']
321 ),
322 array(
323 'form_id' => $formID,
324 'id' => $entryID
325 )
326 );
327 $log_id = null;
328 if ($formEntry) {
329 $log_id = $formEntryLogModel->form_log_insert(
330 array(
331 "user_id" => $user_details['id'],
332 "action_type" => "update",
333 "log_type" => "entry",
334 "ip" => $user_details['ip'],
335 "form_entry_id" => $entryID,
336 "form_id" => $formID,
337 "created_at" => $user_details['time'],
338 )
339 );
340 }
341
342 if (is_wp_error($formEntry) || !$formEntry) {
343 return new WP_Error('empty_form', __('provided form entries does not exists', 'bitform'));
344 }
345
346 $file_fields = $this->getUploadFields();
347 if (count($file_fields) > 0) {
348 $fileHandler = new FileHandler();
349 foreach ($file_fields as $file_name) {
350 if (isset($updatedValue[$file_name . '_old'])) {
351 $file_exists = $entryMeta->get(
352 'meta_value',
353 array(
354 'bitforms_form_entry_id' => $entryID,
355 'meta_key' => $file_name
356 )
357 );
358 if (!is_wp_error($file_exists) && count($file_exists) > 0) {
359 $files_in_db = json_decode($file_exists[0]->meta_value);
360 $files_old = empty($updatedValue[$file_name . '_old']) ? [] : explode(',', $updatedValue[$file_name . '_old']);
361 $deleted_file = array_diff($files_in_db, $files_old);
362 if (count($deleted_file) > 0) {
363 $fileHandler->deleteFiles($formID, $entryID, $deleted_file);
364 }
365 $updatedValue[$file_name] = wp_json_encode($files_old);
366 }
367 }
368 if (!empty($_FILES[$file_name]['name'])) {
369 $meta_value = $fileHandler->moveUploadedFiles($_FILES[$file_name], $formID, $entryID);
370 if (!empty($meta_value)) {
371 if (isset($updatedValue[$file_name . '_old']) && !is_wp_error($file_exists) && count($file_exists) > 0) {
372 $meta_value = empty($files_old) ? $meta_value : array_merge($meta_value, $files_old);
373 $updatedValue[$file_name] = wp_json_encode($meta_value);
374 } else {
375 $data = array(
376 'bitforms_form_entry_id' => $entryID,
377 'meta_key' => $file_name,
378 'meta_value' => wp_json_encode($meta_value)
379 );
380 $newFileInsertStatus = $entryMeta->insert(
381 $data
382 );
383 $updatedValue[$file_name] = wp_json_encode($meta_value);
384 }
385 }
386 }
387 }
388 }
389
390
391 unset($updatedValue['_ajax_nonce']);
392 unset($_REQUEST['g-recaptcha-response']);
393 $workFlowRunHelper = new WorkFlowRunHelper($formID);
394 $workFlowreturnedOnSubmit = $workFlowRunHelper->executeOnSubmit(
395 'edit',
396 $this->getFormContentWithValue($updatedValue)->fields,
397 $updatedValue,
398 $entryID,
399 $log_id
400 );
401 if (!empty($workFlowreturnedOnSubmit['fields'])) {
402 $updatedValue = $workFlowreturnedOnSubmit['fields'];
403 unset($workFlowreturnedOnSubmit['fields']);
404 }
405 $formEntryMetaUpdateStatus = $entryMeta->update(
406 $updatedValue,
407 array(
408 'bitforms_form_entry_id' => $entryID
409 )
410 );
411 if (is_wp_error($formEntryMetaUpdateStatus) || isset($newFileInsertStatus) && is_wp_error($newFileInsertStatus)) {
412 return $formEntryMetaUpdateStatus;
413 }
414 $updatedValue = array_merge($formEntryMetaUpdateStatus, array('entry_id' => $entryID));
415 if (empty($workFlowreturnedOnSubmit['message'])) {
416 $workFlowreturnedOnSubmit['message'] = __('Entry Updated Successfully', 'bitform');
417 }
418 $workFlowreturnedOnSubmit['updatedData'] = $updatedValue;
419 $counter = 0;
420 for ($i = 0; $i < count($formOldData); $i++) {
421 if (array_key_exists($formOldData[$i]->meta_key . '_old', $updatedValue)) {
422 unset($updatedValue[$formOldData[$i]->meta_key . '_old']);
423 }
424 if (in_array($formOldData[$i]->meta_key, $file_fields)) {
425 if (
426 empty($_FILES[$formOldData[$i]->meta_key]['name'])
427 || (is_array($_FILES[$formOldData[$i]->meta_key]['name'])
428 && count($_FILES[$formOldData[$i]->meta_key]['name']) === 1
429 && empty($_FILES[$formOldData[$i]->meta_key]['name'][0]))
430 ) {
431 unset($updatedValue[$formOldData[$i]->meta_key]);
432 continue;
433 }
434 if (is_array($_FILES[$formOldData[$i]->meta_key]['name']) && !in_array($_FILES[$formOldData[$i]->meta_key]['name'], json_decode($formOldData[$i]->meta_value))) {
435 $key[$i] = '${' . $formOldData[$i]->meta_key . '} file was Updated To ' . json_encode($_FILES[$formOldData[$i]->meta_key]['name']);
436 } elseif (!is_array($_FILES[$formOldData[$i]->meta_key]['name']) && !in_array($_FILES[$formOldData[$i]->meta_key]['name'], json_decode($formOldData[$i]->meta_value))) {
437 $key[$i] = '${' . $formOldData[$i]->meta_key . '} file was Updated To ' . $_FILES[$formOldData[$i]->meta_key]['name'];
438 }
439 unset($updatedValue[$formOldData[$i]->meta_key]);
440 } else {
441 if (is_array($updatedValue[$formOldData[$i]->meta_key])) {
442 if (json_decode($formOldData[$i]->meta_value) !== $updatedValue[$formOldData[$i]->meta_key]) {
443 $key[$i] = '${' . $formOldData[$i]->meta_key . '} was Updated From ' . implode(',', json_decode($formOldData[$i]->meta_value)) . ' To ' . implode(",", $updatedValue[$formOldData[$i]->meta_key]);
444 }
445 } elseif (is_string($updatedValue[$formOldData[$i]->meta_key]) && !empty($updatedValue[$formOldData[$i]->meta_key])) {
446 if ($formOldData[$i]->meta_value !== $updatedValue[$formOldData[$i]->meta_key]) {
447 $key[$i] = '${' . $formOldData[$i]->meta_key . '} was Updated' . ($formOldData[$i]->meta_value ? ' From ' . $formOldData[$i]->meta_value : '') . ' To ' . $updatedValue[$formOldData[$i]->meta_key];
448 }
449 }
450 }
451 $counter++;
452 }
453
454 $newField = array_keys(array_diff_key($formEntryMetaUpdateStatus, $field_map));
455 for ($i = 0; $i < count($newField); $i++) {
456 if (is_array($updatedValue[$newField[$i]])) {
457 if (!empty($updatedValue[$newField[$i]])) {
458 $key[$counter + $i] = '${' . $newField[$i] . '} Updated To ' . implode(",", $updatedValue[$newField[$i]]);
459 }
460 } elseif (is_string($newField[$i])) {
461 if (!empty($updatedValue[$newField[$i]])) {
462 $key[$counter + $i] = '${' . $newField[$i] . '} Updated To ' . $updatedValue[$newField[$i]];
463 }
464 }
465 }
466 if ($key !== null) {
467 $logUpdate = implode("b::f", (array) $key);
468 $formEntryLogUpdate = $formEntryLogModel->logUpdate($logUpdate, $log_id);
469 }
470 return $workFlowreturnedOnSubmit;
471 }
472
473 public function setSubmissionCount($countStep = 1)
474 {
475 $update_status = $this->formModel->update(
476 array(
477 'entries' => intval(static::$form[0]->entries) + $countStep
478 ),
479 array(
480 'id' => $this->form_id
481 )
482 );
483 }
484 public function resetSubmissionCount($countStep)
485 {
486 $update_status = $this->formModel->update(
487 array(
488 'entries' => intval($countStep)
489 ),
490 array(
491 'id' => $this->form_id
492 )
493 );
494 }
495
496 public function getCaptchaSettings()
497 {
498 $formContents = $this->getFormContent();
499 $fieldStr = wp_json_encode($formContents->fields);
500 if (strpos($fieldStr, '"typ":"recaptcha"') !== false) {
501 return true;
502 }
503 }
504 public function isGCLIDEnabled()
505 {
506 $formContents = $this->getFormContent();
507 if (isset($formContents->additional->enabled->captureGCLID) && $formContents->additional->enabled->captureGCLID) {
508 return true;
509 }
510 return false;
511 }
512 }
513