Completed
Push — master ( 17edcb...7372de )
by Torben
06:23
created

findWaitlistMoveUpRegistrations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 5
cp 0
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
5
 *
6
 * For the full copyright and license information, please read the
7
 * LICENSE.txt file that was distributed with this source code.
8
 */
9
10
namespace DERHANSEN\SfEventMgt\Domain\Repository;
11
12
use DERHANSEN\SfEventMgt\Domain\Model\Dto\CustomNotification;
13
use DERHANSEN\SfEventMgt\Domain\Model\Event;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
16
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
17
18
/**
19
 * The repository for registrations
20
 *
21
 * @author Torben Hansen <[email protected]>
22
 */
23
class RegistrationRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
24
{
25
    /**
26
     * Disable the use of storage records, because the StoragePage can be set
27
     * in the plugin
28
     */
29
    public function initializeObject()
30
    {
31
        $this->defaultQuerySettings = $this->objectManager->get(Typo3QuerySettings::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->objectManager->ge...o3QuerySettings::class) of type object<TYPO3\CMS\Extbase\Object\object> is incompatible with the declared type object<TYPO3\CMS\Extbase...QuerySettingsInterface> of property $defaultQuerySettings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Deprecated Code introduced by
The method TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated with message: since TYPO3 10.4, will be removed in version 12.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
32
        $this->defaultQuerySettings->setRespectStoragePage(false);
33
        $this->defaultQuerySettings->setRespectSysLanguage(false);
34
        $this->defaultQuerySettings->setLanguageOverlayMode(false);
35
    }
36
37
    /**
38
     * Override the default findByUid function
39
     *
40
     * Note: This is required in order to make Extbase return the registration object with the language
41
     *       overlay of all sub-properties overlayed correctly. If this function is not used, findByUid will
42
     *       return the registration object in the expected language, but the included event object will be
43
     *       in the default language.
44
     *
45
     *       This is no bug in Extbase, but a special behavior in sf_event_mgt, since the UID of the event in
46
     *       the default language is saved to the registration (to make event limitations work correctly)
47
     *
48
     * @param int $uid
49
     * @return object
50
     */
51
    public function findByUid($uid)
52
    {
53
        $query = $this->createQuery();
54
55
        return $query->matching($query->equals('uid', $uid))->execute()->getFirst();
56
    }
57
58
    /**
59
     * Returns all registrations, where the confirmation date is less than the
60
     * given date
61
     *
62
     * @param \Datetime $dateNow Date
63
     *
64
     * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface|array
65
     */
66
    public function findExpiredRegistrations($dateNow)
67
    {
68
        $constraints = [];
69
        $query = $this->createQuery();
70
        $constraints[] = $query->lessThanOrEqual('confirmationUntil', $dateNow);
71
        $constraints[] = $query->equals('confirmed', false);
72
73
        return $query->matching($query->logicalAnd($constraints))->execute();
74
    }
75
76
    /**
77
     * Returns all registrations for the given event with the given constraints
78
     * Constraints are combined with a logical AND
79
     *
80
     * @param Event $event Event
81
     * @param CustomNotification $customNotification
82
     * @param array $findConstraints FindConstraints
83
     *
84
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
85
     */
86
    public function findNotificationRegistrations(
87
        Event $event,
88
        CustomNotification $customNotification,
89
        array $findConstraints = []
90
    ) {
91
        $constraints = [];
92
        $query = $this->createQuery();
93
        $constraints[] = $query->equals('event', $event);
94
        $constraints[] = $query->equals('ignoreNotifications', false);
95
96
        if ($customNotification->getRecipients() === CustomNotification::RECIPIENTS_CONFIRMED) {
97
            $constraints[] = $query->equals('confirmed', true);
98
        } elseif ($customNotification->getRecipients() === CustomNotification::RECIPIENTS_UNCONFIRMED) {
99
            $constraints[] = $query->equals('confirmed', false);
100
        }
101
102
        if (!is_array($findConstraints) || count($findConstraints) == 0) {
103
            return $query->matching($query->logicalAnd($constraints))->execute();
104
        }
105
106
        foreach ($findConstraints as $findConstraint => $value) {
107
            $condition = key($value);
108
            switch ($condition) {
109
                case 'equals':
110
                    $constraints[] = $query->equals($findConstraint, $value[$condition]);
111
                    break;
112
                case 'lessThan':
113
                    $constraints[] = $query->lessThan($findConstraint, $value[$condition]);
114
                    break;
115
                case 'lessThanOrEqual':
116
                    $constraints[] = $query->lessThanOrEqual($findConstraint, $value[$condition]);
117
                    break;
118
                case 'greaterThan':
119
                    $constraints[] = $query->greaterThan($findConstraint, $value[$condition]);
120
                    break;
121
                case 'greaterThanOrEqual':
122
                    $constraints[] = $query->greaterThanOrEqual($findConstraint, $value[$condition]);
123
                    break;
124
                default:
125
                    throw new \InvalidArgumentException('An error occured - Unknown condition: ' . $condition);
126
            }
127
        }
128
129
        return $query->matching($query->logicalAnd($constraints))->execute();
130
    }
131
132
    /**
133
     * Returns registrations for the given UserRegistrationDemand demand
134
     *
135
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
136
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
137
     */
138
    public function findRegistrationsByUserRegistrationDemand($demand)
139
    {
140
        if (!$demand->getUser()) {
141
            return [];
142
        }
143
        $constraints = [];
144
        $query = $this->createQuery();
145
        $query->getQuerySettings()
146
            ->setLanguageOverlayMode(true)
147
            ->setRespectSysLanguage(true);
148
        $this->setStoragePageConstraint($query, $demand, $constraints);
149
        $this->setDisplayModeConstraint($query, $demand, $constraints);
150
        $this->setUserConstraint($query, $demand, $constraints);
151
        $this->setOrderingsFromDemand($query, $demand);
152
153
        return $query->matching($query->logicalAnd($constraints))->execute();
154
    }
155
156
    /**
157
     * Returns all registrations for the given event and where the waitlist flag is as given
158
     *
159
     * @param Event $event
160
     * @param bool $waitlist
161
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
162
     */
163
    public function findByEventAndWaitlist($event, $waitlist = false)
164
    {
165
        $constraints = [];
166
        $query = $this->createQuery();
167
        $query->getQuerySettings()
168
            ->setLanguageOverlayMode(false)
169
            ->setRespectSysLanguage(false)
170
            ->setRespectStoragePage(false);
171
        $constraints[] = $query->equals('event', $event->getUid());
172
        $constraints[] = $query->equals('waitlist', $waitlist);
173
174
        return $query->matching($query->logicalAnd($constraints))->execute();
175
    }
176
177
    /**
178
     * Returns all potential move up registrations for the given event ordered by "registration_date"
179
     *
180
     * @param Event $event
181
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
182
     */
183
    public function findWaitlistMoveUpRegistrations(Event $event)
184
    {
185
        $constraints = [];
186
        $query = $this->createQuery();
187
        $query->getQuerySettings()
188
            ->setLanguageOverlayMode(false)
189
            ->setRespectSysLanguage(false)
190
            ->setRespectStoragePage(false);
191
        $constraints[] = $query->equals('event', $event->getUid());
192
        $constraints[] = $query->equals('waitlist', true);
193
        $constraints[] = $query->equals('confirmed', true);
194
        $constraints[] = $query->greaterThan('registrationDate', 0);
195
        $query->setOrderings(['registration_date' => QueryInterface::ORDER_ASCENDING]);
196
197
        return $query->matching($query->logicalAnd($constraints))->execute();
198
    }
199
200
    /**
201
     * Sets the displayMode constraint to the given constraints array
202
     *
203
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
204
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
205
     * @param array $constraints Constraints
206
     */
207
    protected function setDisplayModeConstraint($query, $demand, &$constraints)
208
    {
209
        switch ($demand->getDisplayMode()) {
210
            case 'future':
211
                $constraints[] = $query->greaterThan('event.startdate', $demand->getCurrentDateTime());
212
                break;
213
            case 'past':
214
                $constraints[] = $query->lessThanOrEqual('event.enddate', $demand->getCurrentDateTime());
215
                break;
216
            default:
217
        }
218
    }
219
220
    /**
221
     * Sets the storagePage constraint to the given constraints array
222
     *
223
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
224
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
225
     * @param array $constraints Constraints
226
     */
227
    protected function setStoragePageConstraint($query, $demand, &$constraints)
228
    {
229
        if ($demand->getStoragePage() != '') {
230
            $pidList = GeneralUtility::intExplode(',', $demand->getStoragePage(), true);
231
            $constraints[] = $query->in('pid', $pidList);
232
        }
233
    }
234
235
    /**
236
     * Sets the user constraint to the given constraints array
237
     *
238
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
239
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
240
     * @param array $constraints Constraints
241
     */
242
    protected function setUserConstraint($query, $demand, &$constraints)
243
    {
244
        if ($demand->getUser()) {
245
            $constraints[] = $query->equals('feUser', $demand->getUser());
246
        }
247
    }
248
249
    /**
250
     * Sets the ordering to the given query for the given demand
251
     *
252
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
253
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\UserRegistrationDemand $demand
254
     */
255
    protected function setOrderingsFromDemand($query, $demand)
256
    {
257
        $orderings = [];
258
        if ($demand->getOrderField() != '' && $demand->getOrderDirection() != '') {
259
            $orderings[$demand->getOrderField()] = ((strtolower($demand->getOrderDirection()) == 'desc') ?
260
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING :
261
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
262
            $query->setOrderings($orderings);
263
        }
264
    }
265
}
266