| 1 |
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 |
|
| 3 |
namespace MailPoet\Form; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) exit; |
| 6 |
|
| 7 |
|
| 8 |
use MailPoet\Doctrine\Repository; |
| 9 |
use MailPoet\Entities\FormEntity; |
| 10 |
|
| 11 |
/** |
| 12 |
* @extends Repository<FormEntity> |
| 13 |
*/ |
| 14 |
class FormsRepository extends Repository { |
| 15 |
protected function getEntityClassName() { |
| 16 |
return FormEntity::class; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* @return FormEntity[] |
| 21 |
*/ |
| 22 |
public function findAllNotDeleted(): array { |
| 23 |
return $this->entityManager |
| 24 |
->createQueryBuilder() |
| 25 |
->select('f') |
| 26 |
->from(FormEntity::class, 'f') |
| 27 |
->where('f.deletedAt IS NULL') |
| 28 |
->orderBy('f.updatedAt', 'desc') |
| 29 |
->getQuery() |
| 30 |
->getResult(); |
| 31 |
} |
| 32 |
|
| 33 |
public function getNamesOfFormsForSegments(): array { |
| 34 |
$allNonDeletedForms = $this->findAllNotDeleted(); |
| 35 |
|
| 36 |
$nameMap = []; |
| 37 |
foreach ($allNonDeletedForms as $form) { |
| 38 |
$blockSegmentsIds = $form->getSettingsSegmentIds(); |
| 39 |
foreach ($blockSegmentsIds as $blockSegmentId) { |
| 40 |
$nameMap[$blockSegmentId][] = $form->getName(); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
return $nameMap; |
| 45 |
} |
| 46 |
|
| 47 |
public function count(): int { |
| 48 |
return (int)$this->doctrineRepository |
| 49 |
->createQueryBuilder('f') |
| 50 |
->select('count(f.id)') |
| 51 |
->getQuery() |
| 52 |
->getSingleScalarResult(); |
| 53 |
} |
| 54 |
|
| 55 |
public function delete(FormEntity $form) { |
| 56 |
$this->entityManager->remove($form); |
| 57 |
$this->flush(); |
| 58 |
} |
| 59 |
|
| 60 |
public function trash(FormEntity $form) { |
| 61 |
$this->bulkTrash([$form->getId()]); |
| 62 |
$this->entityManager->refresh($form); |
| 63 |
} |
| 64 |
|
| 65 |
public function restore(FormEntity $form) { |
| 66 |
$this->bulkRestore([$form->getId()]); |
| 67 |
$this->entityManager->refresh($form); |
| 68 |
} |
| 69 |
|
| 70 |
public function bulkTrash(array $ids): int { |
| 71 |
if (empty($ids)) { |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
$result = $this->entityManager->createQueryBuilder() |
| 76 |
->update(FormEntity::class, 'f') |
| 77 |
->set('f.deletedAt', 'CURRENT_TIMESTAMP()') |
| 78 |
->where('f.id IN (:ids)') |
| 79 |
->setParameter('ids', $ids) |
| 80 |
->getQuery()->execute(); |
| 81 |
|
| 82 |
// update was done via DQL, make sure the entities are also refreshed in the entity manager |
| 83 |
$this->refreshAll(function (FormEntity $entity) use ($ids) { |
| 84 |
return in_array($entity->getId(), $ids, true); |
| 85 |
}); |
| 86 |
|
| 87 |
return $result; |
| 88 |
} |
| 89 |
|
| 90 |
public function bulkRestore(array $ids): int { |
| 91 |
if (empty($ids)) { |
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
$result = $this->entityManager->createQueryBuilder() |
| 96 |
->update(FormEntity::class, 'f') |
| 97 |
->set('f.deletedAt', ':deletedAt') |
| 98 |
->where('f.id IN (:ids)') |
| 99 |
->setParameter('deletedAt', null) |
| 100 |
->setParameter('ids', $ids) |
| 101 |
->getQuery()->execute(); |
| 102 |
|
| 103 |
// update was done via DQL, make sure the entities are also refreshed in the entity manager |
| 104 |
$this->refreshAll(function (FormEntity $entity) use ($ids) { |
| 105 |
return in_array($entity->getId(), $ids, true); |
| 106 |
}); |
| 107 |
|
| 108 |
return $result; |
| 109 |
} |
| 110 |
|
| 111 |
public function bulkDelete(array $ids): int { |
| 112 |
if (empty($ids)) { |
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
$result = $this->entityManager->createQueryBuilder() |
| 117 |
->delete(FormEntity::class, 'f') |
| 118 |
->where('f.id IN (:ids)') |
| 119 |
->setParameter('ids', $ids) |
| 120 |
->getQuery()->execute(); |
| 121 |
|
| 122 |
// delete was done via DQL, make sure the entities are also detached from the entity manager |
| 123 |
$this->detachAll(function (FormEntity $entity) use ($ids) { |
| 124 |
return in_array($entity->getId(), $ids, true); |
| 125 |
}); |
| 126 |
|
| 127 |
return $result; |
| 128 |
} |
| 129 |
} |
| 130 |
|