Issues (202)

Classes/System/Language/FrontendOverlayService.php (2 issues)

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 TYPO3\CMS\Core\Database\ConnectionPool;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
33
34
/**
35
 * Class FrontendOverlayService
36
 */
37
class FrontendOverlayService {
38
39
    /**
40
     * @var TCAService
41
     */
42
    protected $tcaService = null;
43
44
    /**
45
     * @var TypoScriptFrontendController
46
     */
47
    protected $tsfe = null;
48
49
    /**
50
     * Relation constructor.
51
     * @param TCAService|null $tcaService
52
     * @param TypoScriptFrontendController|null $tsfe
53
     */
54
    public function __construct(TCAService $tcaService = null, TypoScriptFrontendController $tsfe = null)
55
    {
56
        $this->tcaService = $tcaService ?? GeneralUtility::makeInstance(TCAService::class);
57
        $this->tsfe = $tsfe ?? $GLOBALS['TSFE'];
58
    }
59
60
    /**
61
     * Return the translated record
62
     *
63
     * @param string $tableName
64
     * @param array $record
65
     * @return array
66
     */
67
    public function getOverlay($tableName, $record)
68
    {
69
        if ($tableName === 'pages') {
70
            // @extensionScannerIgnoreLine
71
            return $this->tsfe->sys_page->getPageOverlay($record, Util::getLanguageUid());
72
        }
73
74
        // @extensionScannerIgnoreLine
75
        return $this->tsfe->sys_page->getRecordOverlay($tableName, $record, Util::getLanguageUid());
76
    }
77
78
    /**
79
     * When the record has an overlay we retrieve the uid of the translated record,
80
     * to resolve the relations from the translation.
81
     *
82
     * @param string $table
83
     * @param string $field
84
     * @param int $uid
85
     * @return int
86
     */
87
    public function getUidOfOverlay($table, $field, $uid)
0 ignored issues
show
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

87
    public function getUidOfOverlay($table, /** @scrutinizer ignore-unused */ $field, $uid)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
    {
89
        // when no language is set at all we do not need to overlay
90
        if (Util::getLanguageUid() === null) {
0 ignored issues
show
The condition ApacheSolrForTypo3\Solr\...tLanguageUid() === null is always false.
Loading history...
91
            return $uid;
92
        }
93
        // when no language is set we can return the passed recordUid
94
        if (!(Util::getLanguageUid() > 0)) {
95
            return $uid;
96
        }
97
98
        $record = $this->getRecord($table, $uid);
99
100
        // when the overlay is not an array, we return the localRecordUid
101
        if (!is_array($record)) {
102
            return $uid;
103
        }
104
105
        $overlayUid = $this->getLocalRecordUidFromOverlay($table, $record);
106
        $uid = ($overlayUid !== 0) ? $overlayUid : $uid;
107
        return $uid;
108
    }
109
110
    /**
111
     * This method retrieves the _PAGES_OVERLAY_UID or _LOCALIZED_UID from the localized record.
112
     *
113
     * @param string $localTableName
114
     * @param array $originalRecord
115
     * @return int
116
     */
117
    protected function getLocalRecordUidFromOverlay($localTableName, $originalRecord)
118
    {
119
        $overlayRecord = $this->getOverlay($localTableName, $originalRecord);
120
121
        // when there is a _PAGES_OVERLAY_UID | _LOCALIZED_UID in the overlay, we return it
122
        if ($localTableName === 'pages' && isset($overlayRecord['_PAGES_OVERLAY_UID'])) {
123
            return (int)$overlayRecord['_PAGES_OVERLAY_UID'];
124
        } elseif (isset($overlayRecord['_LOCALIZED_UID'])) {
125
            return (int)$overlayRecord['_LOCALIZED_UID'];
126
        }
127
128
        return 0;
129
    }
130
131
    /**
132
     * @param $localTableName
133
     * @param $localRecordUid
134
     * @return mixed
135
     */
136
    protected function getRecord($localTableName, $localRecordUid)
137
    {
138
        /** @var QueryBuilder $queryBuilder */
139
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($localTableName);
140
141
        $record = $queryBuilder->select('*')->from($localTableName)->where($queryBuilder->expr()->eq('uid', $localRecordUid))->execute()->fetch();
142
        return $record;
143
    }
144
}
145