Passed
Branch development (7a3bc2)
by Elk
06:00
created

Loader   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 114
c 4
b 1
f 0
dl 0
loc 216
rs 10
wmc 22
1
<?php
2
3
/**
4
 * This class takes care of loading language files
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * @version 2.0 dev
11
 *
12
 */
13
14
namespace ElkArte\Languages;
15
16
use ElkArte\Debug;
17
use ElkArte\Errors;
18
use ElkArte\Database\QueryInterface;
19
20
/**
21
 * This class takes care of loading language files
22
 */
23
class Loader
24
{
25
	/** @var string */
26
	protected $path = '';
27
28
	/** @var QueryInterface */
29
	protected $db = '';
30
31
	/** @var string */
32
	protected $language = 'English';
33
34
	/** @var string */
35
	protected $variable_name = '';
36
37
	/** @var bool */
38
	protected $load_fallback = true;
39
40
	/** @var mixed[] */
41
	protected $variable = true;
42
43
	/** @var string[] Holds the name of the files already loaded to load them only once */
44
	protected $loaded = [];
45
46
	public function __construct($lang = null, &$variable, QueryInterface $db, string $variable_name = 'txt')
47
	{
48
		if ($lang !== null)
49
		{
50
			$this->language = ucfirst($lang);
51
		}
52
53
		$this->path = SOURCEDIR . '/ElkArte/Languages/';
54
		$this->db = $db;
55
56
		$this->variable = &$variable;
57
		$this->variable_name = $variable_name;
58
		if (empty($this->variable))
59
		{
60
			$this->variable = [];
61
		}
62
	}
63
64
	public function setFallback(bool $new_Status)
65
	{
66
		$this->load_fallback = $new_Status;
67
	}
68
69
	public function changePath($path)
70
	{
71
		$this->path = $path;
72
	}
73
74
	public function load($file_name, $fatal = true, $fix_calendar_arrays = false)
75
	{
76
		global $db_show_debug, $txt;
77
78
		$file_names = explode('+', $file_name);
79
80
		// For each file open it up and write it out!
81
		foreach ($file_names as $file)
82
		{
83
			$file = ucfirst($file);
84
			if (isset($this->loaded[$file]) || in_array($file, Editor::IGNORE_FILES))
85
			{
86
				continue;
87
			}
88
89
			$found_fallback = false;
90
			if ($this->load_fallback)
91
			{
92
				$found_fallback = $this->loadFile($file, 'English');
93
			}
94
			$found = $this->loadFile($file, $this->language);
95
96
			$this->loaded[$file] = true;
97
98
			// Keep track of what we're up to, soldier.
99
			if ($found && $db_show_debug === true)
100
			{
101
				Debug::instance()->add(
102
					'language_files',
103
					$file . '.' . $this->language .
104
					' (' . str_replace(BOARDDIR, '', $this->path) . ')'
105
				);
106
			}
107
108
			// That couldn't be found!  Log the error, but *try* to continue normally.
109
			if (!$found && $fatal)
110
			{
111
				Errors::instance()->log_error(
112
					sprintf(
113
						$txt['theme_language_error'],
114
						$file . '.' . $this->language,
115
						'template'
116
					)
117
				);
118
				// If we do have a fallback it may not be necessary to break out.
119
				if ($found_fallback === false)
120
				{
121
					break;
122
				}
123
			}
124
		}
125
		$this->loadFromDb($file_names);
126
127
		if ($fix_calendar_arrays)
128
		{
129
			$this->fix_calendar_text();
130
		}
131
	}
132
133
	protected function loadFromDb($files)
134
	{
135
		$result = $this->db->fetchQuery('
136
			SELECT language_key, value
137
			FROM {db_prefix}languages
138
			WHERE language = {string:language}
139
				AND file IN ({array_string:files})',
140
			[
141
				'language' => $this->language,
142
				'files' => $files
143
			]
144
		);
145
		while ($row = $result->fetch_assoc())
146
		{
147
			$this->variable[$row['language_key']] = $row['value'];
148
		}
149
		$result->free_result();
150
	}
151
152
	protected function loadFile($name, $language)
153
	{
154
		$filepath = $this->path . $name . '/' . $this->language . '.php';
155
		if (file_exists($filepath))
156
		{
157
			require($filepath);
158
			if (!empty(${$this->variable_name}))
159
			{
160
				$this->variable += ${$this->variable_name};
161
			}
162
			return true;
163
		}
164
		return false;
165
	}
166
167
	/**
168
	 * Loads / Sets arrays for use in date display
169
	 * This is here and not in a language file for two reasons:
170
	 *  1. the structure is required by the code, so better be sure
171
	 *     to have it the way we are supposed to have it
172
	 *  2. Transifex (that we use for translating the strings) doesn't
173
	 *     support array of arrays, so if we move this to a language file
174
	 *     we'd need to move away from Tx.
175
	 */
176
	protected function fix_calendar_text()
177
	{
178
		global $txt;
179
180
		$txt['days'] = array(
181
			$txt['sunday'],
182
			$txt['monday'],
183
			$txt['tuesday'],
184
			$txt['wednesday'],
185
			$txt['thursday'],
186
			$txt['friday'],
187
			$txt['saturday'],
188
		);
189
		$txt['days_short'] = array(
190
			$txt['sunday_short'],
191
			$txt['monday_short'],
192
			$txt['tuesday_short'],
193
			$txt['wednesday_short'],
194
			$txt['thursday_short'],
195
			$txt['friday_short'],
196
			$txt['saturday_short'],
197
		);
198
		$txt['months'] = array(
199
			1 => $txt['january'],
200
			$txt['february'],
201
			$txt['march'],
202
			$txt['april'],
203
			$txt['may'],
204
			$txt['june'],
205
			$txt['july'],
206
			$txt['august'],
207
			$txt['september'],
208
			$txt['october'],
209
			$txt['november'],
210
			$txt['december'],
211
		);
212
		$txt['months_titles'] = array(
213
			1 => $txt['january_titles'],
214
			$txt['february_titles'],
215
			$txt['march_titles'],
216
			$txt['april_titles'],
217
			$txt['may_titles'],
218
			$txt['june_titles'],
219
			$txt['july_titles'],
220
			$txt['august_titles'],
221
			$txt['september_titles'],
222
			$txt['october_titles'],
223
			$txt['november_titles'],
224
			$txt['december_titles'],
225
		);
226
		$txt['months_short'] = array(
227
			1 => $txt['january_short'],
228
			$txt['february_short'],
229
			$txt['march_short'],
230
			$txt['april_short'],
231
			$txt['may_short'],
232
			$txt['june_short'],
233
			$txt['july_short'],
234
			$txt['august_short'],
235
			$txt['september_short'],
236
			$txt['october_short'],
237
			$txt['november_short'],
238
			$txt['december_short'],
239
		);
240
	}
241
}