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

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

- Page: https://pluginprobe.com/plugins/ivyforms/0.1.2/code/backend/src/Controllers/Form/AddFormController.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.1.2/raw/backend/src/Controllers/Form/AddFormController.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/AddFormController.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\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\Factory\Notification\NotificationFactory;
use IvyForms\Factory\Confirmation\ConfirmationFactory;
use IvyForms\Services\Field\FieldService;
use IvyForms\Services\Form\FormService;
use IvyForms\Services\Notification\NotificationService;
use IvyForms\Services\Confirmation\ConfirmationService;
use WP_REST_Request;
use WP_REST_Response;

/**
 * Class AddFormController
 *
 * @package IvyForms\Controllers\Form
 */
class AddFormController extends Controller
{
    private FormService $formService;
    private FieldService $fieldService;
    private NotificationService $notificationService;
    private ConfirmationService $confirmationService;

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

    /**
     * @param WP_REST_Request $data
     *
     * @return WP_REST_Response
     *
     * @throws QueryExecutionException
     * @throws InvalidArgumentException
     * @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 data is provided
        if (empty($data->get_params())) {
            throw new InvalidArgumentException('Invalid request data.');
        }

        // Sanitize the input data
        $params = Sanitizer::sanitizeFormData($data->get_params());

        $form = FormFactory::create($params);

        $formId = $this->formService->createForm($form);

        if (!$formId) {
            throw new QueryExecutionException('Failed to create form.');
        }

        // Create default notification
        $adminEmail = get_option('admin_email');

        $defaultNotification = [
            'formId'      => $formId,
            'name'        => 'Admin Notification Email',
            'sender'      => $adminEmail,
            'receiver'    => $adminEmail,
            'status'      => 1,
            'subject'     => $params['name'],
            'message'     => '{all_data}',
            'smartLogic'  => 0,
        ];

        $notification = NotificationFactory::create($defaultNotification);
        $notificationId = $this->notificationService->createNotification($notification);

        if (!$notificationId) {
            throw new QueryExecutionException('Failed to create default notification.');
        }

        $notification->setId($notificationId);

        $defaultConfirmation = [
            'formId'   => $formId,
            'type'     => 'successMessage',
            'status'   => 1,
            'showForm' => 0,
            'message'  => 'Thanks for reaching out! We’ve received your message and will get back to you shortly.',
            'url'      => '',
            'page'     => '',
        ];

        $confirmation = ConfirmationFactory::create($defaultConfirmation);
        $confirmationId = $this->confirmationService->createConfirmation($confirmation);

        if (!$confirmationId) {
            throw new QueryExecutionException('Failed to create confirmation.');
        }

        $confirmation->setId($confirmationId);

        // Save form fields
        if (!empty($params['fields'])) {
            foreach ($params['fields'] as $fieldData) {
                $fieldData['formId'] = $fieldData['formId'] ?? $formId;
                $field = FieldFactory::create($fieldData);
                $this->fieldService->addField($field);
            }
        }

        $form->setId($formId);

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

```
