1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
/** |
5
|
|
|
* Build up the Question. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace HDNET\Faq\Domain\Repository; |
9
|
|
|
|
10
|
|
|
use HDNET\Faq\Domain\Model\Question; |
11
|
|
|
use HDNET\Faq\Domain\Model\Questioncategory; |
12
|
|
|
use HDNET\Faq\Domain\Model\Request\Faq; |
13
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool; |
14
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
15
|
|
|
use TYPO3\CMS\Extbase\Persistence\QueryInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Build up the Question. |
19
|
|
|
*/ |
20
|
|
|
class QuestionRepository extends AbstractRepository |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Orderings. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $defaultOrderings = [ |
28
|
|
|
'title' => QueryInterface::ORDER_ASCENDING, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructs a new Repository |
33
|
|
|
* |
34
|
|
|
* @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager |
35
|
|
|
*/ |
36
|
|
|
public function __construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) |
37
|
|
|
{ |
38
|
|
|
parent::__construct($objectManager); |
39
|
|
|
|
40
|
|
|
$configuration = (array) @\unserialize((string) $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['faq']); |
41
|
|
|
$enableManuallySorting = isset($configuration['enableManuallySorting']) ? (bool) $configuration['enableManuallySorting'] : false; |
42
|
|
|
|
43
|
|
|
if ($enableManuallySorting) { |
44
|
|
|
$this->defaultOrderings = [ |
45
|
|
|
'sorting' => QueryInterface::ORDER_ASCENDING, |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the top questions. |
53
|
|
|
* |
54
|
|
|
* @param int $limit |
55
|
|
|
* @param int $topCategoryId |
56
|
|
|
* @param array $topQuestions |
57
|
|
|
* |
58
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
59
|
|
|
*/ |
60
|
|
|
public function findTop($limit = 5, $topCategoryId = 0, $topQuestions = []) |
61
|
|
|
{ |
62
|
|
|
$questions = $this->getStaticQuestionsAndReduceLimit($topQuestions, $limit); |
63
|
|
|
if ($limit > 0) { |
64
|
|
|
$query = $this->createQuery(); |
65
|
|
|
$constraintsOr = []; |
66
|
|
|
$constraintsOr[] = $query->contains('categories', $topCategoryId); |
67
|
|
|
$constraintsOr[] = $query->equals('categories.parent', $topCategoryId); |
68
|
|
|
|
69
|
|
|
$constraintsAnd = []; |
70
|
|
|
$constraintsAnd[] = $query->logicalOr($constraintsOr); |
71
|
|
|
if (!empty($topQuestions)) { |
72
|
|
|
$constraintsAnd[] = $query->logicalNot($query->in('uid', $topQuestions)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$query->matching($query->logicalAnd($constraintsAnd)); |
76
|
|
|
|
77
|
|
|
$query->setOrderings(['topCounter' => QueryInterface::ORDER_DESCENDING]); |
78
|
|
|
$query->setLimit($limit); |
79
|
|
|
$results = $query->execute() |
80
|
|
|
->toArray(); |
81
|
|
|
|
82
|
|
|
$questions = \array_merge($questions, $results); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $questions; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the newest questions. |
90
|
|
|
* |
91
|
|
|
* @param int $limit |
92
|
|
|
* @param int $topCategoryId |
93
|
|
|
* |
94
|
|
|
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException |
95
|
|
|
* |
96
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
97
|
|
|
*/ |
98
|
|
|
public function findNewest($limit = 5, $topCategoryId = 0) |
99
|
|
|
{ |
100
|
|
|
$query = $this->createQuery(); |
101
|
|
|
|
102
|
|
|
$constraintsOr = []; |
103
|
|
|
$constraintsOr[] = $query->contains('categories', $topCategoryId); |
104
|
|
|
$constraintsOr[] = $query->equals('categories.parent', $topCategoryId); |
105
|
|
|
|
106
|
|
|
$query->matching($query->logicalOr($constraintsOr)); |
107
|
|
|
|
108
|
|
|
$query->setOrderings(['crdate' => QueryInterface::ORDER_DESCENDING]); |
109
|
|
|
if ($limit) { |
110
|
|
|
$query->setLimit($limit); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $query->execute(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns all objects of this repository. |
118
|
|
|
* |
119
|
|
|
* @param int $topCategoryId |
120
|
|
|
* |
121
|
|
|
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException |
122
|
|
|
* |
123
|
|
|
* @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface|array |
124
|
|
|
*/ |
125
|
|
|
public function findAll($topCategoryId = 0) |
126
|
|
|
{ |
127
|
|
|
if (!$topCategoryId) { |
128
|
|
|
return parent::findAll(); |
129
|
|
|
} |
130
|
|
|
$query = $this->createQuery(); |
131
|
|
|
|
132
|
|
|
$constraintsOr = []; |
133
|
|
|
$constraintsOr[] = $query->contains('categories', $topCategoryId); |
134
|
|
|
$constraintsOr[] = $query->equals('categories.parent', $topCategoryId); |
135
|
|
|
|
136
|
|
|
$query->matching($query->logicalOr($constraintsOr)); |
137
|
|
|
|
138
|
|
|
return $query->execute(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Find by FAQ model. |
143
|
|
|
* |
144
|
|
|
* @param int $topCategoryId |
145
|
|
|
* |
146
|
|
|
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException |
147
|
|
|
* |
148
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
149
|
|
|
*/ |
150
|
|
|
public function findByFaq(Faq $faq, $topCategoryId = 0) |
151
|
|
|
{ |
152
|
|
|
$query = $this->createQuery(); |
153
|
|
|
|
154
|
|
|
$constraintsOr = []; |
155
|
|
|
$constraintsOr[] = $query->contains('categories', $topCategoryId); |
156
|
|
|
$constraintsOr[] = $query->equals('categories.parent', $topCategoryId); |
157
|
|
|
|
158
|
|
|
$constraints = []; |
159
|
|
|
$constraints[] = $query->logicalOr($constraintsOr); |
160
|
|
|
|
161
|
|
|
$categories = GeneralUtility::intExplode(',', \implode(',', $faq->getCategories()), true); |
162
|
|
|
if ($faq->getCategory() instanceof Questioncategory) { |
163
|
|
|
$categories[] = (int)$faq->getCategory() |
164
|
|
|
->getUid(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if ($categories) { |
|
|
|
|
168
|
|
|
$catSelection = []; |
169
|
|
|
foreach ($categories as $catId) { |
170
|
|
|
$catSelection[] = $query->contains('categories', $catId); |
171
|
|
|
} |
172
|
|
|
$constraints[] = $query->logicalOr($catSelection); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (\mb_strlen($faq->getSearchWord())) { |
176
|
|
|
$constraints[] = $query->logicalOr([ |
177
|
|
|
$query->like('title', '%' . $faq->getSearchWord() . '%'), |
178
|
|
|
$query->like('answer', '%' . $faq->getSearchWord() . '%'), |
179
|
|
|
]); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (\count($constraints)) { |
183
|
|
|
$query->matching($query->logicalAnd($constraints)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $query->execute(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get the teaser questions. |
191
|
|
|
* |
192
|
|
|
* @param int $limit |
193
|
|
|
* |
194
|
|
|
* @throws \Exception |
195
|
|
|
* |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
|
|
public function findByTeaserConfiguration(array $topQuestions, array $categories, $limit) |
199
|
|
|
{ |
200
|
|
|
$questions = $this->getStaticQuestionsAndReduceLimit($topQuestions, $limit); |
201
|
|
|
if ($limit > 0) { |
202
|
|
|
$t = $this->getTableName(); |
203
|
|
|
|
204
|
|
|
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_faq_mm_question_questioncategory'); |
205
|
|
|
$queryBuilder |
206
|
|
|
->getRestrictions() |
207
|
|
|
->removeAll(); |
208
|
|
|
|
209
|
|
|
$where = [ |
210
|
|
|
$queryBuilder->expr()->eq($t . '.uid', 'mm.uid_local'), |
211
|
|
|
]; |
212
|
|
|
if (\count($topQuestions)) { |
213
|
|
|
$where[] = $queryBuilder->expr()->notIn($t . '.uid', $topQuestions); |
214
|
|
|
} |
215
|
|
|
if (!empty($categories)) { |
216
|
|
|
$where[] = $queryBuilder->expr()->in('mm.uid_foreign', $categories); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$rows = $queryBuilder->select($t . '.*') |
220
|
|
|
->from($t) |
221
|
|
|
->join($t, 'tx_faq_mm_question_questioncategory', 'mm') |
222
|
|
|
->where(...$where) |
223
|
|
|
->groupBy($t . '.uid') |
224
|
|
|
->execute() |
225
|
|
|
->fetchAll(); |
226
|
|
|
|
227
|
|
|
\shuffle($rows); |
228
|
|
|
|
229
|
|
|
foreach ($rows as $row) { |
230
|
|
|
$q = $this->findByUid((int)$row['uid']); |
231
|
|
|
if ($q instanceof Question) { |
232
|
|
|
$questions[] = $q; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $questions; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Get the Questsions with the given IDS and reduce the limit. |
242
|
|
|
* |
243
|
|
|
* @param int $limit |
244
|
|
|
* |
245
|
|
|
* @return array |
246
|
|
|
*/ |
247
|
|
|
protected function getStaticQuestionsAndReduceLimit(array $ids, &$limit) |
248
|
|
|
{ |
249
|
|
|
$questions = []; |
250
|
|
|
foreach ($ids as $id) { |
251
|
|
|
$q = $this->findByUid((int)$id); |
252
|
|
|
if ($q instanceof Question) { |
253
|
|
|
$questions[] = $q; |
254
|
|
|
--$limit; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $questions; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.