Completed
Push — 7LTS_compatible ( b51d35...40d99a )
by Tomas Norre
32:08
created

T3libPage::getRecordOverlay_preProcess()   D

Complexity

Conditions 9
Paths 10

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 38
rs 4.909
c 1
b 0
f 0
cc 9
eloc 22
nc 10
nop 5
1
<?php
2
3
namespace AOE\Languagevisibility\Hooks;
4
5
/***************************************************************
6
 * Copyright notice
7
 *
8
 * (c) 2007 AOE media ([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 2 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
use AOE\Languagevisibility\Exceptions\InvalidRowException;
28
use AOE\Languagevisibility\FceElement;
29
use AOE\Languagevisibility\FceOverlayElement;
30
use AOE\Languagevisibility\Services\FeServices;
31
32
/**
33
 * Class tx_languagevisibility_hooks_t3lib_page
34
 *
35
 * @package Aoe\Languagevisibility\Hooks
36
 */
37
class T3libPage implements \TYPO3\CMS\Frontend\Page\PageRepositoryGetPageOverlayHookInterface, \TYPO3\CMS\Frontend\Page\PageRepositoryGetRecordOverlayHookInterface {
38
39
	/**
40
	 * This function has various possible results:
41
	 * 1)    $lUid unchanged -
42
	 * there was nothing to do for langvis and the overlay is requested is fine
43
	 * 2)    $lUid == null
44
	 * is relevant if we did the overlay ourselfs and the processing within getPageOverlay function is not relevant anymore
45
	 * 3)    $lUid changed
46
	 * is relevant if we just changed the target-languge but require getPageOverlay to proceed with the overlay-chrunching
47
	 * 4)   $lUid changed to 0 (which may be the case for forced fallbacks to default). Please check Core Setting hideIfNotTranslated in this case to be sure the page can be shown in this case
48
	 *
49
	 * @param mixed $pageInput
50
	 * @param integer $lUid Passed ad reference!
51
	 * @param \TYPO3\CMS\Frontend\Page\PageRepository $parent
52
	 * @return void
53
	 */
54
	public function getPageOverlay_preProcess(&$pageInput, &$lUid, \TYPO3\CMS\Frontend\Page\PageRepository $parent) {
55
		if (is_int($pageInput)) {
56
			$page_id = $pageInput;
57
		} elseif (is_array($pageInput) && isset($pageInput['uid'])) {
58
			$page_id = $pageInput['uid'];
59
		} else {
60
			return;
61
		}
62
63
		// call service to know if element is visible and which overlay language to use
64
		$overlayLanguage = FesSrvices::getOverlayLanguageIdForElementRecord($page_id, 'pages', $lUid);
65
66
		if ($overlayLanguage === FALSE) {
67
			if (is_array($pageInput)) {
68
				$pageInput['_NOTVISIBLE'] = TRUE;
69
			}
70
			$lUid = NULL;
71
		} else {
72
			$lUid = $overlayLanguage;
73
		}
74
	}
75
76
	/**
77
	 * The flow in t3lib_page is:
78
	 *  - call preProcess
79
	 *  - if uid and pid > then overlay if langauge != 0
80
	 *  - after this postProcess is called - which only corrects the overlay row for certain elements
81
	 *
82
	 * @param string $table
83
	 * @param array $row
84
	 * @param integer $sys_language_content
85
	 * @param string $OLmode
86
	 * @param \TYPO3\CMS\Frontend\Page\PageRepository $parent
87
	 * @return void
88
	 */
89
	public function getRecordOverlay_preProcess($table, &$row, &$sys_language_content, $OLmode, \TYPO3\CMS\Frontend\Page\PageRepository $parent) {
90
		if (!FeServices::isSupportedTable($table)
91
			|| (!is_array($row))
92
			|| (!isset($row['uid']))) {
93
			return;
94
		}
95
96
		try {
97
			$element = FeServices::getElement($row['uid'], $table);
98
			$overlayLanguage = FeServices::getOverlayLanguageIdForElement($element, $sys_language_content);
99
		} catch ( InvalidRowException $e ) {
100
			$row['uid'] = 0;
101
			$row['pid'] = 0;
102
			return;
103
		}
104
		catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class AOE\Languagevisibility\Hooks\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
105
			return;
106
		}
107
108
		if ($overlayLanguage === FALSE) {
109
			$row['uid'] = 0;
110
			$row['pid'] = 0;
111
			return;
112
		} elseif (!$element->isMonolithicTranslated()) {
113
				// for monolytic elements the tx_languagevisibility_feservices::getOverlayLanguageIdForElement return 0 to "tell" us that no overlay is required
114
				// but since the TYPO3 Core interprets a language with id 0 to not return anything we need to leave the $sys_language_content untouched for MonolithicTranslated elements
115
			$sys_language_content = $overlayLanguage;
116
		}
117
118
			/**
119
			 * the original value will be replaced by the original getRecordOverlay process
120
			 * therefore we've to store this elsewhere to make sure that the flexdata is available
121
			 * for the postProcess
122
			 */
123
		if ($element instanceof FceOverlayElement) {
124
			$row['_ORIG_tx_templavoila_flex'] = $row['tx_templavoila_flex'];
125
		}
126
	}
127
128
	/**
129
	 *
130
	 * @param string $table
131
	 * @param array $row
132
	 * @param integer $sys_language_content
133
	 * @param string $OLmode
134
	 * @param \TYPO3\CMS\Frontend\Page\PageRepository $parent
135
	 * @return void
136
	 */
137
	public function getRecordOverlay_postProcess($table, &$row, &$sys_language_content, $OLmode, \TYPO3\CMS\Frontend\Page\PageRepository $parent) {
138
		if (is_array($row) && $row['uid'] === 0 && $row['pid'] === 0) {
139
			$row = FALSE;
140
			return;
141
		}
142
143
		if (!FeServices::isSupportedTable($table)
144
			|| (!is_array($row))
145
			|| (!isset($row['uid']))
146
			|| ($sys_language_content == 0)) {
147
			return;
148
		}
149
150
		try {
151
			$element = FeServices::getElement($row['uid'], $table);
152
			$overlayLanguage = FeServices::getOverlayLanguageIdForElement($element, $sys_language_content);
153
		} catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class AOE\Languagevisibility\Hooks\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
154
			return;
155
		}
156
157
		if ($element instanceof FceElement) {
158
				//for FCE the overlay processing is handled by templavoila module, so mark the row with additional infos:
159
			$languageRep = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_languagevisibility_languagerepository');
160
			$overlayLanguageObj = $languageRep->getLanguageById($overlayLanguage);
161
			$row['_OVERLAYLANGUAGEISOCODE'] = $overlayLanguageObj->getIsoCode();
162
		} elseif ($element instanceof FceOverlayElement) {
163
				//now its getting tricky: we need to return overlay record with merged XML
164
			$row['tx_templavoila_flex'] = $row['_ORIG_tx_templavoila_flex'];
165
			unset($row['_ORIG_tx_templavoila_flex']);
166
			$olrow = $this->_getDatabaseTranslationOverlayRecord('tt_content', $row, $overlayLanguage);
167
			if ($GLOBALS['TSFE']) {
168
				$GLOBALS['TSFE']->includeTCA('tt_content');
169
			}
170
				//parse fce xml, and where a xml field is empty in olrow -> use default one
171
			$flexObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools');
172
			$this->_callbackVar_defaultXML = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($row['tx_templavoila_flex']);
173
			$this->_callbackVar_overlayXML = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($olrow['tx_templavoila_flex']);
174
			if (! is_array($this->_callbackVar_overlayXML)) {
175
				$this->_callbackVar_overlayXML = array();
176
			}
177
			$return = $flexObj->traverseFlexFormXMLData('tt_content', 'tx_templavoila_flex', $row, $this, '_callback_checkXMLFieldsForFallback');
0 ignored issues
show
Unused Code introduced by
$return is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
178
179
			if ($sys_language_content != $overlayLanguage) {
180
				$row = $parent->getRecordOverlay($table, $row, $overlayLanguage, $OLmode);
181
			}
182
			$row['tx_templavoila_flex'] = \TYPO3\CMS\Core\Utility\GeneralUtility::array2xml_cs($this->_callbackVar_overlayXML, 'T3FlexForms', array('useCDATA' => TRUE));
183
		}
184
	}
185
186
	/**
187
	 * @param $dsArr
188
	 * @param $dataValue
189
	 * @param $PA
190
	 * @param $structurePath
191
	 * @param $pObj
192
	 */
193
	public function _callback_checkXMLFieldsForFallback($dsArr, $dataValue, $PA, $structurePath, $pObj) {
194
		if ($dsArr['TCEforms']['l10n_mode'] == 'exclude') {
195
			$pObj->setArrayValueByPath($structurePath, $this->_callbackVar_overlayXML, $dataValue);
196
		} elseif ($dataValue != '' && $dsArr['TCEforms']['l10n_mode'] == 'mergeIfNotBlank') {
197
			$overlayValue = $pObj->getArrayValueByPath($structurePath, $this->_callbackVar_overlayXML);
198
			if ($overlayValue == '') {
199
				$pObj->setArrayValueByPath($structurePath, $this->_callbackVar_overlayXML, $dataValue);
200
			}
201
		}
202
	}
203
204
	/**
205
	 * @param $table
206
	 * @param $row
207
	 * @param $languageId
208
	 * @return mixed
209
	 */
210
	protected function _getDatabaseTranslationOverlayRecord($table, $row, $languageId) {
211
			// Select overlay record:
212
		$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $table, 'pid=' . intval($row['pid']) . ' AND ' . $GLOBALS['TCA'][$table]['ctrl']['languageField'] . '=' . intval($languageId) . ' AND ' . $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'] . '=' . intval($row['uid']) . $GLOBALS['TSFE']->sys_page->enableFields($table), '', '', '1');
213
		$olrow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
214
		$GLOBALS['TSFE']->sys_page->versionOL($table, $olrow);
215
		$GLOBALS['TYPO3_DB']->sql_free_result($res);
216
		return $olrow;
217
	}
218
}
219