FlexFormLanguageContainer.php 6.39 KB
Newer Older
Christian Kuhn's avatar
Christian Kuhn committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
namespace TYPO3\CMS\Backend\Form\Container;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\Form\Utility\FormEngineUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
23
use TYPO3\CMS\Backend\Form\NodeFactory;
Christian Kuhn's avatar
Christian Kuhn committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

/**
 * Handle flex form language overlays.
 *
 * This container is falled from the entry FlexFormContainer. For each existing language overlay
 * it forks a FlexFormTabsContainer or a FlexFormNoTabsContainer for rendering a full flex form
 * record of the specific language.
 */
class FlexFormLanguageContainer extends AbstractContainer {

	/**
	 * Entry method
	 *
	 * @return array As defined in initializeResultArray() of AbstractNode
	 */
	public function render() {
		$table = $this->globalOptions['table'];
		$row = $this->globalOptions['databaseRow'];
		$flexFormDataStructureArray = $this->globalOptions['flexFormDataStructureArray'];
		$flexFormRowData = $this->globalOptions['flexFormRowData'];

		// Determine available languages
		$langChildren = (bool)$flexFormDataStructureArray['meta']['langChildren'];
		$langDisabled = (bool)$flexFormDataStructureArray['meta']['langDisable'];
		$flexFormRowData['meta']['currentLangId'] = array();
		// Look up page language overlays
		$checkPageLanguageOverlay = $this->getBackendUserAuthentication()->getTSConfigVal('options.checkPageLanguageOverlay') ? TRUE : FALSE;
		$pageOverlays = array();
		if ($checkPageLanguageOverlay) {
			$whereClause = 'pid=' . (int)$row['pid'] . BackendUtility::deleteClause('pages_language_overlay')
				. BackendUtility::versioningPlaceholderClause('pages_language_overlay');
			$pageOverlays = $this->getDatabaseConnection()->exec_SELECTgetRows('*', 'pages_language_overlay', $whereClause, '', '', '', 'sys_language_uid');
		}
		$languages = $this->getAvailableLanguages();
		foreach ($languages as $langInfo) {
			if (
				$this->getBackendUserAuthentication()->checkLanguageAccess($langInfo['uid'])
				&& (!$checkPageLanguageOverlay || $langInfo['uid'] <= 0 || is_array($pageOverlays[$langInfo['uid']]))
			) {
				$flexFormRowData['meta']['currentLangId'][] = $langInfo['ISOcode'];
			}
		}
		if (!is_array($flexFormRowData['meta']['currentLangId']) || !count($flexFormRowData['meta']['currentLangId'])) {
			$flexFormRowData['meta']['currentLangId'] = array('DEF');
		}
		$flexFormRowData['meta']['currentLangId'] = array_unique($flexFormRowData['meta']['currentLangId']);
		$flexFormNoEditDefaultLanguage = FALSE;
		if ($langChildren || $langDisabled) {
			$availableLanguages = array('DEF');
		} else {
			if (!in_array('DEF', $flexFormRowData['meta']['currentLangId'])) {
				array_unshift($flexFormRowData['meta']['currentLangId'], 'DEF');
				$flexFormNoEditDefaultLanguage = TRUE;
			}
			$availableLanguages = $flexFormRowData['meta']['currentLangId'];
		}

		// Tabs or no tabs - that's the question
		$hasTabs = FALSE;
		if (is_array($flexFormDataStructureArray['sheets'])) {
			$hasTabs = TRUE;
		}

		$resultArray = $this->initializeResultArray();

		foreach ($availableLanguages as $lKey) {
			// Add language as header
			if (!$langChildren && !$langDisabled) {
				$resultArray['html'] .= LF . '<strong>' . FormEngineUtility::getLanguageIcon($table, $row, ('v' . $lKey)) . $lKey . ':</strong>';
			}

			// Default language "lDEF", other options are "lUK" or whatever country code
			$flexFormCurrentLanguage = 'l' . $lKey;

			$options = $this->globalOptions;
			$options['flexFormCurrentLanguage'] = $flexFormCurrentLanguage;
			$options['flexFormNoEditDefaultLanguage'] = $flexFormNoEditDefaultLanguage;
			if (!$hasTabs) {
102
				$options['renderType'] = 'flexFormNoTabsContainer';
103
104
105
				/** @var NodeFactory $nodeFactory */
				$nodeFactory = $this->globalOptions['nodeFactory'];
				$flexFormNoTabsResult = $nodeFactory->create($options)->render();
Christian Kuhn's avatar
Christian Kuhn committed
106
107
				$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $flexFormNoTabsResult);
			} else {
108
				$options['renderType'] = 'flexFormTabsContainer';
109
110
111
				/** @var NodeFactory $nodeFactory */
				$nodeFactory = $this->globalOptions['nodeFactory'];
				$flexFormTabsContainerResult = $nodeFactory->create($options)->render();
Christian Kuhn's avatar
Christian Kuhn committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
				$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $flexFormTabsContainerResult);
			}
		}

		return $resultArray;
	}

	/**
	 * Returns an array of available languages (to use for FlexForms)
	 *
	 * @return array
	 */
	protected function getAvailableLanguages() {
		$isLoaded = ExtensionManagementUtility::isLoaded('static_info_tables');

		// Find all language records in the system
		$db = $this->getDatabaseConnection();
		$res = $db->exec_SELECTquery(
			'language_isocode,static_lang_isocode,title,uid',
			'sys_language',
			'pid=0 AND hidden=0' . BackendUtility::deleteClause('sys_language'),
			'',
			'title'
		);

		// Traverse them
		$output = array(
			0 => array(
				'uid' => 0,
				'title' => 'Default language',
				'ISOcode' => 'DEF',
			)
		);

		while ($row = $db->sql_fetch_assoc($res)) {
			$output[$row['uid']] = $row;
			if (!empty($row['language_isocode'])) {
				$output[$row['uid']]['ISOcode'] = $row['language_isocode'];
			} elseif ($isLoaded && $row['static_lang_isocode']) {
				GeneralUtility::deprecationLog('Usage of the field "static_lang_isocode" is discouraged, and will stop working with CMS 8. Use the built-in language field "language_isocode" in your sys_language records.');
				$rr = BackendUtility::getRecord('static_languages', $row['static_lang_isocode'], 'lg_iso_2');
				if ($rr['lg_iso_2']) {
					$output[$row['uid']]['ISOcode'] = $rr['lg_iso_2'];
				}
			}
			if (!$output[$row['uid']]['ISOcode']) {
				unset($output[$row['uid']]);
			}
		}
		$db->sql_free_result($res);

		return $output;
	}

	/**
	 * @return BackendUserAuthentication
	 */
	protected function getBackendUserAuthentication() {
		return $GLOBALS['BE_USER'];
	}

	/**
	 * @return DatabaseConnection
	 */
	protected function getDatabaseConnection() {
		return $GLOBALS['TYPO3_DB'];
	}

}