# ivyforms/0.1.2/backend/src/Controllers/Form/DuplicateFormController.php

The Innovative Form Builder – IvyForms, version 0.1.2. 107 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.1.2/code/backend/src/Controllers/Form/DuplicateFormController.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.1.2/raw/backend/src/Controllers/Form/DuplicateFormController.php
- Modified: 2025-06-12T10:29:14+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ivyforms/0.1.2/code/backend/src/Controllers/Form/DuplicateFormController.php#L10-L20`.

```php
<?php

/**
 * @copyright © TMS-Plugins. All rights reserved.
 * @licence   See COPYING.md for license details.
 */

namespace IvyForms\Controllers\Form;

// phpcs:disable PSR1.Files.SideEffects
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

use IvyForms\Common\Exceptions\ForbiddenException;
use IvyForms\Common\Exceptions\InvalidArgumentException;
use IvyForms\Common\Exceptions\NotFoundException;
use IvyForms\Common\Exceptions\QueryExecutionException;
use IvyForms\Common\Exceptions\ValidationException;
use IvyForms\Common\Sanitizer\Sanitizer;
use IvyForms\Controllers\Controller;
use IvyForms\Factory\Field\FieldFactory;
use IvyForms\Factory\Form\FormFactory;
use IvyForms\Services\Field\FieldService;
use IvyForms\Services\Form\FormService;
use WP_REST_Request;
use WP_REST_Response;

/**
 * Class DuplicateFormController
 *
 * @package IvyForms\Controllers\Form
 */
class DuplicateFormController extends Controller
{
    private FormService $formService;
    private FieldService $fieldService;

    public function __construct(
        FormService $formService,
        FieldService $fieldService
    ) {
        $this->formService = $formService;
        $this->fieldService = $fieldService;
    }

    /**
     * @param WP_REST_Request $data
     *
     * @return WP_REST_Response
     *
     * @throws QueryExecutionException
     * @throws InvalidArgumentException
     * @throws NotFoundException
     * @throws ValidationException
     * @throws ForbiddenException
     */
    public function handle(WP_REST_Request $data): WP_REST_Response
    {
        // Verify the nonce
        Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));

        // Check if the form ID is provided
        if (empty($data->get_params())) {
            throw new InvalidArgumentException('Invalid request data.');
        }

        $formId = Sanitizer::sanitizeId($data->get_param('id')) ;

        // Retrieve the original form
        $originalForm = $this->formService->getFormById($formId);

        // Duplicate the form's parameters, excluding the ID
        $newFormData = $originalForm->toArray();
        unset($newFormData['id']); // Exclude the ID
        $newFormData['name'] .= ' (Copy)'; // Append "(Copy)" to the name
        $newForm = FormFactory::create($newFormData);

        // Save the new form to the database
        $newFormId = $this->formService->createForm($newForm);
        if (!$newFormId) {
            throw new QueryExecutionException('Failed to duplicate form.');
        }

        // Retrieve and duplicate the fields associated with the original form
        $originalFields = $this->fieldService->getAllFields($formId);

        if (!empty($originalFields)) {
            foreach ($originalFields as $field) {
                $fieldData = $field->toArray();
                $fieldData['formId'] = $newFormId; // Link to the new form
                unset($fieldData['id']); // Exclude the field ID
                $newField = FieldFactory::create($fieldData);
                $this->fieldService->addField($newField);
            }
        }

        // Set the new form ID
        $newForm->setId($newFormId);

        return new WP_REST_Response([
            'message' => 'OK',
            'data'    => $newForm->toArray()
        ], 200);
    }
}

```
