Completed
Push — 7LTS_compatible ( d8e85d...3c5c36 )
by Tomas Norre
28:52 queued 26:53
created

LanguageRepository::getLanguagesForBEUser()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 11

Duplication

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