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 */ 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 $formFields * @param int $entryId * @param array $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 $params * * @return array */ public function searchEntries(array $params): array { return $this->entryRepository->search($params); } /** * Get the count of entry filters. * * @param array|null $params * * @return array */ 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 */ 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> $entries * @return array */ 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 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); } }