Completed
Push — master ( 07b37d...b10e41 )
by Torben
02:00
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 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
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 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 30
    public function initializeObject()
46
    {
47 30
        $this->defaultQuerySettings = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
48 30
        $this->defaultQuerySettings->setRespectStoragePage(false);
49 30
    }
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 29
    public function findDemanded(EventDemand $eventDemand)
59
    {
60 29
        $constraints = [];
61 29
        $query = $this->createQuery();
62 29
        $this->setStoragePageConstraint($query, $eventDemand, $constraints);
63 29
        $this->setDisplayModeConstraint($query, $eventDemand, $constraints);
64 29
        $this->setCategoryConstraint($query, $eventDemand, $constraints);
65 29
        $this->setLocationConstraint($query, $eventDemand, $constraints);
66 29
        $this->setLocationCityConstraint($query, $eventDemand, $constraints);
67 29
        $this->setLocationCountryConstraint($query, $eventDemand, $constraints);
68 29
        $this->setStartEndDateConstraint($query, $eventDemand, $constraints);
69 29
        $this->setSearchConstraint($query, $eventDemand, $constraints);
70 29
        $this->setTopEventConstraint($query, $eventDemand, $constraints);
71 29
        $this->setOrderingsFromDemand($query, $eventDemand);
72
73 29
        if (count($constraints) > 0) {
74 29
            $query->matching($query->logicalAnd($constraints));
75 29
        }
76
77 29
        $this->setQueryLimitFromDemand($query, $eventDemand);
78 29
        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 29
    protected function setQueryLimitFromDemand($query, EventDemand $eventDemand)
90
    {
91 29
        if ($eventDemand->getQueryLimit() != null &&
92 29
            MathUtility::canBeInterpretedAsInteger($eventDemand->getQueryLimit()) &&
93 1
            (int)$eventDemand->getQueryLimit() > 0
94 29
        ) {
95 1
            $query->setLimit((int)$eventDemand->getQueryLimit());
96 1
        }
97 29
    }
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 29
    protected function setOrderingsFromDemand($query, EventDemand $eventDemand)
108
    {
109 29
        $orderings = [];
110 29
        if ($eventDemand->getOrderField() != '' && $eventDemand->getOrderDirection() != '') {
111 6
            $orderings[$eventDemand->getOrderField()] = ((strtolower($eventDemand->getOrderDirection()) == 'desc') ?
112 6
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING :
113 3
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
114 6
            $query->setOrderings($orderings);
115 6
        }
116 29
    }
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 29
    protected function setStoragePageConstraint($query, $eventDemand, &$constraints)
128
    {
129 29
        if ($eventDemand->getStoragePage() != '') {
130 29
            $pidList = GeneralUtility::intExplode(',', $eventDemand->getStoragePage(), true);
131 29
            $constraints[] = $query->in('pid', $pidList);
132 29
        }
133 29
    }
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 29
    protected function setDisplayModeConstraint($query, $eventDemand, &$constraints)
145
    {
146 29
        switch ($eventDemand->getDisplayMode()) {
147 29
            case 'future':
148 1
                $constraints[] = $query->greaterThan('startdate', $eventDemand->getCurrentDateTime());
149 1
                break;
150 28
            case 'past':
151 1
                $constraints[] = $query->lessThanOrEqual('enddate', $eventDemand->getCurrentDateTime());
152 1
                break;
153 27
            default:
154 29
        }
155 29
    }
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 29
    protected function setCategoryConstraint($query, $eventDemand, &$constraints)
167
    {
168 29
        if ($eventDemand->getCategory() != '') {
169 4
            $categoryConstraints = [];
170 4
            $categories = GeneralUtility::intExplode(',', $eventDemand->getCategory(), true);
171 4
            foreach ($categories as $category) {
172 4
                $categoryConstraints[] = $query->contains('category', $category);
173 4
            }
174 4
            if (count($categoryConstraints) > 0) {
175 4
                $constraints[] = $query->logicalOr($categoryConstraints);
176 4
            }
177 4
        }
178 29
    }
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 29
    protected function setLocationConstraint($query, $eventDemand, &$constraints)
190
    {
191 29
        if ($eventDemand->getLocation() !== null && $eventDemand->getLocation() != '') {
192 3
            $constraints[] = $query->equals('location', $eventDemand->getLocation());
193 3
        }
194 29
    }
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 29
    protected function setLocationCityConstraint($query, $eventDemand, &$constraints)
206
    {
207 29
        if ($eventDemand->getLocationCity() !== null && $eventDemand->getLocationCity() != '') {
208 2
            $constraints[] = $query->equals('location.city', $eventDemand->getLocationCity());
209 2
        }
210 29
    }
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 29
    protected function setLocationCountryConstraint($query, $eventDemand, &$constraints)
222
    {
223 29
        if ($eventDemand->getLocationCountry() !== null && $eventDemand->getLocationCountry() != '') {
224 2
            $constraints[] = $query->equals('location.country', $eventDemand->getLocationCountry());
225 2
        }
226 29
    }
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 29
    protected function setStartEndDateConstraint($query, $eventDemand, &$constraints)
238
    {
239
        /* StartDate */
240 29
        if ($eventDemand->getSearchDemand() && $eventDemand->getSearchDemand()->getStartDate() !== null) {
241 1
            $constraints[] = $query->greaterThanOrEqual('startdate', $eventDemand->getSearchDemand()->getStartDate());
242 1
        }
243
244
        /* EndDate */
245 29
        if ($eventDemand->getSearchDemand() && $eventDemand->getSearchDemand()->getEndDate() !== null) {
246 1
            $constraints[] = $query->lessThanOrEqual('enddate', $eventDemand->getSearchDemand()->getEndDate());
247 1
        }
248 29
    }
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 29
    protected function setSearchConstraint($query, $eventDemand, &$constraints)
260
    {
261 29
        if ($eventDemand->getSearchDemand() &&
262 29
            $eventDemand->getSearchDemand()->getSearch() !== null &&
263 3
            $eventDemand->getSearchDemand()->getSearch() !== ''
264 29
        ) {
265 1
            $searchFields = GeneralUtility::trimExplode(',', $eventDemand->getSearchDemand()->getFields(), true);
266 1
            $searchConstraints = [];
267
268 1
            if (count($searchFields) === 0) {
269
                throw new \UnexpectedValueException('No search fields defined', 1318497755);
270
            }
271
272 1
            $searchSubject = $eventDemand->getSearchDemand()->getSearch();
273 1
            foreach ($searchFields as $field) {
274 1
                if (!empty($searchSubject)) {
275 1
                    $searchConstraints[] = $query->like($field, '%' . $searchSubject . '%', false);
276 1
                }
277 1
            }
278
279 1
            if (count($searchConstraints)) {
280 1
                $constraints[] = $query->logicalOr($searchConstraints);
281 1
            }
282 1
        }
283 29
    }
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 29
    protected function setTopEventConstraint($query, $eventDemand, &$constraints)
295
    {
296 29
        if ($eventDemand->getTopEventRestriction() > 0) {
297 2
            $constraints[] = $query->equals('topEvent', (bool)($eventDemand->getTopEventRestriction() - 1));
298 2
        }
299 29
    }
300
301
}