forked from fabarea/messenger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecipientRepository.php
More file actions
172 lines (148 loc) · 4.85 KB
/
Copy pathRecipientRepository.php
File metadata and controls
172 lines (148 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace Fab\Messenger\Domain\Repository;
/*
* This file is part of the Fab/Messenger project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Exception;
use Fab\Messenger\Utility\ConfigurationUtility;
use TYPO3\CMS\Core\Localization\LanguageService;
class RecipientRepository extends AbstractContentRepository
{
protected string $tableName = '';
public function __construct()
{
$this->tableName = ConfigurationUtility::getInstance()->get('recipient_data_type');
}
/**
* @throws DBALException
* @throws Exception
*/
public function findByUid(int $uid): array
{
$query = $this->getQueryBuilder();
$query
->select('*')
->from($this->tableName)
->where(
$this->getQueryBuilder()
->expr()
->eq('uid', $this->getQueryBuilder()->expr()->literal($uid)),
);
$messages = $query->executeQuery()->fetchAssociative();
return is_array($messages) ? $messages : [];
}
/**
* @throws Exception
* @throws DBALException
*/
public function findByDemand(array $demand = [], array $orderings = [], int $offset = 0, int $limit = 0): array
{
$queryBuilder = $this->getQueryBuilder();
$queryBuilder->select('*')->from($this->tableName);
$constraints = [];
if (!empty($demand['likes'])) {
foreach ($demand['likes'] as $field => $value) {
$constraints[] = $queryBuilder
->expr()
->like(
$field,
$queryBuilder->createNamedParameter('%' . $queryBuilder->escapeLikeWildcards($value) . '%'),
);
}
$queryBuilder->where($queryBuilder->expr()->or(...$constraints));
}
if (!empty($demand['uids'])) {
$queryBuilder->andWhere($queryBuilder->expr()->in('uid', $demand['uids']));
}
if ($orderings === []) {
$orderings = ['uid' => 'ASC'];
}
// We handle the sorting
$queryBuilder->addOrderBy(key($orderings), current($orderings));
if ($limit > 0) {
$queryBuilder->setMaxResults($limit);
}
return $queryBuilder->executeQuery()->fetchAllAssociative();
}
/**
* @throws Exception
* @throws DBALException
*/
public function findAll(): array
{
$query = $this->getQueryBuilder();
$query->select('id')->from($this->tableName);
return $query->executeQuery()->fetchAllAssociative();
}
public function findAllEmails(): array
{
$query = $this->getQueryBuilder();
$query->select('email')->from($this->tableName);
return $query->executeQuery()->fetchAllAssociative();
}
/**
* @throws DBALException
* @throws Exception
*/
public function findByUids(array $uids): array
{
$query = $this->getQueryBuilder();
$query
->select('*')
->from($this->tableName)
->where($this->getQueryBuilder()->expr()->in('uid', $uids));
return $query->executeQuery()->fetchAllAssociative();
}
/**
* @throws DBALException
*/
public function deleteAllAction(): void
{
$this->getQueryBuilder()->delete($this->tableName)->executeStatement();
}
/**
* @throws Exception
* @throws DBALException
*/
public function exists(string $email): bool
{
$query = $this->getQueryBuilder();
$record = $query
->select('*')
->from($this->tableName)->where($this->getQueryBuilder()
->expr()
->eq('email', $this->getQueryBuilder()->expr()->literal($email)))->executeQuery()
->fetchAllAssociative();
return !empty($record);
}
/**
* @throws DBALException
*/
public function insert(array $values): bool
{
// Add required TYPO3 fields for fe_users table
$values['tstamp'] = time();
$values['crdate'] = time();
// Set pid if not already set (required for TYPO3)
if (!isset($values['pid'])) {
$values['pid'] = 0; // Default to root page
}
$result = $this->getQueryBuilder()
->insert($this->tableName)->values($values)->executeStatement();
return (bool)$result;
}
public function deleteByUids(array $uids): int
{
$query = $this->getQueryBuilder();
$query->delete($this->tableName)->where($query->expr()->in('uid', $uids));
return $query->executeStatement();
}
protected function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}
}