PluginProbe
MailPoet – Newsletters, Email Marketing, and Automation / 3.100.0
MailPoet – Newsletters, Email Marketing, and Automation v3.100.0
5.38.0 5.37.0 5.36.1 5.36.0 5.35.1 5.35.0 5.34.3 5.34.2 5.34.1 5.34.0 5.33.1 5.33.0 5.32.0 5.31.0 5.30.0 5.29.0 5.28.1 5.28.0 5.27.0 5.26.0 5.26.1 5.25.0 5.24.0 4.43.0 4.43.1 All 542 releases
mailpoet / lib / Form / FormsRepository.php

FormsRepository.php in MailPoet – Newsletters, Email Marketing, and Automation 3.100.0, at lib/Form/FormsRepository.php

109 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
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 return $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
83 public function bulkRestore(array $ids): int {
84 if (empty($ids)) {
85 return 0;
86 }
87
88 return $this->entityManager->createQueryBuilder()
89 ->update(FormEntity::class, 'f')
90 ->set('f.deletedAt', ':deletedAt')
91 ->where('f.id IN (:ids)')
92 ->setParameter('deletedAt', null)
93 ->setParameter('ids', $ids)
94 ->getQuery()->execute();
95 }
96
97 public function bulkDelete(array $ids): int {
98 if (empty($ids)) {
99 return 0;
100 }
101
102 return $this->entityManager->createQueryBuilder()
103 ->delete(FormEntity::class, 'f')
104 ->where('f.id IN (:ids)')
105 ->setParameter('ids', $ids)
106 ->getQuery()->execute();
107 }
108 }
109