LanguageRepository::getDefaultLanguage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace AOE\Languagevisibility;
4
5
/***************************************************************
6
 * Copyright notice
7
 *
8
 * (c) 2016 AOE GmbH <[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
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 *
32
 * @author	Daniel Poetzinger <[email protected]>
33
 * @coauthor Tolleiv Nietsch <[email protected]>
34
 * @coauthor Timo Schmidt <[email protected]>
35
 */
36
class LanguageRepository {
37
38
	protected static $instance;
39
40
	/**
41
	 * Internal method to fetch all language rows from the database.
42
	 *
43
	 * @see self::$allLanguageRows
44
	 * @param void
45
	 * @return void
46
	 */
47
	protected function fetchAllLanguageRows() {
48
		$cacheManager = CacheManager::getInstance();
49
		$cacheData = $cacheManager->get('allLanguageRows');
50
51
		if (count($cacheData) <= 0) {
52
			$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_language', '', '', '', '');
53
			while ( $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res) ) {
54
				$cacheData[$row['uid']] = $row;
55
			}
56
57
			$GLOBALS['TYPO3_DB']->sql_free_result($res);
58
59
			$cacheManager->set('allLanguageRows', $cacheData);
60
		}
61
	}
62
63
	/**
64
	 * Returns an array with all languages depending on the cache setting directly from
65
	 * the database or cached.
66
	 *
67
	 * @return array
68
	 */
69
	protected function getCachedOrUncacheResults() {
70
		$results = array();
71
		$cacheManager = CacheManager::getInstance();
72
		$isCacheEnabled = $cacheManager->isCacheEnabled();
73
74
		if ($isCacheEnabled) {
75
			$this->fetchAllLanguageRows();
76
			$results = $cacheManager->get('allLanguageRows');
77
		} else {
78
			$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_language', '', '', '', '');
79
			while ( $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res) ) {
80
				$results[] = $row;
81
			}
82
			$GLOBALS['TYPO3_DB']->sql_free_result($res);
83
		}
84
85
		return $results;
86
	}
87
88
	/**
89
	 * This method returns an array with all available language objects in the system.
90
	 *
91
	 * @see tx_languagevisibility_language
92
	 * @return array
93
	 */
94
	public function getLanguages() {
95
		$return = array();
96
		$results = $this->getCachedOrUncacheResults();
97
98
		$return[] = $this->getDefaultLanguage();
99
		foreach ( $results as $row ) {
100
			$language = GeneralUtility::makeInstance('AOE\\Languagevisibility\\Language');
101
			$language->setData($row);
102
			$return[] = $language;
103
		}
104
105
		return $return;
106
	}
107
108
	/**
109
	 * Returns an array with all available languages of a backend user.
110
	 *
111
	 * @return array
112
	 */
113
	public function getLanguagesForBEUser() {
114
		$return = array();
115
		$results = $this->getCachedOrUncacheResults();
116
117
		if ($GLOBALS['BE_USER']->checkLanguageAccess(0)) {
118
			$return[] = $this->getDefaultLanguage();
119
		}
120
121
		foreach ( $results as $row ) {
122
			if ($GLOBALS['BE_USER']->checkLanguageAccess($row['uid'])) {
123
				$language = GeneralUtility::makeInstance('AOE\\Languagevisibility\\Language');
124
				$language->setData($row);
125
				$return[] = $language;
126
			}
127
		}
128
129
		return $return;
130
	}
131
132
	/**
133
	 * Retruns an instance of the language object for the default language.
134
	 *
135
	 * @param void
136
	 * @return tx_languagevisibility_language
137
	 */
138
	public function getDefaultLanguage() {
139
		$row = array();
140
		$language = GeneralUtility::makeInstance('AOE\\Languagevisibility\\Language');
141
		$row['uid'] = 0;
142
		$row['title'] = 'Default';
143
144
		$language->setData($row);
145
		return $language;
146
	}
147
148
	/**
149
	 * Returns an instance for a language by the id.
150
	 * Note: since the language is an value object all languages can be cached
151
	 *
152
	 * @param $id
153
	 * @return tx_languagevisibility_language
154
	 */
155
	public function getLanguageById($id) {
156
		$cacheManager = CacheManager::getInstance();
157
		$cacheData = $cacheManager->get('languagesCache');
158
		$isCacheEnabled = $cacheManager->isCacheEnabled();
159
		$id = is_array($id) ? array_shift($id) : $id;
160
		
161
		if (! $isCacheEnabled || ! isset($cacheData[$id])) {
162
			if ($id == 0) {
163
				$cacheData[$id] = $this->getDefaultLanguage();
164
			} else {
165
				$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_language', 'uid=' . intval($id), '', '', '');
166
				$row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
167
				$language = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('AOE\\Languagevisibility\\Language');
168
169
				$language->setData($row);
170
				$cacheData[$id] = $language;
171
				$GLOBALS['TYPO3_DB']->sql_free_result($res);
172
173
				$cacheManager->set('languagesCache', $cacheData);
174
			}
175
		}
176
		return $cacheData[$id];
177
	}
178
179
	/**
180
	 * returns an instance of the language repository as singleton.
181
	 *
182
	 * @param void
183
	 * @return Languagerepository
184
	 */
185
	public static function makeInstance() {
186
		if (! self::$instance instanceof LanguageRepository) {
187
			self::$instance = GeneralUtility::makeInstance('AOE\\Languagevisibility\\LanguageRepository');
188
		}
189
190
		return self::$instance;
191
	}
192
}
193