Completed
Push — master ( 70dfe1...a97a1c )
by Torben
04:18
created

Classes/Domain/Repository/EventRepository.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
5
 *
6
 * For the full copyright and license information, please read the
7
 * LICENSE.txt file that was distributed with this source code.
8
 */
9
10
namespace DERHANSEN\SfEventMgt\Domain\Repository;
11
12
use DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand;
13
use DERHANSEN\SfEventMgt\Event\ModifyEventQueryConstraintsEvent;
14
use DERHANSEN\SfEventMgt\Service\CategoryService;
15
use Psr\EventDispatcher\EventDispatcherInterface;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use TYPO3\CMS\Core\Utility\MathUtility;
18
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
19
20
/**
21
 * The repository for Events
22
 *
23
 * @author Torben Hansen <[email protected]>
24
 */
25
class EventRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
26
{
27
    /**
28
     * Set default sorting
29
     *
30
     * @var array
31
     */
32
    protected $defaultOrderings = [
33
        'startdate' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
34
    ];
35
36
    /**
37
     * @var EventDispatcherInterface
38
     */
39
    protected $eventDispatcher;
40
41
    /**
42
     * @param EventDispatcherInterface $eventDispatcher
43
     */
44
    public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher)
45 31
    {
46
        $this->eventDispatcher = $eventDispatcher;
47 31
    }
48 31
49 31
    /**
50
     * Disable the use of storage records, because the StoragePage can be set
51
     * in the plugin
52
     */
53
    public function initializeObject()
54
    {
55
        $this->defaultQuerySettings = $this->objectManager->get(Typo3QuerySettings::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->objectManager->ge...o3QuerySettings::class) of type object<TYPO3\CMS\Extbase\Object\object> is incompatible with the declared type object<TYPO3\CMS\Extbase...QuerySettingsInterface> of property $defaultQuerySettings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Deprecated Code introduced by
The method TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated with message: since TYPO3 10.4, will be removed in version 12.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
56
        $this->defaultQuerySettings->setRespectStoragePage(false);
57
    }
58 30
59
    /**
60 30
     * Returns the objects of this repository matching the given demand
61 30
     *
62 30
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
63 30
     *
64 30
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface QueryResultInterface
65 30
     */
66 30
    public function findDemanded(EventDemand $eventDemand)
67 30
    {
68 30
        $constraints = [];
69 30
        $query = $this->createQuery();
70 30
        $this->setStoragePageConstraint($query, $eventDemand, $constraints);
71 30
        $this->setDisplayModeConstraint($query, $eventDemand, $constraints);
72
        $this->setCategoryConstraint($query, $eventDemand, $constraints);
73 30
        $this->setLocationConstraint($query, $eventDemand, $constraints);
74 30
        $this->setLocationCityConstraint($query, $eventDemand, $constraints);
75 30
        $this->setLocationCountryConstraint($query, $eventDemand, $constraints);
76
        $this->setSpeakerConstraint($query, $eventDemand, $constraints);
77 30
        $this->setOrganisatorConstraint($query, $eventDemand, $constraints);
78 30
        $this->setStartEndDateConstraint($query, $eventDemand, $constraints);
79
        $this->setSearchConstraint($query, $eventDemand, $constraints);
80
        $this->setTopEventConstraint($query, $eventDemand, $constraints);
81
        $this->setYearMonthDayRestriction($query, $eventDemand, $constraints);
82
83
        $modifyEventQueryConstraintsEvent = new ModifyEventQueryConstraintsEvent(
84
            $constraints,
85
            $query,
86
            $eventDemand,
87
            $this
88
        );
89 30
        $this->eventDispatcher->dispatch($modifyEventQueryConstraintsEvent);
0 ignored issues
show
$modifyEventQueryConstraintsEvent is of type object<DERHANSEN\SfEvent...tQueryConstraintsEvent>, but the function expects a object<Psr\EventDispatcher\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
        $constraints = $modifyEventQueryConstraintsEvent->getConstraints();
91 30
92 30
        $this->setOrderingsFromDemand($query, $eventDemand);
93 1
94 30
        if (count($constraints) > 0) {
95 1
            $query->matching($query->logicalAnd($constraints));
96 1
        }
97 30
98
        $this->setQueryLimitFromDemand($query, $eventDemand);
99
100
        return $query->execute();
101
    }
102
103
    /**
104
     * Sets a query limit to the given query for the given demand
105
     *
106
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
107 30
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
108
     */
109 30
    protected function setQueryLimitFromDemand($query, EventDemand $eventDemand)
110 30
    {
111 30
        if ($eventDemand->getQueryLimit() != null &&
112 30
            MathUtility::canBeInterpretedAsInteger($eventDemand->getQueryLimit()) &&
113
            (int)$eventDemand->getQueryLimit() > 0
114
        ) {
115
            $query->setLimit((int)$eventDemand->getQueryLimit());
116
        }
117
    }
118 30
119
    /**
120
     * Sets the ordering to the given query for the given demand
121
     *
122
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
123
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
124
     */
125
    protected function setOrderingsFromDemand($query, EventDemand $eventDemand)
126
    {
127
        $orderings = [];
128
        $orderFieldAllowed = GeneralUtility::trimExplode(',', $eventDemand->getOrderFieldAllowed(), true);
129 30
        if ($eventDemand->getOrderField() != '' && $eventDemand->getOrderDirection() != '' &&
130
            !empty($orderFieldAllowed) && in_array($eventDemand->getOrderField(), $orderFieldAllowed, true)) {
131 30
            $orderings[$eventDemand->getOrderField()] = ((strtolower($eventDemand->getOrderDirection()) == 'desc') ?
132 30
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING :
133 30
                \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
134 30
            $query->setOrderings($orderings);
135 30
        }
136
    }
137
138
    /**
139
     * Sets the storagePage constraint to the given constraints array
140
     *
141
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
142
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
143
     * @param array $constraints Constraints
144
     */
145
    protected function setStoragePageConstraint($query, $eventDemand, &$constraints)
146 30
    {
147
        if ($eventDemand->getStoragePage() && $eventDemand->getStoragePage() !== '') {
148 30
            $pidList = GeneralUtility::intExplode(',', $eventDemand->getStoragePage(), true);
149 30
            $constraints[] = $query->in('pid', $pidList);
150 1
        }
151 1
    }
152 29
153 1
    /**
154 1
     * Sets the displayMode constraint to the given constraints array
155 28
     *
156 30
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
157 30
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
158
     * @param array $constraints Constraints
159
     */
160
    protected function setDisplayModeConstraint($query, $eventDemand, &$constraints)
161
    {
162
        switch ($eventDemand->getDisplayMode()) {
163
            case 'future':
164
                $constraints[] = $query->greaterThan('startdate', $eventDemand->getCurrentDateTime());
165
                break;
166
            case 'current_future':
167
                $constraints[] = $query->logicalOr([
168 30
                    $query->greaterThan('startdate', $eventDemand->getCurrentDateTime()),
169
                    $query->logicalAnd([
170 30
                        $query->greaterThanOrEqual('enddate', $eventDemand->getCurrentDateTime()),
171 5
                        $query->lessThanOrEqual('startdate', $eventDemand->getCurrentDateTime())
172 5
                    ])
173 1
                ]);
174 1
                break;
175 1
            case 'past':
176 4
                $constraints[] = $query->lessThanOrEqual('enddate', $eventDemand->getCurrentDateTime());
177
                break;
178 5
            default:
179 5
        }
180 5
    }
181 5
182 5
    /**
183 5
     * Sets the category constraint to the given constraints array
184 5
     *
185 30
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
186
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
187
     * @param array $constraints Constraints
188
     */
189
    protected function setCategoryConstraint($query, $eventDemand, &$constraints)
190
    {
191
        // If no category constraint is set, categories should not be respected in the query
192
        if ($eventDemand->getCategoryConjunction() === '') {
193
            return;
194
        }
195
196 30
        if ($eventDemand->getCategory() != '') {
197
            $categoryConstraints = [];
198 30
            if ($eventDemand->getIncludeSubcategories()) {
199 3
                $categoryList = CategoryService::getCategoryListWithChilds($eventDemand->getCategory());
200 3
                $categories = GeneralUtility::intExplode(',', $categoryList, true);
201 30
            } else {
202
                $categories = GeneralUtility::intExplode(',', $eventDemand->getCategory(), true);
203
            }
204
            foreach ($categories as $category) {
205
                $categoryConstraints[] = $query->contains('category', $category);
206
            }
207
            if (count($categoryConstraints) > 0) {
208
                $constraints[] = $this->getCategoryConstraint($query, $eventDemand, $categoryConstraints);
209
            }
210
        }
211
    }
212 30
213
    /**
214 30
     * Returns the category constraint depending on the category conjunction configured in eventDemand
215 2
     *
216 2
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
217 30
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand
218
     * @param array $categoryConstraints
219
     * @return mixed
220
     */
221
    public function getCategoryConstraint($query, $eventDemand, $categoryConstraints)
222
    {
223
        switch (strtolower($eventDemand->getCategoryConjunction())) {
224
            case 'and':
225
                $constraint = $query->logicalAnd($categoryConstraints);
226
                break;
227
            case 'notor':
228 30
                $constraint = $query->logicalNot($query->logicalOr($categoryConstraints));
229
                break;
230 30
            case 'notand':
231 2
                $constraint = $query->logicalNot($query->logicalAnd($categoryConstraints));
232 2
                break;
233 30
            case 'or':
234
            default:
235
                $constraint = $query->logicalOr($categoryConstraints);
236
        }
237
238
        return $constraint;
239
    }
240
241
    /**
242
     * Sets the location constraint to the given constraints array
243
     *
244 30
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
245
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
246
     * @param array $constraints Constraints
247 30
     */
248 1
    protected function setLocationConstraint($query, $eventDemand, &$constraints)
249 1
    {
250
        if ($eventDemand->getLocation() !== null && $eventDemand->getLocation() != '') {
251
            $constraints[] = $query->equals('location', $eventDemand->getLocation());
252 30
        }
253 1
    }
254 1
255 30
    /**
256
     * Sets the location.city constraint to the given constraints array
257
     *
258
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
259
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
260
     * @param array $constraints Constraints
261
     */
262
    protected function setLocationCityConstraint($query, $eventDemand, &$constraints)
263
    {
264
        if ($eventDemand->getLocationCity() !== null && $eventDemand->getLocationCity() != '') {
265
            $constraints[] = $query->equals('location.city', $eventDemand->getLocationCity());
266 30
        }
267
    }
268 30
269 30
    /**
270 3
     * Sets the location.country constraint to the given constraints array
271 30
     *
272 1
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
273 1
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
274
     * @param array $constraints Constraints
275 1
     */
276
    protected function setLocationCountryConstraint($query, $eventDemand, &$constraints)
277
    {
278
        if ($eventDemand->getLocationCountry() !== null && $eventDemand->getLocationCountry() != '') {
279 1
            $constraints[] = $query->equals('location.country', $eventDemand->getLocationCountry());
280 1
        }
281 1
    }
282 1
283 1
    /**
284 1
     * Sets the speaker constraint to the given constraints array
285
     *
286 1
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
287 1
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
288 1
     * @param array $constraints Constraints
289 1
     */
290 30
    protected function setSpeakerConstraint($query, $eventDemand, &$constraints)
291
    {
292
        if ($eventDemand->getSpeaker() !== null && $eventDemand->getSpeaker() != '') {
293
            $constraints[] = $query->contains('speaker', $eventDemand->getSpeaker());
294
        }
295
    }
296
297
    /**
298
     * Sets the organisator constraint to the given constraints array
299
     *
300
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
301 30
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
302
     * @param array $constraints Constraints
303 30
     */
304 2
    protected function setOrganisatorConstraint($query, $eventDemand, &$constraints)
305 2
    {
306 30
        if ($eventDemand->getOrganisator() !== null && $eventDemand->getOrganisator() != '') {
307
            $constraints[] = $query->equals('organisator', $eventDemand->getOrganisator());
308
        }
309
    }
310
311
    /**
312
     * Sets the start- and enddate constraint to the given constraints array
313
     *
314
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
315
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
316
     * @param array $constraints Constraints
317
     */
318
    protected function setStartEndDateConstraint($query, $eventDemand, &$constraints)
319
    {
320
        if ($eventDemand->getSearchDemand() && $eventDemand->getSearchDemand()->getStartDate() !== null &&
321
            $eventDemand->getSearchDemand()->getEndDate() !== null
322
        ) {
323
            /* StartDate and EndDate  - Search for events between two given dates */
324
            $begin = $eventDemand->getSearchDemand()->getStartDate();
325
            $end = $eventDemand->getSearchDemand()->getEndDate();
326
            $constraints[] = $query->logicalOr([
327
                $query->between('startdate', $begin, $end),
328
                $query->between('enddate', $begin, $end),
329
                $query->logicalAnd([
330
                    $query->greaterThanOrEqual('enddate', $begin),
331
                    $query->lessThanOrEqual('startdate', $begin)
332
                ])
333
            ]);
334
        } elseif ($eventDemand->getSearchDemand() && $eventDemand->getSearchDemand()->getStartDate() !== null) {
335
            /* StartDate - Search for events beginning at a given date */
336
            $constraints[] = $query->greaterThanOrEqual('startdate', $eventDemand->getSearchDemand()->getStartDate());
337
        } elseif ($eventDemand->getSearchDemand() && $eventDemand->getSearchDemand()->getEndDate() !== null) {
338
            /* EndDate - Search for events ending on a given date */
339
            $constraints[] = $query->lessThanOrEqual('enddate', $eventDemand->getSearchDemand()->getEndDate());
340
        }
341
    }
342
343
    /**
344
     * Sets the search constraint to the given constraints array
345
     *
346
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
347
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
348
     * @param array $constraints Constraints
349
     */
350
    protected function setSearchConstraint($query, $eventDemand, &$constraints)
351
    {
352
        if ($eventDemand->getSearchDemand() &&
353
            $eventDemand->getSearchDemand()->getSearch() !== null &&
354
            $eventDemand->getSearchDemand()->getSearch() !== ''
355
        ) {
356
            $searchFields = GeneralUtility::trimExplode(',', $eventDemand->getSearchDemand()->getFields(), true);
357
            $searchConstraints = [];
358
359
            if (count($searchFields) === 0) {
360
                throw new \UnexpectedValueException('No search fields defined', 1318497755);
361
            }
362
363
            $searchSubject = $eventDemand->getSearchDemand()->getSearch();
364
            foreach ($searchFields as $field) {
365
                if (!empty($searchSubject)) {
366
                    $searchConstraints[] = $query->like($field, '%' . addcslashes($searchSubject, '_%') . '%');
367
                }
368
            }
369
370
            if (count($searchConstraints)) {
371
                $constraints[] = $query->logicalOr($searchConstraints);
372
            }
373
        }
374
    }
375
376
    /**
377
     * Sets the topEvent constraint to the given constraints array
378
     *
379
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query Query
380
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand EventDemand
381
     * @param array $constraints Constraints
382
     */
383
    protected function setTopEventConstraint($query, $eventDemand, &$constraints)
384
    {
385
        if ($eventDemand->getTopEventRestriction() > 0) {
386
            $constraints[] = $query->equals('topEvent', (bool)($eventDemand->getTopEventRestriction() - 1));
387
        }
388
    }
389
390
    /**
391
     * Sets the restriction for year, year/month or year/month/day to the given constraints array
392
     *
393
     * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
394
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $eventDemand
395
     * @param array $constraints
396
     */
397
    protected function setYearMonthDayRestriction($query, $eventDemand, &$constraints)
398
    {
399
        if ($eventDemand->getYear() > 0) {
400
            if ($eventDemand->getMonth() > 0) {
401
                if ($eventDemand->getDay() > 0) {
402
                    $begin = mktime(0, 0, 0, $eventDemand->getMonth(), $eventDemand->getDay(), $eventDemand->getYear());
403
                    $end = mktime(23, 59, 59, $eventDemand->getMonth(), $eventDemand->getDay(), $eventDemand->getYear());
404
                } else {
405
                    $begin = mktime(0, 0, 0, $eventDemand->getMonth(), 1, $eventDemand->getYear());
406
                    $end = mktime(23, 59, 59, ($eventDemand->getMonth() + 1), 0, $eventDemand->getYear());
407
                }
408
            } else {
409
                $begin = mktime(0, 0, 0, 1, 1, $eventDemand->getYear());
410
                $end = mktime(23, 59, 59, 12, 31, $eventDemand->getYear());
411
            }
412
            $constraints[] = $query->logicalOr([
413
                $query->between('startdate', $begin, $end),
414
                $query->between('enddate', $begin, $end),
415
                $query->logicalAnd([
416
                    $query->greaterThanOrEqual('enddate', $begin),
417
                    $query->lessThanOrEqual('startdate', $begin)
418
                ])
419
            ]);
420
        }
421
    }
422
}
423