-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEventRepository.php
More file actions
122 lines (99 loc) · 3.57 KB
/
EventRepository.php
File metadata and controls
122 lines (99 loc) · 3.57 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
<?php
declare(strict_types=1);
namespace Extcode\CartEvents\Domain\Repository;
/*
* This file is part of the package extcode/cart-events.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/
use Extcode\CartEvents\Domain\Model\Dto\EventDemand;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;
class EventRepository extends Repository
{
public function findDemanded(EventDemand $demand): QueryResultInterface
{
$query = $this->createQuery();
$constraints = [];
if ($demand->getSku()) {
$constraints[] = $query->equals('sku', $demand->getSku());
}
if ($demand->getTitle()) {
$constraints[] = $query->like('title', '%' . $demand->getTitle() . '%');
}
if (!empty($demand->getCategories())) {
$categoryConstraints = [];
foreach ($demand->getCategories() as $category) {
$categoryConstraints[] = $query->equals('category', $category);
$categoryConstraints[] = $query->contains('categories', $category);
}
$constraints[] = $query->logicalOr(...array_values($categoryConstraints));
}
if (!empty($constraints)) {
$query->matching(
$query->logicalAnd(...array_values($constraints))
);
}
if ($orderings = $this->createOrderingsFromDemand($demand)) {
$query->setOrderings($orderings);
}
if ($limit = $demand->getLimit()) {
$query->setLimit($limit);
}
return $query->execute();
}
/**
* Find all events based on selected uids
*/
public function findByUids(string $uids, ?int $limit = null): array
{
$uids = explode(',', $uids);
$query = $this->createQuery();
$query->getQuerySettings()->setRespectStoragePage(false);
$query->matching(
$query->in('uid', $uids)
);
if ($limit) {
$query->setLimit($limit);
}
return $this->orderByField($query->execute(), $uids);
}
protected function createOrderingsFromDemand(EventDemand $demand): array
{
$orderings = [];
$orderList = GeneralUtility::trimExplode(',', $demand->getOrder(), true);
if (!empty($orderList)) {
foreach ($orderList as $orderItem) {
[$orderField, $ascDesc]
= GeneralUtility::trimExplode(' ', $orderItem, true);
if ($ascDesc) {
$orderings[$orderField] = ((strtolower($ascDesc) === 'desc')
? QueryInterface::ORDER_DESCENDING
: QueryInterface::ORDER_ASCENDING);
} else {
$orderings[$orderField] = QueryInterface::ORDER_ASCENDING;
}
}
}
return $orderings;
}
protected function orderByField(QueryResultInterface $events, array $uids): array
{
$indexedEvents = [];
$orderedEvents = [];
// Create an associative array
foreach ($events as $object) {
$indexedEvents[$object->getUid()] = $object;
}
// add to ordered array in right order
foreach ($uids as $uid) {
if (isset($indexedEvents[$uid])) {
$orderedEvents[] = $indexedEvents[$uid];
}
}
return $orderedEvents;
}
}