Passed
Pull Request — development (#3540)
by Emanuele
07:11
created

Loader::changePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $found is dead and can be removed.
Loading history...
90
			$found_fallback = false;
91
			if ($this->load_fallback)
92
			{
93
				$found_fallback = $this->loadFile($file, 'English');
94
			}
95
			$found = $this->loadFile($file, $this->language);
96
97
			$this->loaded[$file] = true;
98
99
			// Keep track of what we're up to, soldier.
100
			if ($found && $db_show_debug === true)
101
			{
102
				Debug::instance()->add(
103
					'language_files',
104
					$file . '.' . $this->language .
105
					' (' . str_replace(BOARDDIR, '', $this->path) . ')'
106
				);
107
			}
108
109
			// That couldn't be found!  Log the error, but *try* to continue normally.
110
			if (!$found && $fatal)
111
			{
112
				Errors::instance()->log_error(
113
					sprintf(
114
						$txt['theme_language_error'],
115
						$file . '.' . $this->language,
116
						'template'
117
					)
118
				);
119
				// If we do have a fallback it may not be necessary to break out.
120
				if ($found_fallback === false)
121
				{
122
					break;
123
				}
124
			}
125
		}
126
		$this->loadFromDb($file_names);
127
128
		if ($fix_calendar_arrays)
129
		{
130
			$this->fix_calendar_text();
131
		}
132
	}
133
134
	protected function loadFromDb($files)
135
	{
136
		$result = $this->db->fetchQuery('
137
			SELECT language_key, value
138
			FROM {db_prefix}languages
139
			WHERE language = {string:language}
140
				AND file IN ({array_string:files})',
141
			[
142
				'language' => $this->language,
143
				'files' => $files
144
			]
145
		);
146
		while ($row = $result->fetch_assoc())
147
		{
148
			$this->variable[$row['language_key']] = $row['value'];
149
		}
150
		$result->free_result();
151
	}
152
153
	protected function loadFile($name, $language)
154
	{
155
		$filepath = $this->path . $name . '/' . $this->language . '.php';
156
		if (file_exists($filepath))
157
		{
158
			require($filepath);
159
			if (!empty(${$this->variable_name}))
160
			{
161
				$this->variable += ${$this->variable_name};
162
			}
163
			return true;
164
		}
165
		return false;
166
	}
167
168
	/**
169
	 * Loads / Sets arrays for use in date display
170
	 * This is here and not in a language file for two reasons:
171
	 *  1. the structure is required by the code, so better be sure
172
	 *     to have it the way we are supposed to have it
173
	 *  2. Transifex (that we use for translating the strings) doesn't
174
	 *     support array of arrays, so if we move this to a language file
175
	 *     we'd need to move away from Tx.
176
	 */
177
	protected function fix_calendar_text()
178
	{
179
		global $txt;
180
181
		$txt['days'] = array(
182
			$txt['sunday'],
183
			$txt['monday'],
184
			$txt['tuesday'],
185
			$txt['wednesday'],
186
			$txt['thursday'],
187
			$txt['friday'],
188
			$txt['saturday'],
189
		);
190
		$txt['days_short'] = array(
191
			$txt['sunday_short'],
192
			$txt['monday_short'],
193
			$txt['tuesday_short'],
194
			$txt['wednesday_short'],
195
			$txt['thursday_short'],
196
			$txt['friday_short'],
197
			$txt['saturday_short'],
198
		);
199
		$txt['months'] = array(
200
			1 => $txt['january'],
201
			$txt['february'],
202
			$txt['march'],
203
			$txt['april'],
204
			$txt['may'],
205
			$txt['june'],
206
			$txt['july'],
207
			$txt['august'],
208
			$txt['september'],
209
			$txt['october'],
210
			$txt['november'],
211
			$txt['december'],
212
		);
213
		$txt['months_titles'] = array(
214
			1 => $txt['january_titles'],
215
			$txt['february_titles'],
216
			$txt['march_titles'],
217
			$txt['april_titles'],
218
			$txt['may_titles'],
219
			$txt['june_titles'],
220
			$txt['july_titles'],
221
			$txt['august_titles'],
222
			$txt['september_titles'],
223
			$txt['october_titles'],
224
			$txt['november_titles'],
225
			$txt['december_titles'],
226
		);
227
		$txt['months_short'] = array(
228
			1 => $txt['january_short'],
229
			$txt['february_short'],
230
			$txt['march_short'],
231
			$txt['april_short'],
232
			$txt['may_short'],
233
			$txt['june_short'],
234
			$txt['july_short'],
235
			$txt['august_short'],
236
			$txt['september_short'],
237
			$txt['october_short'],
238
			$txt['november_short'],
239
			$txt['december_short'],
240
		);
241
	}
242
}