1
|
|
|
<?php |
2
|
|
|
namespace DERHANSEN\SfEventMgt\Domain\Repository; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the Extension "sf_event_mgt" for TYPO3 CMS. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please read the |
8
|
|
|
* LICENSE.txt file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
use DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand; |
12
|
|
|
use DERHANSEN\SfEventMgt\Service\CategoryService; |
13
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
14
|
|
|
use TYPO3\CMS\Core\Utility\MathUtility; |
15
|
|
|
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The repository for Events |
19
|
|
|
* |
20
|
|
|
* @author Torben Hansen <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class EventRepository extends \TYPO3\CMS\Extbase\Persistence\Repository |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Set default sorting |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $defaultOrderings = [ |
30
|
|
|
'startdate' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Disable the use of storage records, because the StoragePage can be set |
35
|
|
|
* in the plugin |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function initializeObject() |
40
|
|
|
{ |
41
|
|
|
$this->defaultQuerySettings = $this->objectManager->get(Typo3QuerySettings::class); |
42
|
|
|
$this->defaultQuerySettings->setRespectStoragePage(false); |
43
|
|
|
} |
44
|
|
|
|
45
|
31 |
|
/** |
46
|
|
|
* Returns the objects of this repository matching the given demand |
47
|
31 |
|
* |
48
|
31 |
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand |
49
|
31 |
|
* |
50
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface QueryResultInterface |
51
|
|
|
*/ |
52
|
|
|
public function findDemanded(EventDemand $eventDemand) |
53
|
|
|
{ |
54
|
|
|
$constraints = []; |
55
|
|
|
$query = $this->createQuery(); |
56
|
|
|
$this->setStoragePageConstraint($query, $eventDemand, $constraints); |
57
|
|
|
$this->setDisplayModeConstraint($query, $eventDemand, $constraints); |
58
|
30 |
|
$this->setCategoryConstraint($query, $eventDemand, $constraints); |
59
|
|
|
$this->setLocationConstraint($query, $eventDemand, $constraints); |
60
|
30 |
|
$this->setLocationCityConstraint($query, $eventDemand, $constraints); |
61
|
30 |
|
$this->setLocationCountryConstraint($query, $eventDemand, $constraints); |
62
|
30 |
|
$this->setOrganisatorConstraint($query, $eventDemand, $constraints); |
63
|
30 |
|
$this->setStartEndDateConstraint($query, $eventDemand, $constraints); |
64
|
30 |
|
$this->setSearchConstraint($query, $eventDemand, $constraints); |
65
|
30 |
|
$this->setTopEventConstraint($query, $eventDemand, $constraints); |
66
|
30 |
|
$this->setYearMonthDayRestriction($query, $eventDemand, $constraints); |
67
|
30 |
|
$this->setOrderingsFromDemand($query, $eventDemand); |
68
|
30 |
|
|
69
|
30 |
|
if (count($constraints) > 0) { |
70
|
30 |
|
$query->matching($query->logicalAnd($constraints)); |
71
|
30 |
|
} |
72
|
|
|
|
73
|
30 |
|
$this->setQueryLimitFromDemand($query, $eventDemand); |
74
|
30 |
|
|
75
|
30 |
|
return $query->execute(); |
76
|
|
|
} |
77
|
30 |
|
|
78
|
30 |
|
/** |
79
|
|
|
* Sets a query limit to the given query for the given demand |
80
|
|
|
* |
81
|
|
|
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query |
82
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
protected function setQueryLimitFromDemand($query, EventDemand $eventDemand) |
87
|
|
|
{ |
88
|
|
|
if ($eventDemand->getQueryLimit() != null && |
89
|
30 |
|
MathUtility::canBeInterpretedAsInteger($eventDemand->getQueryLimit()) && |
90
|
|
|
(int)$eventDemand->getQueryLimit() > 0 |
91
|
30 |
|
) { |
92
|
30 |
|
$query->setLimit((int)$eventDemand->getQueryLimit()); |
93
|
1 |
|
} |
94
|
30 |
|
} |
95
|
1 |
|
|
96
|
1 |
|
/** |
97
|
30 |
|
* Sets the ordering to the given query for the given demand |
98
|
|
|
* |
99
|
|
|
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query |
100
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
protected function setOrderingsFromDemand($query, EventDemand $eventDemand) |
105
|
|
|
{ |
106
|
|
|
$orderings = []; |
107
|
30 |
|
$orderFieldAllowed = GeneralUtility::trimExplode(',', $eventDemand->getOrderFieldAllowed(), true); |
108
|
|
|
if ($eventDemand->getOrderField() != '' && $eventDemand->getOrderDirection() != '' && |
109
|
30 |
|
!empty($orderFieldAllowed) && in_array($eventDemand->getOrderField(), $orderFieldAllowed, true)) { |
110
|
30 |
|
$orderings[$eventDemand->getOrderField()] = ((strtolower($eventDemand->getOrderDirection()) == 'desc') ? |
111
|
6 |
|
\TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING : |
112
|
6 |
|
\TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING); |
113
|
3 |
|
$query->setOrderings($orderings); |
114
|
6 |
|
} |
115
|
6 |
|
} |
116
|
30 |
|
|
117
|
|
|
/** |
118
|
|
|
* Sets the storagePage constraint to the given constraints array |
119
|
|
|
* |
120
|
|
|
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query |
121
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand |
122
|
|
|
* @param array $constraints Constraints |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
protected function setStoragePageConstraint($query, $eventDemand, &$constraints) |
127
|
30 |
|
{ |
128
|
|
|
if ($eventDemand->getStoragePage() != '') { |
129
|
30 |
|
$pidList = GeneralUtility::intExplode(',', $eventDemand->getStoragePage(), true); |
130
|
30 |
|
$constraints[] = $query->in('pid', $pidList); |
131
|
30 |
|
} |
132
|
30 |
|
} |
133
|
30 |
|
|
134
|
|
|
/** |
135
|
|
|
* Sets the displayMode constraint to the given constraints array |
136
|
|
|
* |
137
|
|
|
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query |
138
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand |
139
|
|
|
* @param array $constraints Constraints |
140
|
|
|
* |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
protected function setDisplayModeConstraint($query, $eventDemand, &$constraints) |
144
|
30 |
|
{ |
145
|
|
|
switch ($eventDemand->getDisplayMode()) { |
146
|
30 |
|
case 'future': |
147
|
30 |
|
$constraints[] = $query->greaterThan('startdate', $eventDemand->getCurrentDateTime()); |
148
|
1 |
|
break; |
149
|
1 |
|
case 'current_future': |
150
|
29 |
|
$constraints[] = $query->logicalOr([ |
151
|
1 |
|
$query->greaterThan('startdate', $eventDemand->getCurrentDateTime()), |
152
|
1 |
|
$query->logicalAnd([ |
153
|
28 |
|
$query->greaterThanOrEqual('enddate', $eventDemand->getCurrentDateTime()), |
154
|
30 |
|
$query->lessThanOrEqual('startdate', $eventDemand->getCurrentDateTime()) |
155
|
30 |
|
]) |
156
|
|
|
]); |
157
|
|
|
break; |
158
|
|
|
case 'past': |
159
|
|
|
$constraints[] = $query->lessThanOrEqual('enddate', $eventDemand->getCurrentDateTime()); |
160
|
|
|
break; |
161
|
|
|
default: |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
30 |
|
* Sets the category constraint to the given constraints array |
167
|
|
|
* |
168
|
30 |
|
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query |
169
|
5 |
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand |
170
|
5 |
|
* @param array $constraints Constraints |
171
|
1 |
|
* |
172
|
1 |
|
* @return void |
173
|
1 |
|
*/ |
174
|
4 |
|
protected function setCategoryConstraint($query, $eventDemand, &$constraints) |
175
|
|
|
{ |
176
|
5 |
|
// If no category constraint is set, categories should not be respected in the query |
177
|
5 |
|
if ($eventDemand->getCategoryConjunction() === '') { |
178
|
5 |
|
return; |
179
|
5 |
|
} |
180
|
5 |
|
|
181
|
5 |
|
if ($eventDemand->getCategory() != '') { |
182
|
5 |
|
$categoryConstraints = []; |
183
|
30 |
|
if ($eventDemand->getIncludeSubcategories()) { |
184
|
|
|
$categoryList = CategoryService::getCategoryListWithChilds($eventDemand->getCategory()); |
185
|
|
|
$categories = GeneralUtility::intExplode(',', $categoryList, true); |
186
|
|
|
} else { |
187
|
|
|
$categories = GeneralUtility::intExplode(',', $eventDemand->getCategory(), true); |
188
|
|
|
} |
189
|
|
|
foreach ($categories as $category) { |
190
|
|
|
$categoryConstraints[] = $query->contains('category', $category); |
191
|
|
|
} |
192
|
|
|
if (count($categoryConstraints) > 0) { |
193
|
|
|
$constraints[] = $this->getCategoryConstraint($query, $eventDemand, $categoryConstraints); |
194
|
30 |
|
} |
195
|
|
|
} |
196
|
30 |
|
} |
197
|
3 |
|
|
198
|
3 |
|
/** |
199
|
30 |
|
* Returns the category constraint depending on the category conjunction configured in eventDemand |
200
|
|
|
* |
201
|
|
|
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query |
202
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand |
203
|
|
|
* @param array $categoryConstraints |
204
|
|
|
* @return mixed |
205
|
|
|
*/ |
206
|
|
|
public function getCategoryConstraint($query, $eventDemand, $categoryConstraints) |
207
|
|
|
{ |
208
|
|
|
switch (strtolower($eventDemand->getCategoryConjunction())) { |
209
|
|
|
case 'and': |
210
|
30 |
|
$constraint = $query->logicalAnd($categoryConstraints); |
211
|
|
|
break; |
212
|
30 |
|
case 'notor': |
213
|
2 |
|
$constraint = $query->logicalNot($query->logicalOr($categoryConstraints)); |
214
|
2 |
|
break; |
215
|
30 |
|
case 'notand': |
216
|
|
|
$constraint = $query->logicalNot($query->logicalAnd($categoryConstraints)); |
217
|
|
|
break; |
218
|
|
|
case 'or': |
219
|
|
|
default: |
220
|
|
|
$constraint = $query->logicalOr($categoryConstraints); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $constraint; |
224
|
|
|
} |
225
|
|
|
|
226
|
30 |
|
/** |
227
|
|
|
* Sets the location constraint to the given constraints array |
228
|
30 |
|
* |
229
|
2 |
|
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query |
230
|
2 |
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand |
231
|
30 |
|
* @param array $constraints Constraints |
232
|
|
|
* |
233
|
|
|
* @return void |
234
|
|
|
*/ |
235
|
|
|
protected function setLocationConstraint($query, $eventDemand, &$constraints) |
236
|
|
|
{ |
237
|
|
|
if ($eventDemand->getLocation() !== null && $eventDemand->getLocation() != '') { |
238
|
|
|
$constraints[] = $query->equals('location', $eventDemand->getLocation()); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
30 |
|
/** |
243
|
|
|
* Sets the location.city constraint to the given constraints array |
244
|
|
|
* |
245
|
30 |
|
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query |
246
|
1 |
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand |
247
|
1 |
|
* @param array $constraints Constraints |
248
|
|
|
* |
249
|
|
|
* @return void |
250
|
30 |
|
*/ |
251
|
1 |
|
protected function setLocationCityConstraint($query, $eventDemand, &$constraints) |
252
|
1 |
|
{ |
253
|
30 |
|
if ($eventDemand->getLocationCity() !== null && $eventDemand->getLocationCity() != '') { |
254
|
|
|
$constraints[] = $query->equals('location.city', $eventDemand->getLocationCity()); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Sets the location.country constraint to the given constraints array |
260
|
|
|
* |
261
|
|
|
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query |
262
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand |
263
|
|
|
* @param array $constraints Constraints |
264
|
30 |
|
* |
265
|
|
|
* @return void |
266
|
30 |
|
*/ |
267
|
30 |
|
protected function setLocationCountryConstraint($query, $eventDemand, &$constraints) |
268
|
3 |
|
{ |
269
|
30 |
|
if ($eventDemand->getLocationCountry() !== null && $eventDemand->getLocationCountry() != '') { |
270
|
1 |
|
$constraints[] = $query->equals('location.country', $eventDemand->getLocationCountry()); |
271
|
1 |
|
} |
272
|
|
|
} |
273
|
1 |
|
|
274
|
|
|
/** |
275
|
|
|
* Sets the organisator constraint to the given constraints array |
276
|
|
|
* |
277
|
1 |
|
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query |
278
|
1 |
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand |
279
|
1 |
|
* @param array $constraints Constraints |
280
|
1 |
|
* |
281
|
1 |
|
* @return void |
282
|
1 |
|
*/ |
283
|
|
|
protected function setOrganisatorConstraint($query, $eventDemand, &$constraints) |
284
|
1 |
|
{ |
285
|
1 |
|
if ($eventDemand->getOrganisator() !== null && $eventDemand->getOrganisator() != '') { |
286
|
1 |
|
$constraints[] = $query->equals('organisator', $eventDemand->getOrganisator()); |
287
|
1 |
|
} |
288
|
30 |
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Sets the start- and enddate constraint to the given constraints array |
292
|
|
|
* |
293
|
|
|
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query |
294
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand |
295
|
|
|
* @param array $constraints Constraints |
296
|
|
|
* |
297
|
|
|
* @return void |
298
|
|
|
*/ |
299
|
30 |
|
protected function setStartEndDateConstraint($query, $eventDemand, &$constraints) |
300
|
|
|
{ |
301
|
30 |
|
/* StartDate */ |
302
|
2 |
|
if ($eventDemand->getSearchDemand() && $eventDemand->getSearchDemand()->getStartDate() !== null) { |
303
|
2 |
|
$constraints[] = $query->greaterThanOrEqual('startdate', $eventDemand->getSearchDemand()->getStartDate()); |
304
|
30 |
|
} |
305
|
|
|
|
306
|
|
|
/* EndDate */ |
307
|
|
|
if ($eventDemand->getSearchDemand() && $eventDemand->getSearchDemand()->getEndDate() !== null) { |
308
|
|
|
$constraints[] = $query->lessThanOrEqual('enddate', $eventDemand->getSearchDemand()->getEndDate()); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Sets the search constraint to the given constraints array |
314
|
|
|
* |
315
|
|
|
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query |
316
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand |
317
|
|
|
* @param array $constraints Constraints |
318
|
|
|
* |
319
|
|
|
* @return void |
320
|
|
|
*/ |
321
|
|
|
protected function setSearchConstraint($query, $eventDemand, &$constraints) |
322
|
|
|
{ |
323
|
|
|
if ($eventDemand->getSearchDemand() && |
324
|
|
|
$eventDemand->getSearchDemand()->getSearch() !== null && |
325
|
|
|
$eventDemand->getSearchDemand()->getSearch() !== '' |
326
|
|
|
) { |
327
|
|
|
$searchFields = GeneralUtility::trimExplode(',', $eventDemand->getSearchDemand()->getFields(), true); |
328
|
|
|
$searchConstraints = []; |
329
|
|
|
|
330
|
|
|
if (count($searchFields) === 0) { |
331
|
|
|
throw new \UnexpectedValueException('No search fields defined', 1318497755); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
$searchSubject = $eventDemand->getSearchDemand()->getSearch(); |
335
|
|
|
foreach ($searchFields as $field) { |
336
|
|
|
if (!empty($searchSubject)) { |
337
|
|
|
$searchConstraints[] = $query->like($field, '%' . addcslashes($searchSubject, '_%') . '%', false); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
if (count($searchConstraints)) { |
342
|
|
|
$constraints[] = $query->logicalOr($searchConstraints); |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Sets the topEvent constraint to the given constraints array |
349
|
|
|
* |
350
|
|
|
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query |
351
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand |
352
|
|
|
* @param array $constraints Constraints |
353
|
|
|
* |
354
|
|
|
* @return void |
355
|
|
|
*/ |
356
|
|
|
protected function setTopEventConstraint($query, $eventDemand, &$constraints) |
357
|
|
|
{ |
358
|
|
|
if ($eventDemand->getTopEventRestriction() > 0) { |
359
|
|
|
$constraints[] = $query->equals('topEvent', (bool)($eventDemand->getTopEventRestriction() - 1)); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Sets the restriction for year, year/month or year/month/day to the given constraints array |
365
|
|
|
* |
366
|
|
|
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query |
367
|
|
|
* @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand |
368
|
|
|
* @param array $constraints |
369
|
|
|
* |
370
|
|
|
* @return void |
371
|
|
|
*/ |
372
|
|
|
protected function setYearMonthDayRestriction($query, $eventDemand, &$constraints) |
373
|
|
|
{ |
374
|
|
|
if ($eventDemand->getYear() > 0) { |
375
|
|
|
if ($eventDemand->getMonth() > 0) { |
376
|
|
|
if ($eventDemand->getDay() > 0) { |
377
|
|
|
$begin = mktime(0, 0, 0, $eventDemand->getMonth(), $eventDemand->getDay(), $eventDemand->getYear()); |
378
|
|
|
$end = mktime(23, 59, 59, $eventDemand->getMonth(), $eventDemand->getDay(), $eventDemand->getYear()); |
379
|
|
|
} else { |
380
|
|
|
$begin = mktime(0, 0, 0, $eventDemand->getMonth(), 1, $eventDemand->getYear()); |
381
|
|
|
$end = mktime(23, 59, 59, ($eventDemand->getMonth() + 1), 0, $eventDemand->getYear()); |
382
|
|
|
} |
383
|
|
|
} else { |
384
|
|
|
$begin = mktime(0, 0, 0, 1, 1, $eventDemand->getYear()); |
385
|
|
|
$end = mktime(23, 59, 59, 12, 31, $eventDemand->getYear()); |
386
|
|
|
} |
387
|
|
|
$constraints[] = $query->logicalOr([ |
388
|
|
|
$query->between('startdate', $begin, $end), |
389
|
|
|
$query->between('enddate', $begin, $end), |
390
|
|
|
$query->logicalAnd([ |
391
|
|
|
$query->greaterThanOrEqual('enddate', $begin), |
392
|
|
|
$query->lessThanOrEqual('startdate', $begin) |
393
|
|
|
]) |
394
|
|
|
]); |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|