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 / Admin / Form / AdminFormHandler.php

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

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