PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 1.4
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v1.4
3.3.1 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 All 138 releases
bit-form / includes / Admin / Form / AdminFormHandler.php

AdminFormHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 1.4, at includes/Admin/Form/AdminFormHandler.php

1,942 lines 83.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Handle Form Create,Update,delete Operation
5 *
6 */
7
8 namespace BitCode\BitForm\Admin\Form;
9
10 use WP_Error;
11 use BitCode\BitForm\Core\Util\IpTool;
12 use BitCode\BitForm\Core\Util\FileHandler;
13 use BitCode\BitForm\Core\Database\FormModel;
14 use BitCode\BitForm\Core\Database\ReportsModel;
15 use BitCode\BitForm\Admin\Form\AdminFormManager;
16 use BitCode\BitForm\Core\Database\WorkFlowModel;
17 use BitCode\BitForm\Core\Database\FormEntryModel;
18 use BitCode\BitForm\Core\Util\FormDuplicateHelper;
19 use BitCode\BitForm\Core\WorkFlow\WorkFlowHandler;
20 use BitCode\BitForm\Core\Database\IntegrationModel;
21 use BitCode\BitForm\Core\Database\FormEntryLogModel;
22 use BitCode\BitForm\Core\WorkFlow\WorkFlowRunHelper;
23 use BitCode\BitForm\Core\Database\EmailTemplateModel;
24 use BitCode\BitForm\Core\Database\FormEntryMetaModel;
25 use BitCode\BitForm\Core\Database\SuccessMessageModel;
26 use BitCode\BitForm\Core\Messages\EmailTemplateHandler;
27 use BitCode\BitForm\Core\Integration\IntegrationHandler;
28 use BitCode\BitForm\Core\Messages\SuccessMessageHandler;
29 use BitCode\BitForm\Admin\Form\Template\TemplateProvider;
30 use BitCode\BitForm\Admin\Form\CustomFieldHandler;
31 class AdminFormHandler
32 {
33 private static $formModel;
34 private static $ipTool;
35 public function __construct()
36 {
37 static::$formModel = new FormModel();
38 static::$ipTool = new IpTool();
39 }
40 public function getTemplate($Request, $post)
41 {
42 $templateName = null;
43 $newFormId = null;
44 if (isset($post->template)) {
45 $templateName = $post->template;
46 $newFormId = $post->newFormId;
47 }
48
49 $templateProvider = new TemplateProvider();
50 $form_content_raw = $templateProvider->getTemplate($templateName, $newFormId);
51 if (!$form_content_raw) {
52 return new WP_Error("template_empty", __("Template not found, by this name: ", "bitform") . $templateName);
53 } else {
54 return \json_encode(
55 array(
56 'id' => 0,
57 'form_content' => $form_content_raw,
58 )
59 );
60 }
61 }
62
63 public function saveStyleSheet($styles, $sheetName)
64 {
65 if (file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles')
66 && is_writable(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles')
67 ) {
68 $createStyleFile = fopen(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . $sheetName, 'w');
69 fwrite($createStyleFile, $styles);
70 fclose($createStyleFile);
71 return true;
72 }
73 return false;
74 }
75
76 public function saveLayoutStyleSheet($layout, $sheetName, $lay_row_height = 43)
77 {
78 $md_width = 600;
79 $sm_width = 400;
80 $lg_styles = '';
81 $md_styles = '';
82 $sm_styles = '';
83 for ($i = 0; $i < count($layout->lg); $i++) {
84 $fld = $layout->lg[$i];
85
86 $class = $fld->i;
87 $lg_g_r_s = $fld->y + 1;
88 $lg_g_c_s = $fld->x + 1;
89 $lg_g_r_e = $fld->y !== 1 ? $fld->h + ($fld->y + 1) : 1;
90 $lg_g_c_e = ($fld->x + 1) + $fld->w;
91 $lg_g_r_span = $lg_g_r_e - $lg_g_r_s;
92 $lg_g_c_span = $lg_g_c_e - $lg_g_c_s;
93 $lg_min_height = $fld->h * $lay_row_height . "px;";
94
95 $lg_styles .= ".$class { grid-area: $lg_g_r_s / $lg_g_c_s / $lg_g_r_e / $lg_g_c_e ; -ms-grid-row: $lg_g_r_s; -ms-grid-row-span: $lg_g_r_span; -ms-grid-column: $lg_g_c_s; -ms-grid-column-span: $lg_g_c_span; min-height: $lg_min_height; }";
96
97 $fld = $layout->md[$i];
98
99 $class = $fld->i;
100 $md_g_r_s = $fld->y + 1;
101 $md_g_c_s = $fld->x + 1;
102 $md_g_r_e = $fld->y !== 1 ? $fld->h + ($fld->y + 1) : 1;
103 $md_g_c_e = ($fld->x + 1) + $fld->w;
104 $md_g_r_span = $lg_g_r_e - $md_g_r_s;
105 $md_g_c_span = $lg_g_c_e - $md_g_c_s;
106 $md_min_height = $fld->h * $lay_row_height . "px;";
107
108 $md_styles .= ".$class { grid-area: $md_g_r_s / $md_g_c_s / $md_g_r_e / $md_g_c_e ; -ms-grid-row: $md_g_r_s; -ms-grid-row-span: $md_g_r_span; -ms-grid-column: $md_g_c_s; -ms-grid-column-span: $md_g_c_span; min-height: $md_min_height; }";
109
110 $fld = $layout->sm[$i];
111
112 $class = $fld->i;
113 $sm_g_r_s = $fld->y + 1;
114 $sm_g_c_s = $fld->x + 1;
115 $sm_g_r_e = $fld->y !== 1 ? $fld->h + ($fld->y + 1) : 1;
116 $sm_g_c_e = ($fld->x + 1) + $fld->w;
117 $sm_g_r_span = $sm_g_r_e - $sm_g_r_s;
118 $sm_g_c_span = $sm_g_c_e - $sm_g_c_s;
119 $sm_min_height = $fld->h * $lay_row_height . "px;";
120
121 $sm_styles .= ".$class { grid-area: $sm_g_r_s / $sm_g_c_s / $sm_g_r_e / $sm_g_c_e ; -ms-grid-row: $sm_g_r_s; -ms-grid-row-span: $sm_g_r_span; -ms-grid-column: $sm_g_c_s; -ms-grid-column-span: $sm_g_c_span; min-height: $sm_min_height; }";
122 }
123 $formStyle = "._frm-g{
124 display: grid;
125 display: -ms-grid;
126 grid-template-columns: repeat( 6 , minmax( 30px , 1fr ));
127 -ms-grid-columns: minmax( 30px , 1fr ) minmax( 30px , 1fr ) minmax( 30px , 1fr ) minmax( 30px , 1fr ) minmax( 30px , 1fr ) minmax( 30px , 1fr );
128 }
129 {$lg_styles}
130 @media only screen and (max-width:{$md_width}px) {
131 ._frm-g {grid-template-columns: repeat( 4 , minmax( 30px , 1fr ));-ms-grid-columns: minmax( 30px , 1fr ) minmax( 30px , 1fr ) minmax( 30px , 1fr ) minmax( 30px , 1fr );}
132 {$md_styles}
133 }
134 @media only screen and (max-width:{$sm_width}px) {
135 ._frm-g {grid-template-columns: repeat( 2 , minmax( 30px , 1fr ));-ms-grid-columns: minmax( 30px , 1fr ) minmax( 30px , 1fr );}
136 {$sm_styles}
137 }";
138
139 if (file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles')
140 && is_writable(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles')
141 ) {
142 $createStyleFile = fopen(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . $sheetName, 'w');
143 fwrite($createStyleFile, $formStyle);
144 fclose($createStyleFile);
145 return true;
146 }
147 return false;
148 }
149
150 public function createNewForm($Request, $post)
151 {
152 if (!isset($post->form_id) || $post->form_id == '') {
153 return new WP_Error('empty_form', __('Error Occurred, Please Reload', 'bitform'));
154 }
155
156 $formManager = new AdminFormManager($post->form_id);
157 if ($formManager->isExist()) {
158 return new WP_Error('empty_form', __('Error Occurred, Please Reload', 'bitform'));
159 }
160
161 if (!class_exists("BitCode\\BitFormPro\\Plugin")) {
162 if (!empty($workFlows) && count($workFlows) > 2) {
163 return new WP_Error(
164 'free_limit',
165 __('You are allowed to add maximum 2 workflows ', 'bitform')
166 );
167 }
168 }
169
170 if (isset($post->formStyle) && $post->formStyle) {
171 $sheetName = 'bitform-' . $post->form_id . '.css';
172 $this->saveStyleSheet($post->formStyle, $sheetName);
173 }
174
175 if (isset($post->layoutChanged) && $post->layoutChanged) {
176 $sheetName = 'bitform-layout-' . $post->form_id . '.css';
177 $rowHeight = (int)$post->rowHeight;
178 $this->saveLayoutStyleSheet($post->layout, $sheetName, $rowHeight);
179 }
180
181 $fields = wp_unslash($post->fields);
182 $layout = wp_unslash($post->layout);
183 $form_name = wp_unslash($post->form_name);
184 $formSettings = (object) wp_unslash($post->formSettings);
185 $workFlows = wp_unslash($post->workFlows);
186 $additional = wp_unslash($post->additional);
187 $reports = !empty($post->reports) ? $post->reports : (object) [];
188
189 $mailTem = $formSettings->mailTem;
190 $integrations = $formSettings->integrations;
191
192 $user_details = static::$ipTool->getUserDetail();
193 // var_dump($fields);
194 if (empty($fields) || empty($layout)) {
195 return new WP_Error(
196 'empty_form',
197 __('Can not save empty form.', 'bitform')
198 );
199 }
200 if (strlen($form_name) > 50) {
201 return new WP_Error(
202 'empty_form',
203 __('Form Name Should Be Within 50 Characters', 'bitform')
204 );
205 }
206 $form_content = array(
207 'fields' => $fields,
208 'layout' => $layout,
209 );
210
211 /* if (!empty($formSettings->submitBtn)) {
212 $form_content = array_merge($form_content, array('buttons' => $formSettings->submitBtn));
213 } */
214 if (!empty($formSettings->theme)) {
215 $form_content = array_merge($form_content, array('theme' => $formSettings->theme));
216 }
217
218 if (!empty($additional)) {
219 $form_content = array_merge($form_content, array('additional' => $additional));
220 }
221 if (!empty($workFlows)) {
222 $workFlowsStr = is_string($workFlows) ? $workFlows : json_encode($workFlows);
223 $workFlowExist = [];
224 if (\strpos($workFlowsStr, 'onload')) {
225 $workFlowExist['onload'] = true;
226 }
227 if (\strpos($workFlowsStr, 'oninput')) {
228 $workFlowExist['oninput'] = true;
229 }
230 $form_content = array_merge($form_content, array('workFlowExist' => $workFlowExist));
231 }
232 $form_content = \wp_json_encode($form_content);
233
234 $fdata = array(
235 "id" => $post->form_id,
236 "form_content" => $form_content,
237 "user_id" => $user_details['id'],
238 "user_ip" => $user_details['ip'],
239 "user_device" => $user_details['device'],
240 "status" => 1,
241 "form_name" => $form_name,
242 "created_at" => $user_details['time'],
243 "updated_at" => $user_details['time'],
244 );
245 $save_status = static::$formModel->insert($fdata);
246
247 if (is_wp_error($save_status)) {
248 return $save_status;
249 } elseif (!$save_status) {
250 return false;
251 } else {
252 wp_mkdir_p(BITFORMS_UPLOAD_DIR . "/$save_status");
253 $formID = $save_status;
254 $integartionIDForWorkflow = array();
255 // Confiramtion Message [start]
256 if (!empty($formSettings->confirmation->type->successMsg)) {
257 $successMessages = $formSettings->confirmation->type->successMsg;
258 $successMessageHandler
259 = new SuccessMessageHandler($formID, $user_details);
260 foreach ($successMessages as $messageKey => $messageDetail) {
261 $savedID = $successMessageHandler->saveMessage($messageDetail);
262 if ($savedID) {
263 $msgIdx = empty($messageDetail->id) ? $messageKey : \json_encode(["id" => $messageDetail->id]);
264 $integartionIDForWorkflow['successMsg'][$msgIdx] = $savedID;
265 }
266 }
267 unset($formSettings->confirmation->type->successMsg);
268 }
269 // return $formSettings;
270 // Confiramtion Message [end] */
271 // Email Template [start]
272 if (!empty($mailTem)) {
273 $emailTemplateHandler
274 = new EmailTemplateHandler($formID, $user_details);
275 foreach ($mailTem as $templateKey => $templateDetail) {
276 $savedID = $emailTemplateHandler->saveTemplate($templateDetail);
277 if ($savedID) {
278 $tempIdx = empty($templateDetail->id) ? $templateKey : \json_encode(["id" => $templateDetail->id]);
279 $integartionIDForWorkflow['mailNotify'][$tempIdx] = $savedID;
280 }
281 }
282 }
283 // Email Template [end] */
284 //Integration [start] */
285 $integrationHandler = new IntegrationHandler($formID, $user_details);
286 if (!empty($formSettings->confirmation->type)) {
287 $allIntegrations = $formSettings->confirmation->type;
288 foreach ($allIntegrations as $integrationType => $integrationGroup) {
289 foreach ($integrationGroup as $singleIntegrationKey => $singleIntegrationDetail) {
290 if (empty($singleIntegrationDetail->details)) {
291 $integrationName = $singleIntegrationDetail->title;
292 unset($singleIntegrationDetail->title);
293 unset($singleIntegrationDetail->id);
294 $integrationDetails = !is_string($singleIntegrationDetail) ?
295 wp_json_encode($singleIntegrationDetail) : $singleIntegrationDetail;
296 } else {
297 $integrationName = $singleIntegrationDetail->title;
298 $integrationDetails = !is_string($singleIntegrationDetail->details) ?
299 wp_json_encode($singleIntegrationDetail->details) : $singleIntegrationDetail->details;
300 }
301 $savedID = $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'form');
302 $integIdx = empty($singleIntegrationDetail->id) ? $savedID : \json_encode(["id" => $singleIntegrationDetail->id]);
303 $integartionIDForWorkflow[$integrationType][$integIdx] = $savedID;
304 }
305 }
306 }
307 if (!empty($integrations)) {
308 foreach ($integrations as $singleIntegrationKey => $singleIntegrationDetail) {
309 $integrationName = $singleIntegrationDetail->name;
310 unset($singleIntegrationDetail->name);
311 $integrationType = $singleIntegrationDetail->type;
312 unset($singleIntegrationDetail->type);
313 $singleIntegrationDetailID = empty($singleIntegrationDetail->id) ? null : $singleIntegrationDetail->id;
314 unset($singleIntegrationDetail->id);
315 if (empty($singleIntegrationDetail->details)) {
316 $integrationDetails = !is_string($singleIntegrationDetail) ?
317 wp_json_encode($singleIntegrationDetail) : $singleIntegrationDetail;
318 } else {
319 $integrationDetails = !is_string($singleIntegrationDetail->details) ?
320 wp_json_encode($singleIntegrationDetail->details) : $singleIntegrationDetail->details;
321 }
322 $savedID = $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'form');
323 $integIdx = empty($singleIntegrationDetailID) ? $singleIntegrationKey : \json_encode(["id" => $singleIntegrationDetailID]);
324 $integartionIDForWorkflow['integ'][$integIdx] = $savedID;
325 }
326 }
327 //Integrations [end]
328 if (!empty($workFlows)) {
329 $workFlowHandler = new WorkFlowHandler($formID, $user_details);
330 $workflowCount = count($workFlows);
331 while ($workflowCount) {
332 $workFlow = $workFlows[--$workflowCount];
333 $savedID = $workFlowHandler->saveworkFlow($workFlow, $integartionIDForWorkflow);
334 $newData['workflow'] = 1;
335 }
336 }
337 //wrokFlows [end]
338 if (!empty($reports)) {
339 $reportsModel = new ReportsModel();
340 foreach ($reports as $reportIndex => $report) {
341 $reportsModel->insert(
342 array(
343 "type" => 'table',
344 "category" => 'form',
345 "context" => $formID,
346 "details" => is_string($report->details) ? $report->details : wp_json_encode($report->details),
347 "isDefault" => 1,
348 "user_id" => $user_details['id'],
349 "user_ip" => $user_details['ip'],
350 "user_device" => $user_details['device'],
351 "created_at" => $user_details['time'],
352 "updated_at" => $user_details['time'],
353 )
354 );
355 $newData['reports'] = 1;
356 }
357 }
358 $updated_data = $this->getAForm(array('id' => $save_status), null);
359 $updated_data['message'] = __('Form Saved successfully', 'bitform');
360
361 return $updated_data;
362 }
363 }
364
365 public function updateForm($Request, $post)
366 {
367 if ($post->formStyle) {
368 $sheetName = 'bitform-' . $post->id . '.css';
369 $this->saveStyleSheet($post->formStyle, $sheetName);
370 }
371
372 if ($post->layoutChanged) {
373 $sheetName = 'bitform-layout-' . $post->id . '.css';
374 $rowHeight = (int)$post->rowHeight;
375 $this->saveLayoutStyleSheet($post->layout, $sheetName, $rowHeight);
376 }
377
378 if (property_exists($post, 'fields') && $post->fields !== null && property_exists($post, 'layout') && $post->layout) {
379 $fields = wp_unslash($post->fields);
380 $layout = wp_unslash($post->layout);
381 $formID = wp_unslash($post->id);
382 $form_name = wp_unslash($post->form_name);
383 $formSettings = wp_unslash($post->formSettings);
384 $workFlows = wp_unslash($post->workFlows);
385 $additional = wp_unslash($post->additional);
386 $reports = wp_unslash($post->reports);
387 }
388
389 $mailTem = $formSettings->mailTem;
390 $integrations = $formSettings->integrations;
391
392 $user_details = static::$ipTool->getUserDetail();
393 if (empty($fields) || empty($layout) || is_null($formID)) {
394 return new WP_Error('empty_form', 'Can not update empty form.');
395 }
396
397 if (!class_exists("BitCode\\BitFormPro\\Plugin")) {
398 if (count($workFlows) > 2) {
399 return new WP_Error(
400 'free_limit',
401 __('You are allowed to add maximum 2 workflows ', 'bitform')
402 );
403 }
404 }
405 $form_content = array(
406 'fields' => $fields,
407 'layout' => $layout,
408 );
409 /* if (!empty($formSettings->submitBtn)) {
410 $form_content = array_merge($form_content, array('buttons' => $formSettings->submitBtn));
411 } */
412 if (!empty($formSettings->theme)) {
413 $form_content = array_merge($form_content, array('theme' => $formSettings->theme));
414 }
415 if (!empty($additional)) {
416 $form_content = array_merge($form_content, array('additional' => $additional));
417 }
418 $integartionIDForWorkflow = array();
419 $newData['settingConfiramation'] = 0;
420 $newData['integation'] = 0;
421 $newData['mailTemplate'] = 0;
422 $newData['workflow'] = 0;
423 $newData['reports'] = 0;
424 // Confiramtion Message [start]
425 if (!empty($formSettings->confirmation->type->successMsg)) {
426 $successMessages = $formSettings->confirmation->type->successMsg;
427 $successMessageHandler
428 = new SuccessMessageHandler($formID, $user_details);
429 foreach ($successMessages as $messageKey => $messageDetail) {
430 if (empty($messageDetail->id)) {
431 $savedID = $successMessageHandler->saveMessage($messageDetail);
432 if ($savedID) {
433 $newData['settingConfiramation'] = 1;
434 $integartionIDForWorkflow['successMsg'][$messageKey] = $savedID;
435 }
436 } else {
437 $successMessageHandler->updateMessage($messageDetail);
438 }
439 }
440 unset($formSettings->confirmation->type->successMsg);
441 }
442 // return $formSettings;
443 // Confiramtion Message [end] */
444 // Email Template [start]
445 if (!empty($mailTem)) {
446 $emailTemplateHandler
447 = new EmailTemplateHandler($formID, $user_details);
448 foreach ($mailTem as $templateKey => $templateDetail) {
449 if (empty($templateDetail->id)) {
450 $savedID = $emailTemplateHandler->saveTemplate($templateDetail);
451 if (is_wp_error($savedID) && $savedID->get_error_code() === 'result_empty') {
452 $newData['mailTemplate'] = 2;
453 } else {
454 $newData['mailTemplate'] = 1;
455 $integartionIDForWorkflow['mailTem'][$templateKey] = $savedID;
456 }
457 } else {
458 $emailTemplateHandler->updateTemplate($templateDetail);
459 }
460 }
461 }
462 // return $formSettings;
463 // Email Template [end] */
464 //Integration [start] */
465 $integrationHandler = new IntegrationHandler($formID, $user_details);
466 if (!empty($formSettings->confirmation->type)) {
467 $allIntegrations = $formSettings->confirmation->type;
468 foreach ($allIntegrations as $integrationType => $integrationGroup) {
469 foreach ($integrationGroup as $singleIntegrationKey => $singleIntegrationDetail) {
470 if (empty($singleIntegrationDetail->details)) {
471 $integrationName = $singleIntegrationDetail->title;
472 unset($singleIntegrationDetail->title);
473 $singleIntegrationDetailID = empty($singleIntegrationDetail->id) ? null : $singleIntegrationDetail->id;
474 unset($singleIntegrationDetail->id);
475 $integrationDetails = !is_string($singleIntegrationDetail) ?
476 wp_json_encode($singleIntegrationDetail) : $singleIntegrationDetail;
477 } else {
478 $singleIntegrationDetailID = empty($singleIntegrationDetail->id) ? null : $singleIntegrationDetail->id;
479 unset($singleIntegrationDetail->id);
480 $integrationName = $singleIntegrationDetail->title;
481 $integrationDetails = !is_string($singleIntegrationDetail->details) ?
482 wp_json_encode($singleIntegrationDetail->details) : $singleIntegrationDetail->details;
483 }
484 if (empty($singleIntegrationDetailID)) {
485 $savedID = $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'form');
486 $newData['settingConfiramation'] = 1;
487 $integartionIDForWorkflow[$integrationType][$singleIntegrationKey] = $savedID;
488 } else {
489 $integrationHandler->updateIntegration($singleIntegrationDetailID, $integrationName, $integrationType, $integrationDetails, 'form');
490 }
491 }
492 }
493 }
494 if (!empty($integrations)) {
495 foreach ($integrations as $singleIntegrationKey => $singleIntegrationDetail) {
496 $integrationName = $singleIntegrationDetail->name;
497 unset($singleIntegrationDetail->name);
498 $integrationType = $singleIntegrationDetail->type;
499 unset($singleIntegrationDetail->type);
500 $singleIntegrationDetailID = empty($singleIntegrationDetail->id) ? null : $singleIntegrationDetail->id;
501 unset($singleIntegrationDetail->id);
502 if (empty($singleIntegrationDetail->details)) {
503 $integrationDetails = !is_string($singleIntegrationDetail) ?
504 wp_json_encode($singleIntegrationDetail) : $singleIntegrationDetail;
505 } else {
506 $integrationDetails = !is_string($singleIntegrationDetail->details) ?
507 wp_json_encode($singleIntegrationDetail->details) : $singleIntegrationDetail->details;
508 }
509 if (empty($singleIntegrationDetailID)) {
510 $savedID = $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'form');
511 $integartionIDForWorkflow[$integrationType][$singleIntegrationKey] = $savedID;
512 if (is_wp_error($savedID) && $savedID->get_error_code() !== 'result_empty') {
513 $newData['integation'] = 2;
514 } else {
515 $newData['integation'] = 1;
516 }
517 } else {
518 $integrationHandler->updateIntegration($singleIntegrationDetailID, $integrationName, $integrationType, $integrationDetails, 'form');
519 }
520 }
521 }
522 //Integrations [end]
523 //workFlows [start] */
524 $workFlowExist = [];
525 if (!empty($workFlows)) {
526 $workFlowHandler = new WorkFlowHandler($formID, $user_details);
527 // foreach ($workFlows as $workFlowIndex => $workFlow) {
528 $workflowCount = count($workFlows);
529 while ($workflowCount) {
530 $workFlow = $workFlows[--$workflowCount];
531 if ($workFlow->action_type === 'onload') {
532 $workFlowExist['onload'] = true;
533 }
534 if ($workFlow->action_type === 'oninput') {
535 $workFlowExist['oninput'] = true;
536 }
537 if (empty($workFlow->id)) {
538 $savedID = $workFlowHandler->saveworkFlow($workFlow, $integartionIDForWorkflow);
539 if (is_wp_error($savedID) && $savedID->get_error_code() !== 'result_empty') {
540 $newData['workflow'] = 2;
541 } else {
542 $newData['workflow'] = 1;
543 }
544 } else {
545 $workFlowHandler->updateworkFlow($workFlow->id, $workFlow, $integartionIDForWorkflow);
546 }
547 }
548 }
549 $form_content = array_merge($form_content, array('workFlowExist' => $workFlowExist));
550 //wrokFlows [end]
551 //reports [start] */
552 if (!empty($reports)) {
553 $reportsModel = new ReportsModel();
554 $fieldNames = [];
555 foreach ($fields as $key => $field) {
556 if (!empty($field->lbl)) {
557 // $name = preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\\!]/', '_', $field->lbl);
558 $fieldNames["$key"] = $field->lbl;
559 }
560 }
561
562 $fieldNames['__user_id'] = __('User');
563 $fieldNames['__user_ip'] = __('IP address');
564 // $fieldNames['__user_location'] = __('');
565 $fieldNames['__referer'] = __('Refer URL');
566 $fieldNames['__user_device'] = __('Device');
567 $fieldNames['__created_at'] = __('Created Time');
568 $fieldNames['__updated_at'] = __('Modified Time');
569 foreach ($reports as $reportIndex => $report) {
570 $reportDetails = $reportsModel->validateReportFields($report, $fieldNames);
571
572 if (empty($report->id)) {
573 $reportSaveStatus = $reportsModel->insert(
574 array(
575 "type" => 'table',
576 "category" => 'form',
577 "context" => $formID,
578 "details" => is_string($reportDetails) ? $reportDetails : wp_json_encode($reportDetails),
579 "isDefault" => 1,
580 "user_id" => $user_details['id'],
581 "user_ip" => $user_details['ip'],
582 "user_device" => $user_details['device'],
583 "created_at" => $user_details['time'],
584 "updated_at" => $user_details['time'],
585 )
586 );
587 if (is_wp_error($reportSaveStatus) && $reportSaveStatus->get_error_code() !== 'result_empty') {
588 $newData['reports'] = 2;
589 } else {
590 $newData['reports'] = 1;
591 }
592 } else {
593 $reportsModel->update(
594 array(
595 "type" => 'table',
596 "category" => 'form',
597 "context" => $formID,
598 "details" => is_string($reportDetails) ? $reportDetails : wp_json_encode($reportDetails),
599 "isDefault" => 1,
600 "user_id" => $user_details['id'],
601 "user_ip" => $user_details['ip'],
602 "user_device" => $user_details['device'],
603 "updated_at" => $user_details['time'],
604 ),
605 array(
606 'id' => $report->id,
607 )
608 );
609 }
610 }
611 }
612 //reports [end]
613 $form_content = wp_json_encode($form_content);
614 $update_status = static::$formModel->update(
615 array(
616 "form_name" => $form_name,
617 "form_content" => $form_content,
618 "user_id" => $user_details['id'],
619 "user_ip" => $user_details['ip'],
620 "user_device" => $user_details['device'],
621 "status" => 1,
622 "updated_at" => $user_details['time'],
623 ),
624 array(
625 "id" => $formID,
626 )
627 );
628 if (is_wp_error($update_status)) {
629 return $update_status;
630 } elseif (!$update_status) {
631 return new WP_Error('empty_form', __('Form update failed.', 'bitform'));
632 } else {
633 $updated_data = $this->getAForm(array('id' => $formID), null);
634 if ($newData['settingConfiramation'] === 0 && $newData['mailTemplate'] === 0 && $newData['integation'] === 0) {
635 unset($updated_data['formSettings']);
636 }
637 if ($newData['integation'] === 0) {
638 unset($updated_data['integrations']);
639 } elseif ($newData['integation'] === 2) {
640 $errorIN = '';
641 $errorIN .= empty($errorIN) ? 'integation' : ', integation';
642 }
643 if ($newData['mailTemplate'] === 0) {
644 unset($updated_data['mailTem']);
645 } elseif ($newData['mailTemplate'] === 2) {
646 $errorIN .= empty($errorIN) ? 'mailTemplate' : ', mailTemplate';
647 }
648 if ($newData['workflow'] === 0) {
649 unset($updated_data['workFlows']);
650 } elseif ($newData['workflow'] === 2) {
651 $errorIN .= empty($errorIN) ? 'workflow' : ', workflow';
652 }
653 if ($newData['reports'] === 2) {
654 $errorIN .= empty($errorIN) ? 'reports' : ', reports';
655 }
656 if (!empty($errorIN)) {
657 $updated_data['message'] = sprintf(__('Error Occured in saving %s', 'bitform'), $errorIN);
658 return new WP_Error('Form update Error.', $updated_data);
659 }
660 $updated_data['message'] = __('Form updated successfully.', 'bitform');
661 return $updated_data;
662 }
663 }
664 public function changeFormStatus($Request, $post)
665 {
666 if (isset($Request['status']) && $Request['id']) {
667 $status = wp_unslash($Request['status']);
668 $id = wp_unslash($Request['id']);
669 } else {
670 $status = wp_unslash($post->status);
671 $id = wp_unslash($post->id);
672 }
673 $user_details = static::$ipTool->getUserDetail();
674 // return $post->fields;
675 $status = $status === 'true' || $status === true ? true : false;
676 if (!is_bool($status) || is_null($id)) {
677 // echo $status;
678 return new WP_Error('status_change', __('Form status change failed.', 'bitform'));
679 }
680 $update_status = static::$formModel->update(
681 array(
682 "user_id" => $user_details['id'],
683 "user_ip" => $user_details['ip'],
684 "user_device" => $user_details['device'],
685 "status" => $status ? 1 : 0,
686 "updated_at" => $user_details['time'],
687 ),
688 array(
689 "id" => $id,
690 )
691 );
692 if (is_wp_error($update_status)) {
693 return $update_status;
694 } elseif (!$update_status) {
695 return false;
696 } else {
697 return __('Form status changed successfully', 'bitform');
698 }
699 }
700 public function changeBulkFormStatus($Request, $post)
701 {
702 if (isset($Request['status']) && $Request['formID']) {
703 $status = wp_unslash($Request['status']);
704 $formID = wp_unslash($Request['formID']);
705 } else {
706 $status = wp_unslash($post->status);
707 $formID = wp_unslash($post->formID);
708 }
709 $user_details = static::$ipTool->getUserDetail();
710 // return $post->fields;
711 $status = $status === 'true' || $status === true ? true : false;
712 if (!is_bool($status) || is_null($formID) || !is_array($formID)) {
713 // echo $status;
714 return new WP_Error('status_change', __('Form status change failed.', 'bitform'));
715 }
716 $update_status = static::$formModel->bulkUpdate(
717 array(
718 "user_id" => $user_details['id'],
719 "user_ip" => $user_details['ip'],
720 "user_device" => $user_details['device'],
721 "status" => $status ? 1 : 0,
722 "updated_at" => $user_details['time'],
723 ),
724 array(
725 "id" => $formID,
726 )
727 );
728 if (is_wp_error($update_status)) {
729 return $update_status;
730 } elseif (!$update_status) {
731 return false;
732 } else {
733 return __('Form status changed successfully', 'bitform');
734 }
735 }
736 public function getAllForm()
737 {
738 $get_status = static::$formModel->get(
739 array(
740 "id",
741 "status",
742 "views",
743 "entries",
744 "form_name",
745 "created_at",
746 ),
747 array(
748 "status" => array(
749 'operator' => '!=',
750 'value' => 2,
751 ),
752 )
753 );
754 if (is_wp_error($get_status)) {
755 return $get_status;
756 } elseif (!$get_status) {
757 return false;
758 } else {
759 return $get_status;
760 }
761 }
762
763 public function getAForm($Request, $post)
764 {
765 if (isset($Request['id'])) {
766 $formID = wp_unslash($Request['id']);
767 } else {
768 $formID = wp_unslash($post->id);
769 }
770 // return $post->fields;
771 if (is_null($formID)) {
772 return new WP_Error('empty_form', __('Form id is empty.', 'bitform'));
773 }
774 $formManager = new AdminFormManager($formID);
775 if (!$formManager->isExist()) {
776 return new WP_Error('not_exists', __('Form is not exists.', 'bitform'));
777 }
778 $form_content = $formManager->getFormContent();
779 if ($formManager->isExist()) {
780 $form_content_arr = array(
781 'layout' => $form_content->layout,
782 'fields' => $form_content->fields,
783 'form_name' => $formManager->getFormName(),
784 'workFlowExist' => $form_content->workFlowExist,
785 );
786 $successMessageHandler
787 = new SuccessMessageHandler($formID);
788 $successMessages = $successMessageHandler->getAllMessage();
789 if (!is_wp_error($successMessages)) {
790 foreach ($successMessages as $sucessMessagekey => $sucessMessagevalue) {
791 $allConfirmation['type']['successMsg'][$sucessMessagekey] =
792 array(
793 'id' => $sucessMessagevalue->id,
794 'title' => $sucessMessagevalue->message_title,
795 'msg' => $sucessMessagevalue->message_content,
796 );
797 }
798 }
799 $emailTemplateHandler
800 = new EmailTemplateHandler($formID);
801 $emailTemplates = $emailTemplateHandler->getAllTemplate();
802 if (!is_wp_error($emailTemplates)) {
803 foreach ($emailTemplates as $emailTemplatekey => $emailTemplatevalue) {
804 $mailTem[] =
805 array(
806 'id' => $emailTemplatevalue->id,
807 'title' => $emailTemplatevalue->title,
808 'sub' => $emailTemplatevalue->sub,
809 'body' => $emailTemplatevalue->body,
810 );
811 }
812 }
813 $integrationHandler = new IntegrationHandler($formID);
814 $formIntegrations = $integrationHandler->getAllIntegration('form');
815 if (!is_wp_error($formIntegrations)) {
816 foreach ($formIntegrations as $integrationkey => $integrationValue) {
817 if ($integrationValue->integration_type === 'redirectPage' || $integrationValue->integration_type === 'webHooks') {
818 $integrationData = array(
819 'id' => $integrationValue->id,
820 'title' => $integrationValue->integration_name,
821 );
822 if (!empty($integrationValue->integration_details)) {
823 // var_dump(json_decode($integrationValue->integration_details));
824 $integration_details = (array) json_decode($integrationValue->integration_details);
825 $integrationData = array_merge($integration_details, $integrationData);
826 } else {
827 $integration_details = array(
828 'url' => '',
829 );
830 $integrationData = array_merge($integrationData, $integration_details);
831 }
832 $allConfirmation['type'][$integrationValue->integration_type][]
833 = $integrationData;
834 } else {
835 $integrationData = array(
836 'id' => $integrationValue->id,
837 'name' => $integrationValue->integration_name,
838 'type' => $integrationValue->integration_type,
839 );
840 $integrations[] = array_merge(
841 $integrationData,
842 is_string($integrationValue->integration_details) ?
843 (array) json_decode($integrationValue->integration_details) :
844 $integrationValue->integration_details
845 );
846 }
847 }
848 }
849 $settingsContent = array(
850 'formName' => $formManager->getFormName(),
851 'theme' => $form_content->theme,
852 // 'submitBtn' => $form_content->buttons,
853 );
854
855 if (!empty($allConfirmation)) {
856 $confirmation = array(
857 'confirmation' => $allConfirmation,
858 );
859 } else {
860 $confirmation = array(
861 'confirmation' => array('type' => []),
862 );
863 }
864 $settingsContent = array_merge($settingsContent, $confirmation);
865 if (!empty($integrations)) {
866 $settingsContent = array_merge(
867 $settingsContent,
868 array(
869 'integrations' => $integrations,
870 )
871 );
872 } else {
873 $settingsContent = array_merge(
874 $settingsContent,
875 array(
876 'integrations' => []
877 )
878 );
879 }
880 if (!empty($mailTem)) {
881 $settingsContent = array_merge(
882 $settingsContent,
883 array(
884 'mailTem' => $mailTem,
885 )
886 );
887 } else {
888 $settingsContent = array_merge(
889 $settingsContent,
890 array(
891 'mailTem' => []
892 )
893 );
894 }
895 $formSettings = array(
896 'formSettings' => $settingsContent,
897 );
898 $data = array(
899 "id" => $formID,
900 "form_name" => $formManager->getFormName(),
901 "form_content" => $form_content_arr,
902 "additional" => empty($form_content->additional) ? array(
903 "enabled" => array(
904 "blocked_ip" => false,
905 "restrict_form" => false,
906 ),
907 "settings" => array(
908 "restrict_form" => array(
909 'day' => [],
910 'date' => array(
911 'from' => null,
912 'to' => null,
913 ),
914 'time' => array(
915 'from' => null,
916 'to' => null,
917 ),
918 ),
919 "entry_limit" => null,
920 "blocked_ip" => [],
921 ),
922 "onePerIp" => false,
923 ) : $form_content->additional,
924 "created_at" => $formManager->getFormMetaData()['created_at'],
925 "views" => $formManager->getFormMetaData()['views'],
926 "entries" => $formManager->getFormMetaData()['entries'],
927 "status" => $formManager->getFormMetaData()['status'],
928 );
929 $data = array_merge($data, $formSettings);
930 $workFlowHandler = new WorkFlowHandler($formID);
931 $workFlows = array('workFlows' => $workFlowHandler->getAllworkFlow());
932 $data = array_merge($data, $workFlows);
933 $reportsModel = new ReportsModel();
934 $returnedReportData = $reportsModel->get(
935 array(
936 "id",
937 "details",
938 "isDefault",
939 "type",
940 ),
941 array(
942 "category" => 'form',
943 "context" => $formID,
944 )
945 );
946 if (!is_wp_error($returnedReportData)) {
947 $fieldNames = [];
948 foreach ($formManager->getFieldLabel() as $key => $field) {
949 $fieldNames[$field['key']] = isset($field->lbl) ? $field->lbl : '';
950 }
951 if ($formManager->isGCLIDEnabled()) {
952 $fieldNames["GCLID"] = "GCLID";
953 }
954 foreach ($returnedReportData as $reportKey => $reportData) {
955 $reportDetails = $reportsModel->validateReportFields($reportData, $fieldNames);
956 if (!empty($reportDetails) && is_string($reportDetails)) {
957 $returnedReportData[$reportKey]->details = json_decode($reportDetails);
958 } elseif (!empty($reportDetails)) {
959 $returnedReportData[$reportKey]->details = $reportDetails;
960 } else {
961 $returnedReportData[$reportKey]->details = [];
962 }
963 }
964 $reports = array('reports' => $returnedReportData);
965 $data = array_merge($data, $reports);
966 }
967
968 $formFields = $formManager->getFieldLabel();
969 $data = array_merge($data, array('Labels' => $formFields));
970
971 return $data;
972 }
973 }
974
975 public function deleteAForm($Request, $post)
976 {
977 if (isset($Request['id'])) {
978 $formID = wp_unslash($Request['id']);
979 } else {
980 $formID = wp_unslash($post->id);
981 }
982 if (is_null($formID)) {
983 return new WP_Error('empty_form', __('Form id is empty.', 'bitform'));
984 }
985 $delete_status = static::$formModel->delete(
986 array(
987 "id" => $formID,
988 )
989 );
990
991 $successMessageModel
992 = new SuccessMessageModel();
993 $deleteMsgStatus = $successMessageModel->delete(['form_id' => $formID]);
994
995 $emailTemplateModel
996 = new EmailTemplateModel();
997 $deleteTemplateStatus = $emailTemplateModel->delete(['form_id' => $formID]);
998
999 $integrationModel = new IntegrationModel();
1000 $deleteIntegrationStatus = $integrationModel->delete(['form_id' => $formID]);
1001
1002 $workFlowModel = new WorkFlowModel();
1003 $deleteworkFlowStatus = $workFlowModel->delete(['form_id' => $formID]);
1004
1005 $reportsModel = new ReportsModel();
1006 $deleteReportStatus = $reportsModel->delete(['context' => $formID]);
1007
1008 $formEntryModel = new FormEntryModel();
1009 $returnedEntries = $formEntryModel->get("id", ["form_id" => $formID]);
1010 $entries = [];
1011
1012 if (!is_wp_error($returnedEntries)) {
1013 foreach ($returnedEntries as $entryKey => $entryInfo) {
1014 $entries[] = $entryInfo->id;
1015 }
1016 global $wpdb;
1017 $prefix = $wpdb->prefix;
1018 if (count($entries) > 0) {
1019 $deleteEntriesStatus = $formEntryModel->bulkDelete(
1020 array(
1021 "`{$prefix}bitforms_form_entries`.`id`" => $entries,
1022 "`{$prefix}bitforms_form_entries`.`form_id`" => $formID,
1023 )
1024 );
1025 }
1026 }
1027
1028 $fileHandler = new FileHandler();
1029 if (file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID)) {
1030 $fileHandler->rmrf(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID);
1031 }
1032 if (file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID)) {
1033 $fileHandler->rmrf(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID);
1034 }
1035 if (file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . 'bitform-' . $formID . '.css')) {
1036 unlink(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . 'bitform-' . $formID . '.css');
1037 }
1038 if (file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . 'bitform-layout-' . $formID . '.css')) {
1039 unlink(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . 'bitform-layout-' . $formID . '.css');
1040 }
1041 return is_wp_error($delete_status) ? $delete_status : __('Form deleted successfully.', 'bitform');
1042 }
1043 public function deleteBlukForm($Request, $post)
1044 {
1045 if (isset($Request['formID'])) {
1046 $formID = wp_unslash($Request['formID']);
1047 } else {
1048 $formID = wp_unslash($post->formID);
1049 }
1050 if (is_null($formID) || !is_array($formID)) {
1051 return new WP_Error('empty_form', __('Form id is empty.', 'bitform'));
1052 }
1053
1054 foreach ($formID as $id) {
1055 unlink(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . 'bitform-' . $id . '.css');
1056 unlink(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . 'bitform-layout-' . $id . '.css');
1057 }
1058 $delete_status = static::$formModel->bulkDelete(
1059 array(
1060 "id" => $formID,
1061 )
1062 );
1063 $successMessageModel
1064 = new SuccessMessageModel();
1065 $deleteMsgStatus = $successMessageModel->bulkDelete(['form_id' => $formID]);
1066
1067 $emailTemplateModel
1068 = new EmailTemplateModel();
1069 $deleteTemplateStatus = $emailTemplateModel->bulkDelete(['form_id' => $formID]);
1070
1071 $integrationModel = new IntegrationModel();
1072 $deleteIntegrationStatus = $integrationModel->bulkDelete(['form_id' => $formID]);
1073
1074 $workFlowModel = new WorkFlowModel();
1075 $deleteworkFlowStatus = $workFlowModel->bulkDelete(['form_id' => $formID]);
1076
1077 $reportsModel = new ReportsModel();
1078 $deleteReportStatus = $reportsModel->bulkDelete(['context' => $formID]);
1079
1080 $formEntryModel = new FormEntryModel();
1081 $returnedEntries = $formEntryModel->get("id", ["form_id" => $formID]);
1082 $entries = [];
1083 foreach ($returnedEntries as $entryKey => $entryInfo) {
1084 $entries[] = $entryInfo->id;
1085 }
1086 global $wpdb;
1087 $prefix = $wpdb->prefix;
1088 if (count($entries) > 0) {
1089 $deleteEntriesStatus = $formEntryModel->bulkDelete(
1090 array(
1091 "`{$prefix}bitforms_form_entries`.`id`" => $entries,
1092 "`{$prefix}bitforms_form_entries`.`form_id`" => $formID,
1093 )
1094 );
1095 }
1096
1097 $fileHandler = new FileHandler();
1098 foreach ($formID as $fId) {
1099 if (file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $fId)) {
1100 $fileHandler->rmrf(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $fId);
1101 }
1102 unlink(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . 'bitform-' . $fId . '.css');
1103 unlink(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . 'bitform-layout-' . $fId . '.css');
1104 }
1105
1106 return is_wp_error($delete_status) ? $delete_status : __('Forms deleted successfully.', 'bitform');
1107 }
1108
1109 public function genarateNewLayoutNField($layout, $fields, $oldId, $newId)
1110 {
1111 $newField = (object) [];
1112 foreach ($layout->lg as $ind => $itm) {
1113 $fld_tmp = $fields->{$layout->lg[$ind]->i};
1114 $layout->lg[$ind]->i = str_replace("bf$oldId-", "bf$newId-", $layout->lg[$ind]->i);
1115 $layout->md[$ind]->i = str_replace("bf$oldId-", "bf$newId-", $layout->md[$ind]->i);
1116 $layout->sm[$ind]->i = str_replace("bf$oldId-", "bf$newId-", $layout->sm[$ind]->i);
1117 $newField->{$layout->lg[$ind]->i} = $fld_tmp;
1118 }
1119 return ['layout' => $layout, 'fields' => $newField];
1120 }
1121
1122 public function duplicateAForm($Request, $post)
1123 {
1124 $oldFormId = intval($post->id);
1125 $newFormId = intval($post->newFormId);
1126 if ($oldFormId < 1) {
1127 return new WP_Error('empty_form', __('Form id is empty.', 'bitform'));
1128 }
1129
1130 $formManager = new AdminFormManager($oldFormId);
1131 if (!$formManager->isExist()) {
1132 return new WP_Error('empty_form', __('Form does not exists.', 'bitform'));
1133 }
1134
1135 $duplicatedForm = $this->getAForm($Request, $post);
1136 $duplicatedForm['form_name'] = 'Duplicate of ' . $duplicatedForm['form_name'];
1137 $formData = FormDuplicateHelper::createReplica($duplicatedForm, $oldFormId, $newFormId);
1138
1139
1140 /* echo wp_json_encode($formData);
1141 \wp_die(); */
1142 $duplicateResponse = $this->createNewForm(null, $formData);
1143
1144 if (!is_wp_error($duplicateResponse) || (is_wp_error($duplicateResponse) && $duplicateResponse->get_error_code() === 'result_empty')) {
1145 // duplicate stylesheet
1146 $style_dir = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR;
1147 // layout style copy
1148 $mainFormLayStyle = fopen($style_dir . 'bitform-layout-' . $oldFormId . '.css', 'r');
1149 $styleStr = fread($mainFormLayStyle, filesize($style_dir . 'bitform-layout-' . $oldFormId . '.css'));
1150 $newStyle = str_replace(".bf$oldFormId-", ".bf$newFormId-", $styleStr);
1151 $createStyleFile = fopen($style_dir . "bitform-layout-$newFormId.css", 'w');
1152 fwrite($createStyleFile, $newStyle);
1153 fclose($createStyleFile);
1154 // style copy
1155 $mainFormStyle = fopen($style_dir . 'bitform-' . $oldFormId . '.css', 'r');
1156 $styleStr = fread($mainFormStyle, filesize($style_dir . 'bitform-' . $oldFormId . '.css'));
1157 $newStyle = str_replace("-$oldFormId", "-$newFormId", $styleStr);
1158 $createStyleFile = fopen($style_dir . "bitform-$newFormId.css", 'w');
1159 fwrite($createStyleFile, $newStyle);
1160 fclose($createStyleFile);
1161 $duplicatedForm = $this->getAForm(null, (object) ['id' => $newFormId]);
1162 $duplicatedForm['message'] = __('Form duplicated successfully', 'bitform');
1163 return $duplicatedForm;
1164 }
1165 return $duplicateResponse;
1166 }
1167
1168 public function importAForm($post)
1169 {
1170
1171 $oldFormId = !empty($post->formDetail->form_id) ? $post->formDetail->form_id : null;
1172 $newFormId = intval($post->newFormId);
1173 if (!$oldFormId || empty($post->formDetail->fields) || empty($post->formDetail->layout)) {
1174 return new WP_Error('invalid_form', __('Please import a valid json.', 'bitform'));
1175 }
1176 $importtedForm = (array)$post->formDetail;
1177 $importtedForm['form_content']['fields'] = $importtedForm['fields'];
1178 $importtedForm['form_content']['layout'] = $importtedForm['layout'];
1179 unset($importtedForm['fields'], $importtedForm['layout']);
1180 $formData = FormDuplicateHelper::createReplica((array)$importtedForm, $oldFormId, $newFormId);
1181
1182 if (!empty($importtedForm['rowHeight'])) {
1183 $formData->rowHeight = $importtedForm['rowHeight'];
1184 }
1185
1186 if (!empty($importtedForm['formStyle'])) {
1187 $formData->formStyle = str_replace("-$oldFormId", "-$newFormId", $importtedForm['formStyle']);
1188 $formData->layoutChanged = '-';
1189 }
1190 $importResponse = $this->createNewForm(null, $formData);
1191
1192 if (!is_wp_error($importResponse) || (is_wp_error($importResponse) && $importResponse->get_error_code() === 'result_empty')) {
1193 $responseData = $this->getAForm(null, (object) ['id' => $newFormId]);
1194 $responseData['message'] = __('Form imported successfully.', 'bitform');
1195 return $responseData;
1196 }
1197 return $importResponse;
1198 }
1199
1200 public function exportAForm($Request)
1201 {
1202 $oldFormId = intval($Request['id']);
1203 if ($oldFormId < 1) {
1204 return new WP_Error('empty_form', __('Form id is empty.', 'bitform'));
1205 }
1206 $formManager = new AdminFormManager($oldFormId);
1207 if (!$formManager->isExist()) {
1208 return new WP_Error('empty_form', __('Form does not exists.', 'bitform'));
1209 }
1210
1211 $newFormId = $oldFormId;
1212 $duplicatedForm = $this->getAForm($Request, (object)["id" => $oldFormId]);
1213 $formData = FormDuplicateHelper::createReplica($duplicatedForm, $oldFormId, $newFormId);
1214
1215 $style_dir = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR;
1216 $mainFormStyle = fopen($style_dir . 'bitform-' . $oldFormId . '.css', 'r');
1217 $styleStr = fread($mainFormStyle, filesize($style_dir . 'bitform-' . $oldFormId . '.css'));
1218
1219 $formData->formStyle = str_replace("-$oldFormId", "-$newFormId", $styleStr);
1220 $formData->layoutChanged = "-";
1221 $formData->rowHeight = FormDuplicateHelper::calcRowHeight($styleStr, $oldFormId);
1222
1223 $formJson = json_encode($formData);
1224 header("Content-Type: application/force-download");
1225 header("Content-Type: application/octet-stream");
1226 header("Content-Type: application/download");
1227 header('Content-Disposition: attachment; filename="bitform_export_' . $oldFormId . '.json"');
1228 header('Content-Description: File Transfer');
1229 header('Expires: 0');
1230 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
1231 header('Pragma: public');
1232 header('Content-Length: ' . strlen($formJson));
1233 header("Content-Transfer-Encoding: binary ");
1234 flush();
1235 echo $formJson;
1236 die();
1237 }
1238
1239 public function getFormEntryLabelAndCount($Request, $post)
1240 {
1241 if (isset($Request['id'])) {
1242 $id = wp_unslash($Request['id']);
1243 } else {
1244 $id = wp_unslash($post->id);
1245 }
1246 if (is_null($id)) {
1247 return new WP_Error('empty_form', __('Form id is empty.', 'bitform'));
1248 }
1249 $formManager = new AdminFormManager($id);
1250 if (!$formManager->isExist()) {
1251 return new WP_Error('empty_form', __('Form does not exists.', 'bitform'));
1252 }
1253 $formFields = $formManager->getFieldLabel();
1254 $formEntry = new FormEntryModel();
1255 $count = $formEntry->count(
1256 array(
1257 'form_id' => $id,
1258 )
1259 );
1260 $reportsModel = new ReportsModel();
1261 $returnedReportData = $reportsModel->get(
1262 array(
1263 "id",
1264 "details",
1265 "isDefault",
1266 "type",
1267 ),
1268 array(
1269 "category" => 'form',
1270 "context" => $id,
1271 )
1272 );
1273 $labels = [];
1274 if (!is_wp_error($returnedReportData)) {
1275 $fieldNames = [];
1276 foreach ($formFields as $field) {
1277 $fieldNames[$field['key']] = $field['name'];
1278 $labels[$field['key']] = $field;
1279 }
1280 foreach ($returnedReportData as $reportKey => $reportData) {
1281 $reportDetails = $reportsModel->validateReportFields($reportData, $fieldNames);
1282 if (!empty($reportDetails) && is_string($reportDetails)) {
1283 $returnedReportData[$reportKey]->details = json_decode($reportDetails);
1284 } elseif (!empty($reportDetails)) {
1285 $returnedReportData[$reportKey]->details = $reportDetails;
1286 }
1287 }
1288 $response['reports'] = empty($returnedReportData) ? [] : $returnedReportData;
1289 }
1290
1291 $response['count'] = intval($count[0]->count);
1292 $response['Labels'] = $formFields;
1293 $response['fieldDetails'] = $labels;
1294 return $response;
1295 }
1296 public function getFormEntry($Request, $post)
1297 {
1298 if (isset($Request['id'])) {
1299 $id = wp_unslash($Request['id']);
1300 $offset = isset($Request['offset']) ?
1301 wp_unslash($Request['offset']) : 0;
1302 $pageSize = isset($Request['pageSize']) ?
1303 wp_unslash($Request['pageSize']) : 10;
1304 } else {
1305 $id = wp_unslash($post->id);
1306 $offset = isset($post->offset) ? wp_unslash($post->offset) : 0;
1307 $sortBy = isset($post->sortBy) ? wp_unslash($post->sortBy) : null;
1308 $filters = isset($post->filters) ? wp_unslash($post->filters) : null;
1309 $globalFilter = isset($post->globalFilter) ? wp_unslash($post->globalFilter) : null;
1310 $pageSize = isset($post->pageSize) ?
1311 wp_unslash($post->pageSize) : 10;
1312 }
1313 if (is_null($id)) {
1314 return new WP_Error('empty_form', __('Form id is empty.', 'bitform'));
1315 }
1316 $formManager = new AdminFormManager($id);
1317 if (!$formManager->isExist()) {
1318 return new WP_Error('empty_form', __('Form does not exists', 'bitform'));
1319 }
1320
1321 $formEntry = new FormEntryModel();
1322 $entries = $formEntry->get(
1323 'id',
1324 array(
1325 'form_id' => $id,
1326 ),
1327 null,
1328 null,
1329 'created_at',
1330 'DESC'
1331 );
1332 if (is_wp_error($entries)) {
1333 if ($entries->get_error_code() === 'result_empty') {
1334 return array(
1335 'count' => 0,
1336 'entries' => [],
1337 );
1338 }
1339 return $entries;
1340 }
1341 $filter['field'] = $filters;
1342 $filter['global'] = $globalFilter;
1343
1344 $formFields = $formManager->getFieldLabel(true);
1345 $fieldDetails = $formManager->getFields();
1346
1347 $entryMeta = new FormEntryMetaModel();
1348 $formEntries = $entryMeta->getEntryMeta($formFields, $entries, $pageSize, $offset, $filter, $sortBy);
1349 $customFieldHandler =new CustomFieldHandler();
1350 $formEntries = $customFieldHandler->updatedEntries($formEntries,$fieldDetails);
1351 return $formEntries;
1352 }
1353
1354 public function getExportEntry($post)
1355 {
1356 $formId = wp_unslash($post->formId);
1357 $limit = isset($post->limit) ? wp_unslash($post->limit) : null;
1358 $sortBy = isset($post->sort) ? wp_unslash($post->sort) : 'ASC';
1359 $sortByField = isset($post->sortField) ? wp_unslash($post->sortField) : 'bitforms_form_entry_id';
1360 $formFields = json_decode($post->selectedField);
1361 if (is_null($formId)) {
1362 return new WP_Error('empty_form', __('Form id is empty.', 'bitform'));
1363 }
1364 $formManager = new AdminFormManager($formId);
1365 $fieldLabels = $formManager->getFieldLabel(true);
1366 if (!$formManager->isExist()) {
1367 return new WP_Error('empty_form', __('Form does not exists', 'bitform'));
1368 }
1369
1370 $formEntry = new FormEntryModel();
1371 $entries = $formEntry->get(
1372 'id',
1373 array(
1374 'form_id' => $formId,
1375 ),
1376 null,
1377 null,
1378 'created_at',
1379 'DESC'
1380 );
1381 if (is_wp_error($entries)) {
1382 if ($entries->get_error_code() === 'result_empty') {
1383 return array(
1384 'count' => 0,
1385 'entries' => [],
1386 );
1387 }
1388 return $entries;
1389 }
1390 $entryMeta = new FormEntryMetaModel();
1391 $filter = [];
1392 $formEntries = $entryMeta->getExportEntry($formFields, $entries, $formId, $fieldLabels, $limit, $sortBy, $sortByField);
1393 return $formEntries;
1394 }
1395 public function deleteBlukFormEntries($Request, $post)
1396 {
1397 if (isset($Request['formID'])) {
1398 $formID = wp_unslash($Request['formID']);
1399 $entries = wp_unslash($Request['entries']);
1400 } else {
1401 $formID = wp_unslash($post->formID);
1402 $entries = wp_unslash($post->entries);
1403 }
1404 if (is_null($formID) || !is_array($entries) || count($entries) === 0) {
1405 return new WP_Error('empty_form', __('Invalid Form ID or Entries ID.', 'bitform'));
1406 }
1407 $formManager = new AdminFormManager($formID);
1408 if (!$formManager->isExist()) {
1409 return new WP_Error('empty_form', __('Form does not exist.', 'bitform'));
1410 }
1411 $workFlowRunHelper = new WorkFlowRunHelper($formID);
1412 $workFlowreturnedOnDelete = $workFlowRunHelper->executeOnDelete(
1413 $formManager,
1414 $formID,
1415 $entries
1416 );
1417 if (isset($workFlowreturnedOnDelete['entries'])) {
1418 if (count($workFlowreturnedOnDelete['entries']) === 0) {
1419 return ["message" => __('Entry Deletetion prevented by workflow', 'bitform')];
1420 } elseif (count($workFlowreturnedOnDelete['entries']) === count($entries)) {
1421 $message = __('Entry Deleted successfully', 'bitform');
1422 } else {
1423 $result['prevented'] = array_diff($entries, $workFlowreturnedOnDelete['entries']);
1424 $entries = $workFlowreturnedOnDelete['entries'];
1425 $message = __('Entry Deleted successfully, Some prevented by workflow', 'bitform');
1426 }
1427 } else {
1428 $message = __('Entry Deleted successfully', 'bitform');
1429 }
1430 global $wpdb;
1431 $prefix = $wpdb->prefix;
1432 $formEntryModel = new FormEntryModel();
1433 $delete_status = $formEntryModel->bulkDelete(
1434 array(
1435 "`{$prefix}bitforms_form_entries`.`id`" => $entries,
1436 "`{$prefix}bitforms_form_entries`.`form_id`" => $formID,
1437 )
1438 );
1439 if (file_exists(BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID)) {
1440 $fileHandler = new FileHandler();
1441 foreach ($entries as $enrtyKey => $entryID) {
1442 $fileEntries = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID;
1443 if (file_exists($fileEntries)) {
1444 $fileHandler->rmrf($fileEntries);
1445 }
1446 }
1447 }
1448 $count = $formEntryModel->count(
1449 array(
1450 'form_id' => $formID,
1451 )
1452 );
1453 $formManager->resetSubmissionCount(intval($count[0]->count));
1454 if (is_wp_error($delete_status)) {
1455 return new WP_Error('entry_not_exists', __("Form entry deletion failed.", "bitform"));
1456 }
1457 $result['message'] = $message;
1458 return $result;
1459 }
1460 public function duplicateFormEntry($Request, $post)
1461 {
1462 if (isset($Request['formID'])) {
1463 $formID = wp_unslash($Request['formID']);
1464 $entries = wp_unslash($Request['entries']);
1465 } else {
1466 $formID = wp_unslash($post->formID);
1467 $entries = wp_unslash($post->entries);
1468 }
1469 if (is_null($formID) || !is_array($entries) || empty($entries)) {
1470 return new WP_Error('empty_form', __('Form id or entries id is invalid', 'bitform'));
1471 }
1472
1473 $formManager = new AdminFormManager($formID);
1474 if (!$formManager->isExist()) {
1475 return new WP_Error('empty_form', __('Form does not exist', 'bitform'));
1476 }
1477 $formEntryModel = new FormEntryModel();
1478 $entryMeta = new FormEntryMetaModel();
1479 $user_details = static::$ipTool->getUserDetail();
1480 $total_entries = count($entries);
1481 $duplicate_count = 0;
1482 $test = '';
1483 $fileHandler = new FileHandler();
1484 $result = array();
1485 foreach ($entries as $entryIndex => $entryID) {
1486 $duplicatedEntryId = $formEntryModel->insert(
1487 array(
1488 'form_id' => $formID,
1489 'user_id' => $user_details['id'],
1490 'user_ip' => $user_details['ip'],
1491 'user_device' => $user_details['device'],
1492 'referer' => 'duplicate of #' . $entryID,
1493 'status' => 0,
1494 'created_at' => $user_details['time'],
1495 )
1496 );
1497 if ($duplicatedEntryId) {
1498 $duplicate_status = $entryMeta->duplicateEntryMeta(
1499 array(
1500 'duplicateID' => $duplicatedEntryId,
1501 'entryID' => $entryID,
1502 )
1503 );
1504 if ($duplicate_status) {
1505 $result['details'][$entryID] = $duplicatedEntryId;
1506 $duplicate_count = $duplicate_count + 1;
1507 if (file_exists(BITFORMS_UPLOAD_DIR . "/$formID/$entryID")) {
1508 $fileHandler->cpyr(
1509 BITFORMS_UPLOAD_DIR . "/$formID/$entryID",
1510 BITFORMS_UPLOAD_DIR . "/$formID/$duplicatedEntryId"
1511 );
1512 }
1513 }
1514 }
1515 }
1516
1517 $count = $formEntryModel->count(
1518 array(
1519 'form_id' => $formID,
1520 )
1521 );
1522 $formManager->resetSubmissionCount(intval($count[0]->count));
1523 $result['message'] = count($entries) === 1 ? __('Entry Duplicated successfully', 'bitform') : __('Entries Duplicated successfully', 'bitform');
1524 return ($total_entries === $duplicate_count) ? $result : false;
1525 }
1526 public function editFormEntry($Request, $post)
1527 {
1528 if (isset($Request['formID'])) {
1529 $formID = wp_unslash($Request['formID']);
1530 $entryID = wp_unslash($Request['entryID']);
1531 } else {
1532 $formID = wp_unslash($post->formID);
1533 $entryID = wp_unslash($post->entryID);
1534 }
1535 if (is_null($formID) || is_null($entryID)) {
1536 return new WP_Error('empty_form', __('Form id or entries id is invalid', 'bitform'));
1537 }
1538
1539 $formManager = new AdminFormManager($formID);
1540 if (!$formManager->isExist()) {
1541 return new WP_Error('empty_form', __('Form does not exist', 'bitform'));
1542 }
1543 $formEntryModel = new FormEntryModel();
1544 $entryMeta = new FormEntryMetaModel();
1545
1546 $formEntry = $formEntryModel->get(
1547 "*",
1548 array(
1549 'form_id' => $formID,
1550 'id' => $entryID,
1551 )
1552 );
1553
1554 if (!$formEntry) {
1555 return new WP_Error('empty_form', __('Form entries does not exist.', 'bitform'));
1556 }
1557 $formEntryMeta = $entryMeta->get(
1558 array(
1559 'meta_key',
1560 'meta_value',
1561 ),
1562 array(
1563 'bitforms_form_entry_id' => $entryID,
1564 )
1565 );
1566 $entries = array();
1567 foreach ($formEntryMeta as $key => $value) {
1568 $entries[$value->meta_key] = $value->meta_value;
1569 }
1570 $formContent = $formManager->getFormContent();
1571 $fieldsKey = $formManager->getFieldsKey();
1572 $form_fields = $formContent->fields;
1573 $layout = $formContent->layout;
1574 foreach ($form_fields as $key => $value) {
1575 // $field_name = preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\!]/', '_', $value->lbl);
1576 if (isset($entries[$key])) {
1577 $form_fields->{$key}->val = $entries[$key];
1578 $form_fields->{$key}->name = $key;
1579 }
1580 }
1581 $workFlowRunHelper = new WorkFlowRunHelper($formID);
1582 $workFlowreturnedOnLoad = $workFlowRunHelper->executeOnLoad(
1583 'edit',
1584 $form_fields
1585 );
1586 $workFlowreturnedOnUserInput = $workFlowRunHelper->executeOnUserInput(
1587 'edit',
1588 $form_fields
1589 );
1590 // var_dump($workFlowreturned);
1591 if (!empty($workFlowreturnedOnLoad['fields'])) {
1592 $form_fields = $workFlowreturnedOnLoad['fields'];
1593 }
1594 $formData = array(
1595 'layout' => $layout,
1596 'fields' => $form_fields,
1597 'conditional' => !empty($workFlowreturnedOnUserInput['conditional']) ? $workFlowreturnedOnUserInput['conditional'] : false,
1598 'fieldToCheck' => !empty($workFlowreturnedOnUserInput['fieldToCheck']) ? $workFlowreturnedOnUserInput['fieldToCheck'] : false,
1599 'fieldToChange' => !empty($workFlowreturnedOnUserInput['fieldToChange']) ? $workFlowreturnedOnUserInput['fieldToChange'] : false,
1600 'fieldsKey' => $fieldsKey,
1601 );
1602 return $formData;
1603 }
1604 public function updateFormEntry($Request, $post)
1605 {
1606 if (isset($Request['formID']) && isset($Request['entryID'])) {
1607 $formID = wp_unslash($Request['formID']);
1608 $entryID = wp_unslash($Request['entryID']);
1609 unset($Request['action']);
1610 unset($Request['formID']);
1611 unset($Request['entryID']);
1612 $updatedValue = wp_unslash($post);
1613 }
1614
1615 if (is_null($updatedValue) || !is_array($updatedValue)) {
1616 return new WP_Error('empty_data', __('Failed to update, Data is empty.', 'bitform'));
1617 }
1618
1619 if (is_null($formID) || is_null($entryID)) {
1620 return new WP_Error('empty_data', __('Invalid Form ID or entries ID.', 'bitform'));
1621 }
1622
1623 $formManager = new AdminFormManager($formID);
1624 if (!$formManager->isExist()) {
1625 return new WP_Error('empty_data', __('Form does not exist.', 'bitform'));
1626 }
1627 return $formManager->updateFormEntry($updatedValue, $formID, $entryID);
1628 }
1629
1630 public function getLogHistory($Request, $post)
1631 {
1632 if (isset($Request['formID'])) {
1633 $formID = wp_unslash($Request['formID']);
1634 $entryID = wp_unslash($Request['entryID']);
1635 } else {
1636 $formID = wp_unslash($post->formID);
1637 $entryID = wp_unslash($post->entryID);
1638 }
1639
1640 if (is_null($formID) || is_null($entryID)) {
1641 return new WP_Error('empty_form', __('Invalid Form ID or Entries ID.', 'bitform'));
1642 }
1643
1644 $formManager = new AdminFormManager($formID);
1645 if (!$formManager->isExist()) {
1646 return new WP_Error('empty_form', __('Form does not exist.', 'bitform'));
1647 }
1648 $formLogModel = new FormEntryLogModel();
1649
1650 $log_history = $formLogModel->geLogHistory($formID, $entryID);
1651 return $log_history;
1652 }
1653
1654 public function importDataStore($Request, $post)
1655 {
1656 if (isset($Request['formID'])) {
1657 $formID = wp_unslash($Request['formID']);
1658 } else {
1659 $formID = wp_unslash($post->formID);
1660 }
1661 if (is_null($formID)) {
1662 return new WP_Error('empty_form', __('Invalid Form id or entries id.', 'bitform'));
1663 }
1664 }
1665
1666 public function getAllWPPages($Request, $post)
1667 {
1668 $pages = get_pages(array('post_status' => 'publish', 'sort_column' => 'post_date', 'sort_order' => 'desc'));
1669 $allPages = array();
1670 foreach ($pages as $pageKey => $pageDetails) {
1671 $allPages[$pageKey]['title'] = $pageDetails->post_title;
1672 $allPages[$pageKey]['url'] = get_page_link($pageDetails->ID);
1673 }
1674 return $allPages;
1675 }
1676 public function deleteAIntegration($Request, $post)
1677 {
1678 if (isset($Request['formID']) && $Request['id']) {
1679 $formID = json_decode(wp_unslash($Request['formID']));
1680 $id = wp_unslash($Request['id']);
1681 } else {
1682 $formID = wp_unslash($post->formID);
1683 $id = wp_unslash($post->id);
1684 }
1685 if ($formID < 0 || empty($id)) {
1686 return new WP_Error('empty_form', 'Invalid Form ID or Integration ID.');
1687 }
1688
1689 $integrationHandler = new IntegrationHandler($formID);
1690 $delete_status = $integrationHandler->deleteIntegration($id);
1691 if (is_wp_error($delete_status)) {
1692 return $delete_status;
1693 }
1694
1695 return array(
1696 'message' => __('Integration deleted', 'bitform'),
1697 );
1698 }
1699
1700 public function deleteSuccessMessage($Request, $post)
1701 {
1702 if (isset($Request['formID']) && $Request['id']) {
1703 $formID = json_decode(wp_unslash($Request['formID']));
1704 $id = wp_unslash($Request['id']);
1705 } else {
1706 $formID = wp_unslash($post->formID);
1707 $id = wp_unslash($post->id);
1708 }
1709 if (empty($formID) || empty($id)) {
1710 return new WP_Error('empty_form', 'Invalid Form ID or Message ID.');
1711 }
1712 $successMessageHandler
1713 = new SuccessMessageHandler($formID);
1714 $delete_status = $successMessageHandler->deleteMessage($id);
1715 if (is_wp_error($delete_status)) {
1716 return $delete_status;
1717 }
1718 return array(
1719 'message' => __('Message deleted', 'bitform'),
1720 );
1721 }
1722
1723 public function deleteAWorkflow($Request, $post)
1724 {
1725 if (isset($Request['formID']) && $Request['id']) {
1726 $formID = json_decode(wp_unslash($Request['formID']));
1727 $id = wp_unslash($Request['id']);
1728 } else {
1729 $formID = wp_unslash($post->formID);
1730 $id = wp_unslash($post->id);
1731 }
1732 if (empty($formID) || empty($id)) {
1733 return new WP_Error('empty_form', 'Invalid Form ID or Workflow ID.');
1734 }
1735 $workFlowHandler = new WorkFlowHandler($formID);
1736 $delete_status = $workFlowHandler->deleteworkFlow($id);
1737 if (is_wp_error($delete_status)) {
1738 return $delete_status;
1739 }
1740 return array(
1741 'message' => __('workflow deleted', 'bitform'),
1742 );
1743 }
1744 public function deleteAMailTemplate($Request, $post)
1745 {
1746 if (isset($Request['formID']) && $Request['id']) {
1747 $formID = json_decode(wp_unslash($Request['formID']));
1748 $id = wp_unslash($Request['id']);
1749 } else {
1750 $formID = wp_unslash($post->formID);
1751 $id = wp_unslash($post->id);
1752 }
1753 if (empty($formID) || empty($id)) {
1754 return new WP_Error('empty_form', 'Invalid Form ID or Email Template ID.');
1755 }
1756 $emailTemplateHandler = new EmailTemplateHandler($formID);
1757 $delete_status = $emailTemplateHandler->deleteTemplate($id);
1758 if (is_wp_error($delete_status)) {
1759 return $delete_status;
1760 }
1761 return array(
1762 'message' => __('Email Template deleted', 'bitform'),
1763 );
1764 }
1765 public function duplicateAMailTemplate($Request, $post)
1766 {
1767 if (isset($Request['formID']) && $Request['id']) {
1768 $formID = json_decode(wp_unslash($Request['formID']));
1769 $id = wp_unslash($Request['id']);
1770 } else {
1771 $formID = wp_unslash($post->formID);
1772 $id = wp_unslash($post->id);
1773 }
1774 if (empty($formID) || empty($id)) {
1775 return new WP_Error('empty_form', 'Invalid Form ID or Email Template ID.');
1776 }
1777 $emailTemplateHandler = new EmailTemplateHandler($formID);
1778 $duplicate_status = $emailTemplateHandler->duplicateTemplate($id);
1779 if (is_wp_error($duplicate_status)) {
1780 return $duplicate_status;
1781 }
1782 return array(
1783 'message' => __('Email Template duplicated', 'bitform'),
1784 );
1785 }
1786 public function setAllFormsReport($Request, $post)
1787 {
1788 $reports = empty($post->reports) ? null : wp_unslash($post->reports);
1789 if (empty($reports)) {
1790 return new WP_Error('empty_report_prefs', 'Report data is Empty, nothing to save or update.');
1791 }
1792 if (!empty($reports)) {
1793 $reportsModel = new ReportsModel();
1794 $user_details = static::$ipTool->getUserDetail();
1795 foreach ($reports as $reportIndex => $report) {
1796 if (empty($report->id)) {
1797 $reportSaveSatus = $reportsModel->insert(
1798 array(
1799 "type" => 'table',
1800 "category" => 'app',
1801 "context" => 'allForm',
1802 "details" => is_string($report->details) ? $report->details : wp_json_encode($report->details),
1803 "isDefault" => 1,
1804 "user_id" => $user_details['id'],
1805 "user_ip" => $user_details['ip'],
1806 "user_device" => $user_details['device'],
1807 "created_at" => $user_details['time'],
1808 "updated_at" => $user_details['time'],
1809 )
1810 );
1811 $newData['reports'] = 1;
1812 } else {
1813 $reportSaveSatus = $reportsModel->update(
1814 array(
1815 "type" => 'table',
1816 "category" => 'app',
1817 "context" => 'allForm',
1818 "details" => is_string($report->details) ? $report->details : wp_json_encode($report->details),
1819 "isDefault" => 1,
1820 "user_id" => $user_details['id'],
1821 "user_ip" => $user_details['ip'],
1822 "user_device" => $user_details['device'],
1823 "updated_at" => $user_details['time'],
1824 ),
1825 array(
1826 'id' => $report->id,
1827 )
1828 );
1829 }
1830 }
1831 }
1832 if (is_wp_error($reportSaveSatus)) {
1833 if ($reportSaveSatus->get_error_code() === 'result_empty') {
1834 return new WP_Error('empty_report_prefs', 'Nothing to save or update');
1835 }
1836 return new WP_Error('error_report_prefs', 'Error occured in saving reports');
1837 }
1838 $returnedReportData = $reportsModel->get(
1839 array(
1840 "id",
1841 "details",
1842 "isDefault",
1843 "category",
1844 "context",
1845 ),
1846 array(
1847 "category" => 'app',
1848 "context" => 'allForm',
1849 )
1850 );
1851 if (!is_wp_error($returnedReportData)) {
1852 foreach ($returnedReportData as $reportKey => $reportData) {
1853 if (isset($reportData->details) && is_string($reportData->details)) {
1854 $returnedReportData[$reportKey]->details = json_decode($reportData->details);
1855 }
1856 }
1857 $reports = $returnedReportData;
1858 }
1859 return array(
1860 'message' => __('Report preferrences saved successfully', 'bitform'),
1861 'reports' => empty($reports) ? [] : $reports,
1862 );
1863 }
1864
1865 public function savegReCaptcha($Request, $post)
1866 {
1867 $reCaptcha = $post->reCaptcha;
1868
1869 if (is_null($reCaptcha)) {
1870 return new WP_Error('empty_gcaptchdetails', __('g-ReCAPTCHA details is empty', 'bitform'));
1871 }
1872 if (is_string($reCaptcha) && !empty(json_decode($reCaptcha)->id)) {
1873 $reCaptcha = json_decode($reCaptcha);
1874 $integrationID = $reCaptcha->id;
1875 unset($reCaptcha->id);
1876 } else {
1877 $integrationID = $reCaptcha->id;
1878 unset($reCaptcha->id);
1879 }
1880 $reCaptcha = json_encode($reCaptcha);
1881 $integrationName = 'google reCaptcha';
1882 $integrationType = $post->version === 'v2' ? 'gReCaptcha' : 'gReCaptchaV3';
1883 $integrationDetails = $reCaptcha;
1884 $user_details = static::$ipTool->getUserDetail();
1885 $integrationHandler = new IntegrationHandler(0, $user_details);
1886 $response = array();
1887 if (empty($integrationID)) {
1888 $captchaSaveStatus = $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'app');
1889 $response['id'] = $captchaSaveStatus;
1890 $response['message'] = __('g-reCAPTCHA saved successfully', 'bitform');
1891 } else {
1892 $captchaSaveStatus = $integrationHandler->updateIntegration($integrationID, $integrationName, $integrationType, $integrationDetails, 'app');
1893 $response['message'] = __('g-reCAPTCHA updated successfully', 'bitform');
1894 }
1895
1896 if (is_wp_error($captchaSaveStatus)) {
1897 return $captchaSaveStatus;
1898 }
1899 return $response;
1900 }
1901
1902 public function savePaymentSetting($Request, $post)
1903 {
1904 if (isset($post->paySetting)) {
1905 $paySetting = $post->paySetting;
1906 } else {
1907 $paySetting = $post->paySetting;
1908 }
1909 if (is_null($paySetting)) {
1910 return new WP_Error('empty_gcaptchdetails', __('Setting details is empty', 'bitform'));
1911 }
1912 if (is_string($paySetting) && !empty(json_decode($paySetting)->id)) {
1913 $paySetting = json_decode($paySetting);
1914 $integrationID = $paySetting->id;
1915 unset($paySetting->id);
1916 } else {
1917 $integrationID = $paySetting->id;
1918 unset($paySetting->id);
1919 }
1920 $integrationName = $paySetting->name;
1921 $integrationType = 'payments';
1922 $paySetting = json_encode($paySetting);
1923 $integrationDetails = $paySetting;
1924 $user_details = static::$ipTool->getUserDetail();
1925 $integrationHandler = new IntegrationHandler(0, $user_details);
1926 $response = array();
1927 if (empty($integrationID)) {
1928 $paymentSaveStatus = $integrationHandler->saveIntegration($integrationName, $integrationType, $integrationDetails, 'app');
1929 $response['id'] = $paymentSaveStatus;
1930 $response['message'] = __('Payment setting saved successfully', 'bitform');
1931 } else {
1932 $paymentSaveStatus = $integrationHandler->updateIntegration($integrationID, $integrationName, $integrationType, $integrationDetails, 'app');
1933 $response['message'] = __('Payment setting updated successfully', 'bitform');
1934 }
1935
1936 if (is_wp_error($paymentSaveStatus)) {
1937 return $paymentSaveStatus;
1938 }
1939 return $response;
1940 }
1941 }
1942