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

Editor::getForEditing()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 13
nc 4
nop 0
dl 0
loc 23
rs 9.8333
c 1
b 0
f 1
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\Util;
17
use ElkArte\Database\QueryInterface;
18
19
/**
20
 * This class takes care of loading language files
21
 */
22
class Editor
23
{
24
	public const IGNORE_FILES = ['Agreement', 'PrivacyPolicy'];
25
26
	/** @var string */
27
	protected $path = '';
28
29
	/** @var QueryInterface */
30
	protected $db = '';
31
32
	/** @var string */
33
	protected $language = 'English';
34
35
	/** @var string */
36
	protected $variable_name = '';
37
38
	/** @var Loader */
39
	protected $loaders = [];
40
41
	/** @var mixed[] */
42
	protected $txt = '';
43
	protected $editortxt = '';
44
	protected $txtBirthdayEmails = '';
45
	protected $editing_strings = [];
46
	protected $ignore_keys = [];
47
48
	public function __construct($lang = null, QueryInterface $db, string $variable_name = 'txt')
49
	{
50
		if ($lang !== null)
51
		{
52
			$this->language = ucfirst($lang);
53
		}
54
55
		$this->path = SOURCEDIR . '/ElkArte/Languages/';
56
		$this->db = $db;
57
58
		$this->txt = [];
59
		$this->editortxt = [];
60
		$this->txtBirthdayEmails = [];
61
		$this->variable_name = $variable_name;
62
		$this->loaders = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('txt' => new ElkAr..., 'txtBirthdayEmails')) of type array<string,ElkArte\Languages\Loader> is incompatible with the declared type ElkArte\Languages\Loader of property $loaders.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63
			'txt' => new Loader($lang, $this->txt, $this->db, 'txt'),
64
			'editortxt' => new Loader($lang, $this->editortxt, $this->db, 'editortxt'),
65
			'txtBirthdayEmails' => new Loader($lang, $this->txtBirthdayEmails, $this->db, 'txtBirthdayEmails'),
66
		];
67
		// Ignore some things that are specific of the language pack (or just require some casting I don't want be bother of considering, like lang_capitalize_dates).
68
		$this->ignore_keys = ['lang_character_set', 'lang_locale', 'lang_dictionary', 'lang_spelling', 'lang_rtl', 'lang_capitalize_dates'];
69
	}
70
71
	public function changePath($path)
72
	{
73
		$this->path = $path;
74
	}
75
76
	public function load($file_name)
77
	{
78
		foreach (array_keys($this->loaders) as $k)
0 ignored issues
show
Bug introduced by
$this->loaders of type ElkArte\Languages\Loader is incompatible with the type array expected by parameter $array of array_keys(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
		foreach (array_keys(/** @scrutinizer ignore-type */ $this->loaders) as $k)
Loading history...
79
		{
80
			$this->loaders[$k]->load($file_name, true, false);
81
		}
82
	}
83
84
	public function get($idx)
85
	{
86
		foreach (array_keys($this->loaders) as $k)
0 ignored issues
show
Bug introduced by
$this->loaders of type ElkArte\Languages\Loader is incompatible with the type array expected by parameter $array of array_keys(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
		foreach (array_keys(/** @scrutinizer ignore-type */ $this->loaders) as $k)
Loading history...
87
		{
88
			if (isset($this->{$k}[$idx]))
89
			{
90
				return $this->{$k}[$idx];
91
			}
92
		}
93
		return null;
94
	}
95
96
	public function getForEditing()
97
	{
98
		$this->editing_strings = [];
99
		foreach (array_keys($this->loaders) as $k)
0 ignored issues
show
Bug introduced by
$this->loaders of type ElkArte\Languages\Loader is incompatible with the type array expected by parameter $array of array_keys(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
		foreach (array_keys(/** @scrutinizer ignore-type */ $this->loaders) as $k)
Loading history...
100
		{
101
			foreach ($this->{$k} as $key => $value)
102
			{
103
				if (in_array($key, $this->ignore_keys))
104
				{
105
					continue;
106
				}
107
				$md5EntryKey = md5($key);
108
				$editing_string = Util::htmlspecialchars(htmlentities($value));
109
110
				$this->editing_strings[$md5EntryKey] = [
111
					'key' => $md5EntryKey,
112
					'display_key' => $key,
113
					'value' => $editing_string,
114
					'rows' => (int) (strlen($editing_string) / 38) + substr_count($editing_string, "\n") + 1,
115
				];
116
			}
117
		}
118
		return $this->editing_strings;
119
	}
120
121
	public function save($file_name, $txt)
122
	{
123
		if (in_array($file_name, self::IGNORE_FILES))
124
		{
125
			return;
126
		}
127
		$to_save = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $to_save is dead and can be removed.
Loading history...
128
		$columns = [
129
			'language' => 'string-40',
130
			'file' => 'string-40',
131
			'language_key' => 'string-40',
132
			'value' => 'text'
133
		];
134
		foreach ($txt as $key => $val)
135
		{
136
			foreach (['txt', 'editortxt', 'txtBirthdayEmails'] as $var)
137
			{
138
				$display_key = $this->editing_strings[$key]['display_key'] ?? null;
139
				if (!isset($this->{$var}[$display_key]))
140
				{
141
					continue;
142
				}
143
				// For some reason, apparently sometimes a carriage return char (ASCII 13) appears in content from textareas, but we only use line feed (ASCII 10), so... just remove them all.
144
				$val = str_replace("\r", "", $val);
145
				if (trim($val) != trim($this->{$var}[$display_key]))
146
				{
147
					$this->db->replace('{db_prefix}languages',
148
						$columns,
149
						[
150
							'language' => $this->language,
151
							'file' => $file_name,
152
							'language_key' => $display_key,
153
							'value' => $val
154
						],
155
						['language', 'file']
156
					);
157
				}
158
			}
159
		}
160
	}
161
}