Completed
Push — master ( 2688b6...22e1bb )
by Torben
141:46 queued 139:04
created

EventRepository::initializeObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
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\MathUtility;
18
use \TYPO3\CMS\Core\Utility\GeneralUtility;
19
use \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand;
20
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
21
22
/**
23
 * The repository for Events
24
 *
25
 * @author Torben Hansen <[email protected]>
26
 */
27
class EventRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
28
{
29
30
    /**
31
     * Set default sorting
32
     *
33
     * @var array
34
     */
35
    protected $defaultOrderings = [
36
        'startdate' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
37
    ];
38
39
    /**
40
     * Disable the use of storage records, because the StoragePage can be set
41
     * in the plugin
42
     *
43
     * @return void
44
     */
45
    public function initializeObject()
46
    {
47
        $this->defaultQuerySettings = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
48
        $this->defaultQuerySettings->setRespectStoragePage(false);
49
    }
50
51
    /**
52
     * Returns the objects of this repository matching the given demand
53
     *
54
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
55
     *
56
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface QueryResultInterface
57
     */
58
    public function findDemanded(EventDemand $eventDemand)
59
    {
60
        $constraints = [];
61
        $query = $this->createQuery();
62
        $this->setStoragePageConstraint($query, $eventDemand, $constraints);
63
        $this->setDisplayModeConstraint($query, $eventDemand, $constraints);
64
        $this->setCategoryConstraint($query, $eventDemand, $constraints);
65
        $this->setLocationConstraint($query, $eventDemand, $constraints);
66
        $this->setLocationCityConstraint($query, $eventDemand, $constraints);
67
        $this->setLocationCountryConstraint($query, $eventDemand, $constraints);
68
        $this->setStartEndDateConstraint($query, $eventDemand, $constraints);
69
        $this->setSearchConstraint($query, $eventDemand, $constraints);
70
        $this->setTopEventConstraint($query, $eventDemand, $constraints);
71
        $this->setOrderingsFromDemand($query, $eventDemand);
72
73
        if (count($constraints) > 0) {
74
            $query->matching($query->logicalAnd($constraints));
75
        }
76
77
        $this->setQueryLimitFromDemand($query, $eventDemand);
78
        return $query->execute();
79
    }
80
81
    /**
82
     * Sets a query limit to the given query for the given demand
83
     *
84
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
85
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
86
     *
87
     * @return void
88
     */
89
    protected function setQueryLimitFromDemand($query, EventDemand $eventDemand)
90
    {
91
        if ($eventDemand->getQueryLimit() != null &&
92
            MathUtility::canBeInterpretedAsInteger($eventDemand->getQueryLimit()) &&
93
            (int)$eventDemand->getQueryLimit() > 0
94
        ) {
95
            $query->setLimit((int)$eventDemand->getQueryLimit());
96
        }
97
    }
98
99
    /**
100
     * Sets the ordering to the given query for the given demand
101
     *
102
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
103
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
104
     *
105
     * @return void
106
     */
107
    protected function setOrderingsFromDemand($query, EventDemand $eventDemand)
108
    {
109
        $orderings = [];
110
        if ($eventDemand->getOrderField() != '' && $eventDemand->getOrderDirection() != '') {
111
            $orderings[$eventDemand->getOrderField()] = ((strtolower($eventDemand->getOrderDirection()) == 'desc') ?
112
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING :
113
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
114
            $query->setOrderings($orderings);
115
        }
116
    }
117
118
    /**
119
     * Sets the storagePage constraint to the given constraints array
120
     *
121
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
122
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
123
     * @param array $constraints Constraints
124
     *
125
     * @return void
126
     */
127
    protected function setStoragePageConstraint($query, $eventDemand, &$constraints)
128
    {
129
        if ($eventDemand->getStoragePage() != '') {
130
            $pidList = GeneralUtility::intExplode(',', $eventDemand->getStoragePage(), true);
131
            $constraints[] = $query->in('pid', $pidList);
132
        }
133
    }
134
135
    /**
136
     * Sets the displayMode constraint to the given constraints array
137
     *
138
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
139
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
140
     * @param array $constraints Constraints
141
     *
142
     * @return void
143
     */
144
    protected function setDisplayModeConstraint($query, $eventDemand, &$constraints)
145
    {
146
        switch ($eventDemand->getDisplayMode()) {
147
            case 'future':
148
                $constraints[] = $query->greaterThan('startdate', $eventDemand->getCurrentDateTime());
149
                break;
150
            case 'past':
151
                $constraints[] = $query->lessThanOrEqual('enddate', $eventDemand->getCurrentDateTime());
152
                break;
153
            default:
154
        }
155
    }
156
157
    /**
158
     * Sets the category constraint to the given constraints array
159
     *
160
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
161
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
162
     * @param array $constraints Constraints
163
     *
164
     * @return void
165
     */
166
    protected function setCategoryConstraint($query, $eventDemand, &$constraints)
167
    {
168
        if ($eventDemand->getCategory() != '') {
169
            $categoryConstraints = [];
170
            $categories = GeneralUtility::intExplode(',', $eventDemand->getCategory(), true);
171
            foreach ($categories as $category) {
172
                $categoryConstraints[] = $query->contains('category', $category);
173
            }
174
            if (count($categoryConstraints) > 0) {
175
                $constraints[] = $query->logicalOr($categoryConstraints);
176
            }
177
        }
178
    }
179
180
    /**
181
     * Sets the location constraint to the given constraints array
182
     *
183
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
184
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
185
     * @param array $constraints Constraints
186
     *
187
     * @return void
188
     */
189
    protected function setLocationConstraint($query, $eventDemand, &$constraints)
190
    {
191
        if ($eventDemand->getLocation() !== null && $eventDemand->getLocation() != '') {
192
            $constraints[] = $query->equals('location', $eventDemand->getLocation());
193
        }
194
    }
195
196
    /**
197
     * Sets the location.city constraint to the given constraints array
198
     *
199
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
200
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
201
     * @param array $constraints Constraints
202
     *
203
     * @return void
204
     */
205
    protected function setLocationCityConstraint($query, $eventDemand, &$constraints)
206
    {
207
        if ($eventDemand->getLocationCity() !== null && $eventDemand->getLocationCity() != '') {
208
            $constraints[] = $query->equals('location.city', $eventDemand->getLocationCity());
209
        }
210
    }
211
212
    /**
213
     * Sets the location.country constraint to the given constraints array
214
     *
215
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
216
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
217
     * @param array $constraints Constraints
218
     *
219
     * @return void
220
     */
221
    protected function setLocationCountryConstraint($query, $eventDemand, &$constraints)
222
    {
223
        if ($eventDemand->getLocationCountry() !== null && $eventDemand->getLocationCountry() != '') {
224
            $constraints[] = $query->equals('location.country', $eventDemand->getLocationCountry());
225
        }
226
    }
227
228
    /**
229
     * Sets the start- and enddate constraint to the given constraints array
230
     *
231
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
232
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
233
     * @param array $constraints Constraints
234
     *
235
     * @return void
236
     */
237
    protected function setStartEndDateConstraint($query, $eventDemand, &$constraints)
238
    {
239
        /* StartDate */
240
        if ($eventDemand->getSearchDemand() && $eventDemand->getSearchDemand()->getStartDate() !== null) {
241
            $constraints[] = $query->greaterThanOrEqual('startdate', $eventDemand->getSearchDemand()->getStartDate());
242
        }
243
244
        /* EndDate */
245
        if ($eventDemand->getSearchDemand() && $eventDemand->getSearchDemand()->getEndDate() !== null) {
246
            $constraints[] = $query->lessThanOrEqual('enddate', $eventDemand->getSearchDemand()->getEndDate());
247
        }
248
    }
249
250
    /**
251
     * Sets the search constraint to the given constraints array
252
     *
253
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
254
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
255
     * @param array $constraints Constraints
256
     *
257
     * @return void
258
     */
259
    protected function setSearchConstraint($query, $eventDemand, &$constraints)
260
    {
261
        if ($eventDemand->getSearchDemand() &&
262
            $eventDemand->getSearchDemand()->getSearch() !== null &&
263
            $eventDemand->getSearchDemand()->getSearch() !== ''
264
        ) {
265
            $searchFields = GeneralUtility::trimExplode(',', $eventDemand->getSearchDemand()->getFields(), true);
266
            $searchConstraints = [];
267
268
            if (count($searchFields) === 0) {
269
                throw new \UnexpectedValueException('No search fields defined', 1318497755);
270
            }
271
272
            $searchSubject = $eventDemand->getSearchDemand()->getSearch();
273
            foreach ($searchFields as $field) {
274
                if (!empty($searchSubject)) {
275
                    $searchConstraints[] = $query->like($field, '%' . $searchSubject . '%', false);
276
                }
277
            }
278
279
            if (count($searchConstraints)) {
280
                $constraints[] = $query->logicalOr($searchConstraints);
281
            }
282
        }
283
    }
284
285
    /**
286
     * Sets the topEvent constraint to the given constraints array
287
     *
288
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
289
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
290
     * @param array $constraints Constraints
291
     *
292
     * @return void
293
     */
294
    protected function setTopEventConstraint($query, $eventDemand, &$constraints)
295
    {
296
        if ($eventDemand->getTopEventRestriction() > 0) {
297
            $constraints[] = $query->equals('topEvent', (bool)($eventDemand->getTopEventRestriction() - 1));
298
        }
299
    }
300
301
}