Completed
Push — master ( c9d31b...4404d6 )
by Torben
07:34
created

EventRepository::setLocationCityConstraint()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 2
nop 3
crap 3
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 DERHANSEN\SfEventMgt\Service\CategoryService;
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 31
    public function initializeObject()
46
    {
47 31
        $this->defaultQuerySettings = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
48 31
        $this->defaultQuerySettings->setRespectStoragePage(false);
49 31
    }
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 30
    public function findDemanded(EventDemand $eventDemand)
59
    {
60 30
        $constraints = [];
61 30
        $query = $this->createQuery();
62 30
        $this->setStoragePageConstraint($query, $eventDemand, $constraints);
63 30
        $this->setDisplayModeConstraint($query, $eventDemand, $constraints);
64 30
        $this->setCategoryConstraint($query, $eventDemand, $constraints);
65 30
        $this->setLocationConstraint($query, $eventDemand, $constraints);
66 30
        $this->setLocationCityConstraint($query, $eventDemand, $constraints);
67 30
        $this->setLocationCountryConstraint($query, $eventDemand, $constraints);
68 30
        $this->setStartEndDateConstraint($query, $eventDemand, $constraints);
69 30
        $this->setSearchConstraint($query, $eventDemand, $constraints);
70 30
        $this->setTopEventConstraint($query, $eventDemand, $constraints);
71 30
        $this->setOrderingsFromDemand($query, $eventDemand);
72
73 30
        if (count($constraints) > 0) {
74 30
            $query->matching($query->logicalAnd($constraints));
75 30
        }
76
77 30
        $this->setQueryLimitFromDemand($query, $eventDemand);
78 30
        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 30
    protected function setQueryLimitFromDemand($query, EventDemand $eventDemand)
90
    {
91 30
        if ($eventDemand->getQueryLimit() != null &&
92 30
            MathUtility::canBeInterpretedAsInteger($eventDemand->getQueryLimit()) &&
93 1
            (int)$eventDemand->getQueryLimit() > 0
94 30
        ) {
95 1
            $query->setLimit((int)$eventDemand->getQueryLimit());
96 1
        }
97 30
    }
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 30
    protected function setOrderingsFromDemand($query, EventDemand $eventDemand)
108
    {
109 30
        $orderings = [];
110 30
        $orderFieldAllowed = GeneralUtility::trimExplode(',', $eventDemand->getOrderFieldAllowed(), true);
111 30
        if ($eventDemand->getOrderField() != '' && $eventDemand->getOrderDirection() != '' &&
112 30
            !empty($orderFieldAllowed) && in_array($eventDemand->getOrderField(), $orderFieldAllowed, true)) {
113
            $orderings[$eventDemand->getOrderField()] = ((strtolower($eventDemand->getOrderDirection()) == 'desc') ?
114
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING :
115
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
116
            $query->setOrderings($orderings);
117
        }
118 30
    }
119
120
    /**
121
     * Sets the storagePage constraint to the given constraints array
122
     *
123
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
124
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
125
     * @param array $constraints Constraints
126
     *
127
     * @return void
128
     */
129 30
    protected function setStoragePageConstraint($query, $eventDemand, &$constraints)
130
    {
131 30
        if ($eventDemand->getStoragePage() != '') {
132 30
            $pidList = GeneralUtility::intExplode(',', $eventDemand->getStoragePage(), true);
133 30
            $constraints[] = $query->in('pid', $pidList);
134 30
        }
135 30
    }
136
137
    /**
138
     * Sets the displayMode constraint to the given constraints array
139
     *
140
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
141
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
142
     * @param array $constraints Constraints
143
     *
144
     * @return void
145
     */
146 30
    protected function setDisplayModeConstraint($query, $eventDemand, &$constraints)
147
    {
148 30
        switch ($eventDemand->getDisplayMode()) {
149 30
            case 'future':
150 1
                $constraints[] = $query->greaterThan('startdate', $eventDemand->getCurrentDateTime());
151 1
                break;
152 29
            case 'past':
153 1
                $constraints[] = $query->lessThanOrEqual('enddate', $eventDemand->getCurrentDateTime());
154 1
                break;
155 28
            default:
156 30
        }
157 30
    }
158
159
    /**
160
     * Sets the category constraint to the given constraints array
161
     *
162
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
163
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
164
     * @param array $constraints Constraints
165
     *
166
     * @return void
167
     */
168 30
    protected function setCategoryConstraint($query, $eventDemand, &$constraints)
169
    {
170 30
        if ($eventDemand->getCategory() != '') {
171 5
            $categoryConstraints = [];
172 5
            if ($eventDemand->getIncludeSubcategories()) {
173 1
                $categoryList = CategoryService::getCategoryListWithChilds($eventDemand->getCategory());
174 1
                $categories = GeneralUtility::intExplode(',', $categoryList, true);
175 1
            } else {
176 4
                $categories = GeneralUtility::intExplode(',', $eventDemand->getCategory(), true);
177
            }
178 5
            foreach ($categories as $category) {
179 5
                $categoryConstraints[] = $query->contains('category', $category);
180 5
            }
181 5
            if (count($categoryConstraints) > 0) {
182 5
                $constraints[] = $query->logicalOr($categoryConstraints);
183 5
            }
184 5
        }
185 30
    }
186
187
    /**
188
     * Sets the location constraint to the given constraints array
189
     *
190
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
191
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
192
     * @param array $constraints Constraints
193
     *
194
     * @return void
195
     */
196 30
    protected function setLocationConstraint($query, $eventDemand, &$constraints)
197
    {
198 30
        if ($eventDemand->getLocation() !== null && $eventDemand->getLocation() != '') {
199 3
            $constraints[] = $query->equals('location', $eventDemand->getLocation());
200 3
        }
201 30
    }
202
203
    /**
204
     * Sets the location.city constraint to the given constraints array
205
     *
206
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
207
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
208
     * @param array $constraints Constraints
209
     *
210
     * @return void
211
     */
212 30
    protected function setLocationCityConstraint($query, $eventDemand, &$constraints)
213
    {
214 30
        if ($eventDemand->getLocationCity() !== null && $eventDemand->getLocationCity() != '') {
215 2
            $constraints[] = $query->equals('location.city', $eventDemand->getLocationCity());
216 2
        }
217 30
    }
218
219
    /**
220
     * Sets the location.country constraint to the given constraints array
221
     *
222
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
223
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
224
     * @param array $constraints Constraints
225
     *
226
     * @return void
227
     */
228 30
    protected function setLocationCountryConstraint($query, $eventDemand, &$constraints)
229
    {
230 30
        if ($eventDemand->getLocationCountry() !== null && $eventDemand->getLocationCountry() != '') {
231 2
            $constraints[] = $query->equals('location.country', $eventDemand->getLocationCountry());
232 2
        }
233 30
    }
234
235
    /**
236
     * Sets the start- and enddate constraint to the given constraints array
237
     *
238
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
239
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
240
     * @param array $constraints Constraints
241
     *
242
     * @return void
243
     */
244 30
    protected function setStartEndDateConstraint($query, $eventDemand, &$constraints)
245
    {
246
        /* StartDate */
247 30
        if ($eventDemand->getSearchDemand() && $eventDemand->getSearchDemand()->getStartDate() !== null) {
248 1
            $constraints[] = $query->greaterThanOrEqual('startdate', $eventDemand->getSearchDemand()->getStartDate());
249 1
        }
250
251
        /* EndDate */
252 30
        if ($eventDemand->getSearchDemand() && $eventDemand->getSearchDemand()->getEndDate() !== null) {
253 1
            $constraints[] = $query->lessThanOrEqual('enddate', $eventDemand->getSearchDemand()->getEndDate());
254 1
        }
255 30
    }
256
257
    /**
258
     * Sets the search constraint to the given constraints array
259
     *
260
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
261
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
262
     * @param array $constraints Constraints
263
     *
264
     * @return void
265
     */
266 30
    protected function setSearchConstraint($query, $eventDemand, &$constraints)
267
    {
268 30
        if ($eventDemand->getSearchDemand() &&
269 30
            $eventDemand->getSearchDemand()->getSearch() !== null &&
270 3
            $eventDemand->getSearchDemand()->getSearch() !== ''
271 30
        ) {
272 1
            $searchFields = GeneralUtility::trimExplode(',', $eventDemand->getSearchDemand()->getFields(), true);
273 1
            $searchConstraints = [];
274
275 1
            if (count($searchFields) === 0) {
276
                throw new \UnexpectedValueException('No search fields defined', 1318497755);
277
            }
278
279 1
            $searchSubject = $eventDemand->getSearchDemand()->getSearch();
280 1
            foreach ($searchFields as $field) {
281 1
                if (!empty($searchSubject)) {
282 1
                    $searchConstraints[] = $query->like($field, '%' . $searchSubject . '%', false);
283 1
                }
284 1
            }
285
286 1
            if (count($searchConstraints)) {
287 1
                $constraints[] = $query->logicalOr($searchConstraints);
288 1
            }
289 1
        }
290 30
    }
291
292
    /**
293
     * Sets the topEvent constraint to the given constraints array
294
     *
295
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
296
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
297
     * @param array $constraints Constraints
298
     *
299
     * @return void
300
     */
301 30
    protected function setTopEventConstraint($query, $eventDemand, &$constraints)
302
    {
303 30
        if ($eventDemand->getTopEventRestriction() > 0) {
304 2
            $constraints[] = $query->equals('topEvent', (bool)($eventDemand->getTopEventRestriction() - 1));
305 2
        }
306 30
    }
307
308
}