Completed
Push — task/630-registration ( 4ac8a7 )
by Torben
04:27
created

RegistrationRepository::findByEventAndWaitlist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\Event;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
14
15
/**
16
 * The repository for registrations
17
 *
18
 * @author Torben Hansen <[email protected]>
19
 */
20
class RegistrationRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
21
{
22
    /**
23
     * Disable the use of storage records, because the StoragePage can be set
24
     * in the plugin
25
     *
26
     * @return void
27
     */
28
    public function initializeObject()
29
    {
30
        $this->defaultQuerySettings = $this->objectManager->get(Typo3QuerySettings::class);
31
        $this->defaultQuerySettings->setRespectStoragePage(false);
32
        $this->defaultQuerySettings->setRespectSysLanguage(false);
33
        $this->defaultQuerySettings->setLanguageOverlayMode(false);
34
    }
35
36
    /**
37
     * Returns all registrations, where the confirmation date is less than the
38
     * given date
39
     *
40
     * @param \Datetime $dateNow Date
41
     *
42
     * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface|array
43
     */
44
    public function findExpiredRegistrations($dateNow)
45
    {
46
        $constraints = [];
47
        $query = $this->createQuery();
48
        $constraints[] = $query->lessThanOrEqual('confirmationUntil', $dateNow);
49
        $constraints[] = $query->equals('confirmed', false);
50
51
        return $query->matching($query->logicalAnd($constraints))->execute();
52
    }
53
54
    /**
55
     * Returns all registrations for the given event with the given constraints
56
     * Constraints are combined with a logical AND
57
     *
58
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event
59
     * @param array $findConstraints FindConstraints
60
     *
61
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
62
     */
63
    public function findNotificationRegistrations($event, $findConstraints)
64
    {
65
        $constraints = [];
66
        $query = $this->createQuery();
67
        $constraints[] = $query->equals('event', $event);
68
        $constraints[] = $query->equals('ignoreNotifications', false);
69
70
        if (!is_array($findConstraints) || count($findConstraints) == 0) {
71
            return $query->matching($query->logicalAnd($constraints))->execute();
72
        }
73
74
        foreach ($findConstraints as $findConstraint => $value) {
75
            $condition = key($value);
76
            switch ($condition) {
77
                case 'equals':
78
                    $constraints[] = $query->equals($findConstraint, $value[$condition]);
79
                    break;
80
                case 'lessThan':
81
                    $constraints[] = $query->lessThan($findConstraint, $value[$condition]);
82
                    break;
83
                case 'lessThanOrEqual':
84
                    $constraints[] = $query->lessThanOrEqual($findConstraint, $value[$condition]);
85
                    break;
86
                case 'greaterThan':
87
                    $constraints[] = $query->greaterThan($findConstraint, $value[$condition]);
88
                    break;
89
                case 'greaterThanOrEqual':
90
                    $constraints[] = $query->greaterThanOrEqual($findConstraint, $value[$condition]);
91
                    break;
92
                default:
93
                    throw new \InvalidArgumentException('An error occured - Unknown condition: ' . $condition);
94
            }
95
        }
96
97
        return $query->matching($query->logicalAnd($constraints))->execute();
98
    }
99
100
    /**
101
     * Returns registrations for the given UserRegistrationDemand demand
102
     *
103
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
104
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
105
     */
106
    public function findRegistrationsByUserRegistrationDemand($demand)
107
    {
108
        if (!$demand->getUser()) {
109
            return [];
110
        }
111
        $constraints = [];
112
        $query = $this->createQuery();
113
        $this->setStoragePageConstraint($query, $demand, $constraints);
114
        $this->setDisplayModeConstraint($query, $demand, $constraints);
115
        $this->setUserConstraint($query, $demand, $constraints);
116
        $this->setOrderingsFromDemand($query, $demand);
117
118
        return $query->matching($query->logicalAnd($constraints))->execute();
119
    }
120
121
    /**
122
     * Returns all registrations for the given event and where the waitlist flag is as given
123
     *
124
     * @param Event $event
125
     * @param bool $waitlist
126
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
127
     */
128
    public function findByEventAndWaitlist($event, $waitlist = false)
129
    {
130
        $constraints = [];
131
        $query = $this->createQuery();
132
        $query->getQuerySettings()
133
            ->setLanguageOverlayMode(false)
134
            ->setRespectStoragePage(false)
135
            ->setLanguageOverlayMode(false)
136
            ->setLanguageUid(0);
137
        $constraints[] = $query->equals('event', $event->getUid());
138
        $constraints[] = $query->equals('waitlist', $waitlist);
139
140
        return $query->matching($query->logicalAnd($constraints))->execute();
141
    }
142
143
    /**
144
     * Sets the displayMode constraint to the given constraints array
145
     *
146
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
147
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
148
     * @param array $constraints Constraints
149
     *
150
     * @return void
151
     */
152
    protected function setDisplayModeConstraint($query, $demand, &$constraints)
153
    {
154
        switch ($demand->getDisplayMode()) {
155
            case 'future':
156
                $constraints[] = $query->greaterThan('event.startdate', $demand->getCurrentDateTime());
157
                break;
158
            case 'past':
159
                $constraints[] = $query->lessThanOrEqual('event.enddate', $demand->getCurrentDateTime());
160
                break;
161
            default:
162
        }
163
    }
164
165
    /**
166
     * Sets the storagePage constraint to the given constraints array
167
     *
168
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
169
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
170
     * @param array $constraints Constraints
171
     *
172
     * @return void
173
     */
174
    protected function setStoragePageConstraint($query, $demand, &$constraints)
175
    {
176
        if ($demand->getStoragePage() != '') {
177
            $pidList = GeneralUtility::intExplode(',', $demand->getStoragePage(), true);
178
            $constraints[] = $query->in('pid', $pidList);
179
        }
180
    }
181
182
    /**
183
     * Sets the user constraint to the given constraints array
184
     *
185
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
186
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
187
     * @param array $constraints Constraints
188
     *
189
     * @return void
190
     */
191
    protected function setUserConstraint($query, $demand, &$constraints)
192
    {
193
        if ($demand->getUser()) {
194
            $constraints[] = $query->equals('feUser', $demand->getUser());
195
        }
196
    }
197
198
    /**
199
     * Sets the ordering to the given query for the given demand
200
     *
201
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
202
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
203
     *
204
     * @return void
205
     */
206
    protected function setOrderingsFromDemand($query, $demand)
207
    {
208
        $orderings = [];
209
        if ($demand->getOrderField() != '' && $demand->getOrderDirection() != '') {
210
            $orderings[$demand->getOrderField()] = ((strtolower($demand->getOrderDirection()) == 'desc') ?
211
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING :
212
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
213
            $query->setOrderings($orderings);
214
        }
215
    }
216
}
217