PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 1.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v1.3
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.3, at includes/Core/Form/FormManager.php

539 lines 21.9 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\Form\ApiResponse;
16 use BitCode\BitForm\Core\Util\FileHandler;
17 use BitCode\BitForm\Core\Database\FormModel;
18 use BitCode\BitForm\Core\Database\FormEntryModel;
19 use BitCode\BitForm\Core\Database\FormEntryLogModel;
20 use BitCode\BitForm\Core\WorkFlow\WorkFlowRunHelper;
21 use BitCode\BitForm\Core\Database\FormEntryMetaModel;
22 use BitCode\BitForm\Core\Form\Validator\FormFieldValidator;
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' || $field->typ === 'html') {
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 global $wpdb;
219 $wpdb->query("START TRANSACTION");
220 $entry_id = $formEntryModel->insert(
221 array(
222 'form_id' => $this->form_id,
223 'user_id' => $user_details['id'],
224 'user_ip' => $user_details['ip'],
225 'user_device' => $user_details['device'],
226 'referer' => $user_details['page'],
227 'status' => 0,
228 'created_at' => $user_details['time']
229 )
230 );
231 $log_id = null;
232 if (is_wp_error($entry_id)) {
233 return new WP_Error('insert_error', __('Sorry, Error occured in saving form entry', 'bitform'));
234 }
235 if ($entry_id) {
236 $log_id = $formEntryLogModel->form_log_insert(
237 array(
238 "user_id" => $user_details['id'],
239 "action_type" => "create",
240 "log_type" => "entry",
241 "ip" => $user_details['ip'],
242 "form_entry_id" => $entry_id,
243 "content" => $key,
244 "form_id" => $this->form_id,
245 "created_at" => $user_details['time'],
246 )
247 );
248
249 if (is_wp_error($log_id)) {
250 $wpdb->query("ROLLBACK");
251 return new WP_Error('error_entry_log', __('Sorry, error occured in logging form entry', 'bitform'));
252 }
253 }
254 if ($entry_id) {
255 $entryMeta = new FormEntryMetaModel();
256 $submitted_fields = $this->getFormContentWithValue($submitted_data)->fields;
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 }
268 $errorInEntryMetaInsert = false;
269 $file_fields = $this->getUploadFields();
270 $fileHandler = new FileHandler;
271 foreach ($_FILES as $file_name => $file_details) {
272 if ($file_fields && in_array($file_name, $file_fields)) {
273 $filePath = $fileHandler->moveUploadedFiles($file_details, $this->form_id, $entry_id);
274 if (!empty($filePath)) {
275 $submitted_data[$file_name] = $filePath;
276 }
277 }
278 }
279 foreach ($submitted_data as $key => $value) {
280 $status = $entryMeta->insert(
281 array(
282 'bitforms_form_entry_id' => $entry_id,
283 'meta_key' => $key,
284 'meta_value' => is_string($submitted_data[$key]) ?
285 $submitted_data[$key] :
286 wp_json_encode($submitted_data[$key])
287 )
288 );
289 if (is_wp_error($status)) {
290 $errorInEntryMetaInsert = true;
291 break;
292 }
293 }
294
295 if ($errorInEntryMetaInsert) {
296 $wpdb->query("ROLLBACK");
297 return new WP_Error('insert_error', __('Sorry, Error occured in saving form entry data', 'bitform'));
298 }
299 $wpdb->query("COMMIT");
300 $workFlowreturnedOnSubmit['fields'] = $submitted_data;
301 return $workFlowreturnedOnSubmit;
302 }
303 }
304
305 public function updateFormEntry($updatedValue, $formID, $entryID)
306 {
307 // var_dump($updatedValue);
308 $formEntryModel = new FormEntryModel();
309 $formEntryLogModel = new FormEntryLogModel();
310 $formOldData = $formEntryLogModel->get_form_value($entryID);
311 $key = null;
312 $entryMeta = new FormEntryMetaModel();
313 $ipTool = new IpTool();
314 $user_details = $ipTool->getUserDetail();
315 $form_fields = $this->getFields();
316 $field_map = [];
317 foreach ($formOldData as $index => $data) {
318 foreach ($form_fields as $field_key => $field) {
319 if ($data->meta_key == $field['key']) {
320 $field_map[$field_key] = $field['key'];
321 }
322 }
323 }
324 $formFieldValidator = new FormFieldValidator($form_fields, $updatedValue, $_FILES);
325 $validateField = $formFieldValidator->validate('edit', $formID);
326 if (!$validateField) {
327 $errorMessage = count($formFieldValidator->getMessage()) > 0 ?
328 $formFieldValidator->getMessage() : __("Internal error occured!!!", 'bitform');
329 return new WP_Error('validation_error', $errorMessage);
330 }
331 $formEntry = $formEntryModel->update(
332 array(
333 "user_id" => $user_details['id'],
334 "user_ip" => $user_details['ip'],
335 "user_device" => $user_details['device'],
336 "status" => 0,
337 "updated_at" => $user_details['time']
338 ),
339 array(
340 'form_id' => $formID,
341 'id' => $entryID
342 )
343 );
344 $log_id = null;
345 if ($formEntry) {
346 $log_id = $formEntryLogModel->form_log_insert(
347 array(
348 "user_id" => $user_details['id'],
349 "action_type" => "update",
350 "log_type" => "entry",
351 "ip" => $user_details['ip'],
352 "form_entry_id" => $entryID,
353 "form_id" => $formID,
354 "created_at" => $user_details['time'],
355 )
356 );
357 }
358
359 if (is_wp_error($formEntry) || !$formEntry) {
360 return new WP_Error('empty_form', __('provided form entries does not exists', 'bitform'));
361 }
362
363 $file_fields = $this->getUploadFields();
364 if (count($file_fields) > 0) {
365 $fileHandler = new FileHandler();
366 foreach ($file_fields as $file_name) {
367 if (isset($updatedValue[$file_name . '_old'])) {
368 $file_exists = $entryMeta->get(
369 'meta_value',
370 array(
371 'bitforms_form_entry_id' => $entryID,
372 'meta_key' => $file_name
373 )
374 );
375 if (!is_wp_error($file_exists) && count($file_exists) > 0) {
376 $files_in_db = json_decode($file_exists[0]->meta_value);
377 $files_old = empty($updatedValue[$file_name . '_old']) ? [] : explode(',', $updatedValue[$file_name . '_old']);
378 $deleted_file = array_diff($files_in_db, $files_old);
379 if (count($deleted_file) > 0) {
380 $fileHandler->deleteFiles($formID, $entryID, $deleted_file);
381 }
382 $updatedValue[$file_name] = wp_json_encode($files_old);
383 }
384 }
385 if (!empty($_FILES[$file_name]['name'])) {
386 $meta_value = $fileHandler->moveUploadedFiles($_FILES[$file_name], $formID, $entryID);
387 if (!empty($meta_value)) {
388 if (isset($updatedValue[$file_name . '_old']) && !is_wp_error($file_exists) && count($file_exists) > 0) {
389 $meta_value = empty($files_old) ? $meta_value : array_merge($meta_value, $files_old);
390 $updatedValue[$file_name] = wp_json_encode($meta_value);
391 } else {
392 $data = array(
393 'bitforms_form_entry_id' => $entryID,
394 'meta_key' => $file_name,
395 'meta_value' => wp_json_encode($meta_value)
396 );
397 $newFileInsertStatus = $entryMeta->insert(
398 $data
399 );
400 $updatedValue[$file_name] = wp_json_encode($meta_value);
401 }
402 }
403 }
404 }
405 }
406
407
408
409 unset($updatedValue['_ajax_nonce']);
410 unset($_REQUEST['g-recaptcha-response']);
411
412 $workFlowRunHelper = new WorkFlowRunHelper($formID);
413 $workFlowreturnedOnSubmit = $workFlowRunHelper->executeOnSubmit(
414 'edit',
415 $this->getFormContentWithValue($updatedValue)->fields,
416 $updatedValue,
417 $entryID,
418 $log_id
419 );
420 if (!empty($workFlowreturnedOnSubmit['fields'])) {
421 $updatedValue = $workFlowreturnedOnSubmit['fields'];
422 }
423
424 $toUpdateValues = [];
425 foreach ($form_fields as $field) {
426 if (isset($updatedValue[$field['key']])) {
427 $toUpdateValues[$field['key']] = $updatedValue[$field['key']];
428 }
429 }
430
431 $formEntryMetaUpdateStatus = $entryMeta->update(
432 $toUpdateValues,
433 array(
434 'bitforms_form_entry_id' => $entryID
435 )
436 );
437 if (is_wp_error($formEntryMetaUpdateStatus) || isset($newFileInsertStatus) && is_wp_error($newFileInsertStatus)) {
438 return $formEntryMetaUpdateStatus;
439 }
440 $toUpdateValues = array_merge($formEntryMetaUpdateStatus, array('entry_id' => $entryID));
441 if (empty($workFlowreturnedOnSubmit['message'])) {
442 $workFlowreturnedOnSubmit['message'] = __('Entry Updated Successfully', 'bitform');
443 }
444 $workFlowreturnedOnSubmit['updatedData'] = $toUpdateValues;
445 $counter = 0;
446 for ($i = 0; $i < count($formOldData); $i++) {
447 if (array_key_exists($formOldData[$i]->meta_key . '_old', $toUpdateValues)) {
448 unset($toUpdateValues[$formOldData[$i]->meta_key . '_old']);
449 }
450 if (in_array($formOldData[$i]->meta_key, $file_fields)) {
451 if (
452 empty($_FILES[$formOldData[$i]->meta_key]['name'])
453 || (is_array($_FILES[$formOldData[$i]->meta_key]['name'])
454 && count($_FILES[$formOldData[$i]->meta_key]['name']) === 1
455 && empty($_FILES[$formOldData[$i]->meta_key]['name'][0]))
456 ) {
457 unset($toUpdateValues[$formOldData[$i]->meta_key]);
458 continue;
459 }
460 if (is_array($_FILES[$formOldData[$i]->meta_key]['name']) && !in_array($_FILES[$formOldData[$i]->meta_key]['name'], json_decode($formOldData[$i]->meta_value))) {
461 $key[$i] = '${' . $formOldData[$i]->meta_key . '} file was Updated To ' . json_encode($_FILES[$formOldData[$i]->meta_key]['name']);
462 } elseif (!is_array($_FILES[$formOldData[$i]->meta_key]['name']) && !in_array($_FILES[$formOldData[$i]->meta_key]['name'], json_decode($formOldData[$i]->meta_value))) {
463 $key[$i] = '${' . $formOldData[$i]->meta_key . '} file was Updated To ' . $_FILES[$formOldData[$i]->meta_key]['name'];
464 }
465 unset($toUpdateValues[$formOldData[$i]->meta_key]);
466 } else if (isset($toUpdateValues[$formOldData[$i]->meta_key])) {
467 if (is_array($toUpdateValues[$formOldData[$i]->meta_key])) {
468 if (json_decode($formOldData[$i]->meta_value) !== $toUpdateValues[$formOldData[$i]->meta_key]) {
469 $key[$i] = '${' . $formOldData[$i]->meta_key . '} was Updated From ' . implode(',', json_decode($formOldData[$i]->meta_value)) . ' To ' . implode(",", $toUpdateValues[$formOldData[$i]->meta_key]);
470 }
471 } elseif (is_string($toUpdateValues[$formOldData[$i]->meta_key]) && !empty($toUpdateValues[$formOldData[$i]->meta_key])) {
472 if ($formOldData[$i]->meta_value !== $toUpdateValues[$formOldData[$i]->meta_key]) {
473 $key[$i] = '${' . $formOldData[$i]->meta_key . '} was Updated' . ($formOldData[$i]->meta_value ? ' From ' . $formOldData[$i]->meta_value : '') . ' To ' . $toUpdateValues[$formOldData[$i]->meta_key];
474 }
475 }
476 }
477 $counter++;
478 }
479
480 $newField = array_keys(array_diff_key($formEntryMetaUpdateStatus, $field_map));
481 for ($i = 0; $i < count($newField); $i++) {
482 if (is_array($toUpdateValues[$newField[$i]])) {
483 if (!empty($toUpdateValues[$newField[$i]])) {
484 $key[$counter + $i] = '${' . $newField[$i] . '} Updated To ' . implode(",", $toUpdateValues[$newField[$i]]);
485 }
486 } elseif (is_string($newField[$i])) {
487 if (!empty($toUpdateValues[$newField[$i]])) {
488 $key[$counter + $i] = '${' . $newField[$i] . '} Updated To ' . $toUpdateValues[$newField[$i]];
489 }
490 }
491 }
492 if ($key !== null) {
493 $logUpdate = implode("b::f", (array) $key);
494 $formEntryLogUpdate = $formEntryLogModel->logUpdate($logUpdate, $log_id);
495 }
496 return $workFlowreturnedOnSubmit;
497 }
498
499 public function setSubmissionCount($countStep = 1)
500 {
501 $update_status = $this->formModel->update(
502 array(
503 'entries' => intval(static::$form[0]->entries) + $countStep
504 ),
505 array(
506 'id' => $this->form_id
507 )
508 );
509 }
510 public function resetSubmissionCount($countStep)
511 {
512 $update_status = $this->formModel->update(
513 array(
514 'entries' => intval($countStep)
515 ),
516 array(
517 'id' => $this->form_id
518 )
519 );
520 }
521
522 public function getCaptchaSettings()
523 {
524 $formContents = $this->getFormContent();
525 $fieldStr = wp_json_encode($formContents->fields);
526 if (strpos($fieldStr, '"typ":"recaptcha"') !== false) {
527 return true;
528 }
529 }
530 public function isGCLIDEnabled()
531 {
532 $formContents = $this->getFormContent();
533 if (isset($formContents->additional->enabled->captureGCLID) && $formContents->additional->enabled->captureGCLID) {
534 return true;
535 }
536 return false;
537 }
538 }
539