| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. 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\TemplateService; |
| 30 |
use IvyForms\ValueObjects\Form\IntegrationSettings; |
| 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 |
|
| 95 |
$form = FormFactory::create($params); |
| 96 |
|
| 97 |
$formId = $this->formService->createForm($form); |
| 98 |
if (!$formId) { |
| 99 |
throw new QueryExecutionException( |
| 100 |
BackendStrings::getAllFormsStrings()['failed_to_create_form'] |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
$adminEmail = get_option('admin_email'); |
| 105 |
|
| 106 |
// Create default notification |
| 107 |
$this->notificationService->createDefaultNotificationForForm( |
| 108 |
$formId, |
| 109 |
$params['name'], |
| 110 |
$adminEmail |
| 111 |
); |
| 112 |
|
| 113 |
// Create default confirmation using the service |
| 114 |
$confirmationId = $this->confirmationService->createDefaultConfirmationForForm($formId); |
| 115 |
|
| 116 |
// Save form fields and their options using the service |
| 117 |
if (!empty($params['fields'])) { |
| 118 |
$this->fieldService->saveFieldsWithOptions($params['fields'], $formId); |
| 119 |
} |
| 120 |
|
| 121 |
$form->setId($formId); |
| 122 |
|
| 123 |
return new WP_REST_Response([ |
| 124 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 125 |
'data' => array_merge($form->toArray(), ['confirmationId' => $confirmationId]) |
| 126 |
], 200); |
| 127 |
} |
| 128 |
} |
| 129 |
|