1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Index repository. |
5
|
|
|
*/ |
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace HDNET\Calendarize\Domain\Repository; |
9
|
|
|
|
10
|
|
|
use Exception; |
11
|
|
|
use HDNET\Calendarize\Domain\Model\Index; |
12
|
|
|
use HDNET\Calendarize\Domain\Model\Request\OptionRequest; |
13
|
|
|
use HDNET\Calendarize\Event\AddTimeFrameConstraintsEvent; |
14
|
|
|
use HDNET\Calendarize\Utility\ConfigurationUtility; |
15
|
|
|
use HDNET\Calendarize\Utility\DateTimeUtility; |
16
|
|
|
use HDNET\Calendarize\Utility\ExtensionConfigurationUtility; |
17
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
18
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool; |
19
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
20
|
|
|
use TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager; |
21
|
|
|
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; |
22
|
|
|
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface; |
23
|
|
|
use TYPO3\CMS\Extbase\Persistence\QueryInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Index repository. |
27
|
|
|
*/ |
28
|
|
|
class IndexRepository extends AbstractRepository |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Default orderings for index records. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $defaultOrderings = [ |
36
|
|
|
'start_date' => QueryInterface::ORDER_ASCENDING, |
37
|
|
|
'start_time' => QueryInterface::ORDER_ASCENDING, |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Index types for selection. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $indexTypes = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Override page ids. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $overridePageIds; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var EventDispatcherInterface |
56
|
|
|
*/ |
57
|
|
|
protected $eventDispatcher; |
58
|
|
|
|
59
|
|
|
public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher): void |
60
|
|
|
{ |
61
|
|
|
$this->eventDispatcher = $eventDispatcher; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create query. |
66
|
|
|
* |
67
|
|
|
* @return QueryInterface |
68
|
|
|
*/ |
69
|
|
|
public function createQuery() |
70
|
|
|
{ |
71
|
|
|
$query = parent::createQuery(); |
72
|
|
|
|
73
|
|
|
return $query; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the index types. |
78
|
|
|
* |
79
|
|
|
* @param array $types |
80
|
|
|
*/ |
81
|
|
|
public function setIndexTypes(array $types) |
82
|
|
|
{ |
83
|
|
|
$this->indexTypes = $types; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Override page IDs. |
88
|
|
|
* |
89
|
|
|
* @param array $overridePageIds |
90
|
|
|
*/ |
91
|
|
|
public function setOverridePageIds($overridePageIds) |
92
|
|
|
{ |
93
|
|
|
$this->overridePageIds = $overridePageIds; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Select indecies for Backend. |
98
|
|
|
* |
99
|
|
|
* @param OptionRequest $options |
100
|
|
|
* |
101
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
102
|
|
|
*/ |
103
|
|
|
public function findAllForBackend(OptionRequest $options) |
104
|
|
|
{ |
105
|
|
|
$query = $this->createQuery(); |
106
|
|
|
$query->getQuerySettings()->setIgnoreEnableFields(true); |
107
|
|
|
$query->getQuerySettings()->setRespectSysLanguage(false); |
108
|
|
|
$query->getQuerySettings()->setLanguageOverlayMode(false); |
109
|
|
|
|
110
|
|
|
// Notice Selection without any language handling |
111
|
|
|
unset($GLOBALS['TCA']['tx_calendarize_domain_model_index']['ctrl']['languageField'], $GLOBALS['TCA']['tx_calendarize_domain_model_index']['ctrl']['transOrigPointerField']); |
112
|
|
|
|
113
|
|
|
if ('asc' === $options->getDirection()) { |
114
|
|
|
$query->setOrderings([ |
115
|
|
|
'start_date' => QueryInterface::ORDER_ASCENDING, |
116
|
|
|
'start_time' => QueryInterface::ORDER_ASCENDING, |
117
|
|
|
]); |
118
|
|
|
} else { |
119
|
|
|
$query->setOrderings([ |
120
|
|
|
'start_date' => QueryInterface::ORDER_DESCENDING, |
121
|
|
|
'start_time' => QueryInterface::ORDER_DESCENDING, |
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ((int)$options->getPid() > 0) { |
126
|
|
|
$query->matching($query->equals('pid', (int)$options->getPid())); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $query->execute(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Find List. |
134
|
|
|
* |
135
|
|
|
* @param int $limit |
136
|
|
|
* @param int|string $listStartTime |
137
|
|
|
* @param int $startOffsetHours |
138
|
|
|
* @param int $overrideStartDate |
139
|
|
|
* @param int $overrideEndDate |
140
|
|
|
* |
141
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
142
|
|
|
*/ |
143
|
|
|
public function findList( |
144
|
|
|
$limit = 0, |
145
|
|
|
$listStartTime = 0, |
146
|
|
|
$startOffsetHours = 0, |
147
|
|
|
$overrideStartDate = 0, |
148
|
|
|
$overrideEndDate = 0 |
149
|
|
|
) { |
150
|
|
|
$startTime = DateTimeUtility::getNow(); |
151
|
|
|
$endTime = null; |
152
|
|
|
|
153
|
|
|
if ($overrideStartDate > 0) { |
154
|
|
|
// Note: setTimestamp does not change the timezone |
155
|
|
|
$startTime->setTimestamp($overrideStartDate); |
156
|
|
|
} else { |
157
|
|
|
if ('now' !== $listStartTime) { |
|
|
|
|
158
|
|
|
$startTime->setTime(0, 0, 0); |
159
|
|
|
} |
160
|
|
|
$startTime->modify($startOffsetHours . ' hours'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if ($overrideEndDate > 0) { |
164
|
|
|
$endTime = DateTimeUtility::getNow()->setTimestamp($overrideEndDate); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$result = $this->findByTimeSlot($startTime, $endTime); |
168
|
|
|
if ($limit > 0) { |
169
|
|
|
$query = $result->getQuery(); |
170
|
|
|
$query->setLimit($limit); |
171
|
|
|
$result = $query->execute(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $result; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Find by custom search. |
179
|
|
|
* |
180
|
|
|
* @param \DateTimeInterface|null $startDate |
181
|
|
|
* @param \DateTimeInterface|null $endDate |
182
|
|
|
* @param array $customSearch |
183
|
|
|
* @param int $limit |
184
|
|
|
* |
185
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
186
|
|
|
*/ |
187
|
|
|
public function findBySearch( |
188
|
|
|
\DateTimeInterface $startDate = null, |
189
|
|
|
\DateTimeInterface $endDate = null, |
190
|
|
|
array $customSearch = [], |
191
|
|
|
int $limit = 0 |
192
|
|
|
) { |
193
|
|
|
$arguments = [ |
194
|
|
|
'indexIds' => [], |
195
|
|
|
'startDate' => $startDate, |
196
|
|
|
'endDate' => $endDate, |
197
|
|
|
'customSearch' => $customSearch, |
198
|
|
|
'indexTypes' => $this->indexTypes, |
199
|
|
|
'emptyPreResult' => false, |
200
|
|
|
]; |
201
|
|
|
$arguments = $this->callSignal(__CLASS__, __FUNCTION__ . 'Pre', $arguments); |
202
|
|
|
|
203
|
|
|
$query = $this->createQuery(); |
204
|
|
|
$constraints = $this->getDefaultConstraints($query); |
205
|
|
|
|
206
|
|
|
if ($limit > 0) { |
207
|
|
|
$query->setLimit($limit); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$this->addTimeFrameConstraints( |
211
|
|
|
$constraints, |
212
|
|
|
$query, |
213
|
|
|
$arguments['startDate'], |
214
|
|
|
$arguments['endDate'] |
215
|
|
|
); |
216
|
|
|
|
217
|
|
|
if ($arguments['indexIds']) { |
218
|
|
|
$indexIds = []; |
219
|
|
|
$tabledIndexIds = []; |
220
|
|
|
foreach ($arguments['indexIds'] as $key => $indexId) { |
221
|
|
|
if (\is_int($key)) { |
222
|
|
|
// Plain integers (= deprecated old way, stays in for compatibility) |
223
|
|
|
$indexIds[] = $indexId; |
224
|
|
|
} elseif (\is_string($key) && \is_array($indexId)) { |
225
|
|
|
// Table based values with array of foreign uids |
226
|
|
|
$tabledIndexIds[] = [ |
227
|
|
|
'table' => $key, |
228
|
|
|
'indexIds' => $indexId, |
229
|
|
|
]; |
230
|
|
|
} elseif (\is_string($key) && \is_int($indexId)) { |
231
|
|
|
// Table based single return value |
232
|
|
|
$tabledIndexIds[] = [ |
233
|
|
|
'table' => $key, |
234
|
|
|
'indexIds' => [$indexId], |
235
|
|
|
]; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
$foreignIdConstraints = []; |
239
|
|
|
// Old way, just accept foreignUids as provided, not checking the table. |
240
|
|
|
// This has a caveat solved with the $tabledIndexIds |
241
|
|
|
if ($indexIds) { |
|
|
|
|
242
|
|
|
$foreignIdConstraints[] = $query->in('foreignUid', $indexIds); |
243
|
|
|
} |
244
|
|
|
if ($tabledIndexIds) { |
|
|
|
|
245
|
|
|
// Handle each table individually on the filters |
246
|
|
|
// allowing for uids to be table specific. |
247
|
|
|
// If 1,3,5 on table_a are ok and 4,5,7 on table_b are ok, |
248
|
|
|
// don't show uid 1 from table_b |
249
|
|
|
foreach ($tabledIndexIds as $tabledIndexId) { |
250
|
|
|
if ($tabledIndexId['indexIds']) { |
251
|
|
|
// This table has used filters and returned some allowed uids. |
252
|
|
|
// Providing non-existing values e.g.: -1 will remove everything |
253
|
|
|
// unless other elements have found elements with the filters |
254
|
|
|
$foreignIdConstraints[] = $query->logicalAnd([ |
255
|
|
|
$query->equals('foreignTable', $tabledIndexId['table']), |
256
|
|
|
$query->in('foreignUid', $tabledIndexId['indexIds']), |
257
|
|
|
]); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
if (\count($foreignIdConstraints) > 1) { |
262
|
|
|
// Multiple valid tables should be grouped by "OR" |
263
|
|
|
// so it's either table_a with uids 1,3,4 OR table_b with uids 1,5,7 |
264
|
|
|
$foreignIdConstraint = $query->logicalOr($foreignIdConstraints); |
265
|
|
|
} else { |
266
|
|
|
// Single constraint or no constraint should just be simply added |
267
|
|
|
$foreignIdConstraint = array_shift($foreignIdConstraints); |
268
|
|
|
} |
269
|
|
|
// If any foreignUid constraint survived, use it on the query |
270
|
|
|
if ($foreignIdConstraint) { |
271
|
|
|
$constraints[] = $foreignIdConstraint; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
if ($arguments['emptyPreResult']) { |
275
|
|
|
$constraints[] = $query->equals('uid', '-1'); |
276
|
|
|
} |
277
|
|
|
$result = [ |
278
|
|
|
'result' => $this->matchAndExecute($query, $constraints), |
279
|
|
|
]; |
280
|
|
|
|
281
|
|
|
$result = $this->callSignal(__CLASS__, __FUNCTION__ . 'Post', $result); |
282
|
|
|
|
283
|
|
|
return $result['result']; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Find Past Events. |
288
|
|
|
* |
289
|
|
|
* @param int $limit |
290
|
|
|
* @param string $sort |
291
|
|
|
* |
292
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
293
|
|
|
*/ |
294
|
|
|
public function findByPast( |
295
|
|
|
$limit, |
296
|
|
|
$sort |
297
|
|
|
) { |
298
|
|
|
//create Query |
299
|
|
|
$query = $this->createQuery(); |
300
|
|
|
//Get actual datetime |
301
|
|
|
$now = DateTimeUtility::getNow(); |
302
|
|
|
|
303
|
|
|
$constraints = $this->getDefaultConstraints($query); |
304
|
|
|
$constraints[] = $query->lessThanOrEqual('startDate', $now->format('Y-m-d')); |
305
|
|
|
$sort = QueryInterface::ORDER_ASCENDING === $sort ? QueryInterface::ORDER_ASCENDING : QueryInterface::ORDER_DESCENDING; |
306
|
|
|
$query->setOrderings($this->getSorting($sort)); |
307
|
|
|
$query->setLimit($limit); |
308
|
|
|
|
309
|
|
|
return $this->matchAndExecute($query, $constraints); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Find by traversing information. |
314
|
|
|
* |
315
|
|
|
* @param Index $index |
316
|
|
|
* @param bool|true $future |
317
|
|
|
* @param bool|false $past |
318
|
|
|
* @param int $limit |
319
|
|
|
* @param string $sort |
320
|
|
|
* @param bool $useIndexTime |
321
|
|
|
* |
322
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
323
|
|
|
*/ |
324
|
|
|
public function findByTraversing( |
325
|
|
|
Index $index, |
326
|
|
|
$future = true, |
327
|
|
|
$past = false, |
328
|
|
|
$limit = 100, |
329
|
|
|
$sort = QueryInterface::ORDER_ASCENDING, |
330
|
|
|
$useIndexTime = false |
331
|
|
|
) { |
332
|
|
|
if (!$future && !$past) { |
333
|
|
|
return []; |
334
|
|
|
} |
335
|
|
|
$query = $this->createQuery(); |
336
|
|
|
|
337
|
|
|
$now = DateTimeUtility::getNow(); |
338
|
|
|
if ($useIndexTime) { |
339
|
|
|
$now = $index->getStartDate(); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$constraints = []; |
343
|
|
|
$constraints[] = $query->logicalNot($query->equals('uid', $index->getUid())); |
344
|
|
|
$constraints[] = $query->equals('foreignTable', $index->getForeignTable()); |
345
|
|
|
$constraints[] = $query->equals('foreignUid', $index->getForeignUid()); |
346
|
|
|
if (!$future) { |
347
|
|
|
$constraints[] = $query->lessThanOrEqual('startDate', $now->format('Y-m-d')); |
348
|
|
|
} |
349
|
|
|
if (!$past) { |
350
|
|
|
$constraints[] = $query->greaterThanOrEqual('startDate', $now->format('Y-m-d')); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
$query->setLimit($limit); |
354
|
|
|
$sort = QueryInterface::ORDER_ASCENDING === $sort ? QueryInterface::ORDER_ASCENDING : QueryInterface::ORDER_DESCENDING; |
355
|
|
|
$query->setOrderings($this->getSorting($sort)); |
356
|
|
|
|
357
|
|
|
return $this->matchAndExecute($query, $constraints); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Find by traversing information. |
362
|
|
|
* |
363
|
|
|
* @param DomainObjectInterface $event |
364
|
|
|
* @param bool|true $future |
365
|
|
|
* @param bool|false $past |
366
|
|
|
* @param int $limit |
367
|
|
|
* @param string $sort |
368
|
|
|
* |
369
|
|
|
* @throws Exception |
370
|
|
|
* |
371
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
372
|
|
|
*/ |
373
|
|
|
public function findByEventTraversing( |
374
|
|
|
DomainObjectInterface $event, |
375
|
|
|
$future = true, |
376
|
|
|
$past = false, |
377
|
|
|
$limit = 100, |
378
|
|
|
$sort = QueryInterface::ORDER_ASCENDING |
379
|
|
|
) { |
380
|
|
|
if (!$future && !$past) { |
381
|
|
|
return []; |
382
|
|
|
} |
383
|
|
|
$query = $this->createQuery(); |
384
|
|
|
|
385
|
|
|
$uniqueRegisterKey = ExtensionConfigurationUtility::getUniqueRegisterKeyForModel($event); |
386
|
|
|
|
387
|
|
|
$this->setIndexTypes([$uniqueRegisterKey]); |
388
|
|
|
|
389
|
|
|
$now = DateTimeUtility::getNow()->format('Y-m-d'); |
390
|
|
|
|
391
|
|
|
$constraints = []; |
392
|
|
|
|
393
|
|
|
$localizedUid = $event->_getProperty('_localizedUid'); |
394
|
|
|
$selectUid = $localizedUid ? $localizedUid : $event->getUid(); |
395
|
|
|
|
396
|
|
|
$constraints[] = $query->equals('foreignUid', $selectUid); |
397
|
|
|
$constraints[] = $query->in('uniqueRegisterKey', $this->indexTypes); |
398
|
|
|
if (!$future) { |
399
|
|
|
$constraints[] = $query->lessThanOrEqual('startDate', $now); |
400
|
|
|
} |
401
|
|
|
if (!$past) { |
402
|
|
|
$constraints[] = $query->greaterThanOrEqual('startDate', $now); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
$query->setLimit($limit); |
406
|
|
|
$sort = QueryInterface::ORDER_ASCENDING === $sort ? QueryInterface::ORDER_ASCENDING : QueryInterface::ORDER_DESCENDING; |
407
|
|
|
$query->setOrderings($this->getSorting($sort)); |
408
|
|
|
|
409
|
|
|
return $this->matchAndExecute($query, $constraints); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* find Year. |
414
|
|
|
* |
415
|
|
|
* @param int $year |
416
|
|
|
* |
417
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
418
|
|
|
*/ |
419
|
|
|
public function findYear(int $year) |
420
|
|
|
{ |
421
|
|
|
$startTime = (new \DateTimeImmutable('midnight'))->setDate($year, 1, 1); |
422
|
|
|
$endTime = $startTime->modify('+1 year -1 second'); |
423
|
|
|
|
424
|
|
|
return $this->findByTimeSlot($startTime, $endTime); |
|
|
|
|
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* find quarter. |
429
|
|
|
* |
430
|
|
|
* @param int $year |
431
|
|
|
* @param int $quarter |
432
|
|
|
* |
433
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
434
|
|
|
*/ |
435
|
|
|
public function findQuarter(int $year, int $quarter) |
436
|
|
|
{ |
437
|
|
|
$startMonth = 1 + (3 * ($quarter - 1)); |
438
|
|
|
$startTime = (new \DateTimeImmutable('midnight'))->setDate($year, $startMonth, 1); |
439
|
|
|
$endTime = $startTime->modify('+3 months -1 second'); |
440
|
|
|
|
441
|
|
|
return $this->findByTimeSlot($startTime, $endTime); |
|
|
|
|
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* find Month. |
446
|
|
|
* |
447
|
|
|
* @param int $year |
448
|
|
|
* @param int $month |
449
|
|
|
* |
450
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
451
|
|
|
*/ |
452
|
|
|
public function findMonth(int $year, int $month) |
453
|
|
|
{ |
454
|
|
|
$startTime = (new \DateTimeImmutable('midnight'))->setDate($year, $month, 1); |
455
|
|
|
$endTime = $startTime->modify('+1 month -1 second'); |
456
|
|
|
|
457
|
|
|
return $this->findByTimeSlot($startTime, $endTime); |
|
|
|
|
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* find Week. |
462
|
|
|
* |
463
|
|
|
* @param int $year |
464
|
|
|
* @param int $week |
465
|
|
|
* @param int $weekStart See documentation for settings.weekStart |
466
|
|
|
* |
467
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
468
|
|
|
*/ |
469
|
|
|
public function findWeek(int $year, int $week, int $weekStart = 1) |
470
|
|
|
{ |
471
|
|
|
$startTime = \DateTimeImmutable::createFromMutable(DateTimeUtility::convertWeekYear2DayMonthYear($week, $year, $weekStart)); |
472
|
|
|
$endTime = $startTime->modify('+1 week -1 second'); |
473
|
|
|
|
474
|
|
|
return $this->findByTimeSlot($startTime, $endTime); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* find day. |
479
|
|
|
* |
480
|
|
|
* @param int $year |
481
|
|
|
* @param int $month |
482
|
|
|
* @param int $day |
483
|
|
|
* |
484
|
|
|
* @throws Exception |
485
|
|
|
* |
486
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
487
|
|
|
*/ |
488
|
|
|
public function findDay(int $year, int $month, int $day) |
489
|
|
|
{ |
490
|
|
|
$startTime = (new \DateTimeImmutable('midnight'))->setDate($year, $month, $day); |
491
|
|
|
$endTime = $startTime->modify('+1 day -1 second'); |
492
|
|
|
|
493
|
|
|
return $this->findByTimeSlot($startTime, $endTime); |
|
|
|
|
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Find different types and locations. |
498
|
|
|
* |
499
|
|
|
* @return array |
500
|
|
|
*/ |
501
|
|
|
public function findDifferentTypesAndLocations(): array |
502
|
|
|
{ |
503
|
|
|
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_calendarize_domain_model_index'); |
504
|
|
|
|
505
|
|
|
return (array)$queryBuilder->select('unique_register_key', 'pid', 'foreign_table')->from('tx_calendarize_domain_model_index')->groupBy('pid', 'foreign_table', 'unique_register_key')->execute()->fetchAll(); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* Set the default sorting direction. |
510
|
|
|
* |
511
|
|
|
* @param string $direction |
512
|
|
|
* @param string $field |
513
|
|
|
*/ |
514
|
|
|
public function setDefaultSortingDirection($direction, $field = '') |
515
|
|
|
{ |
516
|
|
|
$this->defaultOrderings = $this->getSorting($direction, $field); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Find by time slot. |
521
|
|
|
* |
522
|
|
|
* @param \DateTimeInterface|null $startTime |
523
|
|
|
* @param \DateTimeInterface|null $endTime |
524
|
|
|
* |
525
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
526
|
|
|
*/ |
527
|
|
|
public function findByTimeSlot(?\DateTimeInterface $startTime, ?\DateTimeInterface $endTime = null) |
528
|
|
|
{ |
529
|
|
|
$query = $this->createQuery(); |
530
|
|
|
$constraints = $this->getDefaultConstraints($query); |
531
|
|
|
$this->addTimeFrameConstraints($constraints, $query, $startTime, $endTime); |
532
|
|
|
|
533
|
|
|
$arguments = [ |
534
|
|
|
'constraints' => $constraints, |
535
|
|
|
'query' => $query, |
536
|
|
|
]; |
537
|
|
|
$arguments = $this->callSignal(__CLASS__, __FUNCTION__, $arguments); |
538
|
|
|
$constraints = $arguments['constraints'] ?: $constraints; |
539
|
|
|
|
540
|
|
|
return $this->matchAndExecute($query, $constraints); |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
/** |
544
|
|
|
* Find all indices by the given Event model. |
545
|
|
|
* |
546
|
|
|
* @param DomainObjectInterface $event |
547
|
|
|
* |
548
|
|
|
* @throws Exception |
549
|
|
|
* |
550
|
|
|
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface |
551
|
|
|
*/ |
552
|
|
|
public function findByEvent(DomainObjectInterface $event) |
553
|
|
|
{ |
554
|
|
|
$query = $this->createQuery(); |
555
|
|
|
|
556
|
|
|
$uniqueRegisterKey = ExtensionConfigurationUtility::getUniqueRegisterKeyForModel($event); |
557
|
|
|
|
558
|
|
|
$this->setIndexTypes([$uniqueRegisterKey]); |
559
|
|
|
$constraints = $this->getDefaultConstraints($query); |
560
|
|
|
$constraints[] = $query->equals('foreignUid', $event->getUid()); |
561
|
|
|
$query->matching($query->logicalAnd($constraints)); |
562
|
|
|
|
563
|
|
|
return $query->execute(); |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* storage page selection. |
568
|
|
|
* |
569
|
|
|
* @return array |
570
|
|
|
*/ |
571
|
|
|
protected function getStoragePageIds() |
572
|
|
|
{ |
573
|
|
|
if (null !== $this->overridePageIds) { |
574
|
|
|
return $this->overridePageIds; |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
$configurationManager = $this->objectManager->get(ConfigurationManagerInterface::class); |
|
|
|
|
578
|
|
|
$frameworkConfig = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); |
579
|
|
|
$storagePages = isset($frameworkConfig['persistence']['storagePid']) ? GeneralUtility::intExplode( |
580
|
|
|
',', |
581
|
|
|
$frameworkConfig['persistence']['storagePid'] |
582
|
|
|
) : []; |
583
|
|
|
if (!empty($storagePages)) { |
584
|
|
|
return $storagePages; |
585
|
|
|
} |
586
|
|
|
if ($frameworkConfig instanceof BackendConfigurationManager) { |
587
|
|
|
return GeneralUtility::trimExplode(',', $frameworkConfig->getDefaultBackendStoragePid(), true); |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
return $storagePages; |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* Get the default constraint for the queries. |
595
|
|
|
* |
596
|
|
|
* @param QueryInterface $query |
597
|
|
|
* |
598
|
|
|
* @return array |
599
|
|
|
*/ |
600
|
|
|
protected function getDefaultConstraints(QueryInterface $query) |
601
|
|
|
{ |
602
|
|
|
$constraints = []; |
603
|
|
|
if (!empty($this->indexTypes)) { |
604
|
|
|
$indexTypes = $this->indexTypes; |
605
|
|
|
$constraints[] = $query->in('uniqueRegisterKey', $indexTypes); |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
$storagePages = $this->getStoragePageIds(); |
609
|
|
|
if (!empty($storagePages)) { |
610
|
|
|
$constraints[] = $query->in('pid', $storagePages); |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
$arguments = [ |
614
|
|
|
'indexIds' => [], |
615
|
|
|
'indexTypes' => $this->indexTypes, |
616
|
|
|
]; |
617
|
|
|
$arguments = $this->callSignal(__CLASS__, __FUNCTION__, $arguments); |
618
|
|
|
|
619
|
|
|
if ($arguments['indexIds']) { |
620
|
|
|
$constraints[] = $query->in('foreignUid', $arguments['indexIds']); |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
return $constraints; |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
/** |
627
|
|
|
* Add time frame related queries. |
628
|
|
|
* |
629
|
|
|
* @param array $constraints |
630
|
|
|
* @param QueryInterface $query |
631
|
|
|
* @param \DateTimeInterface|null $startTime |
632
|
|
|
* @param \DateTimeInterface|null $endTime |
633
|
|
|
* |
634
|
|
|
* @see IndexUtility::isIndexInRange |
635
|
|
|
*/ |
636
|
|
|
protected function addTimeFrameConstraints( |
637
|
|
|
array &$constraints, |
638
|
|
|
QueryInterface $query, |
639
|
|
|
?\DateTimeInterface $startTime = null, |
640
|
|
|
?\DateTimeInterface $endTime = null |
641
|
|
|
): void { |
642
|
|
|
/** @var AddTimeFrameConstraintsEvent $event */ |
643
|
|
|
$event = $this->eventDispatcher->dispatch(new AddTimeFrameConstraintsEvent( |
|
|
|
|
644
|
|
|
$constraints, |
645
|
|
|
$query, |
646
|
|
|
$this->additionalSlotArguments, |
647
|
|
|
$startTime, |
648
|
|
|
$endTime |
649
|
|
|
)); |
650
|
|
|
|
651
|
|
|
$this->addDateTimeFrameConstraint( |
652
|
|
|
$constraints, |
653
|
|
|
$query, |
654
|
|
|
$event->getStart(), |
655
|
|
|
$event->getEnd(), |
656
|
|
|
(bool)ConfigurationUtility::get('respectTimesInTimeFrameConstraints') |
657
|
|
|
); |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* Adds time frame constraints. The dates are formatted the timezone of the DateTime objects. |
662
|
|
|
* This includes all events, that have an "active" part in the range. |
663
|
|
|
* Do not call this method directly. Call IndexRepository::addTimeFrameConstraints instead. |
664
|
|
|
* |
665
|
|
|
* @param array $constraints |
666
|
|
|
* @param QueryInterface $query |
667
|
|
|
* @param \DateTimeInterface|null $start |
668
|
|
|
* @param \DateTimeInterface|null $end Inclusive end date |
669
|
|
|
* @param bool $respectTime if true, it will also respect the time of the indices |
670
|
|
|
* |
671
|
|
|
* @see IndexRepository::addTimeFrameConstraints |
672
|
|
|
*/ |
673
|
|
|
protected function addDateTimeFrameConstraint( |
674
|
|
|
array &$constraints, |
675
|
|
|
QueryInterface $query, |
676
|
|
|
?\DateTimeInterface $start, |
677
|
|
|
?\DateTimeInterface $end, |
678
|
|
|
bool $respectTime = false |
679
|
|
|
): void { |
680
|
|
|
if (null === $start && null === $end) { |
681
|
|
|
return; |
682
|
|
|
} |
683
|
|
|
$dateConstraints = []; |
684
|
|
|
|
685
|
|
|
// No start means open end |
686
|
|
|
if (null !== $start) { |
687
|
|
|
$startDate = $start->format('Y-m-d'); |
688
|
|
|
|
689
|
|
|
if (false === $respectTime) { |
690
|
|
|
// The endDate of an index must be after the range start, otherwise the event was in the past |
691
|
|
|
$dateConstraints[] = $query->greaterThanOrEqual('endDate', $startDate); |
692
|
|
|
} else { |
693
|
|
|
$startTime = DateTimeUtility::getDaySecondsOfDateTime($start); |
694
|
|
|
|
695
|
|
|
// We split up greaterThan and equal to check more conditions on the same day |
696
|
|
|
// e.g. if it is either allDay, openEnd or the end time is after the start time |
697
|
|
|
// (endDate > $startDate) || (endDate == $startDate && (allDay || openEndTime || endTime >= $startTime)) |
698
|
|
|
$dateConstraints[] = $query->logicalOr([ |
699
|
|
|
$query->greaterThan('endDate', $startDate), |
700
|
|
|
$query->logicalAnd([ |
701
|
|
|
$query->equals('endDate', $startDate), |
702
|
|
|
$query->logicalOr([ |
703
|
|
|
$query->equals('allDay', true), |
704
|
|
|
$query->equals('openEndTime', true), |
705
|
|
|
$query->greaterThanOrEqual('endTime', $startTime), |
706
|
|
|
]), |
707
|
|
|
]), |
708
|
|
|
]); |
709
|
|
|
} |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
// No end means open start |
713
|
|
|
if (null !== $end) { |
714
|
|
|
$endDate = $end->format('Y-m-d'); |
715
|
|
|
|
716
|
|
|
if (false === $respectTime) { |
717
|
|
|
// The startDate of an index must be before the range end, otherwise the event is in the future |
718
|
|
|
$dateConstraints[] = $query->lessThanOrEqual('startDate', $endDate); |
719
|
|
|
} else { |
720
|
|
|
$endTime = DateTimeUtility::getDaySecondsOfDateTime($end); |
721
|
|
|
|
722
|
|
|
$dateConstraints[] = $query->logicalOr([ |
723
|
|
|
$query->lessThan('startDate', $endDate), |
724
|
|
|
$query->logicalAnd([ |
725
|
|
|
$query->equals('startDate', $endDate), |
726
|
|
|
$query->logicalOr([ |
727
|
|
|
$query->equals('allDay', true), |
728
|
|
|
$query->lessThanOrEqual('startTime', $endTime), |
729
|
|
|
]), |
730
|
|
|
]), |
731
|
|
|
]); |
732
|
|
|
} |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
$constraints['dateTimeFrame'] = $query->logicalAnd($dateConstraints); |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
/** |
739
|
|
|
* Get the sorting. |
740
|
|
|
* |
741
|
|
|
* @param string $direction |
742
|
|
|
* @param string $field |
743
|
|
|
* |
744
|
|
|
* @return array |
745
|
|
|
*/ |
746
|
|
|
protected function getSorting($direction, $field = '') |
747
|
|
|
{ |
748
|
|
|
if ('withrangelast' === $field) { |
749
|
|
|
return [ |
750
|
|
|
'endDate' => $direction, |
751
|
|
|
'startDate' => $direction, |
752
|
|
|
'startTime' => $direction, |
753
|
|
|
]; |
754
|
|
|
} |
755
|
|
|
if ('end' !== $field) { |
756
|
|
|
$field = 'start'; |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
return [ |
760
|
|
|
$field . 'Date' => $direction, |
761
|
|
|
$field . 'Time' => $direction, |
762
|
|
|
]; |
763
|
|
|
} |
764
|
|
|
} |
765
|
|
|
|