Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
savefile.php
1<?php
3
7
12 extends Operation
13{
21 public function run($file = '')
22 {
23 Loc::loadLanguageFile(__DIR__. '/operation.php');
24 Loc::loadLanguageFile(__FILE__);
25
26 $result = [];
27 if (empty($file))
28 {
29 $this->addError(new Main\Error(Loc::getMessage('TR_EDIT_FILE_PATH_ERROR')));
30 return $result;
31 }
32 $normalized = Main\IO\Path::normalize($file);
33 if ($normalized != $file)
34 {
35 $this->addError(new Main\Error(Loc::getMessage('TR_EDIT_FILE_WRONG_NAME')));
36 return $result;
37 }
38 $file = $normalized;
39 if (!Translate\IO\Path::isLangDir($file, true) || !Translate\IO\Path::isPhpFile($file))
40 {
41 $this->addError(new Main\Error(Loc::getMessage('TR_EDIT_ERROR_FILE_NOT_LANG', ['#FILE#' => $file])));
42 return $result;
43 }
44 if (!Translate\Permission::isAllowPath($file))
45 {
46 $this->addError(new Main\Error(Loc::getMessage('TR_EDIT_FILE_WRONG_NAME')));
47 return $result;
48 }
49
50 $request = $this->controller->getRequest();
51
52 $phraseIdsToDrop = $languagesToDrop = $phraseIdsToUpdate = $languagesToUpdate = [];
53
54 $enabledLanguagesList = Translate\Config::getEnabledLanguages();
55
56 $currentEncoding = Main\Localization\Translation::getCurrentEncoding();
57 $currentLang = Loc::getCurrentLang();
58 $limitEncoding = !($currentEncoding === 'utf-8' || Main\Localization\Translation::useTranslationRepository());
59
60 $isEncodingCompatible = function ($langId) use ($limitEncoding, $currentEncoding, $currentLang)
61 {
62 $compatible = true;
63 if ($limitEncoding)
64 {
65 $compatible = (
66 $langId == $currentLang ||
67 Translate\Config::getCultureEncoding($langId) == $currentEncoding ||
68 $langId === 'en'
69 );
70 }
71
72 return $compatible;
73 };
74
75 // codes to drop
76 $phraseIdsToDropTmp = $request->getPost('DROP');
77 if ($phraseIdsToDropTmp !== null && \is_array($phraseIdsToDropTmp) && \count($phraseIdsToDropTmp) > 0)
78 {
79 $phraseIdsToDrop = $phraseIdsToDropTmp;
80 $languagesToDrop = $enabledLanguagesList;
81 }
82 unset($phraseIdsToDropTmp);
83
84 // codes to update
85 $phraseIdsToUpdateTmp = $request->getPost('KEYS');
86 if ($phraseIdsToUpdateTmp !== null && \is_array($phraseIdsToUpdateTmp) && \count($phraseIdsToUpdateTmp) > 0)
87 {
88 $phraseIdsToUpdate = $phraseIdsToUpdateTmp;
89 }
90 unset($phraseIdsToUpdateTmp);
91
92 // languages to update
93 $languagesToUpdateTmp = $request->getPost('LANGS');
94 if ($languagesToUpdateTmp !== null && \is_array($languagesToUpdateTmp) && \count($languagesToUpdateTmp) > 0)
95 {
96 $languagesToUpdate = \array_intersect($languagesToUpdateTmp, $enabledLanguagesList);
97 }
98 unset($languagesToUpdateTmp);
99
100 // check
101 if (empty($phraseIdsToUpdate) && empty($phraseIdsToDrop))
102 {
103 $result['SUMMARY'] = Loc::getMessage('TR_EDIT_SAVING_COMPLETED');
104 return $result;
105 }
106 if (!empty($phraseIdsToUpdate) && empty($languagesToUpdate))
107 {
108 $this->addError(new Main\Error(Loc::getMessage('TR_EDIT_PARAM_ERROR')));
109 return $result;
110 }
111
112 $documentRoot = rtrim(Translate\IO\Path::tidy(Main\Application::getDocumentRoot()), '/');
113
114 $result['DROPPED'] = [];
115 $result['UPDATED'] = [];
116 $result['CLEANED'] = [];
117
118 foreach ($enabledLanguagesList as $langId)
119 {
120 if (!\in_array($langId, $languagesToUpdate) && !\in_array($langId, $languagesToDrop))
121 {
122 continue;
123 }
124
125 $langRelPath = Translate\IO\Path::replaceLangId($file, $langId);
126 $fullPath = Translate\IO\Path::tidy($documentRoot. '/'. $langRelPath);
127 $fullPath = Main\Localization\Translation::convertLangPath($fullPath, $langId);
128
129 $langFile = new Translate\File($fullPath);
130 $langFile->setLangId($langId);
131
132
133 // update and drop
134 if (\in_array($langId, $languagesToUpdate))
135 {
136 $langFile->setOperatingEncoding($currentEncoding);
137 }
138 // just drop
139 elseif (\in_array($langId, $languagesToDrop))
140 {
141 $langFile->setOperatingEncoding(Main\Localization\Translation::getSourceEncoding($langId));
142 }
143
144 if (!$langFile->loadTokens())
145 {
146 if (!$langFile->load() && $langFile->hasErrors())
147 {
148 foreach ($langFile->getErrors() as $error)
149 {
150 if ($error->getCode() !== 'EMPTY_CONTENT')
151 {
152 $this->addError($error);
153 }
154 }
155 }
156 }
157 if (\count($this->getErrors()) > 0)
158 {
159 continue;
160 }
161
162 $hasDataToUpdate = false;
163
164 // drop phrases
165 if (\in_array($langId, $languagesToDrop))
166 {
167 foreach ($phraseIdsToDrop as $phraseId)
168 {
169 if (isset($langFile[$phraseId]))
170 {
171 unset($langFile[$phraseId]);
172
173 $hasDataToUpdate = true;
174 if (!\in_array($phraseId, $result['DROPPED']))
175 {
176 $result['DROPPED'][] = $phraseId;
177 }
178 }
179 }
180 }
181
182 // set phrases
183 if (\in_array($langId, $languagesToUpdate) && $isEncodingCompatible($langId))
184 {
185 foreach ($phraseIdsToUpdate as $phraseId)
186 {
187 // has been deleted
188 if (\in_array($phraseId, $phraseIdsToDrop))
189 {
190 continue;
191 }
192
193 $fldName = $this->generateFieldName($phraseId, $langId);
194 if (!isset($request[$fldName]))
195 {
196 continue;
197 }
198 $inpValue = $request->getPost($fldName);
199
201 if (!empty($inpValue) || $inpValue === '0')
202 {
203 if ($langFile[$phraseId] !== $inpValue)
204 {
205 $langFile[$phraseId] = $inpValue;
206
207 $hasDataToUpdate = true;
208 if (!\in_array($fldName, $result['UPDATED']))
209 {
210 $result['UPDATED'][] = $fldName;
211 }
212 }
213 }
214 // remove empty
215 elseif (isset($langFile[$phraseId]) && $inpValue === '')
216 {
217 unset($langFile[$phraseId]);
218
219 $hasDataToUpdate = true;
220 if (!\in_array($fldName, $result['CLEANED']))
221 {
222 $result['CLEANED'][] = $fldName;
223 }
224 }
225 }
226 }
227
228 if ($hasDataToUpdate)
229 {
230 if ($langFile->count() > 0)
231 {
232 if ($this->updateLangFile($langFile))
233 {
234 $langFile->updatePhraseIndex();
235 }
236 }
237 else
238 {
239 if ($langFile->isExists())
240 {
241 $langFile->deletePhraseIndex();
242 if ($this->deleteLangFile($langFile))
243 {
244 $langFile->removeEmptyParents();
245 }
246 }
247 }
248 }
249 }
250
251 if (!$this->hasErrors())
252 {
253 $result['SUMMARY'] = Loc::getMessage('TR_EDIT_SAVING_COMPLETED');
254 }
255
256 return $result;
257 }
258
268 private function generateFieldName($phraseId, $suffix = '', $prefix = '')
269 {
270 return
271 (!empty($prefix) ? $prefix. '_' : '').
272 \str_replace(['.', '-', ' '], '_', $phraseId).
273 (!empty($suffix) ? '_'.$suffix : '')
274 ;
275 }
276}
static loadLanguageFile($file, $language=null, $normalize=true)
Definition loc.php:224
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
updateLangFile(Translate\File $langFile)
Definition operation.php:42
deleteLangFile(Translate\File $langFile)
static isAllowPath(string $path)
addError(Main\Error $error)
Definition error.php:22