| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\Entry; |
| 4 |
|
| 5 |
// phpcs:disable PSR1.Files.SideEffects |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\NotFoundException; |
| 11 |
use IvyForms\Common\Exceptions\QueryExecutionException; |
| 12 |
use IvyForms\Common\Exceptions\ValidationException; |
| 13 |
use IvyForms\Entity\Entry\Entry; |
| 14 |
use IvyForms\Factory\EntryField\EntryFieldFactory; |
| 15 |
use IvyForms\Repository\Entry\EntryRepositoryInterface; |
| 16 |
use IvyForms\Repository\EntryField\EntryFieldRepositoryInterface; |
| 17 |
use IvyForms\Entity\EntryField\EntryField as EntryFieldEntity; |
| 18 |
use IvyForms\Repository\Entry\EntryRequestHelper; |
| 19 |
use IvyForms\Services\Translations\BackendStrings; |
| 20 |
|
| 21 |
class EntryService |
| 22 |
{ |
| 23 |
private EntryRepositoryInterface $entryRepository; |
| 24 |
private EntryFieldRepositoryInterface $entryFieldRepository; |
| 25 |
|
| 26 |
// Constructor injection for the FormRepository via PHP-DI |
| 27 |
public function __construct( |
| 28 |
EntryRepositoryInterface $entryRepository, |
| 29 |
EntryFieldRepositoryInterface $entryFieldRepository |
| 30 |
) { |
| 31 |
$this->entryRepository = $entryRepository; |
| 32 |
$this->entryFieldRepository = $entryFieldRepository; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Create a new entry and its entry fields. |
| 37 |
* |
| 38 |
* @param Entry $entryData |
| 39 |
* |
| 40 |
* @return int Entry ID |
| 41 |
*/ |
| 42 |
public function createEntry(Entry $entryData): int |
| 43 |
{ |
| 44 |
// Insert entry and get ID |
| 45 |
return $this->entryRepository->add($entryData); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Create entry fields for a specific entry. |
| 50 |
* |
| 51 |
* @param int $formId |
| 52 |
* @return array<mixed> |
| 53 |
*/ |
| 54 |
public function buildEntryData(int $formId): array |
| 55 |
{ |
| 56 |
return [ |
| 57 |
'formId' => $formId, |
| 58 |
'userId' => get_current_user_id() ?: null, |
| 59 |
'status' => Entry::DEFAULT_STATUS, |
| 60 |
'ipAddress' => EntryRequestHelper::getClientIpAddress(), |
| 61 |
'userAgent' => EntryRequestHelper::getUserAgent(), |
| 62 |
'sourceURL' => EntryRequestHelper::getSourceURL(), |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Add entry fields to the repository. |
| 68 |
* |
| 69 |
* @param array<int, object> $formFields |
| 70 |
* @param int $entryId |
| 71 |
* @param array<mixed> $submissionData |
| 72 |
* @throws ValidationException |
| 73 |
*/ |
| 74 |
public function addEntryFields(array $formFields, int $entryId, array $submissionData): void |
| 75 |
{ |
| 76 |
foreach ($formFields as $field) { |
| 77 |
// Skip recaptcha fields |
| 78 |
if (method_exists($field, 'getType') && $field->getType() === 'recaptcha') { |
| 79 |
continue; |
| 80 |
} |
| 81 |
$fieldKey = $field->getId(); |
| 82 |
$entryField = EntryFieldFactory::create([ |
| 83 |
'entryId' => $entryId, |
| 84 |
'fieldId' => $fieldKey, |
| 85 |
'fieldValue' => $submissionData[$fieldKey] ?? '', |
| 86 |
]); |
| 87 |
$this->entryFieldRepository->add($entryField); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get a specific entry by its ID. |
| 93 |
* |
| 94 |
* @param int $entryId |
| 95 |
* |
| 96 |
* @return object |
| 97 |
* |
| 98 |
* @throws NotFoundException If the entry is not found |
| 99 |
*/ |
| 100 |
public function getEntry(int $entryId): object |
| 101 |
{ |
| 102 |
$entry = $this->entryRepository->getById($entryId); |
| 103 |
|
| 104 |
if (!$entry) { |
| 105 |
throw new NotFoundException( |
| 106 |
BackendStrings::getExceptionStrings()['entry_not_found'] |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
return $entry; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Search for entries based on parameters. |
| 115 |
* |
| 116 |
* @param array<string, mixed> $params |
| 117 |
* |
| 118 |
* @return array<mixed> |
| 119 |
*/ |
| 120 |
public function searchEntries(array $params): array |
| 121 |
{ |
| 122 |
return $this->entryRepository->search($params); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get the count of entry filters. |
| 127 |
* |
| 128 |
* @param array<string, mixed>|null $params |
| 129 |
* |
| 130 |
* @return array<mixed> |
| 131 |
*/ |
| 132 |
public function getFilterCount(?array $params = null): array |
| 133 |
{ |
| 134 |
return $this->entryRepository->getFilterCount($params); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Delete an entry by its ID. |
| 139 |
* |
| 140 |
* @param int $id |
| 141 |
* |
| 142 |
* @return int |
| 143 |
*/ |
| 144 |
public function deleteEntry(int $id): int |
| 145 |
{ |
| 146 |
return $this->entryRepository->delete($id); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get all entry fields for a specific entry ID. |
| 151 |
* |
| 152 |
* @param int $entryId |
| 153 |
* |
| 154 |
* @return array<mixed> |
| 155 |
*/ |
| 156 |
public function getFieldsByEntryId(int $entryId): array |
| 157 |
{ |
| 158 |
return $this->entryFieldRepository->findBy(['entryId' => $entryId]); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Update the starred status of an entry. |
| 163 |
* |
| 164 |
* @param int $entryId |
| 165 |
* @param bool $value |
| 166 |
* |
| 167 |
* @return void |
| 168 |
*/ |
| 169 |
public function updateEntryStarred(int $entryId, bool $value): void |
| 170 |
{ |
| 171 |
$this->entryRepository->updateEntryStarred($entryId, $value); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Update the status of an entry. |
| 176 |
* |
| 177 |
* @param int $entryId |
| 178 |
* @param string $status |
| 179 |
* |
| 180 |
* @return void |
| 181 |
*/ |
| 182 |
public function updateEntryStatus(int $entryId, string $status): void |
| 183 |
{ |
| 184 |
$this->entryRepository->updateEntryStatus($entryId, $status); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Get the user agent string. |
| 189 |
* |
| 190 |
* @return string |
| 191 |
*/ |
| 192 |
public function getUserAgent(): string |
| 193 |
{ |
| 194 |
return EntryRequestHelper::getUserAgent(); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Fetch entries along with their associated fields. |
| 199 |
* |
| 200 |
* @param array<int, array<string, mixed>> $entries |
| 201 |
* @return array<mixed> |
| 202 |
*/ |
| 203 |
public function getEntryFields(array $entries): array |
| 204 |
{ |
| 205 |
return $this->entryRepository->getEntryFields($entries); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get the count of entries for multiple form IDs. |
| 210 |
* |
| 211 |
* @param int[] $formIds |
| 212 |
* @return array<int, int> formId => count |
| 213 |
*/ |
| 214 |
public function getEntryCountByFormIds(array $formIds): array |
| 215 |
{ |
| 216 |
return $this->entryRepository->getEntryCountByFormIds($formIds); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Delete all entry fields for multiple entry IDs in bulk. |
| 221 |
* |
| 222 |
* @param int[] $entryIds |
| 223 |
* @return int Number of deleted entry fields |
| 224 |
*/ |
| 225 |
public function deleteEntryFieldsByEntryIds(array $entryIds): int |
| 226 |
{ |
| 227 |
if (empty($entryIds)) { |
| 228 |
return 0; |
| 229 |
} |
| 230 |
$allFields = $this->entryFieldRepository->findIdsByEntryIds($entryIds); |
| 231 |
if (empty($allFields)) { |
| 232 |
return 0; |
| 233 |
} |
| 234 |
return $this->entryFieldRepository->deleteMany($allFields); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Delete multiple entries by their IDs in bulk. |
| 239 |
* |
| 240 |
* @param int[] $ids |
| 241 |
* @return int Number of deleted entries |
| 242 |
*/ |
| 243 |
public function deleteEntriesByIds(array $ids): int |
| 244 |
{ |
| 245 |
if (empty($ids)) { |
| 246 |
return 0; |
| 247 |
} |
| 248 |
return $this->entryRepository->deleteMany($ids); |
| 249 |
} |
| 250 |
} |
| 251 |
|