| 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\NotFoundException; |
| 18 |
use IvyForms\Common\Exceptions\QueryExecutionException; |
| 19 |
use IvyForms\Common\Exceptions\ValidationException; |
| 20 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 21 |
use IvyForms\Controllers\Controller; |
| 22 |
use IvyForms\Factory\Field\FieldFactory; |
| 23 |
use IvyForms\Factory\Form\FormFactory; |
| 24 |
use IvyForms\Services\Field\FieldService; |
| 25 |
use IvyForms\Services\Form\FormService; |
| 26 |
use WP_REST_Request; |
| 27 |
use WP_REST_Response; |
| 28 |
|
| 29 |
/** |
| 30 |
* Class DuplicateFormController |
| 31 |
* |
| 32 |
* @package IvyForms\Controllers\Form |
| 33 |
*/ |
| 34 |
class DuplicateFormController extends Controller |
| 35 |
{ |
| 36 |
private FormService $formService; |
| 37 |
private FieldService $fieldService; |
| 38 |
|
| 39 |
public function __construct( |
| 40 |
FormService $formService, |
| 41 |
FieldService $fieldService |
| 42 |
) { |
| 43 |
$this->formService = $formService; |
| 44 |
$this->fieldService = $fieldService; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @param WP_REST_Request $data |
| 49 |
* |
| 50 |
* @return WP_REST_Response |
| 51 |
* |
| 52 |
* @throws QueryExecutionException |
| 53 |
* @throws InvalidArgumentException |
| 54 |
* @throws NotFoundException |
| 55 |
* @throws ValidationException |
| 56 |
* @throws ForbiddenException |
| 57 |
*/ |
| 58 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 59 |
{ |
| 60 |
// Verify the nonce |
| 61 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 62 |
|
| 63 |
// Check if the form ID is provided |
| 64 |
if (empty($data->get_params())) { |
| 65 |
throw new InvalidArgumentException('Invalid request data.'); |
| 66 |
} |
| 67 |
|
| 68 |
$formId = Sanitizer::sanitizeId($data->get_param('id')) ; |
| 69 |
|
| 70 |
// Retrieve the original form |
| 71 |
$originalForm = $this->formService->getFormById($formId); |
| 72 |
|
| 73 |
// Duplicate the form's parameters, excluding the ID |
| 74 |
$newFormData = $originalForm->toArray(); |
| 75 |
unset($newFormData['id']); // Exclude the ID |
| 76 |
$newFormData['name'] .= ' (Copy)'; // Append "(Copy)" to the name |
| 77 |
$newForm = FormFactory::create($newFormData); |
| 78 |
|
| 79 |
// Save the new form to the database |
| 80 |
$newFormId = $this->formService->createForm($newForm); |
| 81 |
if (!$newFormId) { |
| 82 |
throw new QueryExecutionException('Failed to duplicate form.'); |
| 83 |
} |
| 84 |
|
| 85 |
// Retrieve and duplicate the fields associated with the original form |
| 86 |
$originalFields = $this->fieldService->getAllFields($formId); |
| 87 |
|
| 88 |
if (!empty($originalFields)) { |
| 89 |
foreach ($originalFields as $field) { |
| 90 |
$fieldData = $field->toArray(); |
| 91 |
$fieldData['formId'] = $newFormId; // Link to the new form |
| 92 |
unset($fieldData['id']); // Exclude the field ID |
| 93 |
$newField = FieldFactory::create($fieldData); |
| 94 |
$this->fieldService->addField($newField); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// Set the new form ID |
| 99 |
$newForm->setId($newFormId); |
| 100 |
|
| 101 |
return new WP_REST_Response([ |
| 102 |
'message' => 'OK', |
| 103 |
'data' => $newForm->toArray() |
| 104 |
], 200); |
| 105 |
} |
| 106 |
} |
| 107 |
|