| 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 IvyForms\Services\Template\BlankFormTemplate; |
| 30 |
use IvyForms\Services\Template\TemplateService; |
| 31 |
use WP_REST_Request; |
| 32 |
use WP_REST_Response; |
| 33 |
use IvyForms\Services\Translations\BackendStrings; |
| 34 |
|
| 35 |
/** |
| 36 |
* Class AddFormController |
| 37 |
* |
| 38 |
* @package IvyForms\Controllers\Form |
| 39 |
*/ |
| 40 |
class AddFormController extends Controller |
| 41 |
{ |
| 42 |
private FormService $formService; |
| 43 |
private FieldService $fieldService; |
| 44 |
private NotificationService $notificationService; |
| 45 |
private ConfirmationService $confirmationService; |
| 46 |
private TemplateService $templateService; |
| 47 |
|
| 48 |
public function __construct( |
| 49 |
FormService $formService, |
| 50 |
FieldService $fieldService, |
| 51 |
NotificationService $notificationService, |
| 52 |
ConfirmationService $confirmationService, |
| 53 |
TemplateService $templateService |
| 54 |
) { |
| 55 |
$this->formService = $formService; |
| 56 |
$this->fieldService = $fieldService; |
| 57 |
$this->notificationService = $notificationService; |
| 58 |
$this->confirmationService = $confirmationService; |
| 59 |
$this->templateService = $templateService; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @param WP_REST_Request<array<string, mixed>> $data |
| 64 |
* |
| 65 |
* @return WP_REST_Response |
| 66 |
* |
| 67 |
* @throws QueryExecutionException |
| 68 |
* @throws InvalidArgumentException |
| 69 |
* @throws ValidationException |
| 70 |
* @throws ForbiddenException |
| 71 |
*/ |
| 72 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 73 |
{ |
| 74 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 75 |
|
| 76 |
// Check if the form data is provided |
| 77 |
if (empty($data->get_params())) { |
| 78 |
throw new InvalidArgumentException( |
| 79 |
BackendStrings::getExceptionStrings()['invalid_request_data'] |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
// TODO Add validation for template_id with Sanitizer |
| 84 |
//$templateId = Sanitizer::sanitizeText($data->get_param('id')); |
| 85 |
$templateId = sanitize_text_field($data->get_param('template_id')); |
| 86 |
|
| 87 |
if (empty($templateId)) { |
| 88 |
throw new InvalidArgumentException( |
| 89 |
BackendStrings::getTemplateStrings()['template_not_found'] |
| 90 |
); |
| 91 |
} |
| 92 |
$formData = $this->templateService->getFormDataFromTemplate($templateId); |
| 93 |
$params = Sanitizer::sanitizeFormData($formData); |
| 94 |
$form = FormFactory::create($params); |
| 95 |
|
| 96 |
$formId = $this->formService->createForm($form); |
| 97 |
if (!$formId) { |
| 98 |
throw new QueryExecutionException( |
| 99 |
BackendStrings::getAllFormsStrings()['failed_to_create_form'] |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
$adminEmail = get_option('admin_email'); |
| 104 |
|
| 105 |
$defaultNotification = [ |
| 106 |
'formId' => $formId, |
| 107 |
'name' => BackendStrings::getAllFormsStrings()['admin_notification_email'], |
| 108 |
'sender' => $adminEmail, |
| 109 |
'receiver' => $adminEmail, |
| 110 |
'enabled' => 1, |
| 111 |
'subject' => $params['name'], |
| 112 |
'message' => '{all_data}', |
| 113 |
'smartLogic' => 0, |
| 114 |
]; |
| 115 |
|
| 116 |
$notification = NotificationFactory::create($defaultNotification); |
| 117 |
$notificationId = $this->notificationService->createNotification($notification); |
| 118 |
|
| 119 |
if (!$notificationId) { |
| 120 |
throw new QueryExecutionException( |
| 121 |
BackendStrings::getSettingsFormBuilderStrings()['failed_to_create_default_notification'] |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
$notification->setId($notificationId); |
| 126 |
|
| 127 |
$defaultConfirmation = [ |
| 128 |
'formId' => $formId, |
| 129 |
'type' => 'successMessage', |
| 130 |
'enabled' => 1, |
| 131 |
'showForm' => 0, |
| 132 |
'message' => BackendStrings::getAllFormsStrings()['thanks_reaching'], |
| 133 |
'url' => '', |
| 134 |
'page' => '', |
| 135 |
]; |
| 136 |
|
| 137 |
$confirmation = ConfirmationFactory::create($defaultConfirmation); |
| 138 |
$confirmationId = $this->confirmationService->createConfirmation($confirmation); |
| 139 |
|
| 140 |
if (!$confirmationId) { |
| 141 |
throw new QueryExecutionException( |
| 142 |
BackendStrings::getSettingsFormBuilderStrings()['failed_to_create_confirmation'] |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
$confirmation->setId($confirmationId); |
| 147 |
|
| 148 |
// Save form fields |
| 149 |
if (!empty($params['fields'])) { |
| 150 |
foreach ($params['fields'] as $fieldData) { |
| 151 |
$fieldData['formId'] = $fieldData['formId'] ?? $formId; |
| 152 |
$field = FieldFactory::create($fieldData); |
| 153 |
$this->fieldService->addField($field); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
$form->setId($formId); |
| 158 |
|
| 159 |
return new WP_REST_Response([ |
| 160 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 161 |
'data' => array_merge($form->toArray(), ['confirmationId' => $confirmationId]) |
| 162 |
], 200); |
| 163 |
} |
| 164 |
} |
| 165 |
|