| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\Entry\Managers; |
| 4 |
|
| 5 |
// phpcs:disable PSR1.Files.SideEffects |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 11 |
use IvyForms\Repository\Entry\EntryRepositoryInterface; |
| 12 |
use IvyForms\Repository\EntryField\EntryFieldRepositoryInterface; |
| 13 |
use IvyForms\Services\Translations\BackendStrings; |
| 14 |
|
| 15 |
/** |
| 16 |
* Handles bulk entry deletion operations across forms. |
| 17 |
*/ |
| 18 |
class EntryDeletionManager |
| 19 |
{ |
| 20 |
private EntryRepositoryInterface $entryRepository; |
| 21 |
private EntryFieldRepositoryInterface $entryFieldRepository; |
| 22 |
|
| 23 |
public function __construct( |
| 24 |
EntryRepositoryInterface $entryRepository, |
| 25 |
EntryFieldRepositoryInterface $entryFieldRepository |
| 26 |
) { |
| 27 |
$this->entryRepository = $entryRepository; |
| 28 |
$this->entryFieldRepository = $entryFieldRepository; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Delete an entry by its ID. |
| 33 |
* |
| 34 |
* @param int $id |
| 35 |
* @return int |
| 36 |
*/ |
| 37 |
public function deleteEntry(int $id): int |
| 38 |
{ |
| 39 |
return $this->entryRepository->delete($id); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Delete multiple entries by their IDs in bulk. |
| 44 |
* |
| 45 |
* @param int[] $ids |
| 46 |
* @return int Number of deleted entries |
| 47 |
*/ |
| 48 |
public function deleteEntriesByIds(array $ids): int |
| 49 |
{ |
| 50 |
if (empty($ids)) { |
| 51 |
return 0; |
| 52 |
} |
| 53 |
return $this->entryRepository->deleteMany($ids); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Delete all entry fields for multiple entry IDs in bulk. |
| 58 |
* |
| 59 |
* @param int[] $entryIds |
| 60 |
* @return int Number of deleted entry fields |
| 61 |
*/ |
| 62 |
public function deleteEntryFieldsByEntryIds(array $entryIds): int |
| 63 |
{ |
| 64 |
if (empty($entryIds)) { |
| 65 |
return 0; |
| 66 |
} |
| 67 |
$allFields = $this->entryFieldRepository->findIdsByEntryIds($entryIds); |
| 68 |
if (empty($allFields)) { |
| 69 |
return 0; |
| 70 |
} |
| 71 |
return $this->entryFieldRepository->deleteMany($allFields); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Delete all entries for multiple form IDs in bulk. |
| 76 |
* |
| 77 |
* @param int[] $formIds |
| 78 |
* @return int Number of deleted entries |
| 79 |
* @throws InvalidArgumentException |
| 80 |
*/ |
| 81 |
public function deleteEntriesByFormIds(array $formIds): int |
| 82 |
{ |
| 83 |
if (empty($formIds)) { |
| 84 |
throw new InvalidArgumentException( |
| 85 |
BackendStrings::getExceptionStrings()['no_form_ids_provided'] |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
// Delete entry fields by form IDs using a subquery |
| 90 |
$this->entryFieldRepository->deleteByFormIds($formIds); |
| 91 |
// Delete entries by formId |
| 92 |
return $this->entryRepository->deleteManyByForeignKeyValues($formIds); |
| 93 |
} |
| 94 |
} |
| 95 |
|