Completed
Push — master ( 5794c5...08c98b )
by Fabien
53:00
created
Classes/Domain/Repository/ContentRepository.php 1 patch
Indentation   +823 added lines, -823 removed lines patch added patch discarded remove patch
@@ -38,827 +38,827 @@
 block discarded – undo
38 38
  */
39 39
 class ContentRepository implements RepositoryInterface
40 40
 {
41
-    /**
42
-     * Tell whether it is a raw result (array) or object being returned.
43
-     *
44
-     * @var bool
45
-     */
46
-    protected $rawResult = false;
47
-
48
-    /**
49
-     * The data type to be returned, e.g fe_users, fe_groups, tt_content, etc...
50
-     *
51
-     * @var string
52
-     */
53
-    protected $dataType;
54
-
55
-    /**
56
-     * The source field is useful in the context of MM relations to know who is the caller
57
-     * e.g findByItems which eventually corresponds to a field name.
58
-     *
59
-     * @var string
60
-     */
61
-    protected $sourceFieldName = '';
62
-
63
-    /**
64
-     * @var array
65
-     */
66
-    protected $errorMessages = [];
67
-
68
-    /**
69
-     * @var QuerySettingsInterface
70
-     */
71
-    protected $defaultQuerySettings;
72
-
73
-    /**
74
-     * @var DataHandler
75
-     */
76
-    protected $dataHandler;
77
-
78
-    /**
79
-     * Constructor
80
-     *
81
-     * @param string $dataType
82
-     */
83
-    public function __construct($dataType)
84
-    {
85
-        $this->dataType = $dataType;
86
-    }
87
-
88
-    /**
89
-     * Returns all objects of this repository.
90
-     *
91
-     * @return Content[]
92
-     */
93
-    public function findAll()
94
-    {
95
-        $query = $this->createQuery();
96
-        return $query->execute();
97
-    }
98
-
99
-    /**
100
-     * Returns all "distinct" values for a given property.
101
-     *
102
-     * @param string $propertyName
103
-     * @param Matcher $matcher
104
-     * @param Order|null $order
105
-     * @return Content[]
106
-     */
107
-    public function findDistinctValues($propertyName, Matcher $matcher = null, Order $order = null): array
108
-    {
109
-        $query = $this->createQuery();
110
-        $query->setDistinct($propertyName);
111
-
112
-        // Remove empty values from selection.
113
-        $constraint = $query->logicalNot($query->equals($propertyName, ''));
114
-
115
-        // Add some additional constraints from the Matcher object.
116
-        $matcherConstraint = null;
117
-        if ($matcher !== null) {
118
-            $matcherConstraint = $this->computeConstraints($query, $matcher);
119
-        }
120
-
121
-        // Assemble the final constraints or not.
122
-        if ($matcherConstraint) {
123
-            $query->logicalAnd([$matcherConstraint, $constraint]);
124
-            $query->matching($query->logicalAnd([$matcherConstraint, $constraint]));
125
-        } else {
126
-            $query->matching($constraint);
127
-        }
128
-
129
-        if ($order) {
130
-            $query->setOrderings($order->getOrderings());
131
-        }
132
-
133
-        return $query->execute();
134
-    }
135
-
136
-    /**
137
-     * Returns all "distinct" values for a given property.
138
-     *
139
-     * @param string $propertyName
140
-     * @param Matcher $matcher
141
-     * @return int
142
-     */
143
-    public function countDistinctValues($propertyName, Matcher $matcher = null): int
144
-    {
145
-        $query = $this->createQuery();
146
-        $query->setDistinct($propertyName);
147
-
148
-        // Remove empty values from selection.
149
-        $constraint = $query->logicalNot($query->equals($propertyName, ''));
150
-
151
-        // Add some additional constraints from the Matcher object.
152
-        $matcherConstraint = null;
153
-        if (!is_null($matcher)) {
154
-            $matcherConstraint = $this->computeConstraints($query, $matcher);
155
-        }
156
-
157
-        // Assemble the final constraints or not.
158
-        if ($matcherConstraint) {
159
-            $query->logicalAnd([$matcherConstraint, $constraint]);
160
-            $query->matching($query->logicalAnd([$matcherConstraint, $constraint]));
161
-        } else {
162
-            $query->matching($constraint);
163
-        }
164
-
165
-        return $query->count();
166
-    }
167
-
168
-    /**
169
-     * Finds an object matching the given identifier.
170
-     *
171
-     * @param int $uid The identifier of the object to find
172
-     * @return Content|null
173
-     * @api
174
-     */
175
-    public function findByUid($uid)
176
-    {
177
-        return $this->findByIdentifier($uid);
178
-    }
179
-
180
-    /**
181
-     * Finds all Contents given specified matches.
182
-     *
183
-     * @param string $propertyName
184
-     * @param array $values
185
-     * @return Content[]
186
-     */
187
-    public function findIn($propertyName, array $values): array
188
-    {
189
-        $query = $this->createQuery();
190
-        $query->matching($query->in($propertyName, $values));
191
-        return $query->execute();
192
-    }
193
-
194
-    /**
195
-     * Finds all Contents given specified matches.
196
-     *
197
-     * @param Matcher $matcher
198
-     * @param Order $order The order
199
-     * @param int $limit
200
-     * @param int $offset
201
-     * @return Content[]
202
-     */
203
-    public function findBy(Matcher $matcher, Order $order = null, $limit = null, $offset = null): array
204
-    {
205
-        $query = $this->createQuery();
206
-
207
-        $limit = (int)$limit; // make sure to cast
208
-        if ($limit > 0) {
209
-            $query->setLimit($limit);
210
-        }
211
-
212
-        if ($order) {
213
-            $query->setOrderings($order->getOrderings());
214
-
215
-            // Loops around the orderings adding if necessary a dummy condition
216
-            // to make sure the relations can be resolved when transforming the query to plain SQL.
217
-            foreach ($order->getOrderings() as $ordering => $direction) {
218
-                if ($this->hasForeignRelationIn($ordering)) {
219
-                    $relationalField = $this->getForeignRelationFrom($ordering);
220
-                    $matcher->like($relationalField . '.uid', '');
221
-                }
222
-            }
223
-        }
224
-
225
-        if ($offset) {
226
-            $query->setOffset($offset);
227
-        }
228
-
229
-        $constraints = $this->computeConstraints($query, $matcher);
230
-
231
-        if ($constraints) {
232
-            $query->matching($constraints);
233
-        }
234
-
235
-        return $query->execute();
236
-    }
237
-
238
-    /**
239
-     * Find one Content object given specified matches.
240
-     *
241
-     * @param Matcher $matcher
242
-     * @return Content
243
-     */
244
-    public function findOneBy(Matcher $matcher): Content
245
-    {
246
-        $query = $this->createQuery();
247
-
248
-        $constraints = $this->computeConstraints($query, $matcher);
249
-
250
-        if ($constraints) {
251
-            $query->matching($constraints);
252
-        }
253
-
254
-        $query->setLimit(1); // only take one!
255
-
256
-        $resultSet = $query->execute();
257
-        if ($resultSet) {
258
-            $resultSet = current($resultSet);
259
-        }
260
-        return $resultSet;
261
-    }
262
-
263
-    /**
264
-     * Count all Contents given specified matches.
265
-     *
266
-     * @param Matcher $matcher
267
-     * @return int
268
-     */
269
-    public function countBy(Matcher $matcher): int
270
-    {
271
-        $query = $this->createQuery();
272
-
273
-        $constraints = $this->computeConstraints($query, $matcher);
274
-
275
-        if ($constraints) {
276
-            $query->matching($constraints);
277
-        }
278
-
279
-        return $query->count();
280
-    }
281
-
282
-    /**
283
-     * Update a content with new information.
284
-     *
285
-     * @param Content $content
286
-     * @param $language
287
-     * @return bool
288
-     */
289
-    public function localize($content, $language): bool
290
-    {
291
-        // Security check
292
-        $this->getContentValidator()->validate($content);
293
-        $this->getLanguageValidator()->validate($language);
294
-
295
-        $dataType = $content->getDataType();
296
-        $handler = $this->getDataHandlerFactory()->action(ProcessAction::LOCALIZE)->forType($dataType)->getDataHandler();
297
-
298
-        $handlerResult = $handler->processLocalize($content, $language);
299
-        $this->errorMessages = $handler->getErrorMessages();
300
-        return $handlerResult;
301
-    }
302
-
303
-    /**
304
-     * Update a content with new information.
305
-     *
306
-     * @param Content $content
307
-     * @return bool
308
-     */
309
-    public function update($content)
310
-    {
311
-        // Security check.
312
-        $this->getContentValidator()->validate($content);
313
-
314
-        $dataType = $content->getDataType();
315
-        $handler = $this->getDataHandlerFactory()->action(ProcessAction::UPDATE)->forType($dataType)->getDataHandler();
316
-
317
-        $handlerResult = $handler->processUpdate($content);
318
-        $this->errorMessages = $handler->getErrorMessages();
319
-        return $handlerResult;
320
-    }
321
-
322
-    /**
323
-     * Removes an object from this repository.
324
-     *
325
-     * @param Content $content
326
-     * @return boolean
327
-     */
328
-    public function remove($content)
329
-    {
330
-        $dataType = $content->getDataType();
331
-        $handler = $this->getDataHandlerFactory()->action(ProcessAction::REMOVE)->forType($dataType)->getDataHandler();
332
-
333
-        $handlerResult = $handler->processRemove($content);
334
-        $this->errorMessages = $handler->getErrorMessages();
335
-        return $handlerResult;
336
-    }
337
-
338
-    /**
339
-     * Move a content within this repository.
340
-     * The $target corresponds to the pid to move the records to.
341
-     * It can also be a negative value in case of sorting. The negative value would be the uid of its predecessor.
342
-     *
343
-     * @param Content $content
344
-     * @param string $target
345
-     * @return bool
346
-     */
347
-    public function move($content, $target): bool
348
-    {
349
-        // Security check.
350
-        $this->getContentValidator()->validate($content);
351
-
352
-        $dataType = $content->getDataType();
353
-        $handler = $this->getDataHandlerFactory()->action(ProcessAction::MOVE)->forType($dataType)->getDataHandler();
354
-
355
-        $handlerResult = $handler->processMove($content, $target);
356
-        $this->errorMessages = $handler->getErrorMessages();
357
-        return $handlerResult;
358
-    }
359
-
360
-    /**
361
-     * Copy a content within this repository.
362
-     *
363
-     * @param Content $content
364
-     * @return bool
365
-     */
366
-    public function copy($content, $target): bool
367
-    {
368
-        // Security check.
369
-        $this->getContentValidator()->validate($content);
370
-
371
-        $dataType = $content->getDataType();
372
-        $handler = $this->getDataHandlerFactory()->action(ProcessAction::COPY)->forType($dataType)->getDataHandler();
373
-
374
-        $handlerResult = $handler->processCopy($content, $target);
375
-        $this->errorMessages = $handler->getErrorMessages();
376
-        return $handlerResult;
377
-    }
378
-
379
-    /**
380
-     * Adds an object to this repository.
381
-     *
382
-     * @param object $object The object to add
383
-     * @return void
384
-     * @api
385
-     */
386
-    public function add($object)
387
-    {
388
-        throw new \BadMethodCallException('Repository does not support the add() method.', 1375805599);
389
-    }
390
-
391
-    /**
392
-     * Returns the total number objects of this repository.
393
-     *
394
-     * @return integer The object count
395
-     * @api
396
-     */
397
-    public function countAll()
398
-    {
399
-        $query = $this->createQuery();
400
-        return $query->count();
401
-    }
402
-
403
-    /**
404
-     * Removes all objects of this repository as if remove() was called for
405
-     * all of them.
406
-     *
407
-     * @return void
408
-     * @api
409
-     */
410
-    public function removeAll()
411
-    {
412
-        // TODO: Implement removeAll() method.
413
-    }
414
-
415
-    /**
416
-     * Finds an object matching the given identifier.
417
-     *
418
-     * @param mixed $identifier The identifier of the object to find
419
-     * @return Content|null
420
-     * @api
421
-     */
422
-    public function findByIdentifier($identifier)
423
-    {
424
-        $query = $this->createQuery();
425
-
426
-        $result = $query->matching(
427
-            $query->equals('uid', $identifier)
428
-        )
429
-            ->execute();
430
-
431
-        if (is_array($result)) {
432
-            $result = current($result);
433
-        }
434
-
435
-        return $result;
436
-    }
437
-
438
-    /**
439
-     * Dispatches magic methods (findBy[Property]())
440
-     *
441
-     * @param string $methodName The name of the magic method
442
-     * @param string $arguments The arguments of the magic method
443
-     * @return mixed
444
-     * @api
445
-     */
446
-    public function __call($methodName, $arguments)
447
-    {
448
-        if (substr($methodName, 0, 6) === 'findBy' && strlen($methodName) > 7) {
449
-            $propertyName = strtolower(substr(substr($methodName, 6), 0, 1)) . substr(substr($methodName, 6), 1);
450
-            $result = $this->processMagicCall($propertyName, $arguments[0]);
451
-        } elseif (substr($methodName, 0, 9) === 'findOneBy' && strlen($methodName) > 10) {
452
-            $propertyName = strtolower(substr(substr($methodName, 9), 0, 1)) . substr(substr($methodName, 9), 1);
453
-            $result = $this->processMagicCall($propertyName, $arguments[0], 'one');
454
-        } elseif (substr($methodName, 0, 7) === 'countBy' && strlen($methodName) > 8) {
455
-            $propertyName = strtolower(substr(substr($methodName, 7), 0, 1)) . substr(substr($methodName, 7), 1);
456
-            $result = $this->processMagicCall($propertyName, $arguments[0], 'count');
457
-        } else {
458
-            throw new UnsupportedMethodException('The method "' . $methodName . '" is not supported by the repository.', 1360838010);
459
-        }
460
-        return $result;
461
-    }
462
-
463
-    /**
464
-     * Returns a query for objects of this repository
465
-     *
466
-     * @return Query
467
-     * @api
468
-     */
469
-    public function createQuery()
470
-    {
471
-        /** @var Query $query */
472
-        $query = GeneralUtility::makeInstance(Query::class, $this->dataType);
473
-        $query->setSourceFieldName($this->sourceFieldName);
474
-
475
-        if ($this->defaultQuerySettings) {
476
-            $query->setTypo3QuerySettings($this->defaultQuerySettings);
477
-        } else {
478
-            // Initialize and pass the query settings at this level.
479
-            $querySettings = GeneralUtility::makeInstance(Typo3QuerySettings::class);
480
-
481
-            // Default choice for the BE.
482
-            if ($this->isBackendMode()) {
483
-                $querySettings->setIgnoreEnableFields(true);
484
-            }
485
-
486
-            $query->setTypo3QuerySettings($querySettings);
487
-        }
488
-
489
-        return $query;
490
-    }
491
-
492
-    /**
493
-     * Sets the property names to order the result by per default.
494
-     * Expected like this:
495
-     * array(
496
-     * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
497
-     * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
498
-     * )
499
-     *
500
-     * @param array $defaultOrderings The property names to order by
501
-     * @return void
502
-     * @api
503
-     */
504
-    public function setDefaultOrderings(array $defaultOrderings)
505
-    {
506
-        throw new \BadMethodCallException('Repository does not support the setDefaultOrderings() method.', 1375805598);
507
-    }
508
-
509
-    /**
510
-     * Sets the default query settings to be used in this repository
511
-     *
512
-     * @param QuerySettingsInterface $defaultQuerySettings The query settings to be used by default
513
-     * @return void
514
-     * @api
515
-     */
516
-    public function setDefaultQuerySettings(QuerySettingsInterface $defaultQuerySettings)
517
-    {
518
-        $this->defaultQuerySettings = $defaultQuerySettings;
519
-    }
520
-
521
-    /**
522
-     * @return void
523
-     */
524
-    public function resetDefaultQuerySettings(): void
525
-    {
526
-        $this->defaultQuerySettings = null;
527
-    }
528
-
529
-
530
-    /**
531
-     * @return array
532
-     */
533
-    public function getErrorMessages(): array
534
-    {
535
-        return $this->errorMessages;
536
-    }
537
-
538
-    /**
539
-     * @param string $sourceFieldName
540
-     * @return $this
541
-     */
542
-    public function setSourceFieldName($sourceFieldName): self
543
-    {
544
-        $this->sourceFieldName = $sourceFieldName;
545
-        return $this;
546
-    }
547
-
548
-    /**
549
-     * @return string
550
-     */
551
-    public function getDataType(): string
552
-    {
553
-        return $this->dataType;
554
-    }
555
-
556
-    /**
557
-     * Tell whether the order has a foreign table in its expression, e.g. "metadata.title".
558
-     *
559
-     * @param string $ordering
560
-     * @return bool
561
-     */
562
-    protected function hasForeignRelationIn($ordering): bool
563
-    {
564
-        return strpos($ordering, '.') !== false;
565
-    }
566
-
567
-    /**
568
-     * Extract the foreign relation of the ordering "metadata.title" -> "metadata"
569
-     *
570
-     * @param string $ordering
571
-     * @return string
572
-     */
573
-    protected function getForeignRelationFrom($ordering): string
574
-    {
575
-        $parts = explode('.', $ordering);
576
-        return $parts[0];
577
-    }
578
-
579
-    /**
580
-     * Get the constraints
581
-     *
582
-     * @param Query $query
583
-     * @param Matcher $matcher
584
-     * @return ConstraintInterface|null
585
-     */
586
-    protected function computeConstraints(Query $query, Matcher $matcher): ?ConstraintInterface
587
-    {
588
-        $constraints = null;
589
-
590
-        $collectedConstraints = [];
591
-
592
-        // Search term
593
-        $constraint = $this->computeSearchTermConstraint($query, $matcher);
594
-        if ($constraint) {
595
-            $collectedConstraints[] = $constraint;
596
-        }
597
-
598
-        foreach ($matcher->getSupportedOperators() as $operator) {
599
-            $constraint = $this->computeConstraint($query, $matcher, $operator);
600
-            if ($constraint) {
601
-                $collectedConstraints[] = $constraint;
602
-            }
603
-        }
604
-
605
-        if (count($collectedConstraints) > 1) {
606
-            $logical = $matcher->getDefaultLogicalSeparator();
607
-            $constraints = $query->$logical($collectedConstraints);
608
-        } elseif (!empty($collectedConstraints)) {
609
-            // true means there is one constraint only and should become the result
610
-            $constraints = current($collectedConstraints);
611
-        }
612
-
613
-        // Trigger signal for post processing the computed constraints object.
614
-        $constraints = $this->emitPostProcessConstraintsSignal($query, $constraints);
615
-
616
-        return $constraints;
617
-    }
618
-
619
-    /**
620
-     * Computes the search constraint and returns it.
621
-     *
622
-     * @param Query $query
623
-     * @param Matcher $matcher
624
-     * @return ConstraintInterface|null
625
-     */
626
-    protected function computeSearchTermConstraint(Query $query, Matcher $matcher): ?ConstraintInterface
627
-    {
628
-        $result = null;
629
-
630
-        // Search term case
631
-        if ($matcher->getSearchTerm()) {
632
-            $fields = GeneralUtility::trimExplode(',', Tca::table($this->dataType)->getSearchFields(), true);
633
-
634
-            $constraints = [];
635
-            $likeClause = sprintf('%%%s%%', $matcher->getSearchTerm());
636
-            foreach ($fields as $fieldNameAndPath) {
637
-                if ($this->isSuitableForLike($fieldNameAndPath, $matcher->getSearchTerm())) {
638
-                    $dataType = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $this->dataType);
639
-                    $fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $this->dataType);
640
-
641
-                    if (Tca::table($dataType)->hasField($fieldName) && Tca::table($dataType)->field($fieldName)->hasRelation()) {
642
-                        $foreignTable = Tca::table($dataType)->field($fieldName)->getForeignTable();
643
-                        $fieldNameAndPath = $fieldNameAndPath . '.' . Tca::table($foreignTable)->getLabelField();
644
-                    }
645
-                    $constraints[] = $query->like($fieldNameAndPath, $likeClause);
646
-                }
647
-            }
648
-            $logical = $matcher->getLogicalSeparatorForSearchTerm();
649
-            $result = $query->$logical($constraints);
650
-        }
651
-
652
-        return $result;
653
-    }
654
-
655
-    /**
656
-     * It does not make sense to have a "like" in presence of numerical field, e.g "uid".
657
-     * Tell whether the given value makes sense for a "like" clause.
658
-     *
659
-     * @param string $fieldNameAndPath
660
-     * @param string $value
661
-     * @return bool
662
-     */
663
-    protected function isSuitableForLike($fieldNameAndPath, $value): bool
664
-    {
665
-        $isSuitable = true;
666
-
667
-        // true means it is a string
668
-        if (!MathUtility::canBeInterpretedAsInteger($value)) {
669
-            $dataType = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $this->dataType);
670
-            $fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $this->dataType);
671
-
672
-            if (Tca::table($dataType)->field($fieldName)->isNumerical()
673
-                && !Tca::table($dataType)->field($fieldName)->hasRelation()
674
-            ) {
675
-                $isSuitable = false;
676
-            }
677
-        }
678
-
679
-        return $isSuitable;
680
-    }
681
-
682
-    /**
683
-     * Computes the constraint for matches and returns it.
684
-     *
685
-     * @param Query $query
686
-     * @param Matcher $matcher
687
-     * @param string $operator
688
-     * @return ConstraintInterface|null
689
-     */
690
-    protected function computeConstraint(Query $query, Matcher $matcher, $operator): ?ConstraintInterface
691
-    {
692
-        $result = null;
693
-
694
-        $operatorName = ucfirst($operator);
695
-        $getCriteria = sprintf('get%s', $operatorName);
696
-        $criteria = $matcher->$getCriteria();
697
-
698
-        if (!empty($criteria)) {
699
-            $constraints = [];
700
-
701
-            foreach ($criteria as $criterion) {
702
-                $fieldNameAndPath = $criterion['fieldNameAndPath'];
703
-                $operand = $criterion['operand'];
704
-
705
-                // Compute a few variables...
706
-                // $dataType is generally equals to $this->dataType but not always... if fieldName is a path.
707
-                $dataType = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $this->dataType);
708
-                $fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $this->dataType);
709
-                $fieldPath = $this->getFieldPathResolver()->stripFieldName($fieldNameAndPath, $this->dataType);
710
-
711
-                if (Tca::table($dataType)->field($fieldName)->hasRelation()) {
712
-                    if (MathUtility::canBeInterpretedAsInteger($operand)) {
713
-                        $fieldNameAndPath = $fieldName . '.uid';
714
-                    } else {
715
-                        $foreignTableName = Tca::table($dataType)->field($fieldName)->getForeignTable();
716
-                        $foreignTable = Tca::table($foreignTableName);
717
-                        $fieldNameAndPath = $fieldName . '.' . $foreignTable->getLabelField();
718
-                    }
719
-
720
-                    // If different means we should restore the prepended path segment for proper SQL parser.
721
-                    // This is true for a composite field, e.g items.sys_file_metadata for categories.
722
-                    if ($fieldName !== $fieldPath) {
723
-                        $fieldNameAndPath = $fieldPath . '.' . $fieldNameAndPath;
724
-                    }
725
-                }
726
-
727
-                if (strpos($operator, 'not') === 0) {
728
-                    $strippedOperator = strtolower(substr($operator, 3));
729
-                    $constraints[] = $query->logicalNot($query->$strippedOperator($fieldNameAndPath, $criterion['operand']));
730
-                } else {
731
-                    $constraints[] = $query->$operator($fieldNameAndPath, $criterion['operand']);
732
-                }
733
-            }
734
-
735
-            $getLogicalSeparator = sprintf('getLogicalSeparatorFor%s', $operatorName);
736
-            $logical = method_exists($matcher, $getLogicalSeparator)
737
-                ? $matcher->$getLogicalSeparator()
738
-                : $matcher->getDefaultLogicalSeparator();
739
-
740
-            $result = $query->$logical($constraints);
741
-        }
742
-
743
-        return $result;
744
-    }
745
-
746
-    /**
747
-     * @return DataHandler
748
-     */
749
-    protected function getDataHandler(): DataHandler
750
-    {
751
-        if (!$this->dataHandler) {
752
-            $this->dataHandler = GeneralUtility::makeInstance(DataHandler::class);
753
-        }
754
-        return $this->dataHandler;
755
-    }
756
-
757
-    /**
758
-     * Handle the magic call by properly creating a Query object and returning its result.
759
-     *
760
-     * @param string $propertyName
761
-     * @param string $value
762
-     * @param string $flag
763
-     * @return mixed
764
-     */
765
-    protected function processMagicCall($propertyName, $value, $flag = '')
766
-    {
767
-        $fieldName = Property::name($propertyName)->of($this->dataType)->toFieldName();
768
-
769
-        /** @var $matcher Matcher */
770
-        $matcher = GeneralUtility::makeInstance(Matcher::class, [], $this->getDataType());
771
-
772
-        $table = Tca::table($this->dataType);
773
-        if ($table->field($fieldName)->isGroup()) {
774
-            $valueParts = explode('.', $value, 2);
775
-            $fieldName = $fieldName . '.' . $valueParts[0];
776
-            $value = $valueParts[1];
777
-        }
778
-
779
-        $matcher->equals($fieldName, $value);
780
-
781
-        if ($flag === 'count') {
782
-            $result = $this->countBy($matcher);
783
-        } else {
784
-            $result = $this->findBy($matcher);
785
-        }
786
-        return $flag === 'one' && !empty($result) ? reset($result) : $result;
787
-    }
788
-
789
-    /**
790
-     * @return DataHandlerFactory|object
791
-     */
792
-    protected function getDataHandlerFactory()
793
-    {
794
-        return GeneralUtility::makeInstance(DataHandlerFactory::class);
795
-    }
796
-
797
-    protected function isBackendMode(): bool
798
-    {
799
-        return Typo3Mode::isBackendMode();
800
-    }
801
-
802
-    /**
803
-     * @return FieldPathResolver|object
804
-     */
805
-    protected function getFieldPathResolver()
806
-    {
807
-        return GeneralUtility::makeInstance(FieldPathResolver::class);
808
-    }
809
-
810
-    /**
811
-     * @return ContentValidator|object
812
-     */
813
-    protected function getContentValidator(): ContentValidator
814
-    {
815
-        return GeneralUtility::makeInstance(ContentValidator::class);
816
-    }
817
-
818
-    /**
819
-     * @return LanguageValidator|object
820
-     */
821
-    protected function getLanguageValidator(): LanguageValidator
822
-    {
823
-        return GeneralUtility::makeInstance(LanguageValidator::class);
824
-    }
825
-
826
-    /**
827
-     * Signal that is called for post-processing the computed constraints object.
828
-     *
829
-     * @param Query $query
830
-     * @param ConstraintInterface|null $constraints
831
-     * @return ConstraintInterface|null $constraints
832
-     */
833
-    protected function emitPostProcessConstraintsSignal(Query $query, $constraints): ?ConstraintInterface
834
-    {
835
-        /** @var ConstraintContainer $constraintContainer */
836
-        $constraintContainer = GeneralUtility::makeInstance(ConstraintContainer::class);
837
-        $result = $this->getSignalSlotDispatcher()->dispatch(
838
-            self::class,
839
-            'postProcessConstraintsObject',
840
-            [
841
-                $query,
842
-                $constraints,
843
-                $constraintContainer
844
-            ]
845
-        );
846
-
847
-        // Backward compatibility.
848
-        $processedConstraints = $result[1];
849
-
850
-        // New way to transmit the constraints.
851
-        if ($constraintContainer->getConstraint()) {
852
-            $processedConstraints = $constraintContainer->getConstraint();
853
-        }
854
-        return $processedConstraints;
855
-    }
856
-
857
-    /**
858
-     * @return Dispatcher
859
-     */
860
-    protected function getSignalSlotDispatcher(): Dispatcher
861
-    {
862
-        return GeneralUtility::makeInstance(Dispatcher::class);
863
-    }
41
+	/**
42
+	 * Tell whether it is a raw result (array) or object being returned.
43
+	 *
44
+	 * @var bool
45
+	 */
46
+	protected $rawResult = false;
47
+
48
+	/**
49
+	 * The data type to be returned, e.g fe_users, fe_groups, tt_content, etc...
50
+	 *
51
+	 * @var string
52
+	 */
53
+	protected $dataType;
54
+
55
+	/**
56
+	 * The source field is useful in the context of MM relations to know who is the caller
57
+	 * e.g findByItems which eventually corresponds to a field name.
58
+	 *
59
+	 * @var string
60
+	 */
61
+	protected $sourceFieldName = '';
62
+
63
+	/**
64
+	 * @var array
65
+	 */
66
+	protected $errorMessages = [];
67
+
68
+	/**
69
+	 * @var QuerySettingsInterface
70
+	 */
71
+	protected $defaultQuerySettings;
72
+
73
+	/**
74
+	 * @var DataHandler
75
+	 */
76
+	protected $dataHandler;
77
+
78
+	/**
79
+	 * Constructor
80
+	 *
81
+	 * @param string $dataType
82
+	 */
83
+	public function __construct($dataType)
84
+	{
85
+		$this->dataType = $dataType;
86
+	}
87
+
88
+	/**
89
+	 * Returns all objects of this repository.
90
+	 *
91
+	 * @return Content[]
92
+	 */
93
+	public function findAll()
94
+	{
95
+		$query = $this->createQuery();
96
+		return $query->execute();
97
+	}
98
+
99
+	/**
100
+	 * Returns all "distinct" values for a given property.
101
+	 *
102
+	 * @param string $propertyName
103
+	 * @param Matcher $matcher
104
+	 * @param Order|null $order
105
+	 * @return Content[]
106
+	 */
107
+	public function findDistinctValues($propertyName, Matcher $matcher = null, Order $order = null): array
108
+	{
109
+		$query = $this->createQuery();
110
+		$query->setDistinct($propertyName);
111
+
112
+		// Remove empty values from selection.
113
+		$constraint = $query->logicalNot($query->equals($propertyName, ''));
114
+
115
+		// Add some additional constraints from the Matcher object.
116
+		$matcherConstraint = null;
117
+		if ($matcher !== null) {
118
+			$matcherConstraint = $this->computeConstraints($query, $matcher);
119
+		}
120
+
121
+		// Assemble the final constraints or not.
122
+		if ($matcherConstraint) {
123
+			$query->logicalAnd([$matcherConstraint, $constraint]);
124
+			$query->matching($query->logicalAnd([$matcherConstraint, $constraint]));
125
+		} else {
126
+			$query->matching($constraint);
127
+		}
128
+
129
+		if ($order) {
130
+			$query->setOrderings($order->getOrderings());
131
+		}
132
+
133
+		return $query->execute();
134
+	}
135
+
136
+	/**
137
+	 * Returns all "distinct" values for a given property.
138
+	 *
139
+	 * @param string $propertyName
140
+	 * @param Matcher $matcher
141
+	 * @return int
142
+	 */
143
+	public function countDistinctValues($propertyName, Matcher $matcher = null): int
144
+	{
145
+		$query = $this->createQuery();
146
+		$query->setDistinct($propertyName);
147
+
148
+		// Remove empty values from selection.
149
+		$constraint = $query->logicalNot($query->equals($propertyName, ''));
150
+
151
+		// Add some additional constraints from the Matcher object.
152
+		$matcherConstraint = null;
153
+		if (!is_null($matcher)) {
154
+			$matcherConstraint = $this->computeConstraints($query, $matcher);
155
+		}
156
+
157
+		// Assemble the final constraints or not.
158
+		if ($matcherConstraint) {
159
+			$query->logicalAnd([$matcherConstraint, $constraint]);
160
+			$query->matching($query->logicalAnd([$matcherConstraint, $constraint]));
161
+		} else {
162
+			$query->matching($constraint);
163
+		}
164
+
165
+		return $query->count();
166
+	}
167
+
168
+	/**
169
+	 * Finds an object matching the given identifier.
170
+	 *
171
+	 * @param int $uid The identifier of the object to find
172
+	 * @return Content|null
173
+	 * @api
174
+	 */
175
+	public function findByUid($uid)
176
+	{
177
+		return $this->findByIdentifier($uid);
178
+	}
179
+
180
+	/**
181
+	 * Finds all Contents given specified matches.
182
+	 *
183
+	 * @param string $propertyName
184
+	 * @param array $values
185
+	 * @return Content[]
186
+	 */
187
+	public function findIn($propertyName, array $values): array
188
+	{
189
+		$query = $this->createQuery();
190
+		$query->matching($query->in($propertyName, $values));
191
+		return $query->execute();
192
+	}
193
+
194
+	/**
195
+	 * Finds all Contents given specified matches.
196
+	 *
197
+	 * @param Matcher $matcher
198
+	 * @param Order $order The order
199
+	 * @param int $limit
200
+	 * @param int $offset
201
+	 * @return Content[]
202
+	 */
203
+	public function findBy(Matcher $matcher, Order $order = null, $limit = null, $offset = null): array
204
+	{
205
+		$query = $this->createQuery();
206
+
207
+		$limit = (int)$limit; // make sure to cast
208
+		if ($limit > 0) {
209
+			$query->setLimit($limit);
210
+		}
211
+
212
+		if ($order) {
213
+			$query->setOrderings($order->getOrderings());
214
+
215
+			// Loops around the orderings adding if necessary a dummy condition
216
+			// to make sure the relations can be resolved when transforming the query to plain SQL.
217
+			foreach ($order->getOrderings() as $ordering => $direction) {
218
+				if ($this->hasForeignRelationIn($ordering)) {
219
+					$relationalField = $this->getForeignRelationFrom($ordering);
220
+					$matcher->like($relationalField . '.uid', '');
221
+				}
222
+			}
223
+		}
224
+
225
+		if ($offset) {
226
+			$query->setOffset($offset);
227
+		}
228
+
229
+		$constraints = $this->computeConstraints($query, $matcher);
230
+
231
+		if ($constraints) {
232
+			$query->matching($constraints);
233
+		}
234
+
235
+		return $query->execute();
236
+	}
237
+
238
+	/**
239
+	 * Find one Content object given specified matches.
240
+	 *
241
+	 * @param Matcher $matcher
242
+	 * @return Content
243
+	 */
244
+	public function findOneBy(Matcher $matcher): Content
245
+	{
246
+		$query = $this->createQuery();
247
+
248
+		$constraints = $this->computeConstraints($query, $matcher);
249
+
250
+		if ($constraints) {
251
+			$query->matching($constraints);
252
+		}
253
+
254
+		$query->setLimit(1); // only take one!
255
+
256
+		$resultSet = $query->execute();
257
+		if ($resultSet) {
258
+			$resultSet = current($resultSet);
259
+		}
260
+		return $resultSet;
261
+	}
262
+
263
+	/**
264
+	 * Count all Contents given specified matches.
265
+	 *
266
+	 * @param Matcher $matcher
267
+	 * @return int
268
+	 */
269
+	public function countBy(Matcher $matcher): int
270
+	{
271
+		$query = $this->createQuery();
272
+
273
+		$constraints = $this->computeConstraints($query, $matcher);
274
+
275
+		if ($constraints) {
276
+			$query->matching($constraints);
277
+		}
278
+
279
+		return $query->count();
280
+	}
281
+
282
+	/**
283
+	 * Update a content with new information.
284
+	 *
285
+	 * @param Content $content
286
+	 * @param $language
287
+	 * @return bool
288
+	 */
289
+	public function localize($content, $language): bool
290
+	{
291
+		// Security check
292
+		$this->getContentValidator()->validate($content);
293
+		$this->getLanguageValidator()->validate($language);
294
+
295
+		$dataType = $content->getDataType();
296
+		$handler = $this->getDataHandlerFactory()->action(ProcessAction::LOCALIZE)->forType($dataType)->getDataHandler();
297
+
298
+		$handlerResult = $handler->processLocalize($content, $language);
299
+		$this->errorMessages = $handler->getErrorMessages();
300
+		return $handlerResult;
301
+	}
302
+
303
+	/**
304
+	 * Update a content with new information.
305
+	 *
306
+	 * @param Content $content
307
+	 * @return bool
308
+	 */
309
+	public function update($content)
310
+	{
311
+		// Security check.
312
+		$this->getContentValidator()->validate($content);
313
+
314
+		$dataType = $content->getDataType();
315
+		$handler = $this->getDataHandlerFactory()->action(ProcessAction::UPDATE)->forType($dataType)->getDataHandler();
316
+
317
+		$handlerResult = $handler->processUpdate($content);
318
+		$this->errorMessages = $handler->getErrorMessages();
319
+		return $handlerResult;
320
+	}
321
+
322
+	/**
323
+	 * Removes an object from this repository.
324
+	 *
325
+	 * @param Content $content
326
+	 * @return boolean
327
+	 */
328
+	public function remove($content)
329
+	{
330
+		$dataType = $content->getDataType();
331
+		$handler = $this->getDataHandlerFactory()->action(ProcessAction::REMOVE)->forType($dataType)->getDataHandler();
332
+
333
+		$handlerResult = $handler->processRemove($content);
334
+		$this->errorMessages = $handler->getErrorMessages();
335
+		return $handlerResult;
336
+	}
337
+
338
+	/**
339
+	 * Move a content within this repository.
340
+	 * The $target corresponds to the pid to move the records to.
341
+	 * It can also be a negative value in case of sorting. The negative value would be the uid of its predecessor.
342
+	 *
343
+	 * @param Content $content
344
+	 * @param string $target
345
+	 * @return bool
346
+	 */
347
+	public function move($content, $target): bool
348
+	{
349
+		// Security check.
350
+		$this->getContentValidator()->validate($content);
351
+
352
+		$dataType = $content->getDataType();
353
+		$handler = $this->getDataHandlerFactory()->action(ProcessAction::MOVE)->forType($dataType)->getDataHandler();
354
+
355
+		$handlerResult = $handler->processMove($content, $target);
356
+		$this->errorMessages = $handler->getErrorMessages();
357
+		return $handlerResult;
358
+	}
359
+
360
+	/**
361
+	 * Copy a content within this repository.
362
+	 *
363
+	 * @param Content $content
364
+	 * @return bool
365
+	 */
366
+	public function copy($content, $target): bool
367
+	{
368
+		// Security check.
369
+		$this->getContentValidator()->validate($content);
370
+
371
+		$dataType = $content->getDataType();
372
+		$handler = $this->getDataHandlerFactory()->action(ProcessAction::COPY)->forType($dataType)->getDataHandler();
373
+
374
+		$handlerResult = $handler->processCopy($content, $target);
375
+		$this->errorMessages = $handler->getErrorMessages();
376
+		return $handlerResult;
377
+	}
378
+
379
+	/**
380
+	 * Adds an object to this repository.
381
+	 *
382
+	 * @param object $object The object to add
383
+	 * @return void
384
+	 * @api
385
+	 */
386
+	public function add($object)
387
+	{
388
+		throw new \BadMethodCallException('Repository does not support the add() method.', 1375805599);
389
+	}
390
+
391
+	/**
392
+	 * Returns the total number objects of this repository.
393
+	 *
394
+	 * @return integer The object count
395
+	 * @api
396
+	 */
397
+	public function countAll()
398
+	{
399
+		$query = $this->createQuery();
400
+		return $query->count();
401
+	}
402
+
403
+	/**
404
+	 * Removes all objects of this repository as if remove() was called for
405
+	 * all of them.
406
+	 *
407
+	 * @return void
408
+	 * @api
409
+	 */
410
+	public function removeAll()
411
+	{
412
+		// TODO: Implement removeAll() method.
413
+	}
414
+
415
+	/**
416
+	 * Finds an object matching the given identifier.
417
+	 *
418
+	 * @param mixed $identifier The identifier of the object to find
419
+	 * @return Content|null
420
+	 * @api
421
+	 */
422
+	public function findByIdentifier($identifier)
423
+	{
424
+		$query = $this->createQuery();
425
+
426
+		$result = $query->matching(
427
+			$query->equals('uid', $identifier)
428
+		)
429
+			->execute();
430
+
431
+		if (is_array($result)) {
432
+			$result = current($result);
433
+		}
434
+
435
+		return $result;
436
+	}
437
+
438
+	/**
439
+	 * Dispatches magic methods (findBy[Property]())
440
+	 *
441
+	 * @param string $methodName The name of the magic method
442
+	 * @param string $arguments The arguments of the magic method
443
+	 * @return mixed
444
+	 * @api
445
+	 */
446
+	public function __call($methodName, $arguments)
447
+	{
448
+		if (substr($methodName, 0, 6) === 'findBy' && strlen($methodName) > 7) {
449
+			$propertyName = strtolower(substr(substr($methodName, 6), 0, 1)) . substr(substr($methodName, 6), 1);
450
+			$result = $this->processMagicCall($propertyName, $arguments[0]);
451
+		} elseif (substr($methodName, 0, 9) === 'findOneBy' && strlen($methodName) > 10) {
452
+			$propertyName = strtolower(substr(substr($methodName, 9), 0, 1)) . substr(substr($methodName, 9), 1);
453
+			$result = $this->processMagicCall($propertyName, $arguments[0], 'one');
454
+		} elseif (substr($methodName, 0, 7) === 'countBy' && strlen($methodName) > 8) {
455
+			$propertyName = strtolower(substr(substr($methodName, 7), 0, 1)) . substr(substr($methodName, 7), 1);
456
+			$result = $this->processMagicCall($propertyName, $arguments[0], 'count');
457
+		} else {
458
+			throw new UnsupportedMethodException('The method "' . $methodName . '" is not supported by the repository.', 1360838010);
459
+		}
460
+		return $result;
461
+	}
462
+
463
+	/**
464
+	 * Returns a query for objects of this repository
465
+	 *
466
+	 * @return Query
467
+	 * @api
468
+	 */
469
+	public function createQuery()
470
+	{
471
+		/** @var Query $query */
472
+		$query = GeneralUtility::makeInstance(Query::class, $this->dataType);
473
+		$query->setSourceFieldName($this->sourceFieldName);
474
+
475
+		if ($this->defaultQuerySettings) {
476
+			$query->setTypo3QuerySettings($this->defaultQuerySettings);
477
+		} else {
478
+			// Initialize and pass the query settings at this level.
479
+			$querySettings = GeneralUtility::makeInstance(Typo3QuerySettings::class);
480
+
481
+			// Default choice for the BE.
482
+			if ($this->isBackendMode()) {
483
+				$querySettings->setIgnoreEnableFields(true);
484
+			}
485
+
486
+			$query->setTypo3QuerySettings($querySettings);
487
+		}
488
+
489
+		return $query;
490
+	}
491
+
492
+	/**
493
+	 * Sets the property names to order the result by per default.
494
+	 * Expected like this:
495
+	 * array(
496
+	 * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
497
+	 * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
498
+	 * )
499
+	 *
500
+	 * @param array $defaultOrderings The property names to order by
501
+	 * @return void
502
+	 * @api
503
+	 */
504
+	public function setDefaultOrderings(array $defaultOrderings)
505
+	{
506
+		throw new \BadMethodCallException('Repository does not support the setDefaultOrderings() method.', 1375805598);
507
+	}
508
+
509
+	/**
510
+	 * Sets the default query settings to be used in this repository
511
+	 *
512
+	 * @param QuerySettingsInterface $defaultQuerySettings The query settings to be used by default
513
+	 * @return void
514
+	 * @api
515
+	 */
516
+	public function setDefaultQuerySettings(QuerySettingsInterface $defaultQuerySettings)
517
+	{
518
+		$this->defaultQuerySettings = $defaultQuerySettings;
519
+	}
520
+
521
+	/**
522
+	 * @return void
523
+	 */
524
+	public function resetDefaultQuerySettings(): void
525
+	{
526
+		$this->defaultQuerySettings = null;
527
+	}
528
+
529
+
530
+	/**
531
+	 * @return array
532
+	 */
533
+	public function getErrorMessages(): array
534
+	{
535
+		return $this->errorMessages;
536
+	}
537
+
538
+	/**
539
+	 * @param string $sourceFieldName
540
+	 * @return $this
541
+	 */
542
+	public function setSourceFieldName($sourceFieldName): self
543
+	{
544
+		$this->sourceFieldName = $sourceFieldName;
545
+		return $this;
546
+	}
547
+
548
+	/**
549
+	 * @return string
550
+	 */
551
+	public function getDataType(): string
552
+	{
553
+		return $this->dataType;
554
+	}
555
+
556
+	/**
557
+	 * Tell whether the order has a foreign table in its expression, e.g. "metadata.title".
558
+	 *
559
+	 * @param string $ordering
560
+	 * @return bool
561
+	 */
562
+	protected function hasForeignRelationIn($ordering): bool
563
+	{
564
+		return strpos($ordering, '.') !== false;
565
+	}
566
+
567
+	/**
568
+	 * Extract the foreign relation of the ordering "metadata.title" -> "metadata"
569
+	 *
570
+	 * @param string $ordering
571
+	 * @return string
572
+	 */
573
+	protected function getForeignRelationFrom($ordering): string
574
+	{
575
+		$parts = explode('.', $ordering);
576
+		return $parts[0];
577
+	}
578
+
579
+	/**
580
+	 * Get the constraints
581
+	 *
582
+	 * @param Query $query
583
+	 * @param Matcher $matcher
584
+	 * @return ConstraintInterface|null
585
+	 */
586
+	protected function computeConstraints(Query $query, Matcher $matcher): ?ConstraintInterface
587
+	{
588
+		$constraints = null;
589
+
590
+		$collectedConstraints = [];
591
+
592
+		// Search term
593
+		$constraint = $this->computeSearchTermConstraint($query, $matcher);
594
+		if ($constraint) {
595
+			$collectedConstraints[] = $constraint;
596
+		}
597
+
598
+		foreach ($matcher->getSupportedOperators() as $operator) {
599
+			$constraint = $this->computeConstraint($query, $matcher, $operator);
600
+			if ($constraint) {
601
+				$collectedConstraints[] = $constraint;
602
+			}
603
+		}
604
+
605
+		if (count($collectedConstraints) > 1) {
606
+			$logical = $matcher->getDefaultLogicalSeparator();
607
+			$constraints = $query->$logical($collectedConstraints);
608
+		} elseif (!empty($collectedConstraints)) {
609
+			// true means there is one constraint only and should become the result
610
+			$constraints = current($collectedConstraints);
611
+		}
612
+
613
+		// Trigger signal for post processing the computed constraints object.
614
+		$constraints = $this->emitPostProcessConstraintsSignal($query, $constraints);
615
+
616
+		return $constraints;
617
+	}
618
+
619
+	/**
620
+	 * Computes the search constraint and returns it.
621
+	 *
622
+	 * @param Query $query
623
+	 * @param Matcher $matcher
624
+	 * @return ConstraintInterface|null
625
+	 */
626
+	protected function computeSearchTermConstraint(Query $query, Matcher $matcher): ?ConstraintInterface
627
+	{
628
+		$result = null;
629
+
630
+		// Search term case
631
+		if ($matcher->getSearchTerm()) {
632
+			$fields = GeneralUtility::trimExplode(',', Tca::table($this->dataType)->getSearchFields(), true);
633
+
634
+			$constraints = [];
635
+			$likeClause = sprintf('%%%s%%', $matcher->getSearchTerm());
636
+			foreach ($fields as $fieldNameAndPath) {
637
+				if ($this->isSuitableForLike($fieldNameAndPath, $matcher->getSearchTerm())) {
638
+					$dataType = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $this->dataType);
639
+					$fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $this->dataType);
640
+
641
+					if (Tca::table($dataType)->hasField($fieldName) && Tca::table($dataType)->field($fieldName)->hasRelation()) {
642
+						$foreignTable = Tca::table($dataType)->field($fieldName)->getForeignTable();
643
+						$fieldNameAndPath = $fieldNameAndPath . '.' . Tca::table($foreignTable)->getLabelField();
644
+					}
645
+					$constraints[] = $query->like($fieldNameAndPath, $likeClause);
646
+				}
647
+			}
648
+			$logical = $matcher->getLogicalSeparatorForSearchTerm();
649
+			$result = $query->$logical($constraints);
650
+		}
651
+
652
+		return $result;
653
+	}
654
+
655
+	/**
656
+	 * It does not make sense to have a "like" in presence of numerical field, e.g "uid".
657
+	 * Tell whether the given value makes sense for a "like" clause.
658
+	 *
659
+	 * @param string $fieldNameAndPath
660
+	 * @param string $value
661
+	 * @return bool
662
+	 */
663
+	protected function isSuitableForLike($fieldNameAndPath, $value): bool
664
+	{
665
+		$isSuitable = true;
666
+
667
+		// true means it is a string
668
+		if (!MathUtility::canBeInterpretedAsInteger($value)) {
669
+			$dataType = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $this->dataType);
670
+			$fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $this->dataType);
671
+
672
+			if (Tca::table($dataType)->field($fieldName)->isNumerical()
673
+				&& !Tca::table($dataType)->field($fieldName)->hasRelation()
674
+			) {
675
+				$isSuitable = false;
676
+			}
677
+		}
678
+
679
+		return $isSuitable;
680
+	}
681
+
682
+	/**
683
+	 * Computes the constraint for matches and returns it.
684
+	 *
685
+	 * @param Query $query
686
+	 * @param Matcher $matcher
687
+	 * @param string $operator
688
+	 * @return ConstraintInterface|null
689
+	 */
690
+	protected function computeConstraint(Query $query, Matcher $matcher, $operator): ?ConstraintInterface
691
+	{
692
+		$result = null;
693
+
694
+		$operatorName = ucfirst($operator);
695
+		$getCriteria = sprintf('get%s', $operatorName);
696
+		$criteria = $matcher->$getCriteria();
697
+
698
+		if (!empty($criteria)) {
699
+			$constraints = [];
700
+
701
+			foreach ($criteria as $criterion) {
702
+				$fieldNameAndPath = $criterion['fieldNameAndPath'];
703
+				$operand = $criterion['operand'];
704
+
705
+				// Compute a few variables...
706
+				// $dataType is generally equals to $this->dataType but not always... if fieldName is a path.
707
+				$dataType = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $this->dataType);
708
+				$fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $this->dataType);
709
+				$fieldPath = $this->getFieldPathResolver()->stripFieldName($fieldNameAndPath, $this->dataType);
710
+
711
+				if (Tca::table($dataType)->field($fieldName)->hasRelation()) {
712
+					if (MathUtility::canBeInterpretedAsInteger($operand)) {
713
+						$fieldNameAndPath = $fieldName . '.uid';
714
+					} else {
715
+						$foreignTableName = Tca::table($dataType)->field($fieldName)->getForeignTable();
716
+						$foreignTable = Tca::table($foreignTableName);
717
+						$fieldNameAndPath = $fieldName . '.' . $foreignTable->getLabelField();
718
+					}
719
+
720
+					// If different means we should restore the prepended path segment for proper SQL parser.
721
+					// This is true for a composite field, e.g items.sys_file_metadata for categories.
722
+					if ($fieldName !== $fieldPath) {
723
+						$fieldNameAndPath = $fieldPath . '.' . $fieldNameAndPath;
724
+					}
725
+				}
726
+
727
+				if (strpos($operator, 'not') === 0) {
728
+					$strippedOperator = strtolower(substr($operator, 3));
729
+					$constraints[] = $query->logicalNot($query->$strippedOperator($fieldNameAndPath, $criterion['operand']));
730
+				} else {
731
+					$constraints[] = $query->$operator($fieldNameAndPath, $criterion['operand']);
732
+				}
733
+			}
734
+
735
+			$getLogicalSeparator = sprintf('getLogicalSeparatorFor%s', $operatorName);
736
+			$logical = method_exists($matcher, $getLogicalSeparator)
737
+				? $matcher->$getLogicalSeparator()
738
+				: $matcher->getDefaultLogicalSeparator();
739
+
740
+			$result = $query->$logical($constraints);
741
+		}
742
+
743
+		return $result;
744
+	}
745
+
746
+	/**
747
+	 * @return DataHandler
748
+	 */
749
+	protected function getDataHandler(): DataHandler
750
+	{
751
+		if (!$this->dataHandler) {
752
+			$this->dataHandler = GeneralUtility::makeInstance(DataHandler::class);
753
+		}
754
+		return $this->dataHandler;
755
+	}
756
+
757
+	/**
758
+	 * Handle the magic call by properly creating a Query object and returning its result.
759
+	 *
760
+	 * @param string $propertyName
761
+	 * @param string $value
762
+	 * @param string $flag
763
+	 * @return mixed
764
+	 */
765
+	protected function processMagicCall($propertyName, $value, $flag = '')
766
+	{
767
+		$fieldName = Property::name($propertyName)->of($this->dataType)->toFieldName();
768
+
769
+		/** @var $matcher Matcher */
770
+		$matcher = GeneralUtility::makeInstance(Matcher::class, [], $this->getDataType());
771
+
772
+		$table = Tca::table($this->dataType);
773
+		if ($table->field($fieldName)->isGroup()) {
774
+			$valueParts = explode('.', $value, 2);
775
+			$fieldName = $fieldName . '.' . $valueParts[0];
776
+			$value = $valueParts[1];
777
+		}
778
+
779
+		$matcher->equals($fieldName, $value);
780
+
781
+		if ($flag === 'count') {
782
+			$result = $this->countBy($matcher);
783
+		} else {
784
+			$result = $this->findBy($matcher);
785
+		}
786
+		return $flag === 'one' && !empty($result) ? reset($result) : $result;
787
+	}
788
+
789
+	/**
790
+	 * @return DataHandlerFactory|object
791
+	 */
792
+	protected function getDataHandlerFactory()
793
+	{
794
+		return GeneralUtility::makeInstance(DataHandlerFactory::class);
795
+	}
796
+
797
+	protected function isBackendMode(): bool
798
+	{
799
+		return Typo3Mode::isBackendMode();
800
+	}
801
+
802
+	/**
803
+	 * @return FieldPathResolver|object
804
+	 */
805
+	protected function getFieldPathResolver()
806
+	{
807
+		return GeneralUtility::makeInstance(FieldPathResolver::class);
808
+	}
809
+
810
+	/**
811
+	 * @return ContentValidator|object
812
+	 */
813
+	protected function getContentValidator(): ContentValidator
814
+	{
815
+		return GeneralUtility::makeInstance(ContentValidator::class);
816
+	}
817
+
818
+	/**
819
+	 * @return LanguageValidator|object
820
+	 */
821
+	protected function getLanguageValidator(): LanguageValidator
822
+	{
823
+		return GeneralUtility::makeInstance(LanguageValidator::class);
824
+	}
825
+
826
+	/**
827
+	 * Signal that is called for post-processing the computed constraints object.
828
+	 *
829
+	 * @param Query $query
830
+	 * @param ConstraintInterface|null $constraints
831
+	 * @return ConstraintInterface|null $constraints
832
+	 */
833
+	protected function emitPostProcessConstraintsSignal(Query $query, $constraints): ?ConstraintInterface
834
+	{
835
+		/** @var ConstraintContainer $constraintContainer */
836
+		$constraintContainer = GeneralUtility::makeInstance(ConstraintContainer::class);
837
+		$result = $this->getSignalSlotDispatcher()->dispatch(
838
+			self::class,
839
+			'postProcessConstraintsObject',
840
+			[
841
+				$query,
842
+				$constraints,
843
+				$constraintContainer
844
+			]
845
+		);
846
+
847
+		// Backward compatibility.
848
+		$processedConstraints = $result[1];
849
+
850
+		// New way to transmit the constraints.
851
+		if ($constraintContainer->getConstraint()) {
852
+			$processedConstraints = $constraintContainer->getConstraint();
853
+		}
854
+		return $processedConstraints;
855
+	}
856
+
857
+	/**
858
+	 * @return Dispatcher
859
+	 */
860
+	protected function getSignalSlotDispatcher(): Dispatcher
861
+	{
862
+		return GeneralUtility::makeInstance(Dispatcher::class);
863
+	}
864 864
 }
Please login to merge, or discard this patch.
Classes/Persistence/MatcherObjectFactory.php 1 patch
Indentation   +250 added lines, -250 removed lines patch added patch discarded remove patch
@@ -26,254 +26,254 @@
 block discarded – undo
26 26
  */
27 27
 class MatcherObjectFactory implements SingletonInterface
28 28
 {
29
-    /**
30
-     * Gets a singleton instance of this class.
31
-     *
32
-     * @return $this
33
-     */
34
-    public static function getInstance(): self
35
-    {
36
-        return GeneralUtility::makeInstance(self::class);
37
-    }
38
-
39
-    /**
40
-     * Returns a matcher object.
41
-     *
42
-     * @param array $matches
43
-     * @param string $dataType
44
-     * @return Matcher
45
-     */
46
-    public function getMatcher(array $matches = [], $dataType = ''): Matcher
47
-    {
48
-        if ($dataType === '') {
49
-            $dataType = $this->getModuleLoader()->getDataType();
50
-        }
51
-
52
-        /** @var $matcher Matcher */
53
-        $matcher = GeneralUtility::makeInstance(Matcher::class, [], $dataType);
54
-
55
-        $matcher = $this->applyCriteriaFromDataTables($matcher);
56
-        $matcher = $this->applyCriteriaFromMatchesArgument($matcher, $matches);
57
-
58
-        if ($this->isBackendMode()) {
59
-            $matcher = $this->applyCriteriaFromUrl($matcher);
60
-            $matcher = $this->applyCriteriaFromTSConfig($matcher);
61
-        }
62
-
63
-        // Trigger signal for post processing Matcher Object.
64
-        $this->emitPostProcessMatcherObjectSignal($matcher);
65
-
66
-        return $matcher;
67
-    }
68
-
69
-    /**
70
-     * Get a possible id from the URL and apply as filter criteria.
71
-     * Except if the main module belongs to the File. The id would be a combined identifier
72
-     * including the storage and a mount point.
73
-     *
74
-     * @param Matcher $matcher
75
-     * @return Matcher $matcher
76
-     */
77
-    protected function applyCriteriaFromUrl(Matcher $matcher): Matcher
78
-    {
79
-        if (GeneralUtility::_GP('id')
80
-            && !$this->getModuleLoader()->isPidIgnored()
81
-            && $this->getModuleLoader()->getMainModule() !== ModuleName::FILE) {
82
-            $matcher->equals('pid', GeneralUtility::_GP('id'));
83
-        }
84
-
85
-        return $matcher;
86
-    }
87
-
88
-    /**
89
-     * @param Matcher $matcher
90
-     * @return Matcher $matcher
91
-     */
92
-    protected function applyCriteriaFromTSConfig(Matcher $matcher): Matcher
93
-    {
94
-        $dataType = $matcher->getDataType();
95
-        $tsConfigPath = sprintf('tx_vidi.dataType.%s.constraints', $dataType);
96
-        $tsConfig = $this->getBackendUser()->getTSConfig($tsConfigPath);
97
-
98
-        if (is_array($tsConfig['properties']) && !empty($tsConfig['properties'])) {
99
-            foreach ($tsConfig['properties'] as $constraint) {
100
-                if (preg_match('/(.+) (>=|>|<|<=|=|like) (.+)/is', $constraint, $matches) && count($matches) === 4) {
101
-                    $operator = $matcher->getSupportedOperators()[strtolower(trim($matches[2]))];
102
-                    $operand = trim($matches[1]);
103
-                    $value = trim($matches[3]);
104
-
105
-                    $matcher->$operator($operand, $value);
106
-                } elseif (preg_match('/(.+) (in) (.+)/is', $constraint, $matches) && count($matches) === 4) {
107
-                    $operator = $matcher->getSupportedOperators()[trim($matches[2])];
108
-                    $operand = trim($matches[1]);
109
-                    $value = trim($matches[3]);
110
-                    $matcher->$operator($operand, GeneralUtility::trimExplode(',', $value, true));
111
-                }
112
-            }
113
-        }
114
-
115
-        return $matcher;
116
-    }
117
-
118
-    /**
119
-     * @param Matcher $matcher
120
-     * @param array $matches
121
-     * @return Matcher $matcher
122
-     */
123
-    protected function applyCriteriaFromMatchesArgument(Matcher $matcher, $matches): Matcher
124
-    {
125
-        foreach ($matches as $fieldNameAndPath => $value) {
126
-            // CSV values should be considered as "in" operator in the query, otherwise "equals".
127
-            $explodedValues = GeneralUtility::trimExplode(',', $value, true);
128
-            if (count($explodedValues) > 1) {
129
-                $matcher->in($fieldNameAndPath, $explodedValues);
130
-            } else {
131
-                $matcher->equals($fieldNameAndPath, $explodedValues[0]);
132
-            }
133
-        }
134
-
135
-        return $matcher;
136
-    }
137
-
138
-    /**
139
-     * Apply criteria specific to jQuery plugin DataTable.
140
-     *
141
-     * @param Matcher $matcher
142
-     * @return Matcher $matcher
143
-     */
144
-    protected function applyCriteriaFromDataTables(Matcher $matcher): Matcher
145
-    {
146
-        // Special case for Grid in the BE using jQuery DataTables plugin.
147
-        // Retrieve a possible search term from GP.
148
-        $query = GeneralUtility::_GP('search');
149
-        if (is_array($query)) {
150
-            if (!empty($query['value'])) {
151
-                $query = $query['value'];
152
-            } else {
153
-                $query = '';
154
-            }
155
-        }
156
-
157
-        if (strlen($query) > 0) {
158
-            // Parse the json query coming from the Visual Search.
159
-            $query = rawurldecode($query);
160
-            $queryParts = json_decode($query, true);
161
-
162
-            if (is_array($queryParts)) {
163
-                $matcher = $this->parseQuery($queryParts, $matcher);
164
-            } else {
165
-                $matcher->setSearchTerm($query);
166
-            }
167
-        }
168
-        return $matcher;
169
-    }
170
-
171
-    /**
172
-     * @param array $queryParts
173
-     * @param Matcher $matcher
174
-     * @return Matcher $matcher
175
-     */
176
-    protected function parseQuery(array $queryParts, Matcher $matcher): Matcher
177
-    {
178
-        $dataType = $matcher->getDataType();
179
-        foreach ($queryParts as $term) {
180
-            $fieldNameAndPath = key($term);
181
-
182
-            $resolvedDataType = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $dataType);
183
-            $fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $dataType);
184
-
185
-            // Retrieve the value.
186
-            $value = current($term);
187
-
188
-            if (Tca::grid($resolvedDataType)->hasFacet($fieldName) && Tca::grid($resolvedDataType)->facet($fieldName)->canModifyMatcher()) {
189
-                $matcher = Tca::grid($resolvedDataType)->facet($fieldName)->modifyMatcher($matcher, $value);
190
-            } elseif (Tca::table($resolvedDataType)->hasField($fieldName)) {
191
-                // Check whether the field exists and set it as "equal" or "like".
192
-                if ($this->isOperatorEquals($fieldNameAndPath, $dataType, $value)) {
193
-                    $matcher->equals($fieldNameAndPath, $value);
194
-                } else {
195
-                    $matcher->like($fieldNameAndPath, $value);
196
-                }
197
-            } elseif ($fieldNameAndPath === 'text') {
198
-                // Special case if field is "text" which is a pseudo field in this case.
199
-                // Set the search term which means Vidi will
200
-                // search in various fields with operator "like". The fields come from key "searchFields" in the TCA.
201
-                $matcher->setSearchTerm($value);
202
-            }
203
-        }
204
-        return $matcher;
205
-    }
206
-
207
-    /**
208
-     * Tell whether the operator should be equals instead of like for a search, e.g. if the value is numerical.
209
-     *
210
-     * @param string $fieldName
211
-     * @param string $dataType
212
-     * @param string $value
213
-     * @return bool
214
-     */
215
-    protected function isOperatorEquals($fieldName, $dataType, $value): bool
216
-    {
217
-        return (Tca::table($dataType)->field($fieldName)->hasRelation() && MathUtility::canBeInterpretedAsInteger($value))
218
-            || Tca::table($dataType)->field($fieldName)->isNumerical();
219
-    }
220
-
221
-    /**
222
-     * Signal that is called for post-processing a matcher object.
223
-     *
224
-     * @param Matcher $matcher
225
-     */
226
-    protected function emitPostProcessMatcherObjectSignal(Matcher $matcher): void
227
-    {
228
-        if (strlen($matcher->getDataType()) <= 0) {
229
-            /** @var ModuleLoader $moduleLoader */
230
-            $moduleLoader = GeneralUtility::makeInstance(ModuleLoader::class);
231
-            $matcher->setDataType($moduleLoader->getDataType());
232
-        }
233
-
234
-        $this->getSignalSlotDispatcher()->dispatch('Fab\Vidi\Controller\Backend\ContentController', 'postProcessMatcherObject', array($matcher, $matcher->getDataType()));
235
-    }
236
-
237
-    /**
238
-     * Get the SignalSlot dispatcher
239
-     *
240
-     * @return Dispatcher|object
241
-     */
242
-    protected function getSignalSlotDispatcher()
243
-    {
244
-        return GeneralUtility::makeInstance(Dispatcher::class);
245
-    }
246
-
247
-    /**
248
-     * Get the Vidi Module Loader.
249
-     *
250
-     * @return ModuleLoader|object
251
-     */
252
-    protected function getModuleLoader()
253
-    {
254
-        return GeneralUtility::makeInstance(ModuleLoader::class);
255
-    }
256
-
257
-    /**
258
-     * @return FieldPathResolver|object
259
-     */
260
-    protected function getFieldPathResolver()
261
-    {
262
-        return GeneralUtility::makeInstance(FieldPathResolver::class);
263
-    }
264
-
265
-    /**
266
-     * Returns an instance of the current Backend User.
267
-     *
268
-     * @return BackendUserAuthentication
269
-     */
270
-    protected function getBackendUser(): BackendUserAuthentication
271
-    {
272
-        return $GLOBALS['BE_USER'];
273
-    }
274
-
275
-    protected function isBackendMode(): bool
276
-    {
277
-        return Typo3Mode::isBackendMode();
278
-    }
29
+	/**
30
+	 * Gets a singleton instance of this class.
31
+	 *
32
+	 * @return $this
33
+	 */
34
+	public static function getInstance(): self
35
+	{
36
+		return GeneralUtility::makeInstance(self::class);
37
+	}
38
+
39
+	/**
40
+	 * Returns a matcher object.
41
+	 *
42
+	 * @param array $matches
43
+	 * @param string $dataType
44
+	 * @return Matcher
45
+	 */
46
+	public function getMatcher(array $matches = [], $dataType = ''): Matcher
47
+	{
48
+		if ($dataType === '') {
49
+			$dataType = $this->getModuleLoader()->getDataType();
50
+		}
51
+
52
+		/** @var $matcher Matcher */
53
+		$matcher = GeneralUtility::makeInstance(Matcher::class, [], $dataType);
54
+
55
+		$matcher = $this->applyCriteriaFromDataTables($matcher);
56
+		$matcher = $this->applyCriteriaFromMatchesArgument($matcher, $matches);
57
+
58
+		if ($this->isBackendMode()) {
59
+			$matcher = $this->applyCriteriaFromUrl($matcher);
60
+			$matcher = $this->applyCriteriaFromTSConfig($matcher);
61
+		}
62
+
63
+		// Trigger signal for post processing Matcher Object.
64
+		$this->emitPostProcessMatcherObjectSignal($matcher);
65
+
66
+		return $matcher;
67
+	}
68
+
69
+	/**
70
+	 * Get a possible id from the URL and apply as filter criteria.
71
+	 * Except if the main module belongs to the File. The id would be a combined identifier
72
+	 * including the storage and a mount point.
73
+	 *
74
+	 * @param Matcher $matcher
75
+	 * @return Matcher $matcher
76
+	 */
77
+	protected function applyCriteriaFromUrl(Matcher $matcher): Matcher
78
+	{
79
+		if (GeneralUtility::_GP('id')
80
+			&& !$this->getModuleLoader()->isPidIgnored()
81
+			&& $this->getModuleLoader()->getMainModule() !== ModuleName::FILE) {
82
+			$matcher->equals('pid', GeneralUtility::_GP('id'));
83
+		}
84
+
85
+		return $matcher;
86
+	}
87
+
88
+	/**
89
+	 * @param Matcher $matcher
90
+	 * @return Matcher $matcher
91
+	 */
92
+	protected function applyCriteriaFromTSConfig(Matcher $matcher): Matcher
93
+	{
94
+		$dataType = $matcher->getDataType();
95
+		$tsConfigPath = sprintf('tx_vidi.dataType.%s.constraints', $dataType);
96
+		$tsConfig = $this->getBackendUser()->getTSConfig($tsConfigPath);
97
+
98
+		if (is_array($tsConfig['properties']) && !empty($tsConfig['properties'])) {
99
+			foreach ($tsConfig['properties'] as $constraint) {
100
+				if (preg_match('/(.+) (>=|>|<|<=|=|like) (.+)/is', $constraint, $matches) && count($matches) === 4) {
101
+					$operator = $matcher->getSupportedOperators()[strtolower(trim($matches[2]))];
102
+					$operand = trim($matches[1]);
103
+					$value = trim($matches[3]);
104
+
105
+					$matcher->$operator($operand, $value);
106
+				} elseif (preg_match('/(.+) (in) (.+)/is', $constraint, $matches) && count($matches) === 4) {
107
+					$operator = $matcher->getSupportedOperators()[trim($matches[2])];
108
+					$operand = trim($matches[1]);
109
+					$value = trim($matches[3]);
110
+					$matcher->$operator($operand, GeneralUtility::trimExplode(',', $value, true));
111
+				}
112
+			}
113
+		}
114
+
115
+		return $matcher;
116
+	}
117
+
118
+	/**
119
+	 * @param Matcher $matcher
120
+	 * @param array $matches
121
+	 * @return Matcher $matcher
122
+	 */
123
+	protected function applyCriteriaFromMatchesArgument(Matcher $matcher, $matches): Matcher
124
+	{
125
+		foreach ($matches as $fieldNameAndPath => $value) {
126
+			// CSV values should be considered as "in" operator in the query, otherwise "equals".
127
+			$explodedValues = GeneralUtility::trimExplode(',', $value, true);
128
+			if (count($explodedValues) > 1) {
129
+				$matcher->in($fieldNameAndPath, $explodedValues);
130
+			} else {
131
+				$matcher->equals($fieldNameAndPath, $explodedValues[0]);
132
+			}
133
+		}
134
+
135
+		return $matcher;
136
+	}
137
+
138
+	/**
139
+	 * Apply criteria specific to jQuery plugin DataTable.
140
+	 *
141
+	 * @param Matcher $matcher
142
+	 * @return Matcher $matcher
143
+	 */
144
+	protected function applyCriteriaFromDataTables(Matcher $matcher): Matcher
145
+	{
146
+		// Special case for Grid in the BE using jQuery DataTables plugin.
147
+		// Retrieve a possible search term from GP.
148
+		$query = GeneralUtility::_GP('search');
149
+		if (is_array($query)) {
150
+			if (!empty($query['value'])) {
151
+				$query = $query['value'];
152
+			} else {
153
+				$query = '';
154
+			}
155
+		}
156
+
157
+		if (strlen($query) > 0) {
158
+			// Parse the json query coming from the Visual Search.
159
+			$query = rawurldecode($query);
160
+			$queryParts = json_decode($query, true);
161
+
162
+			if (is_array($queryParts)) {
163
+				$matcher = $this->parseQuery($queryParts, $matcher);
164
+			} else {
165
+				$matcher->setSearchTerm($query);
166
+			}
167
+		}
168
+		return $matcher;
169
+	}
170
+
171
+	/**
172
+	 * @param array $queryParts
173
+	 * @param Matcher $matcher
174
+	 * @return Matcher $matcher
175
+	 */
176
+	protected function parseQuery(array $queryParts, Matcher $matcher): Matcher
177
+	{
178
+		$dataType = $matcher->getDataType();
179
+		foreach ($queryParts as $term) {
180
+			$fieldNameAndPath = key($term);
181
+
182
+			$resolvedDataType = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $dataType);
183
+			$fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $dataType);
184
+
185
+			// Retrieve the value.
186
+			$value = current($term);
187
+
188
+			if (Tca::grid($resolvedDataType)->hasFacet($fieldName) && Tca::grid($resolvedDataType)->facet($fieldName)->canModifyMatcher()) {
189
+				$matcher = Tca::grid($resolvedDataType)->facet($fieldName)->modifyMatcher($matcher, $value);
190
+			} elseif (Tca::table($resolvedDataType)->hasField($fieldName)) {
191
+				// Check whether the field exists and set it as "equal" or "like".
192
+				if ($this->isOperatorEquals($fieldNameAndPath, $dataType, $value)) {
193
+					$matcher->equals($fieldNameAndPath, $value);
194
+				} else {
195
+					$matcher->like($fieldNameAndPath, $value);
196
+				}
197
+			} elseif ($fieldNameAndPath === 'text') {
198
+				// Special case if field is "text" which is a pseudo field in this case.
199
+				// Set the search term which means Vidi will
200
+				// search in various fields with operator "like". The fields come from key "searchFields" in the TCA.
201
+				$matcher->setSearchTerm($value);
202
+			}
203
+		}
204
+		return $matcher;
205
+	}
206
+
207
+	/**
208
+	 * Tell whether the operator should be equals instead of like for a search, e.g. if the value is numerical.
209
+	 *
210
+	 * @param string $fieldName
211
+	 * @param string $dataType
212
+	 * @param string $value
213
+	 * @return bool
214
+	 */
215
+	protected function isOperatorEquals($fieldName, $dataType, $value): bool
216
+	{
217
+		return (Tca::table($dataType)->field($fieldName)->hasRelation() && MathUtility::canBeInterpretedAsInteger($value))
218
+			|| Tca::table($dataType)->field($fieldName)->isNumerical();
219
+	}
220
+
221
+	/**
222
+	 * Signal that is called for post-processing a matcher object.
223
+	 *
224
+	 * @param Matcher $matcher
225
+	 */
226
+	protected function emitPostProcessMatcherObjectSignal(Matcher $matcher): void
227
+	{
228
+		if (strlen($matcher->getDataType()) <= 0) {
229
+			/** @var ModuleLoader $moduleLoader */
230
+			$moduleLoader = GeneralUtility::makeInstance(ModuleLoader::class);
231
+			$matcher->setDataType($moduleLoader->getDataType());
232
+		}
233
+
234
+		$this->getSignalSlotDispatcher()->dispatch('Fab\Vidi\Controller\Backend\ContentController', 'postProcessMatcherObject', array($matcher, $matcher->getDataType()));
235
+	}
236
+
237
+	/**
238
+	 * Get the SignalSlot dispatcher
239
+	 *
240
+	 * @return Dispatcher|object
241
+	 */
242
+	protected function getSignalSlotDispatcher()
243
+	{
244
+		return GeneralUtility::makeInstance(Dispatcher::class);
245
+	}
246
+
247
+	/**
248
+	 * Get the Vidi Module Loader.
249
+	 *
250
+	 * @return ModuleLoader|object
251
+	 */
252
+	protected function getModuleLoader()
253
+	{
254
+		return GeneralUtility::makeInstance(ModuleLoader::class);
255
+	}
256
+
257
+	/**
258
+	 * @return FieldPathResolver|object
259
+	 */
260
+	protected function getFieldPathResolver()
261
+	{
262
+		return GeneralUtility::makeInstance(FieldPathResolver::class);
263
+	}
264
+
265
+	/**
266
+	 * Returns an instance of the current Backend User.
267
+	 *
268
+	 * @return BackendUserAuthentication
269
+	 */
270
+	protected function getBackendUser(): BackendUserAuthentication
271
+	{
272
+		return $GLOBALS['BE_USER'];
273
+	}
274
+
275
+	protected function isBackendMode(): bool
276
+	{
277
+		return Typo3Mode::isBackendMode();
278
+	}
279 279
 }
Please login to merge, or discard this patch.
Classes/Persistence/Query.php 1 patch
Indentation   +617 added lines, -617 removed lines patch added patch discarded remove patch
@@ -42,621 +42,621 @@
 block discarded – undo
42 42
  */
43 43
 class Query implements QueryInterface
44 44
 {
45
-    /**
46
-     * An inner join.
47
-     */
48
-    public const JCR_JOIN_TYPE_INNER = '{http://www.jcp.org/jcr/1.0}joinTypeInner';
49
-
50
-    /**
51
-     * A left-outer join.
52
-     */
53
-    public const JCR_JOIN_TYPE_LEFT_OUTER = '{http://www.jcp.org/jcr/1.0}joinTypeLeftOuter';
54
-
55
-    /**
56
-     * A right-outer join.
57
-     */
58
-    public const JCR_JOIN_TYPE_RIGHT_OUTER = '{http://www.jcp.org/jcr/1.0}joinTypeRightOuter';
59
-
60
-    /**
61
-     * Charset of strings in QOM
62
-     */
63
-    public const CHARSET = 'utf-8';
64
-
65
-    /**
66
-     * @var string
67
-     */
68
-    protected $sourceFieldName;
69
-
70
-    /**
71
-     * @var string
72
-     */
73
-    protected $type;
74
-
75
-    /**
76
-     * @var PersistenceManagerInterface
77
-     */
78
-    protected $persistenceManager;
79
-
80
-    /**
81
-     * @var QueryObjectModelFactory
82
-     */
83
-    protected $qomFactory;
84
-
85
-    /**
86
-     * @var SourceInterface
87
-     */
88
-    protected $source;
89
-
90
-    /**
91
-     * @var ConstraintInterface
92
-     */
93
-    protected $constraint;
94
-
95
-    /**
96
-     * @var Statement
97
-     */
98
-    protected $statement;
99
-
100
-    /**
101
-     * @var array
102
-     */
103
-    protected $orderings = [];
104
-
105
-    /**
106
-     * @var int
107
-     */
108
-    protected $limit;
109
-
110
-    /**
111
-     * @var int
112
-     */
113
-    protected $offset;
114
-
115
-    /**
116
-     * Apply DISTINCT upon property.
117
-     *
118
-     * @var string
119
-     */
120
-    protected $distinct;
121
-
122
-    /**
123
-     * The query settings.
124
-     *
125
-     * @var Typo3QuerySettings
126
-     */
127
-    public Typo3QuerySettings $typo3QuerySettings;
128
-
129
-    /**
130
-     * Constructs a query object working on the given class name
131
-     *
132
-     * @param string $type
133
-     */
134
-    public function __construct($type)
135
-    {
136
-        $this->type = $type;
137
-        $this->persistenceManager = GeneralUtility::makeInstance(PersistenceManagerInterface::class);
138
-        $this->qomFactory = GeneralUtility::makeInstance(QueryObjectModelFactory::class);
139
-    }
140
-
141
-    public function injectTypo3QuerySettings(Typo3QuerySettings $querySettings): void
142
-    {
143
-        $this->typo3QuerySettings = $querySettings;
144
-    }
145
-
146
-    /**
147
-     * Sets the Query Settings. These Query settings must match the settings expected by
148
-     * the specific Storage Backend.
149
-     *
150
-     * @param QuerySettingsInterface $typo3QuerySettings The Query Settings
151
-     * @return void
152
-     */
153
-    public function setTypo3QuerySettings(QuerySettingsInterface $typo3QuerySettings)
154
-    {
155
-        $this->typo3QuerySettings = $typo3QuerySettings;
156
-    }
157
-
158
-    /**
159
-     * Returns the Query Settings.
160
-     *
161
-     * @throws \Exception
162
-     * @return Typo3QuerySettings $querySettings The Query Settings
163
-     * @api This method is not part of FLOW3 API
164
-     */
165
-    public function getTypo3QuerySettings()
166
-    {
167
-        if (!$this->typo3QuerySettings instanceof QuerySettingsInterface) {
168
-            throw new Exception('Tried to get the query settings without setting them before.', 1248689115);
169
-        }
170
-
171
-        // Apply possible settings to the query.
172
-        if ($this->isBackendMode()) {
173
-            /** @var BackendConfigurationManager $backendConfigurationManager */
174
-            $backendConfigurationManager = GeneralUtility::makeInstance(BackendConfigurationManager::class);
175
-            $configuration = $backendConfigurationManager->getTypoScriptSetup();
176
-            $querySettings = array('respectSysLanguage');
177
-            foreach ($querySettings as $setting) {
178
-                if (isset($configuration['config.']['tx_vidi.']['persistence.']['backend.'][$this->type . '.'][$setting])) {
179
-                    $value = (bool)$configuration['config.']['tx_vidi.']['persistence.']['backend.'][$this->type . '.'][$setting];
180
-                    ObjectAccess::setProperty($this->typo3QuerySettings, $setting, $value);
181
-                }
182
-            }
183
-        }
184
-
185
-        return $this->typo3QuerySettings;
186
-    }
187
-
188
-    /**
189
-     * Returns the type this query cares for.
190
-     *
191
-     * @return string
192
-     * @api
193
-     */
194
-    public function getType()
195
-    {
196
-        return $this->type;
197
-    }
198
-
199
-    /**
200
-     * Sets the source to fetch the result from
201
-     *
202
-     * @param SourceInterface $source
203
-     */
204
-    public function setSource(SourceInterface $source)
205
-    {
206
-        $this->source = $source;
207
-    }
208
-
209
-    /**
210
-     * Returns the selectorn name or an empty string, if the source is not a selector
211
-     * TODO This has to be checked at another place
212
-     *
213
-     * @return string The selector name
214
-     */
215
-    protected function getSelectorName()
216
-    {
217
-        if ($this->getSource() instanceof SelectorInterface) {
218
-            return $this->source->getSelectorName();
219
-        } else {
220
-            return '';
221
-        }
222
-    }
223
-
224
-    /**
225
-     * Gets the node-tuple source for this query.
226
-     *
227
-     * @return SourceInterface the node-tuple source; non-null
228
-     */
229
-    public function getSource()
230
-    {
231
-        if ($this->source === null) {
232
-            $this->source = $this->qomFactory->selector($this->getType());
233
-        }
234
-        return $this->source;
235
-    }
236
-
237
-    /**
238
-     * Executes the query against the database and returns the result
239
-     *
240
-     * @return QueryResultInterface|array The query result object or an array if $this->getQuerySettings()->getReturnRawQueryResult() is true
241
-     * @api
242
-     */
243
-    public function execute($returnRawQueryResult = false)
244
-    {
245
-        /** @var VidiDbBackend $backend */
246
-        $backend = GeneralUtility::makeInstance(VidiDbBackend::class, $this);
247
-        return $backend->fetchResult();
248
-    }
249
-
250
-    /**
251
-     * Sets the property names to order the result by. Expected like this:
252
-     * array(
253
-     * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
254
-     * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
255
-     * )
256
-     * where 'foo' and 'bar' are property names.
257
-     *
258
-     * @param array $orderings The property names to order by
259
-     * @return QueryInterface
260
-     * @api
261
-     */
262
-    public function setOrderings(array $orderings)
263
-    {
264
-        $this->orderings = $orderings;
265
-        return $this;
266
-    }
267
-
268
-    /**
269
-     * Returns the property names to order the result by. Like this:
270
-     * array(
271
-     * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
272
-     * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
273
-     * )
274
-     *
275
-     * @return array
276
-     */
277
-    public function getOrderings()
278
-    {
279
-        return $this->orderings;
280
-    }
281
-
282
-    /**
283
-     * Sets the maximum size of the result set to limit. Returns $this to allow
284
-     * for chaining (fluid interface)
285
-     *
286
-     * @param integer $limit
287
-     * @throws \InvalidArgumentException
288
-     * @return QueryInterface
289
-     * @api
290
-     */
291
-    public function setLimit($limit)
292
-    {
293
-        if (!is_int($limit) || $limit < 1) {
294
-            throw new \InvalidArgumentException('The limit must be an integer >= 1', 1245071870);
295
-        }
296
-        $this->limit = $limit;
297
-        return $this;
298
-    }
299
-
300
-    /**
301
-     * Resets a previously set maximum size of the result set. Returns $this to allow
302
-     * for chaining (fluid interface)
303
-     *
304
-     * @return QueryInterface
305
-     * @api
306
-     */
307
-    public function unsetLimit()
308
-    {
309
-        unset($this->limit);
310
-        return $this;
311
-    }
312
-
313
-    /**
314
-     * Returns the maximum size of the result set to limit.
315
-     *
316
-     * @return integer
317
-     * @api
318
-     */
319
-    public function getLimit()
320
-    {
321
-        return $this->limit;
322
-    }
323
-
324
-    /**
325
-     * Sets the start offset of the result set to offset. Returns $this to
326
-     * allow for chaining (fluid interface)
327
-     *
328
-     * @param integer $offset
329
-     * @throws \InvalidArgumentException
330
-     * @return QueryInterface
331
-     * @api
332
-     */
333
-    public function setOffset($offset)
334
-    {
335
-        if (!is_int($offset) || $offset < 0) {
336
-            throw new \InvalidArgumentException('The offset must be a positive integer', 1245071872);
337
-        }
338
-        $this->offset = $offset;
339
-        return $this;
340
-    }
341
-
342
-    /**
343
-     * Returns the start offset of the result set.
344
-     *
345
-     * @return integer
346
-     * @api
347
-     */
348
-    public function getOffset()
349
-    {
350
-        return $this->offset;
351
-    }
352
-
353
-    /**
354
-     * The constraint used to limit the result set. Returns $this to allow
355
-     * for chaining (fluid interface)
356
-     *
357
-     * @param ConstraintInterface $constraint
358
-     * @return QueryInterface
359
-     * @api
360
-     */
361
-    public function matching($constraint)
362
-    {
363
-        $this->constraint = $constraint;
364
-        return $this;
365
-    }
366
-
367
-    /**
368
-     * Gets the constraint for this query.
369
-     *
370
-     * @return ConstraintInterface the constraint, or null if none
371
-     * @api
372
-     */
373
-    public function getConstraint()
374
-    {
375
-        return $this->constraint;
376
-    }
377
-
378
-    /**
379
-     * Performs a logical conjunction of the given constraints. The method takes one or more contraints and concatenates them with a boolean AND.
380
-     * It also scepts a single array of constraints to be concatenated.
381
-     *
382
-     * @param mixed $constraint1 The first of multiple constraints or an array of constraints.
383
-     * @throws InvalidNumberOfConstraintsException
384
-     * @return AndInterface
385
-     * @api
386
-     */
387
-    public function logicalAnd($constraint1)
388
-    {
389
-        if (is_array($constraint1)) {
390
-            $resultingConstraint = array_shift($constraint1);
391
-            $constraints = $constraint1;
392
-        } else {
393
-            $constraints = func_get_args();
394
-            $resultingConstraint = array_shift($constraints);
395
-        }
396
-        if ($resultingConstraint === null) {
397
-            throw new InvalidNumberOfConstraintsException('There must be at least one constraint or a non-empty array of constraints given.', 1401289500);
398
-        }
399
-        foreach ($constraints as $constraint) {
400
-            $resultingConstraint = $this->qomFactory->_and($resultingConstraint, $constraint);
401
-        }
402
-        return $resultingConstraint;
403
-    }
404
-
405
-    /**
406
-     * Performs a logical disjunction of the two given constraints
407
-     *
408
-     * @param mixed $constraint1 The first of multiple constraints or an array of constraints.
409
-     * @throws InvalidNumberOfConstraintsException
410
-     * @return OrInterface
411
-     * @api
412
-     */
413
-    public function logicalOr($constraint1)
414
-    {
415
-        if (is_array($constraint1)) {
416
-            $resultingConstraint = array_shift($constraint1);
417
-            $constraints = $constraint1;
418
-        } else {
419
-            $constraints = func_get_args();
420
-            $resultingConstraint = array_shift($constraints);
421
-        }
422
-        if ($resultingConstraint === null) {
423
-            throw new InvalidNumberOfConstraintsException('There must be at least one constraint or a non-empty array of constraints given.', 1401289501);
424
-        }
425
-        foreach ($constraints as $constraint) {
426
-            $resultingConstraint = $this->qomFactory->_or($resultingConstraint, $constraint);
427
-        }
428
-        return $resultingConstraint;
429
-    }
430
-
431
-    /**
432
-     * Performs a logical negation of the given constraint
433
-     *
434
-     * @param ConstraintInterface $constraint Constraint to negate
435
-     * @throws \RuntimeException
436
-     * @return NotInterface
437
-     * @api
438
-     */
439
-    public function logicalNot(ConstraintInterface $constraint)
440
-    {
441
-        return $this->qomFactory->not($constraint);
442
-    }
443
-
444
-    /**
445
-     * Returns an equals criterion used for matching objects against a query
446
-     *
447
-     * @param string $propertyName The name of the property to compare against
448
-     * @param mixed $operand The value to compare with
449
-     * @param boolean $caseSensitive Whether the equality test should be done case-sensitive
450
-     * @return ComparisonInterface
451
-     * @api
452
-     */
453
-    public function equals($propertyName, $operand, $caseSensitive = true)
454
-    {
455
-        if (is_object($operand) || $caseSensitive) {
456
-            $comparison = $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_EQUAL_TO, $operand);
457
-        } else {
458
-            $comparison = $this->qomFactory->comparison($this->qomFactory->lowerCase($this->qomFactory->propertyValue($propertyName, $this->getSelectorName())), QueryInterface::OPERATOR_EQUAL_TO, mb_strtolower($operand, \TYPO3\CMS\Extbase\Persistence\Generic\Query::CHARSET));
459
-        }
460
-        return $comparison;
461
-    }
462
-
463
-    /**
464
-     * Returns a like criterion used for matching objects against a query
465
-     *
466
-     * @param string $propertyName The name of the property to compare against
467
-     * @param mixed $operand The value to compare with
468
-     * @param boolean $caseSensitive Whether the matching should be done case-sensitive
469
-     * @return ComparisonInterface
470
-     * @api
471
-     */
472
-    public function like($propertyName, $operand, $caseSensitive = true)
473
-    {
474
-        return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_LIKE, $operand);
475
-    }
476
-
477
-    /**
478
-     * Returns a "contains" criterion used for matching objects against a query.
479
-     * It matches if the multivalued property contains the given operand.
480
-     *
481
-     * @param string $propertyName The name of the (multivalued) property to compare against
482
-     * @param mixed $operand The value to compare with
483
-     * @return ComparisonInterface
484
-     * @api
485
-     */
486
-    public function contains($propertyName, $operand)
487
-    {
488
-        return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_CONTAINS, $operand);
489
-    }
490
-
491
-    /**
492
-     * Returns an "in" criterion used for matching objects against a query. It
493
-     * matches if the property's value is contained in the multivalued operand.
494
-     *
495
-     * @param string $propertyName The name of the property to compare against
496
-     * @param mixed $operand The value to compare with, multivalued
497
-     * @throws UnexpectedTypeException
498
-     * @return ComparisonInterface
499
-     * @api
500
-     */
501
-    public function in($propertyName, $operand)
502
-    {
503
-        if (!is_array($operand) && !$operand instanceof \ArrayAccess && !$operand instanceof \Traversable) {
504
-            throw new UnexpectedTypeException('The "in" operator must be given a mutlivalued operand (array, ArrayAccess, Traversable).', 1264678095);
505
-        }
506
-        return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_IN, $operand);
507
-    }
508
-
509
-    /**
510
-     * Returns a less than criterion used for matching objects against a query
511
-     *
512
-     * @param string $propertyName The name of the property to compare against
513
-     * @param mixed $operand The value to compare with
514
-     * @return ComparisonInterface
515
-     * @api
516
-     */
517
-    public function lessThan($propertyName, $operand)
518
-    {
519
-        return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_LESS_THAN, $operand);
520
-    }
521
-
522
-    /**
523
-     * Returns a less or equal than criterion used for matching objects against a query
524
-     *
525
-     * @param string $propertyName The name of the property to compare against
526
-     * @param mixed $operand The value to compare with
527
-     * @return ComparisonInterface
528
-     * @api
529
-     */
530
-    public function lessThanOrEqual($propertyName, $operand)
531
-    {
532
-        return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO, $operand);
533
-    }
534
-
535
-    /**
536
-     * Returns a greater than criterion used for matching objects against a query
537
-     *
538
-     * @param string $propertyName The name of the property to compare against
539
-     * @param mixed $operand The value to compare with
540
-     * @return ComparisonInterface
541
-     * @api
542
-     */
543
-    public function greaterThan($propertyName, $operand)
544
-    {
545
-        return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_GREATER_THAN, $operand);
546
-    }
547
-
548
-    /**
549
-     * Returns a greater than or equal criterion used for matching objects against a query
550
-     *
551
-     * @param string $propertyName The name of the property to compare against
552
-     * @param mixed $operand The value to compare with
553
-     * @return ComparisonInterface
554
-     * @api
555
-     */
556
-    public function greaterThanOrEqual($propertyName, $operand)
557
-    {
558
-        return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO, $operand);
559
-    }
560
-
561
-    /**
562
-     * Returns the query result count.
563
-     *
564
-     * @return integer The query result count
565
-     * @api
566
-     */
567
-    public function count()
568
-    {
569
-        /** @var VidiDbBackend $backend */
570
-        $backend = GeneralUtility::makeInstance(VidiDbBackend::class, $this);
571
-        return $backend->countResult();
572
-    }
573
-
574
-    /**
575
-     * Returns an "isEmpty" criterion used for matching objects against a query.
576
-     * It matches if the multivalued property contains no values or is null.
577
-     *
578
-     * @param string $propertyName The name of the multivalued property to compare against
579
-     * @throws NotImplementedException
580
-     * @throws InvalidQueryException if used on a single-valued property
581
-     * @api
582
-     */
583
-    public function isEmpty($propertyName)
584
-    {
585
-        throw new NotImplementedException(__METHOD__);
586
-    }
587
-
588
-    /**
589
-     * @return string
590
-     */
591
-    public function getDistinct()
592
-    {
593
-        return $this->distinct;
594
-    }
595
-
596
-    /**
597
-     * @param string $distinct
598
-     * @return $this
599
-     */
600
-    public function setDistinct($distinct)
601
-    {
602
-        $this->distinct = $distinct;
603
-        return $this;
604
-    }
605
-
606
-    /**
607
-     * Sets the statement of this query. If you use this, you will lose the abstraction from a concrete storage
608
-     * backend (database).
609
-     *
610
-     * @param string|\TYPO3\CMS\Core\Database\PreparedStatement $statement The statement
611
-     * @param array $parameters An array of parameters. These will be bound to placeholders '?' in the $statement.
612
-     * @return QueryInterface
613
-     */
614
-    public function statement($statement, array $parameters = array())
615
-    {
616
-        $this->statement = $this->qomFactory->statement($statement, $parameters);
617
-        return $this;
618
-    }
619
-
620
-    /**
621
-     * Returns the statement of this query.
622
-     *
623
-     * @return Statement
624
-     */
625
-    public function getStatement()
626
-    {
627
-        return $this->statement;
628
-    }
629
-
630
-    protected function isBackendMode(): bool
631
-    {
632
-        return Typo3Mode::isBackendMode();
633
-    }
634
-
635
-    /**
636
-     * @return string
637
-     */
638
-    public function getSourceFieldName()
639
-    {
640
-        return $this->sourceFieldName;
641
-    }
642
-
643
-    /**
644
-     * @param string $sourceFieldName
645
-     * @return $this
646
-     */
647
-    public function setSourceFieldName($sourceFieldName)
648
-    {
649
-        $this->sourceFieldName = $sourceFieldName;
650
-        return $this;
651
-    }
652
-
653
-    public function setQuerySettings(QuerySettingsInterface $querySettings)
654
-    {
655
-        $this->typo3QuerySettings = $querySettings;
656
-    }
657
-
658
-    public function getQuerySettings()
659
-    {
660
-        return $this->typo3QuerySettings;
661
-    }
45
+	/**
46
+	 * An inner join.
47
+	 */
48
+	public const JCR_JOIN_TYPE_INNER = '{http://www.jcp.org/jcr/1.0}joinTypeInner';
49
+
50
+	/**
51
+	 * A left-outer join.
52
+	 */
53
+	public const JCR_JOIN_TYPE_LEFT_OUTER = '{http://www.jcp.org/jcr/1.0}joinTypeLeftOuter';
54
+
55
+	/**
56
+	 * A right-outer join.
57
+	 */
58
+	public const JCR_JOIN_TYPE_RIGHT_OUTER = '{http://www.jcp.org/jcr/1.0}joinTypeRightOuter';
59
+
60
+	/**
61
+	 * Charset of strings in QOM
62
+	 */
63
+	public const CHARSET = 'utf-8';
64
+
65
+	/**
66
+	 * @var string
67
+	 */
68
+	protected $sourceFieldName;
69
+
70
+	/**
71
+	 * @var string
72
+	 */
73
+	protected $type;
74
+
75
+	/**
76
+	 * @var PersistenceManagerInterface
77
+	 */
78
+	protected $persistenceManager;
79
+
80
+	/**
81
+	 * @var QueryObjectModelFactory
82
+	 */
83
+	protected $qomFactory;
84
+
85
+	/**
86
+	 * @var SourceInterface
87
+	 */
88
+	protected $source;
89
+
90
+	/**
91
+	 * @var ConstraintInterface
92
+	 */
93
+	protected $constraint;
94
+
95
+	/**
96
+	 * @var Statement
97
+	 */
98
+	protected $statement;
99
+
100
+	/**
101
+	 * @var array
102
+	 */
103
+	protected $orderings = [];
104
+
105
+	/**
106
+	 * @var int
107
+	 */
108
+	protected $limit;
109
+
110
+	/**
111
+	 * @var int
112
+	 */
113
+	protected $offset;
114
+
115
+	/**
116
+	 * Apply DISTINCT upon property.
117
+	 *
118
+	 * @var string
119
+	 */
120
+	protected $distinct;
121
+
122
+	/**
123
+	 * The query settings.
124
+	 *
125
+	 * @var Typo3QuerySettings
126
+	 */
127
+	public Typo3QuerySettings $typo3QuerySettings;
128
+
129
+	/**
130
+	 * Constructs a query object working on the given class name
131
+	 *
132
+	 * @param string $type
133
+	 */
134
+	public function __construct($type)
135
+	{
136
+		$this->type = $type;
137
+		$this->persistenceManager = GeneralUtility::makeInstance(PersistenceManagerInterface::class);
138
+		$this->qomFactory = GeneralUtility::makeInstance(QueryObjectModelFactory::class);
139
+	}
140
+
141
+	public function injectTypo3QuerySettings(Typo3QuerySettings $querySettings): void
142
+	{
143
+		$this->typo3QuerySettings = $querySettings;
144
+	}
145
+
146
+	/**
147
+	 * Sets the Query Settings. These Query settings must match the settings expected by
148
+	 * the specific Storage Backend.
149
+	 *
150
+	 * @param QuerySettingsInterface $typo3QuerySettings The Query Settings
151
+	 * @return void
152
+	 */
153
+	public function setTypo3QuerySettings(QuerySettingsInterface $typo3QuerySettings)
154
+	{
155
+		$this->typo3QuerySettings = $typo3QuerySettings;
156
+	}
157
+
158
+	/**
159
+	 * Returns the Query Settings.
160
+	 *
161
+	 * @throws \Exception
162
+	 * @return Typo3QuerySettings $querySettings The Query Settings
163
+	 * @api This method is not part of FLOW3 API
164
+	 */
165
+	public function getTypo3QuerySettings()
166
+	{
167
+		if (!$this->typo3QuerySettings instanceof QuerySettingsInterface) {
168
+			throw new Exception('Tried to get the query settings without setting them before.', 1248689115);
169
+		}
170
+
171
+		// Apply possible settings to the query.
172
+		if ($this->isBackendMode()) {
173
+			/** @var BackendConfigurationManager $backendConfigurationManager */
174
+			$backendConfigurationManager = GeneralUtility::makeInstance(BackendConfigurationManager::class);
175
+			$configuration = $backendConfigurationManager->getTypoScriptSetup();
176
+			$querySettings = array('respectSysLanguage');
177
+			foreach ($querySettings as $setting) {
178
+				if (isset($configuration['config.']['tx_vidi.']['persistence.']['backend.'][$this->type . '.'][$setting])) {
179
+					$value = (bool)$configuration['config.']['tx_vidi.']['persistence.']['backend.'][$this->type . '.'][$setting];
180
+					ObjectAccess::setProperty($this->typo3QuerySettings, $setting, $value);
181
+				}
182
+			}
183
+		}
184
+
185
+		return $this->typo3QuerySettings;
186
+	}
187
+
188
+	/**
189
+	 * Returns the type this query cares for.
190
+	 *
191
+	 * @return string
192
+	 * @api
193
+	 */
194
+	public function getType()
195
+	{
196
+		return $this->type;
197
+	}
198
+
199
+	/**
200
+	 * Sets the source to fetch the result from
201
+	 *
202
+	 * @param SourceInterface $source
203
+	 */
204
+	public function setSource(SourceInterface $source)
205
+	{
206
+		$this->source = $source;
207
+	}
208
+
209
+	/**
210
+	 * Returns the selectorn name or an empty string, if the source is not a selector
211
+	 * TODO This has to be checked at another place
212
+	 *
213
+	 * @return string The selector name
214
+	 */
215
+	protected function getSelectorName()
216
+	{
217
+		if ($this->getSource() instanceof SelectorInterface) {
218
+			return $this->source->getSelectorName();
219
+		} else {
220
+			return '';
221
+		}
222
+	}
223
+
224
+	/**
225
+	 * Gets the node-tuple source for this query.
226
+	 *
227
+	 * @return SourceInterface the node-tuple source; non-null
228
+	 */
229
+	public function getSource()
230
+	{
231
+		if ($this->source === null) {
232
+			$this->source = $this->qomFactory->selector($this->getType());
233
+		}
234
+		return $this->source;
235
+	}
236
+
237
+	/**
238
+	 * Executes the query against the database and returns the result
239
+	 *
240
+	 * @return QueryResultInterface|array The query result object or an array if $this->getQuerySettings()->getReturnRawQueryResult() is true
241
+	 * @api
242
+	 */
243
+	public function execute($returnRawQueryResult = false)
244
+	{
245
+		/** @var VidiDbBackend $backend */
246
+		$backend = GeneralUtility::makeInstance(VidiDbBackend::class, $this);
247
+		return $backend->fetchResult();
248
+	}
249
+
250
+	/**
251
+	 * Sets the property names to order the result by. Expected like this:
252
+	 * array(
253
+	 * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
254
+	 * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
255
+	 * )
256
+	 * where 'foo' and 'bar' are property names.
257
+	 *
258
+	 * @param array $orderings The property names to order by
259
+	 * @return QueryInterface
260
+	 * @api
261
+	 */
262
+	public function setOrderings(array $orderings)
263
+	{
264
+		$this->orderings = $orderings;
265
+		return $this;
266
+	}
267
+
268
+	/**
269
+	 * Returns the property names to order the result by. Like this:
270
+	 * array(
271
+	 * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
272
+	 * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
273
+	 * )
274
+	 *
275
+	 * @return array
276
+	 */
277
+	public function getOrderings()
278
+	{
279
+		return $this->orderings;
280
+	}
281
+
282
+	/**
283
+	 * Sets the maximum size of the result set to limit. Returns $this to allow
284
+	 * for chaining (fluid interface)
285
+	 *
286
+	 * @param integer $limit
287
+	 * @throws \InvalidArgumentException
288
+	 * @return QueryInterface
289
+	 * @api
290
+	 */
291
+	public function setLimit($limit)
292
+	{
293
+		if (!is_int($limit) || $limit < 1) {
294
+			throw new \InvalidArgumentException('The limit must be an integer >= 1', 1245071870);
295
+		}
296
+		$this->limit = $limit;
297
+		return $this;
298
+	}
299
+
300
+	/**
301
+	 * Resets a previously set maximum size of the result set. Returns $this to allow
302
+	 * for chaining (fluid interface)
303
+	 *
304
+	 * @return QueryInterface
305
+	 * @api
306
+	 */
307
+	public function unsetLimit()
308
+	{
309
+		unset($this->limit);
310
+		return $this;
311
+	}
312
+
313
+	/**
314
+	 * Returns the maximum size of the result set to limit.
315
+	 *
316
+	 * @return integer
317
+	 * @api
318
+	 */
319
+	public function getLimit()
320
+	{
321
+		return $this->limit;
322
+	}
323
+
324
+	/**
325
+	 * Sets the start offset of the result set to offset. Returns $this to
326
+	 * allow for chaining (fluid interface)
327
+	 *
328
+	 * @param integer $offset
329
+	 * @throws \InvalidArgumentException
330
+	 * @return QueryInterface
331
+	 * @api
332
+	 */
333
+	public function setOffset($offset)
334
+	{
335
+		if (!is_int($offset) || $offset < 0) {
336
+			throw new \InvalidArgumentException('The offset must be a positive integer', 1245071872);
337
+		}
338
+		$this->offset = $offset;
339
+		return $this;
340
+	}
341
+
342
+	/**
343
+	 * Returns the start offset of the result set.
344
+	 *
345
+	 * @return integer
346
+	 * @api
347
+	 */
348
+	public function getOffset()
349
+	{
350
+		return $this->offset;
351
+	}
352
+
353
+	/**
354
+	 * The constraint used to limit the result set. Returns $this to allow
355
+	 * for chaining (fluid interface)
356
+	 *
357
+	 * @param ConstraintInterface $constraint
358
+	 * @return QueryInterface
359
+	 * @api
360
+	 */
361
+	public function matching($constraint)
362
+	{
363
+		$this->constraint = $constraint;
364
+		return $this;
365
+	}
366
+
367
+	/**
368
+	 * Gets the constraint for this query.
369
+	 *
370
+	 * @return ConstraintInterface the constraint, or null if none
371
+	 * @api
372
+	 */
373
+	public function getConstraint()
374
+	{
375
+		return $this->constraint;
376
+	}
377
+
378
+	/**
379
+	 * Performs a logical conjunction of the given constraints. The method takes one or more contraints and concatenates them with a boolean AND.
380
+	 * It also scepts a single array of constraints to be concatenated.
381
+	 *
382
+	 * @param mixed $constraint1 The first of multiple constraints or an array of constraints.
383
+	 * @throws InvalidNumberOfConstraintsException
384
+	 * @return AndInterface
385
+	 * @api
386
+	 */
387
+	public function logicalAnd($constraint1)
388
+	{
389
+		if (is_array($constraint1)) {
390
+			$resultingConstraint = array_shift($constraint1);
391
+			$constraints = $constraint1;
392
+		} else {
393
+			$constraints = func_get_args();
394
+			$resultingConstraint = array_shift($constraints);
395
+		}
396
+		if ($resultingConstraint === null) {
397
+			throw new InvalidNumberOfConstraintsException('There must be at least one constraint or a non-empty array of constraints given.', 1401289500);
398
+		}
399
+		foreach ($constraints as $constraint) {
400
+			$resultingConstraint = $this->qomFactory->_and($resultingConstraint, $constraint);
401
+		}
402
+		return $resultingConstraint;
403
+	}
404
+
405
+	/**
406
+	 * Performs a logical disjunction of the two given constraints
407
+	 *
408
+	 * @param mixed $constraint1 The first of multiple constraints or an array of constraints.
409
+	 * @throws InvalidNumberOfConstraintsException
410
+	 * @return OrInterface
411
+	 * @api
412
+	 */
413
+	public function logicalOr($constraint1)
414
+	{
415
+		if (is_array($constraint1)) {
416
+			$resultingConstraint = array_shift($constraint1);
417
+			$constraints = $constraint1;
418
+		} else {
419
+			$constraints = func_get_args();
420
+			$resultingConstraint = array_shift($constraints);
421
+		}
422
+		if ($resultingConstraint === null) {
423
+			throw new InvalidNumberOfConstraintsException('There must be at least one constraint or a non-empty array of constraints given.', 1401289501);
424
+		}
425
+		foreach ($constraints as $constraint) {
426
+			$resultingConstraint = $this->qomFactory->_or($resultingConstraint, $constraint);
427
+		}
428
+		return $resultingConstraint;
429
+	}
430
+
431
+	/**
432
+	 * Performs a logical negation of the given constraint
433
+	 *
434
+	 * @param ConstraintInterface $constraint Constraint to negate
435
+	 * @throws \RuntimeException
436
+	 * @return NotInterface
437
+	 * @api
438
+	 */
439
+	public function logicalNot(ConstraintInterface $constraint)
440
+	{
441
+		return $this->qomFactory->not($constraint);
442
+	}
443
+
444
+	/**
445
+	 * Returns an equals criterion used for matching objects against a query
446
+	 *
447
+	 * @param string $propertyName The name of the property to compare against
448
+	 * @param mixed $operand The value to compare with
449
+	 * @param boolean $caseSensitive Whether the equality test should be done case-sensitive
450
+	 * @return ComparisonInterface
451
+	 * @api
452
+	 */
453
+	public function equals($propertyName, $operand, $caseSensitive = true)
454
+	{
455
+		if (is_object($operand) || $caseSensitive) {
456
+			$comparison = $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_EQUAL_TO, $operand);
457
+		} else {
458
+			$comparison = $this->qomFactory->comparison($this->qomFactory->lowerCase($this->qomFactory->propertyValue($propertyName, $this->getSelectorName())), QueryInterface::OPERATOR_EQUAL_TO, mb_strtolower($operand, \TYPO3\CMS\Extbase\Persistence\Generic\Query::CHARSET));
459
+		}
460
+		return $comparison;
461
+	}
462
+
463
+	/**
464
+	 * Returns a like criterion used for matching objects against a query
465
+	 *
466
+	 * @param string $propertyName The name of the property to compare against
467
+	 * @param mixed $operand The value to compare with
468
+	 * @param boolean $caseSensitive Whether the matching should be done case-sensitive
469
+	 * @return ComparisonInterface
470
+	 * @api
471
+	 */
472
+	public function like($propertyName, $operand, $caseSensitive = true)
473
+	{
474
+		return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_LIKE, $operand);
475
+	}
476
+
477
+	/**
478
+	 * Returns a "contains" criterion used for matching objects against a query.
479
+	 * It matches if the multivalued property contains the given operand.
480
+	 *
481
+	 * @param string $propertyName The name of the (multivalued) property to compare against
482
+	 * @param mixed $operand The value to compare with
483
+	 * @return ComparisonInterface
484
+	 * @api
485
+	 */
486
+	public function contains($propertyName, $operand)
487
+	{
488
+		return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_CONTAINS, $operand);
489
+	}
490
+
491
+	/**
492
+	 * Returns an "in" criterion used for matching objects against a query. It
493
+	 * matches if the property's value is contained in the multivalued operand.
494
+	 *
495
+	 * @param string $propertyName The name of the property to compare against
496
+	 * @param mixed $operand The value to compare with, multivalued
497
+	 * @throws UnexpectedTypeException
498
+	 * @return ComparisonInterface
499
+	 * @api
500
+	 */
501
+	public function in($propertyName, $operand)
502
+	{
503
+		if (!is_array($operand) && !$operand instanceof \ArrayAccess && !$operand instanceof \Traversable) {
504
+			throw new UnexpectedTypeException('The "in" operator must be given a mutlivalued operand (array, ArrayAccess, Traversable).', 1264678095);
505
+		}
506
+		return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_IN, $operand);
507
+	}
508
+
509
+	/**
510
+	 * Returns a less than criterion used for matching objects against a query
511
+	 *
512
+	 * @param string $propertyName The name of the property to compare against
513
+	 * @param mixed $operand The value to compare with
514
+	 * @return ComparisonInterface
515
+	 * @api
516
+	 */
517
+	public function lessThan($propertyName, $operand)
518
+	{
519
+		return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_LESS_THAN, $operand);
520
+	}
521
+
522
+	/**
523
+	 * Returns a less or equal than criterion used for matching objects against a query
524
+	 *
525
+	 * @param string $propertyName The name of the property to compare against
526
+	 * @param mixed $operand The value to compare with
527
+	 * @return ComparisonInterface
528
+	 * @api
529
+	 */
530
+	public function lessThanOrEqual($propertyName, $operand)
531
+	{
532
+		return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO, $operand);
533
+	}
534
+
535
+	/**
536
+	 * Returns a greater than criterion used for matching objects against a query
537
+	 *
538
+	 * @param string $propertyName The name of the property to compare against
539
+	 * @param mixed $operand The value to compare with
540
+	 * @return ComparisonInterface
541
+	 * @api
542
+	 */
543
+	public function greaterThan($propertyName, $operand)
544
+	{
545
+		return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_GREATER_THAN, $operand);
546
+	}
547
+
548
+	/**
549
+	 * Returns a greater than or equal criterion used for matching objects against a query
550
+	 *
551
+	 * @param string $propertyName The name of the property to compare against
552
+	 * @param mixed $operand The value to compare with
553
+	 * @return ComparisonInterface
554
+	 * @api
555
+	 */
556
+	public function greaterThanOrEqual($propertyName, $operand)
557
+	{
558
+		return $this->qomFactory->comparison($this->qomFactory->propertyValue($propertyName, $this->getSelectorName()), QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO, $operand);
559
+	}
560
+
561
+	/**
562
+	 * Returns the query result count.
563
+	 *
564
+	 * @return integer The query result count
565
+	 * @api
566
+	 */
567
+	public function count()
568
+	{
569
+		/** @var VidiDbBackend $backend */
570
+		$backend = GeneralUtility::makeInstance(VidiDbBackend::class, $this);
571
+		return $backend->countResult();
572
+	}
573
+
574
+	/**
575
+	 * Returns an "isEmpty" criterion used for matching objects against a query.
576
+	 * It matches if the multivalued property contains no values or is null.
577
+	 *
578
+	 * @param string $propertyName The name of the multivalued property to compare against
579
+	 * @throws NotImplementedException
580
+	 * @throws InvalidQueryException if used on a single-valued property
581
+	 * @api
582
+	 */
583
+	public function isEmpty($propertyName)
584
+	{
585
+		throw new NotImplementedException(__METHOD__);
586
+	}
587
+
588
+	/**
589
+	 * @return string
590
+	 */
591
+	public function getDistinct()
592
+	{
593
+		return $this->distinct;
594
+	}
595
+
596
+	/**
597
+	 * @param string $distinct
598
+	 * @return $this
599
+	 */
600
+	public function setDistinct($distinct)
601
+	{
602
+		$this->distinct = $distinct;
603
+		return $this;
604
+	}
605
+
606
+	/**
607
+	 * Sets the statement of this query. If you use this, you will lose the abstraction from a concrete storage
608
+	 * backend (database).
609
+	 *
610
+	 * @param string|\TYPO3\CMS\Core\Database\PreparedStatement $statement The statement
611
+	 * @param array $parameters An array of parameters. These will be bound to placeholders '?' in the $statement.
612
+	 * @return QueryInterface
613
+	 */
614
+	public function statement($statement, array $parameters = array())
615
+	{
616
+		$this->statement = $this->qomFactory->statement($statement, $parameters);
617
+		return $this;
618
+	}
619
+
620
+	/**
621
+	 * Returns the statement of this query.
622
+	 *
623
+	 * @return Statement
624
+	 */
625
+	public function getStatement()
626
+	{
627
+		return $this->statement;
628
+	}
629
+
630
+	protected function isBackendMode(): bool
631
+	{
632
+		return Typo3Mode::isBackendMode();
633
+	}
634
+
635
+	/**
636
+	 * @return string
637
+	 */
638
+	public function getSourceFieldName()
639
+	{
640
+		return $this->sourceFieldName;
641
+	}
642
+
643
+	/**
644
+	 * @param string $sourceFieldName
645
+	 * @return $this
646
+	 */
647
+	public function setSourceFieldName($sourceFieldName)
648
+	{
649
+		$this->sourceFieldName = $sourceFieldName;
650
+		return $this;
651
+	}
652
+
653
+	public function setQuerySettings(QuerySettingsInterface $querySettings)
654
+	{
655
+		$this->typo3QuerySettings = $querySettings;
656
+	}
657
+
658
+	public function getQuerySettings()
659
+	{
660
+		return $this->typo3QuerySettings;
661
+	}
662 662
 }
Please login to merge, or discard this patch.
Classes/Persistence/Storage/VidiDbBackend.php 1 patch
Indentation   +1040 added lines, -1040 removed lines patch added patch discarded remove patch
@@ -53,1044 +53,1044 @@
 block discarded – undo
53 53
  */
54 54
 class VidiDbBackend
55 55
 {
56
-    public const OPERATOR_EQUAL_TO_NULL = 'operatorEqualToNull';
57
-    public const OPERATOR_NOT_EQUAL_TO_NULL = 'operatorNotEqualToNull';
58
-
59
-    /**
60
-     * The TYPO3 page repository. Used for language and workspace overlay
61
-     *
62
-     * @var PageRepository
63
-     */
64
-    protected $pageRepository;
65
-
66
-    /**
67
-     * @var Query
68
-     */
69
-    protected $query;
70
-
71
-    /**
72
-     * Store some info related to table name and its aliases.
73
-     *
74
-     * @var array
75
-     */
76
-    protected $tableNameAliases = array(
77
-        'aliases' => [],
78
-        'aliasIncrement' => [],
79
-    );
80
-
81
-    /**
82
-     * Use to store the current foreign table name alias.
83
-     *
84
-     * @var string
85
-     */
86
-    protected $currentChildTableNameAlias = '';
87
-
88
-    /**
89
-     * @param Query $query
90
-     */
91
-    public function __construct(Query $query)
92
-    {
93
-        $this->query = $query;
94
-    }
95
-
96
-    /**
97
-     * @param $parameters
98
-     * @return array
99
-     */
100
-    protected static function getTypes($parameters)
101
-    {
102
-        $types = [];
103
-        foreach ($parameters as $parameter) {
104
-            if (is_array($parameter)) {
105
-                if (MathUtility::canBeInterpretedAsInteger($parameter[0])) {
106
-                    $types[] = \Doctrine\DBAL\Connection::PARAM_INT_ARRAY;
107
-                } else {
108
-                    $types[] = \Doctrine\DBAL\Connection::PARAM_STR_ARRAY;
109
-                }
110
-            } else {
111
-                if (MathUtility::canBeInterpretedAsInteger($parameter)) {
112
-                    $types[] = ParameterType::INTEGER;
113
-                } else {
114
-                    $types[] = ParameterType::STRING;
115
-                }
116
-            }
117
-        }
118
-        return $types;
119
-    }
120
-
121
-    /**
122
-     * Returns the result of the query
123
-     */
124
-    public function fetchResult()
125
-    {
126
-        $parameters = [];
127
-        $statementParts = $this->parseQuery($parameters);
128
-        $statementParts = $this->processStatementStructureForRecursiveMMRelation($statementParts);
129
-        $sql = $this->buildQuery($statementParts);
130
-        //print $sql; exit();
131
-
132
-        $rows = $this->getConnection()
133
-            ->executeQuery($sql, $parameters, self::getTypes($parameters))
134
-            ->fetchAllAssociative();
135
-
136
-        return $this->getContentObjects($rows);
137
-    }
138
-
139
-    /**
140
-     * Returns the number of tuples matching the query.
141
-     *
142
-     * @return int The number of matching tuples
143
-     */
144
-    public function countResult()
145
-    {
146
-        $parameters = [];
147
-        $statementParts = $this->parseQuery($parameters);
148
-        $statementParts = $this->processStatementStructureForRecursiveMMRelation($statementParts);
149
-        $types = self::getTypes($parameters);
150
-
151
-        // if limit is set, we need to count the rows "manually" as COUNT(*) ignores LIMIT constraints
152
-        if (!empty($statementParts['limit'])) {
153
-            $sql = $this->buildQuery($statementParts);
154
-
155
-            $count = $this
156
-                ->getConnection()
157
-                ->executeQuery($sql, $parameters, $types)
158
-                ->rowCount();
159
-        } else {
160
-            $statementParts['fields'] = array('COUNT(*)');
161
-            // having orderings without grouping is not compatible with non-MySQL DBMS
162
-            $statementParts['orderings'] = [];
163
-            if (isset($statementParts['keywords']['distinct'])) {
164
-                unset($statementParts['keywords']['distinct']);
165
-                $distinctField = $this->query->getDistinct() ? $this->query->getDistinct() : 'uid';
166
-                $statementParts['fields'] = array('COUNT(DISTINCT ' . $statementParts['mainTable'] . '.' . $distinctField . ')');
167
-            }
168
-
169
-            $sql = $this->buildQuery($statementParts);
170
-            $count = $this
171
-                ->getConnection()
172
-                ->executeQuery($sql, $parameters, $types)
173
-                ->fetchColumn(0);
174
-        }
175
-        return (int)$count;
176
-    }
177
-
178
-    /**
179
-     * Parses the query and returns the SQL statement parts.
180
-     *
181
-     * @param array &$parameters
182
-     * @return array
183
-     */
184
-    public function parseQuery(array &$parameters)
185
-    {
186
-        $statementParts = [];
187
-        $statementParts['keywords'] = [];
188
-        $statementParts['tables'] = [];
189
-        $statementParts['unions'] = [];
190
-        $statementParts['fields'] = [];
191
-        $statementParts['where'] = [];
192
-        $statementParts['additionalWhereClause'] = [];
193
-        $statementParts['orderings'] = [];
194
-        $statementParts['limit'] = [];
195
-        $query = $this->query;
196
-        $source = $query->getSource();
197
-        $this->parseSource($source, $statementParts);
198
-        $this->parseConstraint($query->getConstraint(), $source, $statementParts, $parameters);
199
-        $this->parseOrderings($query->getOrderings(), $source, $statementParts);
200
-        $this->parseLimitAndOffset($query->getLimit(), $query->getOffset(), $statementParts);
201
-        $tableNames = array_unique(array_keys($statementParts['tables'] + $statementParts['unions']));
202
-        foreach ($tableNames as $tableNameOrAlias) {
203
-            if (is_string($tableNameOrAlias) && strlen($tableNameOrAlias) > 0) {
204
-                $this->addAdditionalWhereClause($query->getTypo3QuerySettings(), $tableNameOrAlias, $statementParts);
205
-            }
206
-        }
207
-
208
-        return $statementParts;
209
-    }
210
-
211
-    /**
212
-     * Fiddle with the statement structure to handle recursive MM relations.
213
-     * For the recursive MM query to work, we must invert some values.
214
-     * Let see if that is the best way of doing that...
215
-     *
216
-     * @param array $statementParts
217
-     * @return array
218
-     */
219
-    public function processStatementStructureForRecursiveMMRelation(array $statementParts)
220
-    {
221
-        if ($this->hasRecursiveMMRelation()) {
222
-            $tableName = $this->query->getType();
223
-
224
-            // In order the MM query to work for a recursive MM query, we must invert some values.
225
-            // tx_domain_model_foo0 (the alias) <--> tx_domain_model_foo (the origin table name)
226
-            $values = [];
227
-            foreach ($statementParts['fields'] as $key => $value) {
228
-                $values[$key] = str_replace($tableName, $tableName . '0', $value);
229
-            }
230
-            $statementParts['fields'] = $values;
231
-
232
-            // Same comment as above.
233
-            $values = [];
234
-            foreach ($statementParts['where'] as $key => $value) {
235
-                $values[$key] = str_replace($tableName . '0', $tableName, $value);
236
-            }
237
-            $statementParts['where'] = $values;
238
-
239
-            // We must be more restrictive by transforming the "left" union by "inner"
240
-            $values = [];
241
-            foreach ($statementParts['unions'] as $key => $value) {
242
-                $values[$key] = str_replace('LEFT JOIN', 'INNER JOIN', $value);
243
-            }
244
-            $statementParts['unions'] = $values;
245
-        }
246
-
247
-        return $statementParts;
248
-    }
249
-
250
-    /**
251
-     * Tell whether there is a recursive MM relation.
252
-     *
253
-     * @return bool
254
-     */
255
-    public function hasRecursiveMMRelation()
256
-    {
257
-        return isset($this->tableNameAliases['aliasIncrement'][$this->query->getType()]);
258
-    }
259
-
260
-    /**
261
-     * Returns the statement, ready to be executed.
262
-     *
263
-     * @param array $statementParts The SQL statement parts
264
-     * @return string The SQL statement
265
-     */
266
-    public function buildQuery(array $statementParts)
267
-    {
268
-        // Add more statement to the UNION part.
269
-        if (!empty($statementParts['unions'])) {
270
-            foreach ($statementParts['unions'] as $tableName => $unionPart) {
271
-                if (!empty($statementParts['additionalWhereClause'][$tableName])) {
272
-                    $statementParts['unions'][$tableName] .= ' AND ' . implode(' AND ', $statementParts['additionalWhereClause'][$tableName]);
273
-                }
274
-            }
275
-        }
276
-
277
-        $statement = 'SELECT ' . implode(' ', $statementParts['keywords']) . ' ' . implode(',', $statementParts['fields']) . ' FROM ' . implode(' ', $statementParts['tables']) . ' ' . implode(' ', $statementParts['unions']);
278
-        if (!empty($statementParts['where'])) {
279
-            $statement .= ' WHERE ' . implode('', $statementParts['where']);
280
-            if (!empty($statementParts['additionalWhereClause'][$this->query->getType()])) {
281
-                $statement .= ' AND ' . implode(' AND ', $statementParts['additionalWhereClause'][$this->query->getType()]);
282
-            }
283
-        } elseif (!empty($statementParts['additionalWhereClause'])) {
284
-            $statement .= ' WHERE ' . implode(' AND ', $statementParts['additionalWhereClause'][$this->query->getType()]);
285
-        }
286
-        if (!empty($statementParts['orderings'])) {
287
-            $statement .= ' ORDER BY ' . implode(', ', $statementParts['orderings']);
288
-        }
289
-        if (!empty($statementParts['limit'])) {
290
-            $statement .= ' LIMIT ' . $statementParts['limit'];
291
-        }
292
-
293
-        return $statement;
294
-    }
295
-
296
-    /**
297
-     * Transforms a Query Source into SQL and parameter arrays
298
-     *
299
-     * @param SourceInterface $source The source
300
-     * @param array &$sql
301
-     * @return void
302
-     */
303
-    protected function parseSource(SourceInterface $source, array &$sql)
304
-    {
305
-        $tableName = $this->getTableName();
306
-        $sql['fields'][$tableName] = $tableName . '.*';
307
-        if ($this->query->getDistinct()) {
308
-            $sql['fields'][$tableName] = $tableName . '.' . $this->query->getDistinct();
309
-            $sql['keywords']['distinct'] = 'DISTINCT';
310
-        }
311
-        $sql['tables'][$tableName] = $tableName;
312
-        $sql['mainTable'] = $tableName;
313
-    }
314
-
315
-    /**
316
-     * Transforms a constraint into SQL and parameter arrays
317
-     *
318
-     * @param ConstraintInterface $constraint The constraint
319
-     * @param SourceInterface $source The source
320
-     * @param array &$statementParts The query parts
321
-     * @param array &$parameters The parameters that will replace the markers
322
-     * @return void
323
-     */
324
-    protected function parseConstraint(ConstraintInterface $constraint = null, SourceInterface $source, array &$statementParts, array &$parameters)
325
-    {
326
-        if ($constraint instanceof AndInterface) {
327
-            $statementParts['where'][] = '(';
328
-            $this->parseConstraint($constraint->getConstraint1(), $source, $statementParts, $parameters);
329
-            $statementParts['where'][] = ' AND ';
330
-            $this->parseConstraint($constraint->getConstraint2(), $source, $statementParts, $parameters);
331
-            $statementParts['where'][] = ')';
332
-        } elseif ($constraint instanceof OrInterface) {
333
-            $statementParts['where'][] = '(';
334
-            $this->parseConstraint($constraint->getConstraint1(), $source, $statementParts, $parameters);
335
-            $statementParts['where'][] = ' OR ';
336
-            $this->parseConstraint($constraint->getConstraint2(), $source, $statementParts, $parameters);
337
-            $statementParts['where'][] = ')';
338
-        } elseif ($constraint instanceof NotInterface) {
339
-            $statementParts['where'][] = 'NOT (';
340
-            $this->parseConstraint($constraint->getConstraint(), $source, $statementParts, $parameters);
341
-            $statementParts['where'][] = ')';
342
-        } elseif ($constraint instanceof ComparisonInterface) {
343
-            $this->parseComparison($constraint, $source, $statementParts, $parameters);
344
-        }
345
-    }
346
-
347
-    /**
348
-     * Parse a Comparison into SQL and parameter arrays.
349
-     *
350
-     * @param ComparisonInterface $comparison The comparison to parse
351
-     * @param SourceInterface $source The source
352
-     * @param array &$statementParts SQL query parts to add to
353
-     * @param array &$parameters Parameters to bind to the SQL
354
-     * @return void
355
-     * @throws Exception\RepositoryException
356
-     */
357
-    protected function parseComparison(ComparisonInterface $comparison, SourceInterface $source, array &$statementParts, array &$parameters)
358
-    {
359
-        $operand1 = $comparison->getOperand1();
360
-        $operator = $comparison->getOperator();
361
-        $operand2 = $comparison->getOperand2();
362
-        if ($operator === QueryInterface::OPERATOR_IN) {
363
-            $items = [];
364
-            $hasValue = false;
365
-            foreach ($operand2 as $value) {
366
-                $value = $this->getPlainValue($value);
367
-                if ($value !== null) {
368
-                    $items[] = $value;
369
-                    $hasValue = true;
370
-                }
371
-            }
372
-            if ($hasValue === false) {
373
-                $statementParts['where'][] = '1<>1';
374
-            } else {
375
-                $this->parseDynamicOperand($operand1, $operator, $source, $statementParts, $parameters, null);
376
-                $parameters[] = $items;
377
-            }
378
-        } elseif ($operator === QueryInterface::OPERATOR_CONTAINS) {
379
-            if ($operand2 === null) {
380
-                $statementParts['where'][] = '1<>1';
381
-            } else {
382
-                throw new \Exception('Not implemented! Contact extension author.', 1412931227);
383
-                # @todo re-implement me if necessary.
384
-                #$tableName = $this->query->getType();
385
-                #$propertyName = $operand1->getPropertyName();
386
-                #while (strpos($propertyName, '.') !== false) {
387
-                #	$this->addUnionStatement($tableName, $propertyName, $statementParts);
388
-                #}
389
-                #$columnName = $propertyName;
390
-                #$columnMap = $propertyName;
391
-                #$typeOfRelation = $columnMap instanceof ColumnMap ? $columnMap->getTypeOfRelation() : null;
392
-                #if ($typeOfRelation === ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
393
-                #	$relationTableName = $columnMap->getRelationTableName();
394
-                #	$statementParts['where'][] = $tableName . '.uid IN (SELECT ' . $columnMap->getParentKeyFieldName() . ' FROM ' . $relationTableName . ' WHERE ' . $columnMap->getChildKeyFieldName() . '=?)';
395
-                #	$parameters[] = intval($this->getPlainValue($operand2));
396
-                #} elseif ($typeOfRelation === ColumnMap::RELATION_HAS_MANY) {
397
-                #	$parentKeyFieldName = $columnMap->getParentKeyFieldName();
398
-                #	if (isset($parentKeyFieldName)) {
399
-                #		$childTableName = $columnMap->getChildTableName();
400
-                #		$statementParts['where'][] = $tableName . '.uid=(SELECT ' . $childTableName . '.' . $parentKeyFieldName . ' FROM ' . $childTableName . ' WHERE ' . $childTableName . '.uid=?)';
401
-                #		$parameters[] = intval($this->getPlainValue($operand2));
402
-                #	} else {
403
-                #		$statementParts['where'][] = 'FIND_IN_SET(?,' . $tableName . '.' . $columnName . ')';
404
-                #		$parameters[] = intval($this->getPlainValue($operand2));
405
-                #	}
406
-                #} else {
407
-                #	throw new Exception\RepositoryException('Unsupported or non-existing property name "' . $propertyName . '" used in relation matching.', 1327065745);
408
-                #}
409
-            }
410
-        } else {
411
-            if ($operand2 === null) {
412
-                if ($operator === QueryInterface::OPERATOR_EQUAL_TO) {
413
-                    $operator = self::OPERATOR_EQUAL_TO_NULL;
414
-                } elseif ($operator === QueryInterface::OPERATOR_NOT_EQUAL_TO) {
415
-                    $operator = self::OPERATOR_NOT_EQUAL_TO_NULL;
416
-                }
417
-            }
418
-            $this->parseDynamicOperand($operand1, $operator, $source, $statementParts, $parameters);
419
-            $parameters[] = $this->getPlainValue($operand2);
420
-        }
421
-    }
422
-
423
-    /**
424
-     * Returns a plain value, i.e. objects are flattened if possible.
425
-     *
426
-     * @param mixed $input
427
-     * @return mixed
428
-     * @throws UnexpectedTypeException
429
-     */
430
-    protected function getPlainValue($input)
431
-    {
432
-        if (is_array($input)) {
433
-            throw new UnexpectedTypeException('An array could not be converted to a plain value.', 1274799932);
434
-        }
435
-        if ($input instanceof \DateTime) {
436
-            return $input->format('U');
437
-        } elseif (is_object($input)) {
438
-            if ($input instanceof LazyLoadingProxy) {
439
-                $realInput = $input->_loadRealInstance();
440
-            } else {
441
-                $realInput = $input;
442
-            }
443
-            if ($realInput instanceof DomainObjectInterface) {
444
-                return $realInput->getUid();
445
-            } else {
446
-                throw new UnexpectedTypeException('An object of class "' . get_class($realInput) . '" could not be converted to a plain value.', 1274799934);
447
-            }
448
-        } elseif (is_bool($input)) {
449
-            return $input === true ? 1 : 0;
450
-        } else {
451
-            return $input;
452
-        }
453
-    }
454
-
455
-    /**
456
-     * Parse a DynamicOperand into SQL and parameter arrays.
457
-     *
458
-     * @param DynamicOperandInterface $operand
459
-     * @param string $operator One of the JCR_OPERATOR_* constants
460
-     * @param SourceInterface $source The source
461
-     * @param array &$statementParts The query parts
462
-     * @param array &$parameters The parameters that will replace the markers
463
-     * @param string $valueFunction an optional SQL function to apply to the operand value
464
-     * @return void
465
-     */
466
-    protected function parseDynamicOperand(DynamicOperandInterface $operand, $operator, SourceInterface $source, array &$statementParts, array &$parameters, $valueFunction = null)
467
-    {
468
-        if ($operand instanceof LowerCaseInterface) {
469
-            $this->parseDynamicOperand($operand->getOperand(), $operator, $source, $statementParts, $parameters, 'LOWER');
470
-        } elseif ($operand instanceof UpperCaseInterface) {
471
-            $this->parseDynamicOperand($operand->getOperand(), $operator, $source, $statementParts, $parameters, 'UPPER');
472
-        } elseif ($operand instanceof PropertyValueInterface) {
473
-            $propertyName = $operand->getPropertyName();
474
-
475
-            // Reset value.
476
-            $this->currentChildTableNameAlias = '';
477
-
478
-            if ($source instanceof SelectorInterface) {
479
-                $tableName = $this->query->getType();
480
-                while (strpos($propertyName, '.') !== false) {
481
-                    $this->addUnionStatement($tableName, $propertyName, $statementParts);
482
-                }
483
-            } elseif ($source instanceof JoinInterface) {
484
-                $tableName = $source->getJoinCondition()->getSelector1Name();
485
-            }
486
-
487
-            $columnName = $propertyName;
488
-            $resolvedOperator = $this->resolveOperator($operator);
489
-            $constraintSQL = '';
490
-
491
-            $marker = $operator === QueryInterface::OPERATOR_IN
492
-                ? '(?)'
493
-                : '?';
494
-
495
-            if ($valueFunction === null) {
496
-                $constraintSQL .= (!empty($tableName) ? $tableName . '.' : '') . $columnName . ' ' . $resolvedOperator . ' ' . $marker;
497
-            } else {
498
-                $constraintSQL .= $valueFunction . '(' . (!empty($tableName) ? $tableName . '.' : '') . $columnName . ') ' . $resolvedOperator . ' ' . $marker;
499
-            }
500
-
501
-            if (isset($tableName) && !empty($this->currentChildTableNameAlias)) {
502
-                $constraintSQL = $this->replaceTableNameByAlias($tableName, $this->currentChildTableNameAlias, $constraintSQL);
503
-            }
504
-            $statementParts['where'][] = $constraintSQL;
505
-        }
506
-    }
507
-
508
-    /**
509
-     * @param string &$tableName
510
-     * @param string &$propertyPath
511
-     * @param array &$statementParts
512
-     */
513
-    protected function addUnionStatement(&$tableName, &$propertyPath, array &$statementParts)
514
-    {
515
-        $table = Tca::table($tableName);
516
-
517
-        $explodedPropertyPath = explode('.', $propertyPath, 2);
518
-        $fieldName = $explodedPropertyPath[0];
519
-
520
-        // Field of type "group" are special because property path must contain the table name
521
-        // to determine the relation type. Example for sys_category, property path will look like "items.sys_file"
522
-        $parts = explode('.', $propertyPath, 3);
523
-        if ($table->field($fieldName)->isGroup() && count($parts) > 2) {
524
-            $explodedPropertyPath[0] = $parts[0] . '.' . $parts[1];
525
-            $explodedPropertyPath[1] = $parts[2];
526
-            $fieldName = $explodedPropertyPath[0];
527
-        }
528
-
529
-        $parentKeyFieldName = $table->field($fieldName)->getForeignField();
530
-        $childTableName = $table->field($fieldName)->getForeignTable();
531
-
532
-        if ($childTableName === null) {
533
-            throw new InvalidRelationConfigurationException('The relation information for property "' . $fieldName . '" of class "' . $tableName . '" is missing.', 1353170925);
534
-        }
535
-
536
-        if ($table->field($fieldName)->hasOne()) { // includes relation "one-to-one" and "many-to-one"
537
-            // sometimes the opposite relation is not defined. We don't want to force this config for backward compatibility reasons.
538
-            // $parentKeyFieldName === null does the trick somehow. Before condition was if (isset($parentKeyFieldName))
539
-            if ($table->field($fieldName)->hasRelationManyToOne() || $parentKeyFieldName === null) {
540
-                $statementParts['unions'][$childTableName] = 'LEFT JOIN ' . $childTableName . ' ON ' . $tableName . '.' . $fieldName . '=' . $childTableName . '.uid';
541
-            } else {
542
-                $statementParts['unions'][$childTableName] = 'LEFT JOIN ' . $childTableName . ' ON ' . $tableName . '.uid=' . $childTableName . '.' . $parentKeyFieldName;
543
-            }
544
-        } elseif ($table->field($fieldName)->hasRelationManyToMany()) {
545
-            $relationTableName = $table->field($fieldName)->getManyToManyTable();
546
-
547
-            $parentKeyFieldName = $table->field($fieldName)->isOppositeRelation() ? 'uid_foreign' : 'uid_local';
548
-            $childKeyFieldName = !$table->field($fieldName)->isOppositeRelation() ? 'uid_foreign' : 'uid_local';
549
-
550
-            // MM table e.g sys_category_record_mm
551
-            $relationTableNameAlias = $this->generateAlias($relationTableName);
552
-            $join = sprintf(
553
-                'LEFT JOIN %s AS %s ON %s.uid=%s.%s',
554
-                $relationTableName,
555
-                $relationTableNameAlias,
556
-                $tableName,
557
-                $relationTableNameAlias,
558
-                $parentKeyFieldName
559
-            );
560
-            $statementParts['unions'][$relationTableNameAlias] = $join;
561
-
562
-            // Foreign table e.g sys_category
563
-            $childTableNameAlias = $this->generateAlias($childTableName);
564
-            $this->currentChildTableNameAlias = $childTableNameAlias;
565
-            $join = sprintf(
566
-                'LEFT JOIN %s AS %s ON %s.%s=%s.uid',
567
-                $childTableName,
568
-                $childTableNameAlias,
569
-                $relationTableNameAlias,
570
-                $childKeyFieldName,
571
-                $childTableNameAlias
572
-            );
573
-            $statementParts['unions'][$childTableNameAlias] = $join;
574
-
575
-            // Find a possible table name for a MM condition.
576
-            $tableNameCondition = $table->field($fieldName)->getAdditionalTableNameCondition();
577
-            if ($tableNameCondition) {
578
-                // If we can find a source file name,  we can then retrieve more MM conditions from the TCA such as a field name.
579
-                $sourceFileName = $this->query->getSourceFieldName();
580
-                if (empty($sourceFileName)) {
581
-                    $additionalMMConditions = array(
582
-                        'tablenames' => $tableNameCondition,
583
-                    );
584
-                } else {
585
-                    $additionalMMConditions = Tca::table($tableNameCondition)->field($sourceFileName)->getAdditionalMMCondition();
586
-                }
587
-
588
-                foreach ($additionalMMConditions as $additionalFieldName => $additionalMMCondition) {
589
-                    $additionalJoin = sprintf(' AND %s.%s = "%s"', $relationTableNameAlias, $additionalFieldName, $additionalMMCondition);
590
-                    $statementParts['unions'][$relationTableNameAlias] .= $additionalJoin;
591
-
592
-                    $additionalJoin = sprintf(' AND %s.%s = "%s"', $relationTableNameAlias, $additionalFieldName, $additionalMMCondition);
593
-                    $statementParts['unions'][$childTableNameAlias] .= $additionalJoin;
594
-                }
595
-            }
596
-        } elseif ($table->field($fieldName)->hasMany()) { // includes relations "many-to-one" and "csv" relations
597
-            $childTableNameAlias = $this->generateAlias($childTableName);
598
-            $this->currentChildTableNameAlias = $childTableNameAlias;
599
-
600
-            if (isset($parentKeyFieldName)) {
601
-                $join = sprintf(
602
-                    'LEFT JOIN %s AS %s ON %s.uid=%s.%s',
603
-                    $childTableName,
604
-                    $childTableNameAlias,
605
-                    $tableName,
606
-                    $childTableNameAlias,
607
-                    $parentKeyFieldName
608
-                );
609
-                $statementParts['unions'][$childTableNameAlias] = $join;
610
-            } else {
611
-                $join = sprintf(
612
-                    'LEFT JOIN %s AS %s ON (FIND_IN_SET(%s.uid, %s.%s))',
613
-                    $childTableName,
614
-                    $childTableNameAlias,
615
-                    $childTableNameAlias,
616
-                    $tableName,
617
-                    $fieldName
618
-                );
619
-                $statementParts['unions'][$childTableNameAlias] = $join;
620
-            }
621
-        } else {
622
-            throw new Exception('Could not determine type of relation.', 1252502725);
623
-        }
624
-
625
-        $statementParts['keywords']['distinct'] = 'DISTINCT';
626
-        $propertyPath = $explodedPropertyPath[1];
627
-        $tableName = $childTableName;
628
-    }
629
-
630
-    /**
631
-     * Returns the SQL operator for the given JCR operator type.
632
-     *
633
-     * @param string $operator One of the JCR_OPERATOR_* constants
634
-     * @return string an SQL operator
635
-     * @throws Exception
636
-     */
637
-    protected function resolveOperator($operator)
638
-    {
639
-        switch ($operator) {
640
-            case self::OPERATOR_EQUAL_TO_NULL:
641
-                $operator = 'IS';
642
-                break;
643
-            case self::OPERATOR_NOT_EQUAL_TO_NULL:
644
-                $operator = 'IS NOT';
645
-                break;
646
-            case QueryInterface::OPERATOR_IN:
647
-                $operator = 'IN';
648
-                break;
649
-            case QueryInterface::OPERATOR_EQUAL_TO:
650
-                $operator = '=';
651
-                break;
652
-            case QueryInterface::OPERATOR_NOT_EQUAL_TO:
653
-                $operator = '!=';
654
-                break;
655
-            case QueryInterface::OPERATOR_LESS_THAN:
656
-                $operator = '<';
657
-                break;
658
-            case QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO:
659
-                $operator = '<=';
660
-                break;
661
-            case QueryInterface::OPERATOR_GREATER_THAN:
662
-                $operator = '>';
663
-                break;
664
-            case QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO:
665
-                $operator = '>=';
666
-                break;
667
-            case QueryInterface::OPERATOR_LIKE:
668
-                $operator = 'LIKE';
669
-                break;
670
-            default:
671
-                throw new Exception('Unsupported operator encountered.', 1242816073);
672
-        }
673
-        return $operator;
674
-    }
675
-
676
-    /**
677
-     * Adds additional WHERE statements according to the query settings.
678
-     *
679
-     * @param QuerySettingsInterface $querySettings The TYPO3 CMS specific query settings
680
-     * @param string $tableNameOrAlias The table name to add the additional where clause for
681
-     * @param array &$statementParts
682
-     * @return void
683
-     */
684
-    protected function addAdditionalWhereClause(QuerySettingsInterface $querySettings, $tableNameOrAlias, &$statementParts)
685
-    {
686
-        $this->addVisibilityConstraintStatement($querySettings, $tableNameOrAlias, $statementParts);
687
-        if ($querySettings->getRespectSysLanguage()) {
688
-            $this->addSysLanguageStatement($tableNameOrAlias, $statementParts, $querySettings);
689
-        }
690
-    }
691
-
692
-    /**
693
-     * Adds enableFields and deletedClause to the query if necessary
694
-     *
695
-     * @param QuerySettingsInterface $querySettings
696
-     * @param string $tableNameOrAlias The database table name
697
-     * @param array &$statementParts The query parts
698
-     * @return void
699
-     */
700
-    protected function addVisibilityConstraintStatement(QuerySettingsInterface $querySettings, $tableNameOrAlias, array &$statementParts)
701
-    {
702
-        $statement = '';
703
-        $tableName = $this->resolveTableNameAlias($tableNameOrAlias);
704
-        if (is_array($GLOBALS['TCA'][$tableName]['ctrl'])) {
705
-            $ignoreEnableFields = $querySettings->getIgnoreEnableFields();
706
-            $enableFieldsToBeIgnored = $querySettings->getEnableFieldsToBeIgnored();
707
-            $includeDeleted = $querySettings->getIncludeDeleted();
708
-            if (Typo3Mode::isFrontendMode()) {
709
-                $statement .= $this->getFrontendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $enableFieldsToBeIgnored, $includeDeleted);
710
-            } else {
711
-                // 'BE' case
712
-                $statement .= $this->getBackendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $includeDeleted);
713
-            }
714
-
715
-            // Remove the prefixing "AND" if any.
716
-            if (!empty($statement)) {
717
-                $statement = strtolower(substr($statement, 1, 3)) === 'and' ? substr($statement, 5) : $statement;
718
-                $statementParts['additionalWhereClause'][$tableNameOrAlias][] = $statement;
719
-            }
720
-        }
721
-    }
722
-
723
-    /**
724
-     * Returns constraint statement for frontend context
725
-     *
726
-     * @param string $tableNameOrAlias
727
-     * @param boolean $ignoreEnableFields A flag indicating whether the enable fields should be ignored
728
-     * @param array $enableFieldsToBeIgnored If $ignoreEnableFields is true, this array specifies enable fields to be ignored. If it is null or an empty array (default) all enable fields are ignored.
729
-     * @param boolean $includeDeleted A flag indicating whether deleted records should be included
730
-     * @return string
731
-     * @throws Exception\InconsistentQuerySettingsException
732
-     */
733
-    protected function getFrontendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $enableFieldsToBeIgnored, $includeDeleted)
734
-    {
735
-        $statement = '';
736
-        $tableName = $this->resolveTableNameAlias($tableNameOrAlias);
737
-        if ($ignoreEnableFields && !$includeDeleted) {
738
-            if (count($enableFieldsToBeIgnored)) {
739
-                // array_combine() is necessary because of the way \TYPO3\CMS\Frontend\Page\PageRepository::enableFields() is implemented
740
-                $statement .= $this->getPageRepository()->enableFields($tableName, -1, array_combine($enableFieldsToBeIgnored, $enableFieldsToBeIgnored));
741
-            } else {
742
-                $statement .= $this->getPageRepository()->deleteClause($tableName);
743
-            }
744
-        } elseif (!$ignoreEnableFields && !$includeDeleted) {
745
-            $statement .= $this->getPageRepository()->enableFields($tableName);
746
-        } elseif (!$ignoreEnableFields && $includeDeleted) {
747
-            throw new InconsistentQuerySettingsException('Query setting "ignoreEnableFields=false" can not be used together with "includeDeleted=true" in frontend context.', 1327678173);
748
-        }
749
-        return $this->replaceTableNameByAlias($tableName, $tableNameOrAlias, $statement);
750
-    }
751
-
752
-    /**
753
-     * Returns constraint statement for backend context
754
-     *
755
-     * @param string $tableNameOrAlias
756
-     * @param boolean $ignoreEnableFields A flag indicating whether the enable fields should be ignored
757
-     * @param boolean $includeDeleted A flag indicating whether deleted records should be included
758
-     * @return string
759
-     */
760
-    protected function getBackendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $includeDeleted)
761
-    {
762
-        $tableName = $this->resolveTableNameAlias($tableNameOrAlias);
763
-        $statement = '';
764
-        if (!$ignoreEnableFields) {
765
-            $statement .= BackendUtility::BEenableFields($tableName);
766
-        }
767
-
768
-        // If the table is found to have "workspace" support, add the corresponding fields in the statement.
769
-        if (Tca::table($tableName)->hasWorkspaceSupport()) {
770
-            if ($this->getBackendUser()->workspace === 0) {
771
-                $statement .= ' AND ' . $tableName . '.t3ver_state<=' . new VersionState(VersionState::DEFAULT_STATE);
772
-            } else {
773
-                // Show only records of live and of the current workspace
774
-                // In case we are in a Versioning preview
775
-                $statement .= ' AND (' .
776
-                    $tableName . '.t3ver_wsid=0 OR ' .
777
-                    $tableName . '.t3ver_wsid=' . (int)$this->getBackendUser()->workspace .
778
-                    ')';
779
-            }
780
-
781
-            // Check if this segment make sense here or whether it should be in the "if" part when we have workspace = 0
782
-            $statement .= ' AND ' . $tableName . '.pid<>-1';
783
-        }
784
-
785
-        if (!$includeDeleted) {
786
-            $statement .= BackendUtility::deleteClause($tableName);
787
-        }
788
-
789
-        return $this->replaceTableNameByAlias($tableName, $tableNameOrAlias, $statement);
790
-    }
791
-
792
-    /**
793
-     * Builds the language field statement
794
-     *
795
-     * @param string $tableNameOrAlias The database table name
796
-     * @param array &$statementParts The query parts
797
-     * @param QuerySettingsInterface $querySettings The TYPO3 CMS specific query settings
798
-     * @return void
799
-     * @throws Exception
800
-     */
801
-    protected function addSysLanguageStatement($tableNameOrAlias, array &$statementParts, $querySettings)
802
-    {
803
-        $tableName = $this->resolveTableNameAlias($tableNameOrAlias);
804
-        if (is_array($GLOBALS['TCA'][$tableName]['ctrl'])) {
805
-            if (!empty($GLOBALS['TCA'][$tableName]['ctrl']['languageField'])) {
806
-                // Select all entries for the current language
807
-                $additionalWhereClause = $tableNameOrAlias . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . ' IN (' . intval($querySettings->getLanguageUid()) . ',-1)';
808
-                // If any language is set -> get those entries which are not translated yet
809
-                // They will be removed by t3lib_page::getRecordOverlay if not matching overlay mode
810
-                if (isset($GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'])
811
-                    && $querySettings->getLanguageUid() > 0
812
-                ) {
813
-                    $additionalWhereClause .= ' OR (' . $tableNameOrAlias . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . '=0' .
814
-                        ' AND ' . $tableNameOrAlias . '.uid NOT IN (SELECT ' . $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'] .
815
-                        ' FROM ' . $tableName .
816
-                        ' WHERE ' . $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'] . '>0' .
817
-                        ' AND ' . $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . '>0';
818
-
819
-                    // Add delete clause to ensure all entries are loaded
820
-                    if (isset($GLOBALS['TCA'][$tableName]['ctrl']['delete'])) {
821
-                        $additionalWhereClause .= ' AND ' . $tableNameOrAlias . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['delete'] . '=0';
822
-                    }
823
-                    $additionalWhereClause .= '))';
824
-                }
825
-                $statementParts['additionalWhereClause'][$tableNameOrAlias][] = '(' . $additionalWhereClause . ')';
826
-            }
827
-        }
828
-    }
829
-
830
-    /**
831
-     * Transforms orderings into SQL.
832
-     *
833
-     * @param array $orderings An array of orderings (Tx_Extbase_Persistence_QOM_Ordering)
834
-     * @param SourceInterface $source The source
835
-     * @param array &$statementParts The query parts
836
-     * @return void
837
-     * @throws Exception\UnsupportedOrderException
838
-     */
839
-    protected function parseOrderings(array $orderings, SourceInterface $source, array &$statementParts)
840
-    {
841
-        foreach ($orderings as $fieldNameAndPath => $order) {
842
-            switch ($order) {
843
-                case QueryInterface::ORDER_ASCENDING:
844
-                    $order = 'ASC';
845
-                    break;
846
-                case QueryInterface::ORDER_DESCENDING:
847
-                    $order = 'DESC';
848
-                    break;
849
-                default:
850
-                    throw new UnsupportedOrderException('Unsupported order encountered.', 1456845126);
851
-            }
852
-
853
-            $tableName = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $this->query->getType());
854
-            $fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $tableName);
855
-            $statementParts['orderings'][] = sprintf('%s.%s %s', $tableName, $fieldName, $order);
856
-        }
857
-    }
858
-
859
-    /**
860
-     * Transforms limit and offset into SQL
861
-     *
862
-     * @param int $limit
863
-     * @param int $offset
864
-     * @param array &$statementParts
865
-     * @return void
866
-     */
867
-    protected function parseLimitAndOffset($limit, $offset, array &$statementParts)
868
-    {
869
-        if ($limit !== null && $offset !== null) {
870
-            $statementParts['limit'] = intval($offset) . ', ' . intval($limit);
871
-        } elseif ($limit !== null) {
872
-            $statementParts['limit'] = intval($limit);
873
-        }
874
-    }
875
-
876
-    /**
877
-     * @param array $rows
878
-     * @return array
879
-     */
880
-    protected function getContentObjects(array $rows): array
881
-    {
882
-        $contentObjects = [];
883
-        foreach ($rows as $row) {
884
-            // Get language uid from querySettings.
885
-            // Ensure the backend handling is not broken (fallback to Get parameter 'L' if needed)
886
-            $overlaidRow = $this->doLanguageAndWorkspaceOverlay(
887
-                $row,
888
-                $this->query->getTypo3QuerySettings()
889
-            );
890
-
891
-            $contentObjects[] = GeneralUtility::makeInstance(
892
-                Content::class,
893
-                $this->query->getType(),
894
-                $overlaidRow
895
-            );
896
-        }
897
-
898
-        return $contentObjects;
899
-    }
900
-
901
-    /**
902
-     * Performs workspace and language overlay on the given row array. The language and workspace id is automatically
903
-     * detected (depending on FE or BE context). You can also explicitly set the language/workspace id.
904
-     *
905
-     * @param array $row
906
-     * @param QuerySettingsInterface $querySettings The TYPO3 CMS specific query settings
907
-     * @return array
908
-     */
909
-    protected function doLanguageAndWorkspaceOverlay(array $row, $querySettings)
910
-    {
911
-        $tableName = $this->getTableName();
912
-
913
-        $pageRepository = $this->getPageRepository();
914
-        if (is_object($GLOBALS['TSFE'])) {
915
-            $languageMode = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('language', 'legacyLanguageMode');
916
-        #if ($this->isBackendUserLogged() && $this->getBackendUser()->workspace !== 0) {
917
-            #    $pageRepository->versioningWorkspaceId = $this->getBackendUser()->workspace;
918
-        #}
919
-        } else {
920
-            $languageMode = '';
921
-            $workspaceUid = $this->getBackendUser()->workspace;
922
-            #$pageRepository->versioningWorkspaceId = $workspaceUid;
923
-            #if ($this->getBackendUser()->workspace !== 0) {
924
-            #    $pageRepository->versioningPreview = 1;
925
-            #}
926
-        }
927
-
928
-        // If current row is a translation select its parent
929
-        if (isset($GLOBALS['TCA'][$tableName]['ctrl']['languageField'])
930
-            && isset($GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'])
931
-        ) {
932
-            if (isset($row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']])
933
-                && $row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']] > 0
934
-            ) {
935
-                $queryBuilder = $this->getQueryBuilder();
936
-                $row = $queryBuilder
937
-                    ->select($tableName . '.*')
938
-                    ->from($tableName)
939
-                    ->andWhere(
940
-                        $tableName . '.uid=' . (int)$row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']],
941
-                        $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . ' = 0'
942
-                    )
943
-                    ->execute()
944
-                    ->fetch();
945
-            }
946
-        }
947
-
948
-        // Retrieve the original uid; Used for Workspaces!
949
-        if (Typo3Mode::isBackendMode()) {
950
-            $pageRepository->versionOL($tableName, $row, true, true);
951
-        } else {
952
-            \TYPO3\CMS\Backend\Utility\BackendUtility::workspaceOL($tableName, $row);
953
-        }
954
-        if (isset($row['_ORIG_uid'])) {
955
-            $row['uid'] = $row['_ORIG_uid'];
956
-        }
957
-
958
-        // Special case for table "pages"
959
-        if ($tableName == 'pages') {
960
-            $row = $pageRepository->getPageOverlay($row, $querySettings->getLanguageUid());
961
-        } elseif (isset($GLOBALS['TCA'][$tableName]['ctrl']['languageField'])
962
-            && $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] !== ''
963
-        ) {
964
-            if (in_array($row[$GLOBALS['TCA'][$tableName]['ctrl']['languageField']], array(-1, 0))) {
965
-                $overlayMode = $languageMode === 'strict' ? 'hideNonTranslated' : '';
966
-                $row = $pageRepository->getRecordOverlay($tableName, $row, $querySettings->getLanguageUid(), $overlayMode);
967
-            }
968
-        }
969
-
970
-        return $row;
971
-    }
972
-
973
-    /**
974
-     * Return a resolved table name given a possible table name alias.
975
-     *
976
-     * @param string $tableNameOrAlias
977
-     * @return string
978
-     */
979
-    protected function resolveTableNameAlias($tableNameOrAlias)
980
-    {
981
-        $resolvedTableName = $tableNameOrAlias;
982
-        if (!empty($this->tableNameAliases['aliases'][$tableNameOrAlias])) {
983
-            $resolvedTableName = $this->tableNameAliases['aliases'][$tableNameOrAlias];
984
-        }
985
-        return $resolvedTableName;
986
-    }
987
-
988
-    /**
989
-     * Generate a unique table name alias for the given table name.
990
-     *
991
-     * @param string $tableName
992
-     * @return string
993
-     */
994
-    protected function generateAlias($tableName)
995
-    {
996
-        if (!isset($this->tableNameAliases['aliasIncrement'][$tableName])) {
997
-            $this->tableNameAliases['aliasIncrement'][$tableName] = 0;
998
-        }
999
-
1000
-        $numberOfAliases = $this->tableNameAliases['aliasIncrement'][$tableName];
1001
-        $tableNameAlias = $tableName . $numberOfAliases;
1002
-
1003
-        $this->tableNameAliases['aliasIncrement'][$tableName]++;
1004
-        $this->tableNameAliases['aliases'][$tableNameAlias] = $tableName;
1005
-
1006
-        return $tableNameAlias;
1007
-    }
1008
-
1009
-    /**
1010
-     * Replace the table names by its table name alias within the given statement.
1011
-     *
1012
-     * @param string $tableName
1013
-     * @param string $tableNameAlias
1014
-     * @param string $statement
1015
-     * @return string
1016
-     */
1017
-    protected function replaceTableNameByAlias($tableName, $tableNameAlias, $statement)
1018
-    {
1019
-        if ($statement && $tableName !== $tableNameAlias) {
1020
-            $statement = str_replace($tableName, $tableNameAlias, $statement);
1021
-        }
1022
-        return $statement;
1023
-    }
1024
-
1025
-    /**
1026
-     * Returns an instance of the current Backend User.
1027
-     *
1028
-     * @return BackendUserAuthentication
1029
-     */
1030
-    protected function getBackendUser()
1031
-    {
1032
-        return $GLOBALS['BE_USER'];
1033
-    }
1034
-
1035
-    /**
1036
-     * Tell whether a Backend User is logged in.
1037
-     *
1038
-     * @return bool
1039
-     */
1040
-    protected function isBackendUserLogged()
1041
-    {
1042
-        return is_object($GLOBALS['BE_USER']);
1043
-    }
1044
-
1045
-    /**
1046
-     * @return PageRepository|object
1047
-     */
1048
-    protected function getPageRepository()
1049
-    {
1050
-        if (!$this->pageRepository instanceof PageRepository) {
1051
-            if (Typo3Mode::isFrontendMode() && is_object($GLOBALS['TSFE'])) {
1052
-                $this->pageRepository = $GLOBALS['TSFE']->sys_page;
1053
-            } else {
1054
-                $this->pageRepository = GeneralUtility::makeInstance(PageRepository::class);
1055
-            }
1056
-        }
1057
-
1058
-        return $this->pageRepository;
1059
-    }
1060
-
1061
-    /**
1062
-     * @return FieldPathResolver|object
1063
-     */
1064
-    protected function getFieldPathResolver()
1065
-    {
1066
-        return GeneralUtility::makeInstance(FieldPathResolver::class);
1067
-    }
1068
-
1069
-    /**
1070
-     * @return object|Connection
1071
-     */
1072
-    protected function getConnection(): Connection
1073
-    {
1074
-        /** @var ConnectionPool $connectionPool */
1075
-        return GeneralUtility::makeInstance(ConnectionPool::class)
1076
-            ->getConnectionForTable($this->getTableName());
1077
-    }
1078
-
1079
-    /**
1080
-     * @return object|QueryBuilder
1081
-     */
1082
-    protected function getQueryBuilder(): QueryBuilder
1083
-    {
1084
-        /** @var ConnectionPool $connectionPool */
1085
-        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
1086
-        return $connectionPool->getQueryBuilderForTable($this->getTableName());
1087
-    }
1088
-
1089
-    /**
1090
-     * @return string
1091
-     */
1092
-    public function getTableName(): string
1093
-    {
1094
-        return $this->query->getSource()->getNodeTypeName(); // getSelectorName()
1095
-    }
56
+	public const OPERATOR_EQUAL_TO_NULL = 'operatorEqualToNull';
57
+	public const OPERATOR_NOT_EQUAL_TO_NULL = 'operatorNotEqualToNull';
58
+
59
+	/**
60
+	 * The TYPO3 page repository. Used for language and workspace overlay
61
+	 *
62
+	 * @var PageRepository
63
+	 */
64
+	protected $pageRepository;
65
+
66
+	/**
67
+	 * @var Query
68
+	 */
69
+	protected $query;
70
+
71
+	/**
72
+	 * Store some info related to table name and its aliases.
73
+	 *
74
+	 * @var array
75
+	 */
76
+	protected $tableNameAliases = array(
77
+		'aliases' => [],
78
+		'aliasIncrement' => [],
79
+	);
80
+
81
+	/**
82
+	 * Use to store the current foreign table name alias.
83
+	 *
84
+	 * @var string
85
+	 */
86
+	protected $currentChildTableNameAlias = '';
87
+
88
+	/**
89
+	 * @param Query $query
90
+	 */
91
+	public function __construct(Query $query)
92
+	{
93
+		$this->query = $query;
94
+	}
95
+
96
+	/**
97
+	 * @param $parameters
98
+	 * @return array
99
+	 */
100
+	protected static function getTypes($parameters)
101
+	{
102
+		$types = [];
103
+		foreach ($parameters as $parameter) {
104
+			if (is_array($parameter)) {
105
+				if (MathUtility::canBeInterpretedAsInteger($parameter[0])) {
106
+					$types[] = \Doctrine\DBAL\Connection::PARAM_INT_ARRAY;
107
+				} else {
108
+					$types[] = \Doctrine\DBAL\Connection::PARAM_STR_ARRAY;
109
+				}
110
+			} else {
111
+				if (MathUtility::canBeInterpretedAsInteger($parameter)) {
112
+					$types[] = ParameterType::INTEGER;
113
+				} else {
114
+					$types[] = ParameterType::STRING;
115
+				}
116
+			}
117
+		}
118
+		return $types;
119
+	}
120
+
121
+	/**
122
+	 * Returns the result of the query
123
+	 */
124
+	public function fetchResult()
125
+	{
126
+		$parameters = [];
127
+		$statementParts = $this->parseQuery($parameters);
128
+		$statementParts = $this->processStatementStructureForRecursiveMMRelation($statementParts);
129
+		$sql = $this->buildQuery($statementParts);
130
+		//print $sql; exit();
131
+
132
+		$rows = $this->getConnection()
133
+			->executeQuery($sql, $parameters, self::getTypes($parameters))
134
+			->fetchAllAssociative();
135
+
136
+		return $this->getContentObjects($rows);
137
+	}
138
+
139
+	/**
140
+	 * Returns the number of tuples matching the query.
141
+	 *
142
+	 * @return int The number of matching tuples
143
+	 */
144
+	public function countResult()
145
+	{
146
+		$parameters = [];
147
+		$statementParts = $this->parseQuery($parameters);
148
+		$statementParts = $this->processStatementStructureForRecursiveMMRelation($statementParts);
149
+		$types = self::getTypes($parameters);
150
+
151
+		// if limit is set, we need to count the rows "manually" as COUNT(*) ignores LIMIT constraints
152
+		if (!empty($statementParts['limit'])) {
153
+			$sql = $this->buildQuery($statementParts);
154
+
155
+			$count = $this
156
+				->getConnection()
157
+				->executeQuery($sql, $parameters, $types)
158
+				->rowCount();
159
+		} else {
160
+			$statementParts['fields'] = array('COUNT(*)');
161
+			// having orderings without grouping is not compatible with non-MySQL DBMS
162
+			$statementParts['orderings'] = [];
163
+			if (isset($statementParts['keywords']['distinct'])) {
164
+				unset($statementParts['keywords']['distinct']);
165
+				$distinctField = $this->query->getDistinct() ? $this->query->getDistinct() : 'uid';
166
+				$statementParts['fields'] = array('COUNT(DISTINCT ' . $statementParts['mainTable'] . '.' . $distinctField . ')');
167
+			}
168
+
169
+			$sql = $this->buildQuery($statementParts);
170
+			$count = $this
171
+				->getConnection()
172
+				->executeQuery($sql, $parameters, $types)
173
+				->fetchColumn(0);
174
+		}
175
+		return (int)$count;
176
+	}
177
+
178
+	/**
179
+	 * Parses the query and returns the SQL statement parts.
180
+	 *
181
+	 * @param array &$parameters
182
+	 * @return array
183
+	 */
184
+	public function parseQuery(array &$parameters)
185
+	{
186
+		$statementParts = [];
187
+		$statementParts['keywords'] = [];
188
+		$statementParts['tables'] = [];
189
+		$statementParts['unions'] = [];
190
+		$statementParts['fields'] = [];
191
+		$statementParts['where'] = [];
192
+		$statementParts['additionalWhereClause'] = [];
193
+		$statementParts['orderings'] = [];
194
+		$statementParts['limit'] = [];
195
+		$query = $this->query;
196
+		$source = $query->getSource();
197
+		$this->parseSource($source, $statementParts);
198
+		$this->parseConstraint($query->getConstraint(), $source, $statementParts, $parameters);
199
+		$this->parseOrderings($query->getOrderings(), $source, $statementParts);
200
+		$this->parseLimitAndOffset($query->getLimit(), $query->getOffset(), $statementParts);
201
+		$tableNames = array_unique(array_keys($statementParts['tables'] + $statementParts['unions']));
202
+		foreach ($tableNames as $tableNameOrAlias) {
203
+			if (is_string($tableNameOrAlias) && strlen($tableNameOrAlias) > 0) {
204
+				$this->addAdditionalWhereClause($query->getTypo3QuerySettings(), $tableNameOrAlias, $statementParts);
205
+			}
206
+		}
207
+
208
+		return $statementParts;
209
+	}
210
+
211
+	/**
212
+	 * Fiddle with the statement structure to handle recursive MM relations.
213
+	 * For the recursive MM query to work, we must invert some values.
214
+	 * Let see if that is the best way of doing that...
215
+	 *
216
+	 * @param array $statementParts
217
+	 * @return array
218
+	 */
219
+	public function processStatementStructureForRecursiveMMRelation(array $statementParts)
220
+	{
221
+		if ($this->hasRecursiveMMRelation()) {
222
+			$tableName = $this->query->getType();
223
+
224
+			// In order the MM query to work for a recursive MM query, we must invert some values.
225
+			// tx_domain_model_foo0 (the alias) <--> tx_domain_model_foo (the origin table name)
226
+			$values = [];
227
+			foreach ($statementParts['fields'] as $key => $value) {
228
+				$values[$key] = str_replace($tableName, $tableName . '0', $value);
229
+			}
230
+			$statementParts['fields'] = $values;
231
+
232
+			// Same comment as above.
233
+			$values = [];
234
+			foreach ($statementParts['where'] as $key => $value) {
235
+				$values[$key] = str_replace($tableName . '0', $tableName, $value);
236
+			}
237
+			$statementParts['where'] = $values;
238
+
239
+			// We must be more restrictive by transforming the "left" union by "inner"
240
+			$values = [];
241
+			foreach ($statementParts['unions'] as $key => $value) {
242
+				$values[$key] = str_replace('LEFT JOIN', 'INNER JOIN', $value);
243
+			}
244
+			$statementParts['unions'] = $values;
245
+		}
246
+
247
+		return $statementParts;
248
+	}
249
+
250
+	/**
251
+	 * Tell whether there is a recursive MM relation.
252
+	 *
253
+	 * @return bool
254
+	 */
255
+	public function hasRecursiveMMRelation()
256
+	{
257
+		return isset($this->tableNameAliases['aliasIncrement'][$this->query->getType()]);
258
+	}
259
+
260
+	/**
261
+	 * Returns the statement, ready to be executed.
262
+	 *
263
+	 * @param array $statementParts The SQL statement parts
264
+	 * @return string The SQL statement
265
+	 */
266
+	public function buildQuery(array $statementParts)
267
+	{
268
+		// Add more statement to the UNION part.
269
+		if (!empty($statementParts['unions'])) {
270
+			foreach ($statementParts['unions'] as $tableName => $unionPart) {
271
+				if (!empty($statementParts['additionalWhereClause'][$tableName])) {
272
+					$statementParts['unions'][$tableName] .= ' AND ' . implode(' AND ', $statementParts['additionalWhereClause'][$tableName]);
273
+				}
274
+			}
275
+		}
276
+
277
+		$statement = 'SELECT ' . implode(' ', $statementParts['keywords']) . ' ' . implode(',', $statementParts['fields']) . ' FROM ' . implode(' ', $statementParts['tables']) . ' ' . implode(' ', $statementParts['unions']);
278
+		if (!empty($statementParts['where'])) {
279
+			$statement .= ' WHERE ' . implode('', $statementParts['where']);
280
+			if (!empty($statementParts['additionalWhereClause'][$this->query->getType()])) {
281
+				$statement .= ' AND ' . implode(' AND ', $statementParts['additionalWhereClause'][$this->query->getType()]);
282
+			}
283
+		} elseif (!empty($statementParts['additionalWhereClause'])) {
284
+			$statement .= ' WHERE ' . implode(' AND ', $statementParts['additionalWhereClause'][$this->query->getType()]);
285
+		}
286
+		if (!empty($statementParts['orderings'])) {
287
+			$statement .= ' ORDER BY ' . implode(', ', $statementParts['orderings']);
288
+		}
289
+		if (!empty($statementParts['limit'])) {
290
+			$statement .= ' LIMIT ' . $statementParts['limit'];
291
+		}
292
+
293
+		return $statement;
294
+	}
295
+
296
+	/**
297
+	 * Transforms a Query Source into SQL and parameter arrays
298
+	 *
299
+	 * @param SourceInterface $source The source
300
+	 * @param array &$sql
301
+	 * @return void
302
+	 */
303
+	protected function parseSource(SourceInterface $source, array &$sql)
304
+	{
305
+		$tableName = $this->getTableName();
306
+		$sql['fields'][$tableName] = $tableName . '.*';
307
+		if ($this->query->getDistinct()) {
308
+			$sql['fields'][$tableName] = $tableName . '.' . $this->query->getDistinct();
309
+			$sql['keywords']['distinct'] = 'DISTINCT';
310
+		}
311
+		$sql['tables'][$tableName] = $tableName;
312
+		$sql['mainTable'] = $tableName;
313
+	}
314
+
315
+	/**
316
+	 * Transforms a constraint into SQL and parameter arrays
317
+	 *
318
+	 * @param ConstraintInterface $constraint The constraint
319
+	 * @param SourceInterface $source The source
320
+	 * @param array &$statementParts The query parts
321
+	 * @param array &$parameters The parameters that will replace the markers
322
+	 * @return void
323
+	 */
324
+	protected function parseConstraint(ConstraintInterface $constraint = null, SourceInterface $source, array &$statementParts, array &$parameters)
325
+	{
326
+		if ($constraint instanceof AndInterface) {
327
+			$statementParts['where'][] = '(';
328
+			$this->parseConstraint($constraint->getConstraint1(), $source, $statementParts, $parameters);
329
+			$statementParts['where'][] = ' AND ';
330
+			$this->parseConstraint($constraint->getConstraint2(), $source, $statementParts, $parameters);
331
+			$statementParts['where'][] = ')';
332
+		} elseif ($constraint instanceof OrInterface) {
333
+			$statementParts['where'][] = '(';
334
+			$this->parseConstraint($constraint->getConstraint1(), $source, $statementParts, $parameters);
335
+			$statementParts['where'][] = ' OR ';
336
+			$this->parseConstraint($constraint->getConstraint2(), $source, $statementParts, $parameters);
337
+			$statementParts['where'][] = ')';
338
+		} elseif ($constraint instanceof NotInterface) {
339
+			$statementParts['where'][] = 'NOT (';
340
+			$this->parseConstraint($constraint->getConstraint(), $source, $statementParts, $parameters);
341
+			$statementParts['where'][] = ')';
342
+		} elseif ($constraint instanceof ComparisonInterface) {
343
+			$this->parseComparison($constraint, $source, $statementParts, $parameters);
344
+		}
345
+	}
346
+
347
+	/**
348
+	 * Parse a Comparison into SQL and parameter arrays.
349
+	 *
350
+	 * @param ComparisonInterface $comparison The comparison to parse
351
+	 * @param SourceInterface $source The source
352
+	 * @param array &$statementParts SQL query parts to add to
353
+	 * @param array &$parameters Parameters to bind to the SQL
354
+	 * @return void
355
+	 * @throws Exception\RepositoryException
356
+	 */
357
+	protected function parseComparison(ComparisonInterface $comparison, SourceInterface $source, array &$statementParts, array &$parameters)
358
+	{
359
+		$operand1 = $comparison->getOperand1();
360
+		$operator = $comparison->getOperator();
361
+		$operand2 = $comparison->getOperand2();
362
+		if ($operator === QueryInterface::OPERATOR_IN) {
363
+			$items = [];
364
+			$hasValue = false;
365
+			foreach ($operand2 as $value) {
366
+				$value = $this->getPlainValue($value);
367
+				if ($value !== null) {
368
+					$items[] = $value;
369
+					$hasValue = true;
370
+				}
371
+			}
372
+			if ($hasValue === false) {
373
+				$statementParts['where'][] = '1<>1';
374
+			} else {
375
+				$this->parseDynamicOperand($operand1, $operator, $source, $statementParts, $parameters, null);
376
+				$parameters[] = $items;
377
+			}
378
+		} elseif ($operator === QueryInterface::OPERATOR_CONTAINS) {
379
+			if ($operand2 === null) {
380
+				$statementParts['where'][] = '1<>1';
381
+			} else {
382
+				throw new \Exception('Not implemented! Contact extension author.', 1412931227);
383
+				# @todo re-implement me if necessary.
384
+				#$tableName = $this->query->getType();
385
+				#$propertyName = $operand1->getPropertyName();
386
+				#while (strpos($propertyName, '.') !== false) {
387
+				#	$this->addUnionStatement($tableName, $propertyName, $statementParts);
388
+				#}
389
+				#$columnName = $propertyName;
390
+				#$columnMap = $propertyName;
391
+				#$typeOfRelation = $columnMap instanceof ColumnMap ? $columnMap->getTypeOfRelation() : null;
392
+				#if ($typeOfRelation === ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
393
+				#	$relationTableName = $columnMap->getRelationTableName();
394
+				#	$statementParts['where'][] = $tableName . '.uid IN (SELECT ' . $columnMap->getParentKeyFieldName() . ' FROM ' . $relationTableName . ' WHERE ' . $columnMap->getChildKeyFieldName() . '=?)';
395
+				#	$parameters[] = intval($this->getPlainValue($operand2));
396
+				#} elseif ($typeOfRelation === ColumnMap::RELATION_HAS_MANY) {
397
+				#	$parentKeyFieldName = $columnMap->getParentKeyFieldName();
398
+				#	if (isset($parentKeyFieldName)) {
399
+				#		$childTableName = $columnMap->getChildTableName();
400
+				#		$statementParts['where'][] = $tableName . '.uid=(SELECT ' . $childTableName . '.' . $parentKeyFieldName . ' FROM ' . $childTableName . ' WHERE ' . $childTableName . '.uid=?)';
401
+				#		$parameters[] = intval($this->getPlainValue($operand2));
402
+				#	} else {
403
+				#		$statementParts['where'][] = 'FIND_IN_SET(?,' . $tableName . '.' . $columnName . ')';
404
+				#		$parameters[] = intval($this->getPlainValue($operand2));
405
+				#	}
406
+				#} else {
407
+				#	throw new Exception\RepositoryException('Unsupported or non-existing property name "' . $propertyName . '" used in relation matching.', 1327065745);
408
+				#}
409
+			}
410
+		} else {
411
+			if ($operand2 === null) {
412
+				if ($operator === QueryInterface::OPERATOR_EQUAL_TO) {
413
+					$operator = self::OPERATOR_EQUAL_TO_NULL;
414
+				} elseif ($operator === QueryInterface::OPERATOR_NOT_EQUAL_TO) {
415
+					$operator = self::OPERATOR_NOT_EQUAL_TO_NULL;
416
+				}
417
+			}
418
+			$this->parseDynamicOperand($operand1, $operator, $source, $statementParts, $parameters);
419
+			$parameters[] = $this->getPlainValue($operand2);
420
+		}
421
+	}
422
+
423
+	/**
424
+	 * Returns a plain value, i.e. objects are flattened if possible.
425
+	 *
426
+	 * @param mixed $input
427
+	 * @return mixed
428
+	 * @throws UnexpectedTypeException
429
+	 */
430
+	protected function getPlainValue($input)
431
+	{
432
+		if (is_array($input)) {
433
+			throw new UnexpectedTypeException('An array could not be converted to a plain value.', 1274799932);
434
+		}
435
+		if ($input instanceof \DateTime) {
436
+			return $input->format('U');
437
+		} elseif (is_object($input)) {
438
+			if ($input instanceof LazyLoadingProxy) {
439
+				$realInput = $input->_loadRealInstance();
440
+			} else {
441
+				$realInput = $input;
442
+			}
443
+			if ($realInput instanceof DomainObjectInterface) {
444
+				return $realInput->getUid();
445
+			} else {
446
+				throw new UnexpectedTypeException('An object of class "' . get_class($realInput) . '" could not be converted to a plain value.', 1274799934);
447
+			}
448
+		} elseif (is_bool($input)) {
449
+			return $input === true ? 1 : 0;
450
+		} else {
451
+			return $input;
452
+		}
453
+	}
454
+
455
+	/**
456
+	 * Parse a DynamicOperand into SQL and parameter arrays.
457
+	 *
458
+	 * @param DynamicOperandInterface $operand
459
+	 * @param string $operator One of the JCR_OPERATOR_* constants
460
+	 * @param SourceInterface $source The source
461
+	 * @param array &$statementParts The query parts
462
+	 * @param array &$parameters The parameters that will replace the markers
463
+	 * @param string $valueFunction an optional SQL function to apply to the operand value
464
+	 * @return void
465
+	 */
466
+	protected function parseDynamicOperand(DynamicOperandInterface $operand, $operator, SourceInterface $source, array &$statementParts, array &$parameters, $valueFunction = null)
467
+	{
468
+		if ($operand instanceof LowerCaseInterface) {
469
+			$this->parseDynamicOperand($operand->getOperand(), $operator, $source, $statementParts, $parameters, 'LOWER');
470
+		} elseif ($operand instanceof UpperCaseInterface) {
471
+			$this->parseDynamicOperand($operand->getOperand(), $operator, $source, $statementParts, $parameters, 'UPPER');
472
+		} elseif ($operand instanceof PropertyValueInterface) {
473
+			$propertyName = $operand->getPropertyName();
474
+
475
+			// Reset value.
476
+			$this->currentChildTableNameAlias = '';
477
+
478
+			if ($source instanceof SelectorInterface) {
479
+				$tableName = $this->query->getType();
480
+				while (strpos($propertyName, '.') !== false) {
481
+					$this->addUnionStatement($tableName, $propertyName, $statementParts);
482
+				}
483
+			} elseif ($source instanceof JoinInterface) {
484
+				$tableName = $source->getJoinCondition()->getSelector1Name();
485
+			}
486
+
487
+			$columnName = $propertyName;
488
+			$resolvedOperator = $this->resolveOperator($operator);
489
+			$constraintSQL = '';
490
+
491
+			$marker = $operator === QueryInterface::OPERATOR_IN
492
+				? '(?)'
493
+				: '?';
494
+
495
+			if ($valueFunction === null) {
496
+				$constraintSQL .= (!empty($tableName) ? $tableName . '.' : '') . $columnName . ' ' . $resolvedOperator . ' ' . $marker;
497
+			} else {
498
+				$constraintSQL .= $valueFunction . '(' . (!empty($tableName) ? $tableName . '.' : '') . $columnName . ') ' . $resolvedOperator . ' ' . $marker;
499
+			}
500
+
501
+			if (isset($tableName) && !empty($this->currentChildTableNameAlias)) {
502
+				$constraintSQL = $this->replaceTableNameByAlias($tableName, $this->currentChildTableNameAlias, $constraintSQL);
503
+			}
504
+			$statementParts['where'][] = $constraintSQL;
505
+		}
506
+	}
507
+
508
+	/**
509
+	 * @param string &$tableName
510
+	 * @param string &$propertyPath
511
+	 * @param array &$statementParts
512
+	 */
513
+	protected function addUnionStatement(&$tableName, &$propertyPath, array &$statementParts)
514
+	{
515
+		$table = Tca::table($tableName);
516
+
517
+		$explodedPropertyPath = explode('.', $propertyPath, 2);
518
+		$fieldName = $explodedPropertyPath[0];
519
+
520
+		// Field of type "group" are special because property path must contain the table name
521
+		// to determine the relation type. Example for sys_category, property path will look like "items.sys_file"
522
+		$parts = explode('.', $propertyPath, 3);
523
+		if ($table->field($fieldName)->isGroup() && count($parts) > 2) {
524
+			$explodedPropertyPath[0] = $parts[0] . '.' . $parts[1];
525
+			$explodedPropertyPath[1] = $parts[2];
526
+			$fieldName = $explodedPropertyPath[0];
527
+		}
528
+
529
+		$parentKeyFieldName = $table->field($fieldName)->getForeignField();
530
+		$childTableName = $table->field($fieldName)->getForeignTable();
531
+
532
+		if ($childTableName === null) {
533
+			throw new InvalidRelationConfigurationException('The relation information for property "' . $fieldName . '" of class "' . $tableName . '" is missing.', 1353170925);
534
+		}
535
+
536
+		if ($table->field($fieldName)->hasOne()) { // includes relation "one-to-one" and "many-to-one"
537
+			// sometimes the opposite relation is not defined. We don't want to force this config for backward compatibility reasons.
538
+			// $parentKeyFieldName === null does the trick somehow. Before condition was if (isset($parentKeyFieldName))
539
+			if ($table->field($fieldName)->hasRelationManyToOne() || $parentKeyFieldName === null) {
540
+				$statementParts['unions'][$childTableName] = 'LEFT JOIN ' . $childTableName . ' ON ' . $tableName . '.' . $fieldName . '=' . $childTableName . '.uid';
541
+			} else {
542
+				$statementParts['unions'][$childTableName] = 'LEFT JOIN ' . $childTableName . ' ON ' . $tableName . '.uid=' . $childTableName . '.' . $parentKeyFieldName;
543
+			}
544
+		} elseif ($table->field($fieldName)->hasRelationManyToMany()) {
545
+			$relationTableName = $table->field($fieldName)->getManyToManyTable();
546
+
547
+			$parentKeyFieldName = $table->field($fieldName)->isOppositeRelation() ? 'uid_foreign' : 'uid_local';
548
+			$childKeyFieldName = !$table->field($fieldName)->isOppositeRelation() ? 'uid_foreign' : 'uid_local';
549
+
550
+			// MM table e.g sys_category_record_mm
551
+			$relationTableNameAlias = $this->generateAlias($relationTableName);
552
+			$join = sprintf(
553
+				'LEFT JOIN %s AS %s ON %s.uid=%s.%s',
554
+				$relationTableName,
555
+				$relationTableNameAlias,
556
+				$tableName,
557
+				$relationTableNameAlias,
558
+				$parentKeyFieldName
559
+			);
560
+			$statementParts['unions'][$relationTableNameAlias] = $join;
561
+
562
+			// Foreign table e.g sys_category
563
+			$childTableNameAlias = $this->generateAlias($childTableName);
564
+			$this->currentChildTableNameAlias = $childTableNameAlias;
565
+			$join = sprintf(
566
+				'LEFT JOIN %s AS %s ON %s.%s=%s.uid',
567
+				$childTableName,
568
+				$childTableNameAlias,
569
+				$relationTableNameAlias,
570
+				$childKeyFieldName,
571
+				$childTableNameAlias
572
+			);
573
+			$statementParts['unions'][$childTableNameAlias] = $join;
574
+
575
+			// Find a possible table name for a MM condition.
576
+			$tableNameCondition = $table->field($fieldName)->getAdditionalTableNameCondition();
577
+			if ($tableNameCondition) {
578
+				// If we can find a source file name,  we can then retrieve more MM conditions from the TCA such as a field name.
579
+				$sourceFileName = $this->query->getSourceFieldName();
580
+				if (empty($sourceFileName)) {
581
+					$additionalMMConditions = array(
582
+						'tablenames' => $tableNameCondition,
583
+					);
584
+				} else {
585
+					$additionalMMConditions = Tca::table($tableNameCondition)->field($sourceFileName)->getAdditionalMMCondition();
586
+				}
587
+
588
+				foreach ($additionalMMConditions as $additionalFieldName => $additionalMMCondition) {
589
+					$additionalJoin = sprintf(' AND %s.%s = "%s"', $relationTableNameAlias, $additionalFieldName, $additionalMMCondition);
590
+					$statementParts['unions'][$relationTableNameAlias] .= $additionalJoin;
591
+
592
+					$additionalJoin = sprintf(' AND %s.%s = "%s"', $relationTableNameAlias, $additionalFieldName, $additionalMMCondition);
593
+					$statementParts['unions'][$childTableNameAlias] .= $additionalJoin;
594
+				}
595
+			}
596
+		} elseif ($table->field($fieldName)->hasMany()) { // includes relations "many-to-one" and "csv" relations
597
+			$childTableNameAlias = $this->generateAlias($childTableName);
598
+			$this->currentChildTableNameAlias = $childTableNameAlias;
599
+
600
+			if (isset($parentKeyFieldName)) {
601
+				$join = sprintf(
602
+					'LEFT JOIN %s AS %s ON %s.uid=%s.%s',
603
+					$childTableName,
604
+					$childTableNameAlias,
605
+					$tableName,
606
+					$childTableNameAlias,
607
+					$parentKeyFieldName
608
+				);
609
+				$statementParts['unions'][$childTableNameAlias] = $join;
610
+			} else {
611
+				$join = sprintf(
612
+					'LEFT JOIN %s AS %s ON (FIND_IN_SET(%s.uid, %s.%s))',
613
+					$childTableName,
614
+					$childTableNameAlias,
615
+					$childTableNameAlias,
616
+					$tableName,
617
+					$fieldName
618
+				);
619
+				$statementParts['unions'][$childTableNameAlias] = $join;
620
+			}
621
+		} else {
622
+			throw new Exception('Could not determine type of relation.', 1252502725);
623
+		}
624
+
625
+		$statementParts['keywords']['distinct'] = 'DISTINCT';
626
+		$propertyPath = $explodedPropertyPath[1];
627
+		$tableName = $childTableName;
628
+	}
629
+
630
+	/**
631
+	 * Returns the SQL operator for the given JCR operator type.
632
+	 *
633
+	 * @param string $operator One of the JCR_OPERATOR_* constants
634
+	 * @return string an SQL operator
635
+	 * @throws Exception
636
+	 */
637
+	protected function resolveOperator($operator)
638
+	{
639
+		switch ($operator) {
640
+			case self::OPERATOR_EQUAL_TO_NULL:
641
+				$operator = 'IS';
642
+				break;
643
+			case self::OPERATOR_NOT_EQUAL_TO_NULL:
644
+				$operator = 'IS NOT';
645
+				break;
646
+			case QueryInterface::OPERATOR_IN:
647
+				$operator = 'IN';
648
+				break;
649
+			case QueryInterface::OPERATOR_EQUAL_TO:
650
+				$operator = '=';
651
+				break;
652
+			case QueryInterface::OPERATOR_NOT_EQUAL_TO:
653
+				$operator = '!=';
654
+				break;
655
+			case QueryInterface::OPERATOR_LESS_THAN:
656
+				$operator = '<';
657
+				break;
658
+			case QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO:
659
+				$operator = '<=';
660
+				break;
661
+			case QueryInterface::OPERATOR_GREATER_THAN:
662
+				$operator = '>';
663
+				break;
664
+			case QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO:
665
+				$operator = '>=';
666
+				break;
667
+			case QueryInterface::OPERATOR_LIKE:
668
+				$operator = 'LIKE';
669
+				break;
670
+			default:
671
+				throw new Exception('Unsupported operator encountered.', 1242816073);
672
+		}
673
+		return $operator;
674
+	}
675
+
676
+	/**
677
+	 * Adds additional WHERE statements according to the query settings.
678
+	 *
679
+	 * @param QuerySettingsInterface $querySettings The TYPO3 CMS specific query settings
680
+	 * @param string $tableNameOrAlias The table name to add the additional where clause for
681
+	 * @param array &$statementParts
682
+	 * @return void
683
+	 */
684
+	protected function addAdditionalWhereClause(QuerySettingsInterface $querySettings, $tableNameOrAlias, &$statementParts)
685
+	{
686
+		$this->addVisibilityConstraintStatement($querySettings, $tableNameOrAlias, $statementParts);
687
+		if ($querySettings->getRespectSysLanguage()) {
688
+			$this->addSysLanguageStatement($tableNameOrAlias, $statementParts, $querySettings);
689
+		}
690
+	}
691
+
692
+	/**
693
+	 * Adds enableFields and deletedClause to the query if necessary
694
+	 *
695
+	 * @param QuerySettingsInterface $querySettings
696
+	 * @param string $tableNameOrAlias The database table name
697
+	 * @param array &$statementParts The query parts
698
+	 * @return void
699
+	 */
700
+	protected function addVisibilityConstraintStatement(QuerySettingsInterface $querySettings, $tableNameOrAlias, array &$statementParts)
701
+	{
702
+		$statement = '';
703
+		$tableName = $this->resolveTableNameAlias($tableNameOrAlias);
704
+		if (is_array($GLOBALS['TCA'][$tableName]['ctrl'])) {
705
+			$ignoreEnableFields = $querySettings->getIgnoreEnableFields();
706
+			$enableFieldsToBeIgnored = $querySettings->getEnableFieldsToBeIgnored();
707
+			$includeDeleted = $querySettings->getIncludeDeleted();
708
+			if (Typo3Mode::isFrontendMode()) {
709
+				$statement .= $this->getFrontendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $enableFieldsToBeIgnored, $includeDeleted);
710
+			} else {
711
+				// 'BE' case
712
+				$statement .= $this->getBackendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $includeDeleted);
713
+			}
714
+
715
+			// Remove the prefixing "AND" if any.
716
+			if (!empty($statement)) {
717
+				$statement = strtolower(substr($statement, 1, 3)) === 'and' ? substr($statement, 5) : $statement;
718
+				$statementParts['additionalWhereClause'][$tableNameOrAlias][] = $statement;
719
+			}
720
+		}
721
+	}
722
+
723
+	/**
724
+	 * Returns constraint statement for frontend context
725
+	 *
726
+	 * @param string $tableNameOrAlias
727
+	 * @param boolean $ignoreEnableFields A flag indicating whether the enable fields should be ignored
728
+	 * @param array $enableFieldsToBeIgnored If $ignoreEnableFields is true, this array specifies enable fields to be ignored. If it is null or an empty array (default) all enable fields are ignored.
729
+	 * @param boolean $includeDeleted A flag indicating whether deleted records should be included
730
+	 * @return string
731
+	 * @throws Exception\InconsistentQuerySettingsException
732
+	 */
733
+	protected function getFrontendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $enableFieldsToBeIgnored, $includeDeleted)
734
+	{
735
+		$statement = '';
736
+		$tableName = $this->resolveTableNameAlias($tableNameOrAlias);
737
+		if ($ignoreEnableFields && !$includeDeleted) {
738
+			if (count($enableFieldsToBeIgnored)) {
739
+				// array_combine() is necessary because of the way \TYPO3\CMS\Frontend\Page\PageRepository::enableFields() is implemented
740
+				$statement .= $this->getPageRepository()->enableFields($tableName, -1, array_combine($enableFieldsToBeIgnored, $enableFieldsToBeIgnored));
741
+			} else {
742
+				$statement .= $this->getPageRepository()->deleteClause($tableName);
743
+			}
744
+		} elseif (!$ignoreEnableFields && !$includeDeleted) {
745
+			$statement .= $this->getPageRepository()->enableFields($tableName);
746
+		} elseif (!$ignoreEnableFields && $includeDeleted) {
747
+			throw new InconsistentQuerySettingsException('Query setting "ignoreEnableFields=false" can not be used together with "includeDeleted=true" in frontend context.', 1327678173);
748
+		}
749
+		return $this->replaceTableNameByAlias($tableName, $tableNameOrAlias, $statement);
750
+	}
751
+
752
+	/**
753
+	 * Returns constraint statement for backend context
754
+	 *
755
+	 * @param string $tableNameOrAlias
756
+	 * @param boolean $ignoreEnableFields A flag indicating whether the enable fields should be ignored
757
+	 * @param boolean $includeDeleted A flag indicating whether deleted records should be included
758
+	 * @return string
759
+	 */
760
+	protected function getBackendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $includeDeleted)
761
+	{
762
+		$tableName = $this->resolveTableNameAlias($tableNameOrAlias);
763
+		$statement = '';
764
+		if (!$ignoreEnableFields) {
765
+			$statement .= BackendUtility::BEenableFields($tableName);
766
+		}
767
+
768
+		// If the table is found to have "workspace" support, add the corresponding fields in the statement.
769
+		if (Tca::table($tableName)->hasWorkspaceSupport()) {
770
+			if ($this->getBackendUser()->workspace === 0) {
771
+				$statement .= ' AND ' . $tableName . '.t3ver_state<=' . new VersionState(VersionState::DEFAULT_STATE);
772
+			} else {
773
+				// Show only records of live and of the current workspace
774
+				// In case we are in a Versioning preview
775
+				$statement .= ' AND (' .
776
+					$tableName . '.t3ver_wsid=0 OR ' .
777
+					$tableName . '.t3ver_wsid=' . (int)$this->getBackendUser()->workspace .
778
+					')';
779
+			}
780
+
781
+			// Check if this segment make sense here or whether it should be in the "if" part when we have workspace = 0
782
+			$statement .= ' AND ' . $tableName . '.pid<>-1';
783
+		}
784
+
785
+		if (!$includeDeleted) {
786
+			$statement .= BackendUtility::deleteClause($tableName);
787
+		}
788
+
789
+		return $this->replaceTableNameByAlias($tableName, $tableNameOrAlias, $statement);
790
+	}
791
+
792
+	/**
793
+	 * Builds the language field statement
794
+	 *
795
+	 * @param string $tableNameOrAlias The database table name
796
+	 * @param array &$statementParts The query parts
797
+	 * @param QuerySettingsInterface $querySettings The TYPO3 CMS specific query settings
798
+	 * @return void
799
+	 * @throws Exception
800
+	 */
801
+	protected function addSysLanguageStatement($tableNameOrAlias, array &$statementParts, $querySettings)
802
+	{
803
+		$tableName = $this->resolveTableNameAlias($tableNameOrAlias);
804
+		if (is_array($GLOBALS['TCA'][$tableName]['ctrl'])) {
805
+			if (!empty($GLOBALS['TCA'][$tableName]['ctrl']['languageField'])) {
806
+				// Select all entries for the current language
807
+				$additionalWhereClause = $tableNameOrAlias . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . ' IN (' . intval($querySettings->getLanguageUid()) . ',-1)';
808
+				// If any language is set -> get those entries which are not translated yet
809
+				// They will be removed by t3lib_page::getRecordOverlay if not matching overlay mode
810
+				if (isset($GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'])
811
+					&& $querySettings->getLanguageUid() > 0
812
+				) {
813
+					$additionalWhereClause .= ' OR (' . $tableNameOrAlias . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . '=0' .
814
+						' AND ' . $tableNameOrAlias . '.uid NOT IN (SELECT ' . $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'] .
815
+						' FROM ' . $tableName .
816
+						' WHERE ' . $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'] . '>0' .
817
+						' AND ' . $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . '>0';
818
+
819
+					// Add delete clause to ensure all entries are loaded
820
+					if (isset($GLOBALS['TCA'][$tableName]['ctrl']['delete'])) {
821
+						$additionalWhereClause .= ' AND ' . $tableNameOrAlias . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['delete'] . '=0';
822
+					}
823
+					$additionalWhereClause .= '))';
824
+				}
825
+				$statementParts['additionalWhereClause'][$tableNameOrAlias][] = '(' . $additionalWhereClause . ')';
826
+			}
827
+		}
828
+	}
829
+
830
+	/**
831
+	 * Transforms orderings into SQL.
832
+	 *
833
+	 * @param array $orderings An array of orderings (Tx_Extbase_Persistence_QOM_Ordering)
834
+	 * @param SourceInterface $source The source
835
+	 * @param array &$statementParts The query parts
836
+	 * @return void
837
+	 * @throws Exception\UnsupportedOrderException
838
+	 */
839
+	protected function parseOrderings(array $orderings, SourceInterface $source, array &$statementParts)
840
+	{
841
+		foreach ($orderings as $fieldNameAndPath => $order) {
842
+			switch ($order) {
843
+				case QueryInterface::ORDER_ASCENDING:
844
+					$order = 'ASC';
845
+					break;
846
+				case QueryInterface::ORDER_DESCENDING:
847
+					$order = 'DESC';
848
+					break;
849
+				default:
850
+					throw new UnsupportedOrderException('Unsupported order encountered.', 1456845126);
851
+			}
852
+
853
+			$tableName = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $this->query->getType());
854
+			$fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $tableName);
855
+			$statementParts['orderings'][] = sprintf('%s.%s %s', $tableName, $fieldName, $order);
856
+		}
857
+	}
858
+
859
+	/**
860
+	 * Transforms limit and offset into SQL
861
+	 *
862
+	 * @param int $limit
863
+	 * @param int $offset
864
+	 * @param array &$statementParts
865
+	 * @return void
866
+	 */
867
+	protected function parseLimitAndOffset($limit, $offset, array &$statementParts)
868
+	{
869
+		if ($limit !== null && $offset !== null) {
870
+			$statementParts['limit'] = intval($offset) . ', ' . intval($limit);
871
+		} elseif ($limit !== null) {
872
+			$statementParts['limit'] = intval($limit);
873
+		}
874
+	}
875
+
876
+	/**
877
+	 * @param array $rows
878
+	 * @return array
879
+	 */
880
+	protected function getContentObjects(array $rows): array
881
+	{
882
+		$contentObjects = [];
883
+		foreach ($rows as $row) {
884
+			// Get language uid from querySettings.
885
+			// Ensure the backend handling is not broken (fallback to Get parameter 'L' if needed)
886
+			$overlaidRow = $this->doLanguageAndWorkspaceOverlay(
887
+				$row,
888
+				$this->query->getTypo3QuerySettings()
889
+			);
890
+
891
+			$contentObjects[] = GeneralUtility::makeInstance(
892
+				Content::class,
893
+				$this->query->getType(),
894
+				$overlaidRow
895
+			);
896
+		}
897
+
898
+		return $contentObjects;
899
+	}
900
+
901
+	/**
902
+	 * Performs workspace and language overlay on the given row array. The language and workspace id is automatically
903
+	 * detected (depending on FE or BE context). You can also explicitly set the language/workspace id.
904
+	 *
905
+	 * @param array $row
906
+	 * @param QuerySettingsInterface $querySettings The TYPO3 CMS specific query settings
907
+	 * @return array
908
+	 */
909
+	protected function doLanguageAndWorkspaceOverlay(array $row, $querySettings)
910
+	{
911
+		$tableName = $this->getTableName();
912
+
913
+		$pageRepository = $this->getPageRepository();
914
+		if (is_object($GLOBALS['TSFE'])) {
915
+			$languageMode = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('language', 'legacyLanguageMode');
916
+		#if ($this->isBackendUserLogged() && $this->getBackendUser()->workspace !== 0) {
917
+			#    $pageRepository->versioningWorkspaceId = $this->getBackendUser()->workspace;
918
+		#}
919
+		} else {
920
+			$languageMode = '';
921
+			$workspaceUid = $this->getBackendUser()->workspace;
922
+			#$pageRepository->versioningWorkspaceId = $workspaceUid;
923
+			#if ($this->getBackendUser()->workspace !== 0) {
924
+			#    $pageRepository->versioningPreview = 1;
925
+			#}
926
+		}
927
+
928
+		// If current row is a translation select its parent
929
+		if (isset($GLOBALS['TCA'][$tableName]['ctrl']['languageField'])
930
+			&& isset($GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'])
931
+		) {
932
+			if (isset($row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']])
933
+				&& $row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']] > 0
934
+			) {
935
+				$queryBuilder = $this->getQueryBuilder();
936
+				$row = $queryBuilder
937
+					->select($tableName . '.*')
938
+					->from($tableName)
939
+					->andWhere(
940
+						$tableName . '.uid=' . (int)$row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']],
941
+						$tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . ' = 0'
942
+					)
943
+					->execute()
944
+					->fetch();
945
+			}
946
+		}
947
+
948
+		// Retrieve the original uid; Used for Workspaces!
949
+		if (Typo3Mode::isBackendMode()) {
950
+			$pageRepository->versionOL($tableName, $row, true, true);
951
+		} else {
952
+			\TYPO3\CMS\Backend\Utility\BackendUtility::workspaceOL($tableName, $row);
953
+		}
954
+		if (isset($row['_ORIG_uid'])) {
955
+			$row['uid'] = $row['_ORIG_uid'];
956
+		}
957
+
958
+		// Special case for table "pages"
959
+		if ($tableName == 'pages') {
960
+			$row = $pageRepository->getPageOverlay($row, $querySettings->getLanguageUid());
961
+		} elseif (isset($GLOBALS['TCA'][$tableName]['ctrl']['languageField'])
962
+			&& $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] !== ''
963
+		) {
964
+			if (in_array($row[$GLOBALS['TCA'][$tableName]['ctrl']['languageField']], array(-1, 0))) {
965
+				$overlayMode = $languageMode === 'strict' ? 'hideNonTranslated' : '';
966
+				$row = $pageRepository->getRecordOverlay($tableName, $row, $querySettings->getLanguageUid(), $overlayMode);
967
+			}
968
+		}
969
+
970
+		return $row;
971
+	}
972
+
973
+	/**
974
+	 * Return a resolved table name given a possible table name alias.
975
+	 *
976
+	 * @param string $tableNameOrAlias
977
+	 * @return string
978
+	 */
979
+	protected function resolveTableNameAlias($tableNameOrAlias)
980
+	{
981
+		$resolvedTableName = $tableNameOrAlias;
982
+		if (!empty($this->tableNameAliases['aliases'][$tableNameOrAlias])) {
983
+			$resolvedTableName = $this->tableNameAliases['aliases'][$tableNameOrAlias];
984
+		}
985
+		return $resolvedTableName;
986
+	}
987
+
988
+	/**
989
+	 * Generate a unique table name alias for the given table name.
990
+	 *
991
+	 * @param string $tableName
992
+	 * @return string
993
+	 */
994
+	protected function generateAlias($tableName)
995
+	{
996
+		if (!isset($this->tableNameAliases['aliasIncrement'][$tableName])) {
997
+			$this->tableNameAliases['aliasIncrement'][$tableName] = 0;
998
+		}
999
+
1000
+		$numberOfAliases = $this->tableNameAliases['aliasIncrement'][$tableName];
1001
+		$tableNameAlias = $tableName . $numberOfAliases;
1002
+
1003
+		$this->tableNameAliases['aliasIncrement'][$tableName]++;
1004
+		$this->tableNameAliases['aliases'][$tableNameAlias] = $tableName;
1005
+
1006
+		return $tableNameAlias;
1007
+	}
1008
+
1009
+	/**
1010
+	 * Replace the table names by its table name alias within the given statement.
1011
+	 *
1012
+	 * @param string $tableName
1013
+	 * @param string $tableNameAlias
1014
+	 * @param string $statement
1015
+	 * @return string
1016
+	 */
1017
+	protected function replaceTableNameByAlias($tableName, $tableNameAlias, $statement)
1018
+	{
1019
+		if ($statement && $tableName !== $tableNameAlias) {
1020
+			$statement = str_replace($tableName, $tableNameAlias, $statement);
1021
+		}
1022
+		return $statement;
1023
+	}
1024
+
1025
+	/**
1026
+	 * Returns an instance of the current Backend User.
1027
+	 *
1028
+	 * @return BackendUserAuthentication
1029
+	 */
1030
+	protected function getBackendUser()
1031
+	{
1032
+		return $GLOBALS['BE_USER'];
1033
+	}
1034
+
1035
+	/**
1036
+	 * Tell whether a Backend User is logged in.
1037
+	 *
1038
+	 * @return bool
1039
+	 */
1040
+	protected function isBackendUserLogged()
1041
+	{
1042
+		return is_object($GLOBALS['BE_USER']);
1043
+	}
1044
+
1045
+	/**
1046
+	 * @return PageRepository|object
1047
+	 */
1048
+	protected function getPageRepository()
1049
+	{
1050
+		if (!$this->pageRepository instanceof PageRepository) {
1051
+			if (Typo3Mode::isFrontendMode() && is_object($GLOBALS['TSFE'])) {
1052
+				$this->pageRepository = $GLOBALS['TSFE']->sys_page;
1053
+			} else {
1054
+				$this->pageRepository = GeneralUtility::makeInstance(PageRepository::class);
1055
+			}
1056
+		}
1057
+
1058
+		return $this->pageRepository;
1059
+	}
1060
+
1061
+	/**
1062
+	 * @return FieldPathResolver|object
1063
+	 */
1064
+	protected function getFieldPathResolver()
1065
+	{
1066
+		return GeneralUtility::makeInstance(FieldPathResolver::class);
1067
+	}
1068
+
1069
+	/**
1070
+	 * @return object|Connection
1071
+	 */
1072
+	protected function getConnection(): Connection
1073
+	{
1074
+		/** @var ConnectionPool $connectionPool */
1075
+		return GeneralUtility::makeInstance(ConnectionPool::class)
1076
+			->getConnectionForTable($this->getTableName());
1077
+	}
1078
+
1079
+	/**
1080
+	 * @return object|QueryBuilder
1081
+	 */
1082
+	protected function getQueryBuilder(): QueryBuilder
1083
+	{
1084
+		/** @var ConnectionPool $connectionPool */
1085
+		$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
1086
+		return $connectionPool->getQueryBuilderForTable($this->getTableName());
1087
+	}
1088
+
1089
+	/**
1090
+	 * @return string
1091
+	 */
1092
+	public function getTableName(): string
1093
+	{
1094
+		return $this->query->getSource()->getNodeTypeName(); // getSelectorName()
1095
+	}
1096 1096
 }
Please login to merge, or discard this patch.
Classes/Grid/RelationRenderer.php 1 patch
Indentation   +141 added lines, -141 removed lines patch added patch discarded remove patch
@@ -21,145 +21,145 @@
 block discarded – undo
21 21
  */
22 22
 class RelationRenderer extends ColumnRendererAbstract
23 23
 {
24
-    /**
25
-     * Render a representation of the relation on the GUI.
26
-     *
27
-     * @return string
28
-     */
29
-    public function render()
30
-    {
31
-        if ($this->isBackendMode()) {
32
-            $output = $this->renderForBackend();
33
-        } else {
34
-            $output = $this->renderForFrontend();
35
-        }
36
-
37
-        return $output;
38
-    }
39
-
40
-    /**
41
-     * @return string
42
-     */
43
-    protected function renderForBackend()
44
-    {
45
-        $output = '';
46
-
47
-        // Get label of the foreign table.
48
-        $foreignLabelField = $this->getForeignTableLabelField($this->fieldName);
49
-
50
-        if (Tca::table($this->object)->field($this->fieldName)->hasOne()) {
51
-            $foreignObject = $this->object[$this->fieldName];
52
-
53
-            if ($foreignObject) {
54
-                $output = sprintf(
55
-                    '<a href="%s" data-uid="%s" class="btn-edit invisible">%s</a> <span>%s</span>',
56
-                    $this->getEditUri($foreignObject),
57
-                    $this->object->getUid(),
58
-                    $this->getIconFactory()->getIcon('actions-document-open', Icon::SIZE_SMALL),
59
-                    $foreignObject[$foreignLabelField]
60
-                );
61
-            }
62
-        } elseif (Tca::table($this->object)->field($this->fieldName)->hasMany()) {
63
-            if (!empty($this->object[$this->fieldName])) {
64
-                /** @var $foreignObject \Fab\Vidi\Domain\Model\Content */
65
-                foreach ($this->object[$this->fieldName] as $foreignObject) {
66
-                    $output .= sprintf(
67
-                        '<li><a href="%s" data-uid="%s" class="btn-edit invisible">%s</a> <span>%s</span></li>',
68
-                        $this->getEditUri($foreignObject),
69
-                        $this->object->getUid(),
70
-                        $this->getIconFactory()->getIcon('actions-document-open', Icon::SIZE_SMALL),
71
-                        $foreignObject[$foreignLabelField]
72
-                    );
73
-                }
74
-                $output = sprintf('<ul class="list-unstyled">%s</ul>', $output);
75
-            }
76
-        }
77
-        return $output;
78
-    }
79
-
80
-    /**
81
-     * @return string
82
-     */
83
-    protected function renderForFrontend()
84
-    {
85
-        $output = '';
86
-
87
-        // Get label of the foreign table.
88
-        $foreignLabelField = $this->getForeignTableLabelField($this->fieldName);
89
-
90
-        if (Tca::table($this->object)->field($this->fieldName)->hasOne()) {
91
-            $foreignObject = $this->object[$this->fieldName];
92
-
93
-            if ($foreignObject) {
94
-                $output = sprintf(
95
-                    '%s',
96
-                    $foreignObject[$foreignLabelField]
97
-                );
98
-            }
99
-        } elseif (Tca::table($this->object)->field($this->fieldName)->hasMany()) {
100
-            if (!empty($this->object[$this->fieldName])) {
101
-                /** @var $foreignObject \Fab\Vidi\Domain\Model\Content */
102
-                foreach ($this->object[$this->fieldName] as $foreignObject) {
103
-                    $output .= sprintf(
104
-                        '<li>%s</li>',
105
-                        $foreignObject[$foreignLabelField]
106
-                    );
107
-                }
108
-                $output = sprintf('<ul class="list-unstyled">%s</ul>', $output);
109
-            }
110
-        }
111
-        return $output;
112
-    }
113
-
114
-    /**
115
-     * Render an edit URI given an object.
116
-     *
117
-     * @param Content $object
118
-     * @return string
119
-     */
120
-    protected function getEditUri(Content $object)
121
-    {
122
-        $uri = BackendUtility::getModuleUrl(
123
-            'record_edit',
124
-            array(
125
-                $this->getEditParameterName($object) => 'edit',
126
-                'returnUrl' => $this->getModuleLoader()->getModuleUrl()
127
-            )
128
-        );
129
-        return $uri;
130
-    }
131
-
132
-    /**
133
-     * @param Content $object
134
-     * @return string
135
-     */
136
-    protected function getEditParameterName(Content $object)
137
-    {
138
-        return sprintf(
139
-            'edit[%s][%s]',
140
-            $object->getDataType(),
141
-            $object->getUid()
142
-        );
143
-    }
144
-
145
-    /**
146
-     * Return the label field of the foreign table.
147
-     *
148
-     * @param string $fieldName
149
-     * @return string
150
-     */
151
-    protected function getForeignTableLabelField($fieldName)
152
-    {
153
-        // Get TCA table service.
154
-        $table = Tca::table($this->object);
155
-
156
-        // Compute the label of the foreign table.
157
-        $relationDataType = $table->field($fieldName)->relationDataType();
158
-        return Tca::table($relationDataType)->getLabelField();
159
-    }
160
-
161
-    protected function isBackendMode(): bool
162
-    {
163
-        return Typo3Mode::isBackendMode();
164
-    }
24
+	/**
25
+	 * Render a representation of the relation on the GUI.
26
+	 *
27
+	 * @return string
28
+	 */
29
+	public function render()
30
+	{
31
+		if ($this->isBackendMode()) {
32
+			$output = $this->renderForBackend();
33
+		} else {
34
+			$output = $this->renderForFrontend();
35
+		}
36
+
37
+		return $output;
38
+	}
39
+
40
+	/**
41
+	 * @return string
42
+	 */
43
+	protected function renderForBackend()
44
+	{
45
+		$output = '';
46
+
47
+		// Get label of the foreign table.
48
+		$foreignLabelField = $this->getForeignTableLabelField($this->fieldName);
49
+
50
+		if (Tca::table($this->object)->field($this->fieldName)->hasOne()) {
51
+			$foreignObject = $this->object[$this->fieldName];
52
+
53
+			if ($foreignObject) {
54
+				$output = sprintf(
55
+					'<a href="%s" data-uid="%s" class="btn-edit invisible">%s</a> <span>%s</span>',
56
+					$this->getEditUri($foreignObject),
57
+					$this->object->getUid(),
58
+					$this->getIconFactory()->getIcon('actions-document-open', Icon::SIZE_SMALL),
59
+					$foreignObject[$foreignLabelField]
60
+				);
61
+			}
62
+		} elseif (Tca::table($this->object)->field($this->fieldName)->hasMany()) {
63
+			if (!empty($this->object[$this->fieldName])) {
64
+				/** @var $foreignObject \Fab\Vidi\Domain\Model\Content */
65
+				foreach ($this->object[$this->fieldName] as $foreignObject) {
66
+					$output .= sprintf(
67
+						'<li><a href="%s" data-uid="%s" class="btn-edit invisible">%s</a> <span>%s</span></li>',
68
+						$this->getEditUri($foreignObject),
69
+						$this->object->getUid(),
70
+						$this->getIconFactory()->getIcon('actions-document-open', Icon::SIZE_SMALL),
71
+						$foreignObject[$foreignLabelField]
72
+					);
73
+				}
74
+				$output = sprintf('<ul class="list-unstyled">%s</ul>', $output);
75
+			}
76
+		}
77
+		return $output;
78
+	}
79
+
80
+	/**
81
+	 * @return string
82
+	 */
83
+	protected function renderForFrontend()
84
+	{
85
+		$output = '';
86
+
87
+		// Get label of the foreign table.
88
+		$foreignLabelField = $this->getForeignTableLabelField($this->fieldName);
89
+
90
+		if (Tca::table($this->object)->field($this->fieldName)->hasOne()) {
91
+			$foreignObject = $this->object[$this->fieldName];
92
+
93
+			if ($foreignObject) {
94
+				$output = sprintf(
95
+					'%s',
96
+					$foreignObject[$foreignLabelField]
97
+				);
98
+			}
99
+		} elseif (Tca::table($this->object)->field($this->fieldName)->hasMany()) {
100
+			if (!empty($this->object[$this->fieldName])) {
101
+				/** @var $foreignObject \Fab\Vidi\Domain\Model\Content */
102
+				foreach ($this->object[$this->fieldName] as $foreignObject) {
103
+					$output .= sprintf(
104
+						'<li>%s</li>',
105
+						$foreignObject[$foreignLabelField]
106
+					);
107
+				}
108
+				$output = sprintf('<ul class="list-unstyled">%s</ul>', $output);
109
+			}
110
+		}
111
+		return $output;
112
+	}
113
+
114
+	/**
115
+	 * Render an edit URI given an object.
116
+	 *
117
+	 * @param Content $object
118
+	 * @return string
119
+	 */
120
+	protected function getEditUri(Content $object)
121
+	{
122
+		$uri = BackendUtility::getModuleUrl(
123
+			'record_edit',
124
+			array(
125
+				$this->getEditParameterName($object) => 'edit',
126
+				'returnUrl' => $this->getModuleLoader()->getModuleUrl()
127
+			)
128
+		);
129
+		return $uri;
130
+	}
131
+
132
+	/**
133
+	 * @param Content $object
134
+	 * @return string
135
+	 */
136
+	protected function getEditParameterName(Content $object)
137
+	{
138
+		return sprintf(
139
+			'edit[%s][%s]',
140
+			$object->getDataType(),
141
+			$object->getUid()
142
+		);
143
+	}
144
+
145
+	/**
146
+	 * Return the label field of the foreign table.
147
+	 *
148
+	 * @param string $fieldName
149
+	 * @return string
150
+	 */
151
+	protected function getForeignTableLabelField($fieldName)
152
+	{
153
+		// Get TCA table service.
154
+		$table = Tca::table($this->object);
155
+
156
+		// Compute the label of the foreign table.
157
+		$relationDataType = $table->field($fieldName)->relationDataType();
158
+		return Tca::table($relationDataType)->getLabelField();
159
+	}
160
+
161
+	protected function isBackendMode(): bool
162
+	{
163
+		return Typo3Mode::isBackendMode();
164
+	}
165 165
 }
Please login to merge, or discard this patch.
Classes/Grid/RelationCountRenderer.php 1 patch
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -21,61 +21,61 @@
 block discarded – undo
21 21
  */
22 22
 class RelationCountRenderer extends ColumnRendererAbstract
23 23
 {
24
-    /**
25
-     * Render a representation of the relation on the GUI.
26
-     *
27
-     * @return string
28
-     */
29
-    public function render()
30
-    {
31
-        $output = '';
32
-        if ($this->isBackendMode()) {
33
-            $output = $this->renderForBackend();
34
-        }
24
+	/**
25
+	 * Render a representation of the relation on the GUI.
26
+	 *
27
+	 * @return string
28
+	 */
29
+	public function render()
30
+	{
31
+		$output = '';
32
+		if ($this->isBackendMode()) {
33
+			$output = $this->renderForBackend();
34
+		}
35 35
 
36
-        return $output;
37
-    }
36
+		return $output;
37
+	}
38 38
 
39
-    /**
40
-     * @return string
41
-     */
42
-    protected function renderForBackend()
43
-    {
44
-        $numberOfObjects = count($this->object[$this->fieldName]);
39
+	/**
40
+	 * @return string
41
+	 */
42
+	protected function renderForBackend()
43
+	{
44
+		$numberOfObjects = count($this->object[$this->fieldName]);
45 45
 
46
-        if ($numberOfObjects > 1) {
47
-            $label = 'LLL:EXT:vidi/Resources/Private/Language/locallang.xlf:items';
48
-            if (isset($this->gridRendererConfiguration['labelPlural'])) {
49
-                $label = $this->gridRendererConfiguration['labelPlural'];
50
-            }
51
-        } else {
52
-            $label = 'LLL:EXT:vidi/Resources/Private/Language/locallang.xlf:item';
53
-            if (isset($this->gridRendererConfiguration['labelSingular'])) {
54
-                $label = $this->gridRendererConfiguration['labelSingular'];
55
-            }
56
-        }
46
+		if ($numberOfObjects > 1) {
47
+			$label = 'LLL:EXT:vidi/Resources/Private/Language/locallang.xlf:items';
48
+			if (isset($this->gridRendererConfiguration['labelPlural'])) {
49
+				$label = $this->gridRendererConfiguration['labelPlural'];
50
+			}
51
+		} else {
52
+			$label = 'LLL:EXT:vidi/Resources/Private/Language/locallang.xlf:item';
53
+			if (isset($this->gridRendererConfiguration['labelSingular'])) {
54
+				$label = $this->gridRendererConfiguration['labelSingular'];
55
+			}
56
+		}
57 57
 
58
-        $template = '<a href="%s&returnUrl=%s&search=%s&query=%s:%s">%s %s<span class="invisible" style="padding-left: 5px">%s</span></a>';
58
+		$template = '<a href="%s&returnUrl=%s&search=%s&query=%s:%s">%s %s<span class="invisible" style="padding-left: 5px">%s</span></a>';
59 59
 
60
-        $foreignField = Tca::table($this->object)->field($this->fieldName)->getForeignField();
61
-        $search = json_encode(array(array($foreignField => $this->object->getUid())));
60
+		$foreignField = Tca::table($this->object)->field($this->fieldName)->getForeignField();
61
+		$search = json_encode(array(array($foreignField => $this->object->getUid())));
62 62
 
63
-        $moduleTarget = empty($this->gridRendererConfiguration['targetModule']) ? '' : $this->gridRendererConfiguration['targetModule'];
64
-        return sprintf(
65
-            $template,
66
-            BackendUtility::getModuleUrl($moduleTarget),
67
-            rawurlencode(BackendUtility::getModuleUrl($this->gridRendererConfiguration['sourceModule'])),
68
-            rawurlencode($search),
69
-            rawurlencode($foreignField),
70
-            rawurlencode($this->object->getUid()),
71
-            htmlspecialchars($numberOfObjects),
72
-            htmlspecialchars(LocalizationUtility::translate($label, '')),
73
-            $this->getIconFactory()->getIcon('extensions-vidi-go', Icon::SIZE_SMALL)
74
-        );
75
-    }
63
+		$moduleTarget = empty($this->gridRendererConfiguration['targetModule']) ? '' : $this->gridRendererConfiguration['targetModule'];
64
+		return sprintf(
65
+			$template,
66
+			BackendUtility::getModuleUrl($moduleTarget),
67
+			rawurlencode(BackendUtility::getModuleUrl($this->gridRendererConfiguration['sourceModule'])),
68
+			rawurlencode($search),
69
+			rawurlencode($foreignField),
70
+			rawurlencode($this->object->getUid()),
71
+			htmlspecialchars($numberOfObjects),
72
+			htmlspecialchars(LocalizationUtility::translate($label, '')),
73
+			$this->getIconFactory()->getIcon('extensions-vidi-go', Icon::SIZE_SMALL)
74
+		);
75
+	}
76 76
 
77
-    protected function isBackendMode(): bool
78
-    {
79
-        return Typo3Mode::isBackendMode();
80
-    }
77
+	protected function isBackendMode(): bool
78
+	{
79
+		return Typo3Mode::isBackendMode();
80
+	}
81 81
 }
Please login to merge, or discard this patch.
Classes/Grid/RelationEditRenderer.php 1 patch
Indentation   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -19,49 +19,49 @@
 block discarded – undo
19 19
  */
20 20
 class RelationEditRenderer extends ColumnRendererAbstract
21 21
 {
22
-    /**
23
-     * @return string
24
-     */
25
-    public function render()
26
-    {
27
-        $output = '';
28
-        if ($this->isBackendMode()) {
29
-            $output = $this->renderForBackend();
30
-        }
22
+	/**
23
+	 * @return string
24
+	 */
25
+	public function render()
26
+	{
27
+		$output = '';
28
+		if ($this->isBackendMode()) {
29
+			$output = $this->renderForBackend();
30
+		}
31 31
 
32
-        return $output;
33
-    }
32
+		return $output;
33
+	}
34 34
 
35
-    /**
36
-     * @return string
37
-     */
38
-    protected function renderForBackend()
39
-    {
40
-        // Initialize url parameters array.
41
-        $urlParameters = array(
42
-            $this->getModuleLoader()->getParameterPrefix() => array(
43
-                'controller' => 'Content',
44
-                'action' => 'edit',
45
-                'matches' => array('uid' => $this->object->getUid()),
46
-                'fieldNameAndPath' => $this->getFieldName(),
47
-            ),
48
-        );
35
+	/**
36
+	 * @return string
37
+	 */
38
+	protected function renderForBackend()
39
+	{
40
+		// Initialize url parameters array.
41
+		$urlParameters = array(
42
+			$this->getModuleLoader()->getParameterPrefix() => array(
43
+				'controller' => 'Content',
44
+				'action' => 'edit',
45
+				'matches' => array('uid' => $this->object->getUid()),
46
+				'fieldNameAndPath' => $this->getFieldName(),
47
+			),
48
+		);
49 49
 
50
-        $fieldLabel = Tca::table()->field($this->getFieldName())->getLabel();
51
-        if ($fieldLabel) {
52
-            $fieldLabel = str_replace(':', '', $fieldLabel); // sanitize label
53
-        }
50
+		$fieldLabel = Tca::table()->field($this->getFieldName())->getLabel();
51
+		if ($fieldLabel) {
52
+			$fieldLabel = str_replace(':', '', $fieldLabel); // sanitize label
53
+		}
54 54
 
55
-        return sprintf(
56
-            '<div style="text-align: right" class="pull-right invisible"><a href="%s" class="btn-edit-relation" data-field-label="%s">%s</a></div>',
57
-            $this->getModuleLoader()->getModuleUrl($urlParameters),
58
-            $fieldLabel,
59
-            $this->getIconFactory()->getIcon('actions-add', Icon::SIZE_SMALL)
60
-        );
61
-    }
55
+		return sprintf(
56
+			'<div style="text-align: right" class="pull-right invisible"><a href="%s" class="btn-edit-relation" data-field-label="%s">%s</a></div>',
57
+			$this->getModuleLoader()->getModuleUrl($urlParameters),
58
+			$fieldLabel,
59
+			$this->getIconFactory()->getIcon('actions-add', Icon::SIZE_SMALL)
60
+		);
61
+	}
62 62
 
63
-    protected function isBackendMode(): bool
64
-    {
65
-        return Typo3Mode::isBackendMode();
66
-    }
63
+	protected function isBackendMode(): bool
64
+	{
65
+		return Typo3Mode::isBackendMode();
66
+	}
67 67
 }
Please login to merge, or discard this patch.
Classes/ViewHelpers/Be/AdditionalAssetsViewHelper.php 2 patches
Indentation   +35 added lines, -35 removed lines patch added patch discarded remove patch
@@ -22,42 +22,42 @@
 block discarded – undo
22 22
  */
23 23
 class AdditionalAssetsViewHelper extends AbstractBackendViewHelper
24 24
 {
25
-    /**
26
-     * Load the assets (JavaScript, CSS) for this Vidi module.
27
-     *
28
-     * @return void
29
-     * @api
30
-     */
31
-    public function render()
32
-    {
33
-        $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
34
-        /** @var ModuleLoader $moduleLoader */
35
-        $moduleLoader = GeneralUtility::makeInstance(ModuleLoader::class);
25
+	/**
26
+	 * Load the assets (JavaScript, CSS) for this Vidi module.
27
+	 *
28
+	 * @return void
29
+	 * @api
30
+	 */
31
+	public function render()
32
+	{
33
+		$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
34
+		/** @var ModuleLoader $moduleLoader */
35
+		$moduleLoader = GeneralUtility::makeInstance(ModuleLoader::class);
36 36
 
37
-        foreach ($moduleLoader->getAdditionalStyleSheetFiles() as $addCssFile) {
38
-            $fileNameAndPath = $this->resolvePath($addCssFile);
39
-            $pageRenderer->addCssFile($fileNameAndPath);
40
-        }
37
+		foreach ($moduleLoader->getAdditionalStyleSheetFiles() as $addCssFile) {
38
+			$fileNameAndPath = $this->resolvePath($addCssFile);
39
+			$pageRenderer->addCssFile($fileNameAndPath);
40
+		}
41 41
 
42
-        foreach ($moduleLoader->getAdditionalJavaScriptFiles() as $addJsFile) {
43
-            $fileNameAndPath = $this->resolvePath($addJsFile);
44
-            $pageRenderer->addJsFile($fileNameAndPath);
45
-        }
46
-    }
42
+		foreach ($moduleLoader->getAdditionalJavaScriptFiles() as $addJsFile) {
43
+			$fileNameAndPath = $this->resolvePath($addJsFile);
44
+			$pageRenderer->addJsFile($fileNameAndPath);
45
+		}
46
+	}
47 47
 
48
-    /**
49
-     * Resolve a resource path.
50
-     *
51
-     * @param string $uri
52
-     * @return string
53
-     */
54
-    protected function resolvePath($uri)
55
-    {
56
-        $uri = GeneralUtility::getFileAbsFileName($uri);
57
-        $uri = substr($uri, strlen(Environment::getPublicPath() . '/'));
58
-        if (Typo3Mode::isBackendMode() && $uri !== false) {
59
-            $uri = '../' . $uri;
60
-        }
61
-        return $uri;
62
-    }
48
+	/**
49
+	 * Resolve a resource path.
50
+	 *
51
+	 * @param string $uri
52
+	 * @return string
53
+	 */
54
+	protected function resolvePath($uri)
55
+	{
56
+		$uri = GeneralUtility::getFileAbsFileName($uri);
57
+		$uri = substr($uri, strlen(Environment::getPublicPath() . '/'));
58
+		if (Typo3Mode::isBackendMode() && $uri !== false) {
59
+			$uri = '../' . $uri;
60
+		}
61
+		return $uri;
62
+	}
63 63
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -54,9 +54,9 @@
 block discarded – undo
54 54
     protected function resolvePath($uri)
55 55
     {
56 56
         $uri = GeneralUtility::getFileAbsFileName($uri);
57
-        $uri = substr($uri, strlen(Environment::getPublicPath() . '/'));
57
+        $uri = substr($uri, strlen(Environment::getPublicPath().'/'));
58 58
         if (Typo3Mode::isBackendMode() && $uri !== false) {
59
-            $uri = '../' . $uri;
59
+            $uri = '../'.$uri;
60 60
         }
61 61
         return $uri;
62 62
     }
Please login to merge, or discard this patch.
Classes/Tca/Tca.php 1 patch
Indentation   +117 added lines, -117 removed lines patch added patch discarded remove patch
@@ -25,132 +25,132 @@
 block discarded – undo
25 25
  */
26 26
 class Tca implements SingletonInterface, TcaServiceInterface
27 27
 {
28
-    /**
29
-     * Fields that are considered as system.
30
-     *
31
-     * @var array
32
-     */
33
-    protected static $systemFields = array(
34
-        'uid',
35
-        'pid',
36
-        'tstamp',
37
-        'crdate',
38
-        'deleted',
39
-        'hidden',
40
-        'sys_language_uid',
41
-        'l18n_parent',
42
-        'l18n_diffsource',
43
-        't3ver_oid',
44
-        't3ver_id',
45
-        't3ver_wsid',
46
-        't3ver_label',
47
-        't3ver_state',
48
-        't3ver_stage',
49
-        't3ver_count',
50
-        't3ver_tstamp',
51
-        't3_origuid',
52
-    );
28
+	/**
29
+	 * Fields that are considered as system.
30
+	 *
31
+	 * @var array
32
+	 */
33
+	protected static $systemFields = array(
34
+		'uid',
35
+		'pid',
36
+		'tstamp',
37
+		'crdate',
38
+		'deleted',
39
+		'hidden',
40
+		'sys_language_uid',
41
+		'l18n_parent',
42
+		'l18n_diffsource',
43
+		't3ver_oid',
44
+		't3ver_id',
45
+		't3ver_wsid',
46
+		't3ver_label',
47
+		't3ver_state',
48
+		't3ver_stage',
49
+		't3ver_count',
50
+		't3ver_tstamp',
51
+		't3_origuid',
52
+	);
53 53
 
54
-    /**
55
-     * @var array
56
-     */
57
-    protected static $instances;
54
+	/**
55
+	 * @var array
56
+	 */
57
+	protected static $instances;
58 58
 
59
-    /**
60
-     * Returns a class instance of a corresponding TCA service.
61
-     * If the class instance does not exist, create one.
62
-     *
63
-     * @throws NotExistingClassException
64
-     * @param string $dataType
65
-     * @param string $serviceType
66
-     * @return TcaServiceInterface
67
-     * @throws InvalidKeyInArrayException
68
-     * @throws \InvalidArgumentException
69
-     */
70
-    protected static function getService($dataType, $serviceType)
71
-    {
72
-        if (Typo3Mode::isBackendMode() && empty($dataType)) {
73
-            /** @var ModuleLoader $moduleLoader */
74
-            $moduleLoader = GeneralUtility::makeInstance(ModuleLoader::class);
75
-            $dataType = $moduleLoader->getDataType();
76
-        }
59
+	/**
60
+	 * Returns a class instance of a corresponding TCA service.
61
+	 * If the class instance does not exist, create one.
62
+	 *
63
+	 * @throws NotExistingClassException
64
+	 * @param string $dataType
65
+	 * @param string $serviceType
66
+	 * @return TcaServiceInterface
67
+	 * @throws InvalidKeyInArrayException
68
+	 * @throws \InvalidArgumentException
69
+	 */
70
+	protected static function getService($dataType, $serviceType)
71
+	{
72
+		if (Typo3Mode::isBackendMode() && empty($dataType)) {
73
+			/** @var ModuleLoader $moduleLoader */
74
+			$moduleLoader = GeneralUtility::makeInstance(ModuleLoader::class);
75
+			$dataType = $moduleLoader->getDataType();
76
+		}
77 77
 
78
-        if (empty(self::$instances[$dataType][$serviceType])) {
79
-            $className = sprintf('Fab\Vidi\Tca\%sService', ucfirst($serviceType));
78
+		if (empty(self::$instances[$dataType][$serviceType])) {
79
+			$className = sprintf('Fab\Vidi\Tca\%sService', ucfirst($serviceType));
80 80
 
81
-            // Signal to pre-process the TCA of the given $dataType.
82
-            self::emitPreProcessTcaSignal($dataType, $serviceType);
81
+			// Signal to pre-process the TCA of the given $dataType.
82
+			self::emitPreProcessTcaSignal($dataType, $serviceType);
83 83
 
84
-            $instance = GeneralUtility::makeInstance($className, $dataType, $serviceType);
85
-            self::$instances[$dataType][$serviceType] = $instance;
86
-        }
87
-        return self::$instances[$dataType][$serviceType];
88
-    }
84
+			$instance = GeneralUtility::makeInstance($className, $dataType, $serviceType);
85
+			self::$instances[$dataType][$serviceType] = $instance;
86
+		}
87
+		return self::$instances[$dataType][$serviceType];
88
+	}
89 89
 
90
-    /**
91
-     * Returns a "grid" service instance.
92
-     *
93
-     * @param string|Content $tableNameOrContentObject
94
-     * @return GridService
95
-     * @throws NotExistingClassException
96
-     */
97
-    public static function grid($tableNameOrContentObject = '')
98
-    {
99
-        $tableName = $tableNameOrContentObject instanceof Content ? $tableNameOrContentObject->getDataType() : $tableNameOrContentObject;
100
-        return self::getService($tableName, self::TYPE_GRID);
101
-    }
90
+	/**
91
+	 * Returns a "grid" service instance.
92
+	 *
93
+	 * @param string|Content $tableNameOrContentObject
94
+	 * @return GridService
95
+	 * @throws NotExistingClassException
96
+	 */
97
+	public static function grid($tableNameOrContentObject = '')
98
+	{
99
+		$tableName = $tableNameOrContentObject instanceof Content ? $tableNameOrContentObject->getDataType() : $tableNameOrContentObject;
100
+		return self::getService($tableName, self::TYPE_GRID);
101
+	}
102 102
 
103
-    /**
104
-     * Returns a "table" service instance ("ctrl" part of the TCA).
105
-     *
106
-     * @param string|Content $tableNameOrContentObject
107
-     * @return TableService
108
-     * @throws NotExistingClassException
109
-     */
110
-    public static function table($tableNameOrContentObject = '')
111
-    {
112
-        $tableName = $tableNameOrContentObject instanceof Content ? $tableNameOrContentObject->getDataType() : $tableNameOrContentObject;
113
-        return self::getService($tableName, self::TYPE_TABLE);
114
-    }
103
+	/**
104
+	 * Returns a "table" service instance ("ctrl" part of the TCA).
105
+	 *
106
+	 * @param string|Content $tableNameOrContentObject
107
+	 * @return TableService
108
+	 * @throws NotExistingClassException
109
+	 */
110
+	public static function table($tableNameOrContentObject = '')
111
+	{
112
+		$tableName = $tableNameOrContentObject instanceof Content ? $tableNameOrContentObject->getDataType() : $tableNameOrContentObject;
113
+		return self::getService($tableName, self::TYPE_TABLE);
114
+	}
115 115
 
116
-    /**
117
-     * @return array
118
-     */
119
-    public static function getInstanceStorage()
120
-    {
121
-        return self::$instances;
122
-    }
116
+	/**
117
+	 * @return array
118
+	 */
119
+	public static function getInstanceStorage()
120
+	{
121
+		return self::$instances;
122
+	}
123 123
 
124
-    /**
125
-     * @return array
126
-     */
127
-    public static function getSystemFields()
128
-    {
129
-        return self::$systemFields;
130
-    }
124
+	/**
125
+	 * @return array
126
+	 */
127
+	public static function getSystemFields()
128
+	{
129
+		return self::$systemFields;
130
+	}
131 131
 
132
-    /**
133
-     * Signal that is called after the content repository for a content type has been instantiated.
134
-     *
135
-     * @param string $dataType
136
-     * @param string $serviceType
137
-     * @throws InvalidSlotException
138
-     * @throws InvalidSlotReturnException
139
-     * @throws \InvalidArgumentException
140
-     */
141
-    protected static function emitPreProcessTcaSignal($dataType, $serviceType)
142
-    {
143
-        self::getSignalSlotDispatcher()->dispatch(Tca::class, 'preProcessTca', array($dataType, $serviceType));
144
-    }
132
+	/**
133
+	 * Signal that is called after the content repository for a content type has been instantiated.
134
+	 *
135
+	 * @param string $dataType
136
+	 * @param string $serviceType
137
+	 * @throws InvalidSlotException
138
+	 * @throws InvalidSlotReturnException
139
+	 * @throws \InvalidArgumentException
140
+	 */
141
+	protected static function emitPreProcessTcaSignal($dataType, $serviceType)
142
+	{
143
+		self::getSignalSlotDispatcher()->dispatch(Tca::class, 'preProcessTca', array($dataType, $serviceType));
144
+	}
145 145
 
146
-    /**
147
-     * Get the SignalSlot dispatcher
148
-     *
149
-     * @return Dispatcher
150
-     * @throws \InvalidArgumentException
151
-     */
152
-    protected static function getSignalSlotDispatcher()
153
-    {
154
-        return GeneralUtility::makeInstance(Dispatcher::class);
155
-    }
146
+	/**
147
+	 * Get the SignalSlot dispatcher
148
+	 *
149
+	 * @return Dispatcher
150
+	 * @throws \InvalidArgumentException
151
+	 */
152
+	protected static function getSignalSlotDispatcher()
153
+	{
154
+		return GeneralUtility::makeInstance(Dispatcher::class);
155
+	}
156 156
 }
Please login to merge, or discard this patch.