Completed
Push — master ( ec29fe...2e5711 )
by Torben
09:02 queued 07:11
created

RegistrationRepository::setDisplayModeConstraint()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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