Completed
Push — development ( 2c7293...7a0416 )
by Torben
02:37
created

RegistrationRepository::findByUid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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