# ivyforms/0.3/backend/src/Services/Entry/EntryService.php

The Innovative Form Builder – IvyForms, version 0.3. 251 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.3/code/backend/src/Services/Entry/EntryService.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.3/raw/backend/src/Services/Entry/EntryService.php
- Modified: 2025-08-20T10:20:12+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.3/code/backend/src/Services/Entry/EntryService.php#L10-L20`.

```php
<?php

namespace IvyForms\Services\Entry;

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

use IvyForms\Common\Exceptions\NotFoundException;
use IvyForms\Common\Exceptions\QueryExecutionException;
use IvyForms\Common\Exceptions\ValidationException;
use IvyForms\Entity\Entry\Entry;
use IvyForms\Factory\EntryField\EntryFieldFactory;
use IvyForms\Repository\Entry\EntryRepositoryInterface;
use IvyForms\Repository\EntryField\EntryFieldRepositoryInterface;
use IvyForms\Entity\EntryField\EntryField as EntryFieldEntity;
use IvyForms\Repository\Entry\EntryRequestHelper;
use IvyForms\Services\Translations\BackendStrings;

class EntryService
{
    private EntryRepositoryInterface $entryRepository;
    private EntryFieldRepositoryInterface $entryFieldRepository;

    // Constructor injection for the FormRepository via PHP-DI
    public function __construct(
        EntryRepositoryInterface $entryRepository,
        EntryFieldRepositoryInterface $entryFieldRepository
    ) {
        $this->entryRepository = $entryRepository;
        $this->entryFieldRepository = $entryFieldRepository;
    }

    /**
     * Create a new entry and its entry fields.
     *
     * @param Entry $entryData
     *
     * @return int Entry ID
     */
    public function createEntry(Entry $entryData): int
    {
        // Insert entry and get ID
        return $this->entryRepository->add($entryData);
    }

    /**
     * Create entry fields for a specific entry.
     *
     * @param int $formId
     * @return array<mixed>
     */
    public function buildEntryData(int $formId): array
    {
        return [
            'formId'        => $formId,
            'userId'        => get_current_user_id() ?: null,
            'status'        => Entry::DEFAULT_STATUS,
            'ipAddress'     => EntryRequestHelper::getClientIpAddress(),
            'userAgent'     => EntryRequestHelper::getUserAgent(),
            'sourceURL'     => EntryRequestHelper::getSourceURL(),
        ];
    }

    /**
     * Add entry fields to the repository.
     *
     * @param array<int, object> $formFields
     * @param int $entryId
     * @param array<mixed> $submissionData
     * @throws ValidationException
     */
    public function addEntryFields(array $formFields, int $entryId, array $submissionData): void
    {
        foreach ($formFields as $field) {
            // Skip recaptcha fields
            if (method_exists($field, 'getType') && $field->getType() === 'recaptcha') {
                continue;
            }
            $fieldKey = $field->getId();
            $entryField = EntryFieldFactory::create([
                'entryId'   => $entryId,
                'fieldId'    => $fieldKey,
                'fieldValue' => $submissionData[$fieldKey] ?? '',
            ]);
            $this->entryFieldRepository->add($entryField);
        }
    }

    /**
     * Get a specific entry by its ID.
     *
     * @param int $entryId
     *
     * @return object
     *
     * @throws NotFoundException If the entry is not found
     */
    public function getEntry(int $entryId): object
    {
        $entry = $this->entryRepository->getById($entryId);

        if (!$entry) {
            throw new NotFoundException(
                BackendStrings::getExceptionStrings()['entry_not_found']
            );
        }

        return $entry;
    }

    /**
     * Search for entries based on parameters.
     *
     * @param array<string, mixed> $params
     *
     * @return array<mixed>
     */
    public function searchEntries(array $params): array
    {
        return $this->entryRepository->search($params);
    }

    /**
     * Get the count of entry filters.
     *
     * @param array<string, mixed>|null $params
     *
     * @return array<mixed>
     */
    public function getFilterCount(?array $params = null): array
    {
        return $this->entryRepository->getFilterCount($params);
    }

    /**
     * Delete an entry by its ID.
     *
     * @param int $id
     *
     * @return int
    */
    public function deleteEntry(int $id): int
    {
        return $this->entryRepository->delete($id);
    }

    /**
     * Get all entry fields for a specific entry ID.
     *
     * @param int $entryId
     *
     * @return array<mixed>
     */
    public function getFieldsByEntryId(int $entryId): array
    {
        return $this->entryFieldRepository->findBy(['entryId' => $entryId]);
    }

    /**
     * Update the starred status of an entry.
     *
     * @param int $entryId
     * @param bool $value
     *
     * @return void
     */
    public function updateEntryStarred(int $entryId, bool $value): void
    {
        $this->entryRepository->updateEntryStarred($entryId, $value);
    }

    /**
     * Update the status of an entry.
     *
     * @param int $entryId
     * @param string $status
     *
     * @return void
     */
    public function updateEntryStatus(int $entryId, string $status): void
    {
        $this->entryRepository->updateEntryStatus($entryId, $status);
    }

    /**
     * Get the user agent string.
     *
     * @return string
     */
    public function getUserAgent(): string
    {
        return EntryRequestHelper::getUserAgent();
    }

    /**
     * Fetch entries along with their associated fields.
     *
     * @param array<int, array<string, mixed>> $entries
     * @return array<mixed>
     */
    public function getEntryFields(array $entries): array
    {
        return $this->entryRepository->getEntryFields($entries);
    }

    /**
     * Get the count of entries for multiple form IDs.
     *
     * @param int[] $formIds
     * @return array<int, int> formId => count
     */
    public function getEntryCountByFormIds(array $formIds): array
    {
        return $this->entryRepository->getEntryCountByFormIds($formIds);
    }

    /**
     * Delete all entry fields for multiple entry IDs in bulk.
     *
     * @param int[] $entryIds
     * @return int Number of deleted entry fields
     */
    public function deleteEntryFieldsByEntryIds(array $entryIds): int
    {
        if (empty($entryIds)) {
            return 0;
        }
        $allFields = $this->entryFieldRepository->findIdsByEntryIds($entryIds);
        if (empty($allFields)) {
            return 0;
        }
        return $this->entryFieldRepository->deleteMany($allFields);
    }

    /**
     * Delete multiple entries by their IDs in bulk.
     *
     * @param int[] $ids
     * @return int Number of deleted entries
     */
    public function deleteEntriesByIds(array $ids): int
    {
        if (empty($ids)) {
            return 0;
        }
        return $this->entryRepository->deleteMany($ids);
    }
}

```
