Completed
Push — master ( d3022c...70dfe1 )
by Torben
07:18 queued 01:45
created

RegistrationRepository::findByUid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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