| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © TMS-Plugins. All rights reserved. |
| 5 |
* @licence See COPYING.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Controllers\Form; |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 16 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 17 |
use IvyForms\Common\Exceptions\QueryExecutionException; |
| 18 |
use IvyForms\Common\Exceptions\ValidationException; |
| 19 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 20 |
use IvyForms\Controllers\Controller; |
| 21 |
use IvyForms\Factory\Field\FieldFactory; |
| 22 |
use IvyForms\Factory\Form\FormFactory; |
| 23 |
use IvyForms\Factory\Notification\NotificationFactory; |
| 24 |
use IvyForms\Factory\Confirmation\ConfirmationFactory; |
| 25 |
use IvyForms\Services\Field\FieldService; |
| 26 |
use IvyForms\Services\Form\FormService; |
| 27 |
use IvyForms\Services\Notification\NotificationService; |
| 28 |
use IvyForms\Services\Confirmation\ConfirmationService; |
| 29 |
use WP_REST_Request; |
| 30 |
use WP_REST_Response; |
| 31 |
|
| 32 |
/** |
| 33 |
* Class AddFormController |
| 34 |
* |
| 35 |
* @package IvyForms\Controllers\Form |
| 36 |
*/ |
| 37 |
class AddFormController extends Controller |
| 38 |
{ |
| 39 |
private FormService $formService; |
| 40 |
private FieldService $fieldService; |
| 41 |
private NotificationService $notificationService; |
| 42 |
private ConfirmationService $confirmationService; |
| 43 |
|
| 44 |
public function __construct( |
| 45 |
FormService $formService, |
| 46 |
FieldService $fieldService, |
| 47 |
NotificationService $notificationService, |
| 48 |
ConfirmationService $confirmationService |
| 49 |
) { |
| 50 |
$this->formService = $formService; |
| 51 |
$this->fieldService = $fieldService; |
| 52 |
$this->notificationService = $notificationService; |
| 53 |
$this->confirmationService = $confirmationService; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @param WP_REST_Request $data |
| 58 |
* |
| 59 |
* @return WP_REST_Response |
| 60 |
* |
| 61 |
* @throws QueryExecutionException |
| 62 |
* @throws InvalidArgumentException |
| 63 |
* @throws ValidationException |
| 64 |
* @throws ForbiddenException |
| 65 |
*/ |
| 66 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 67 |
{ |
| 68 |
// Verify the nonce |
| 69 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 70 |
|
| 71 |
// Check if the form data is provided |
| 72 |
if (empty($data->get_params())) { |
| 73 |
throw new InvalidArgumentException('Invalid request data.'); |
| 74 |
} |
| 75 |
|
| 76 |
// Sanitize the input data |
| 77 |
$params = Sanitizer::sanitizeFormData($data->get_params()); |
| 78 |
|
| 79 |
$form = FormFactory::create($params); |
| 80 |
|
| 81 |
$formId = $this->formService->createForm($form); |
| 82 |
|
| 83 |
if (!$formId) { |
| 84 |
throw new QueryExecutionException('Failed to create form.'); |
| 85 |
} |
| 86 |
|
| 87 |
// Create default notification |
| 88 |
$adminEmail = get_option('admin_email'); |
| 89 |
|
| 90 |
$defaultNotification = [ |
| 91 |
'formId' => $formId, |
| 92 |
'name' => 'Admin Notification Email', |
| 93 |
'sender' => $adminEmail, |
| 94 |
'receiver' => $adminEmail, |
| 95 |
'status' => 1, |
| 96 |
'subject' => $params['name'], |
| 97 |
'message' => '{all_data}', |
| 98 |
'smartLogic' => 0, |
| 99 |
]; |
| 100 |
|
| 101 |
$notification = NotificationFactory::create($defaultNotification); |
| 102 |
$notificationId = $this->notificationService->createNotification($notification); |
| 103 |
|
| 104 |
if (!$notificationId) { |
| 105 |
throw new QueryExecutionException('Failed to create default notification.'); |
| 106 |
} |
| 107 |
|
| 108 |
$notification->setId($notificationId); |
| 109 |
|
| 110 |
$defaultConfirmation = [ |
| 111 |
'formId' => $formId, |
| 112 |
'type' => 'successMessage', |
| 113 |
'status' => 1, |
| 114 |
'showForm' => 0, |
| 115 |
'message' => 'Thanks for reaching out! We’ve received your message and will get back to you shortly.', |
| 116 |
'url' => '', |
| 117 |
'page' => '', |
| 118 |
]; |
| 119 |
|
| 120 |
$confirmation = ConfirmationFactory::create($defaultConfirmation); |
| 121 |
$confirmationId = $this->confirmationService->createConfirmation($confirmation); |
| 122 |
|
| 123 |
if (!$confirmationId) { |
| 124 |
throw new QueryExecutionException('Failed to create confirmation.'); |
| 125 |
} |
| 126 |
|
| 127 |
$confirmation->setId($confirmationId); |
| 128 |
|
| 129 |
// Save form fields |
| 130 |
if (!empty($params['fields'])) { |
| 131 |
foreach ($params['fields'] as $fieldData) { |
| 132 |
$fieldData['formId'] = $fieldData['formId'] ?? $formId; |
| 133 |
$field = FieldFactory::create($fieldData); |
| 134 |
$this->fieldService->addField($field); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
$form->setId($formId); |
| 139 |
|
| 140 |
return new WP_REST_Response([ |
| 141 |
'message' => 'OK', |
| 142 |
'data' => array_merge($form->toArray(), ['confirmationId' => $confirmationId]) |
| 143 |
], 200); |
| 144 |
} |
| 145 |
} |
| 146 |
|