Passed
Push — master ( cc3f84...4a930e )
by Timo
23:43
created

FrontendOverlayService::getUidOfOverlay()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 10.744

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 28
ccs 13
cts 20
cp 0.65
rs 8.4444
c 0
b 0
f 0
cc 8
nc 10
nop 3
crap 10.744
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\System\Language;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2018 Timo Hund <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\System\TCA\TCAService;
29
use ApacheSolrForTypo3\Solr\Util;
30
use Doctrine\DBAL\Driver\Statement;
31
use TYPO3\CMS\Core\Database\ConnectionPool;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
34
35
/**
36
 * Class FrontendOverlayService
37
 * @package ApacheSolrForTypo3\Solr\System\Language
38
 */
39
class FrontendOverlayService {
40
41
    /**
42
     * @var TCAService
43
     */
44
    protected $tcaService = null;
45
46
    /**
47
     * @var TypoScriptFrontendController
48
     */
49
    protected $tsfe = null;
50
51
    /**
52
     * Relation constructor.
53
     * @param TCAService|null $tcaService
54
     * @param TypoScriptFrontendController|null $tsfe
55
     */
56 46
    public function __construct(TCAService $tcaService = null, TypoScriptFrontendController $tsfe = null)
57
    {
58 46
        $this->tcaService = $tcaService ?? GeneralUtility::makeInstance(TCAService::class);
59 46
        $this->tsfe = $tsfe ?? $GLOBALS['TSFE'];
60 46
    }
61
62
    /**
63
     * Return the translated record
64
     *
65
     * @param string $tableName
66
     * @param array $record
67
     * @return array
68
     */
69 5
    public function getOverlay($tableName, $record)
70
    {
71 5
        if ($tableName === 'pages') {
72 2
            return $this->tsfe->sys_page->getPageOverlay($record, $this->tsfe->sys_language_uid);
73
        }
74
75 4
        return $this->tsfe->sys_page->getRecordOverlay($tableName, $record, $this->tsfe->sys_language_uid);
76
    }
77
78
    /**
79
     * Returns the overlay table for a certain table.
80
     *
81
     * @param string $table
82
     * @param string $field
83
     * @return string
84
     */
85 46
    public function getOverlayTable(string $table, string $field) : string
86
    {
87
        // pages has a special overlay table constriction
88 46
        if ($this->tsfe->sys_language_uid > 0
89 46
            && $table === 'pages'
90 46
            && $this->tcaService->getHasConfigurationForField('pages_language_overlay', $field)
91 46
            && Util::getIsTYPO3VersionBelow9()) {
92 1
            return 'pages_language_overlay';
93
        }
94
95 46
        return $table;
96
    }
97
98
    /**
99
     * When the record has an overlay we retrieve the uid of the translated record,
100
     * to resolve the relations from the translation.
101
     *
102
     * @param string $table
103
     * @param string $field
104
     * @param int $uid
105
     * @return int
106
     */
107 46
    public function getUidOfOverlay($table, $field, $uid)
108
    {
109
        // when no language is set at all we do not need to overlay
110 46
        if (!isset($this->tsfe->sys_language_uid)) {
111
            return $uid;
112
        }
113
        // when no language is set we can return the passed recordUid
114 46
        if (!$this->tsfe->sys_language_uid > 0) {
115 44
            return $uid;
116
        }
117
        // when no TCA configured for pages_language_overlay's field, then use original record Uid
118
        // @todo this can be dropped when TYPO3 8 compatibility is dropped
119 5
        $translatedInPagesLanguageOverlayAndNoTCAPresent = Util::getIsTYPO3VersionBelow9() &&
120 5
            !$this->tcaService->getHasConfigurationForField('pages_language_overlay', $field);
121 5
        if ($table === 'pages' && $translatedInPagesLanguageOverlayAndNoTCAPresent) {
122 2
            return $uid;
123
        }
124
125 3
        $record = $this->getRecord($table, $uid);
126
127
        // when the overlay is not an array, we return the localRecordUid
128 3
        if (!is_array($record)) {
129
            return $uid;
130
        }
131
132 3
        $overlayUid = $this->getLocalRecordUidFromOverlay($table, $record);
133 3
        $uid = ($overlayUid !== 0) ? $overlayUid : $uid;
134 3
        return $uid;
135
    }
136
137
    /**
138
     * This method retrieves the _PAGES_OVERLAY_UID or _LOCALIZED_UID from the localized record.
139
     *
140
     * @param string $localTableName
141
     * @param array $originalRecord
142
     * @return int
143
     */
144 3
    protected function getLocalRecordUidFromOverlay($localTableName, $originalRecord)
145
    {
146 3
        $overlayRecord = $this->getOverlay($localTableName, $originalRecord);
147
148
        // when there is a _PAGES_OVERLAY_UID | _LOCALIZED_UID in the overlay, we return it
149 3
        if ($localTableName === 'pages' && isset($overlayRecord['_PAGES_OVERLAY_UID'])) {
150 1
            return (int)$overlayRecord['_PAGES_OVERLAY_UID'];
151 2
        } elseif (isset($overlayRecord['_LOCALIZED_UID'])) {
152 2
            return (int)$overlayRecord['_LOCALIZED_UID'];
153
        }
154
155
        return 0;
156
    }
157
158
    /**
159
     * @param $localTableName
160
     * @param $localRecordUid
161
     * @return mixed
162
     */
163 3
    protected function getRecord($localTableName, $localRecordUid)
164
    {
165
        /** @var QueryBuilder $queryBuilder */
166 3
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($localTableName);
167
168 3
        $record = $queryBuilder->select('*')->from($localTableName)->where($queryBuilder->expr()->eq('uid', $localRecordUid))->execute()->fetch();
169 3
        return $record;
170
    }
171
}