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

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

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