Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
savesource.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 $languagesToUpdate = [];
53
54 $enabledLanguagesList = Translate\Config::getEnabledLanguages();
55
56 // languages to update
57 $languagesToUpdateTmp = $request->getPost('LANGS');
58 if ($languagesToUpdateTmp !== null && \is_array($languagesToUpdateTmp) && \count($languagesToUpdateTmp) > 0)
59 {
60 $languagesToUpdate = \array_intersect($languagesToUpdateTmp, $enabledLanguagesList);
61 }
62 unset($languagesToUpdateTmp);
63
64 $currentEncoding = Main\Localization\Translation::getCurrentEncoding();
65 $documentRoot = \rtrim(Translate\IO\Path::tidy(Main\Application::getDocumentRoot()), '/');
66
67 foreach ($enabledLanguagesList as $langId)
68 {
69 if (!\in_array($langId, $languagesToUpdate))
70 {
71 continue;
72 }
73
74 $langRelPath = Translate\IO\Path::replaceLangId($file, $langId);
75 $fullPath = Translate\IO\Path::tidy($documentRoot.'/'.$langRelPath);
76 $fullPath = Main\Localization\Translation::convertLangPath($fullPath, $langId);
77
78 $langFile = new Translate\File($fullPath);
79 $langFile
80 ->setLangId($langId)
81 ->setOperatingEncoding($currentEncoding);
82
83 // backup
84 if ($langFile->isExists() && Translate\Config::needToBackUpFiles())
85 {
86 if (!$langFile->backup())
87 {
88 $this->addError(new Main\Error(
89 Loc::getMessage('TR_CREATE_BACKUP_ERROR', ['#FILE#' => $langFile->getPath()])
90 ));
91 }
92 }
93
94 $fileSrcForSave = $request->getPost('SRC_'. $langId);
95
96 if (empty($fileSrcForSave) || !\is_string($fileSrcForSave))
97 {
98 $this->addError(new Main\Error(Loc::getMessage('TR_EDIT_PARAM_ERROR')));
99 continue;
100 }
101
102 if (!$langFile->lint($fileSrcForSave))
103 {
104 $this->addErrors($langFile->getErrors());
105 continue;
106 }
107
108 try
109 {
110 if (!$langFile->putContents($fileSrcForSave))
111 {
112 if ($langFile->hasErrors())
113 {
114 $this->addErrors($langFile->getErrors());
115 }
116 }
117 else
118 {
119 // check
120 if (!$langFile->load() && $langFile->hasErrors())
121 {
122 $this->addErrors($langFile->getErrors());
123 }
124 elseif ($langFile->count() > 0)
125 {
126 $langFile->updatePhraseIndex();
127 }
128 }
129 }
130 catch (Main\IO\IoException $exception)
131 {
132 if (!$langFile->isExists())
133 {
134 $this->addError(new Main\Error(
135 Loc::getMessage('TR_ERROR_WRITE_CREATE', ['#FILE#' => $langFile->getPath()])
136 ));
137 }
138 else
139 {
140 $this->addError(new Main\Error(
141 Loc::getMessage('TR_ERROR_WRITE_UPDATE', ['#FILE#' => $langFile->getPath()])
142 ));
143 }
144 }
145 }
146
147 if (!$this->hasErrors())
148 {
149 $result['SUMMARY'] = Loc::getMessage('TR_EDIT_SAVING_COMPLETED');
150 }
151
152 return $result;
153 }
154}
static loadLanguageFile($file, $language=null, $normalize=true)
Definition loc.php:224
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static isAllowPath(string $path)
addErrors(array $errors)
Definition error.php:41
addError(Main\Error $error)
Definition error.php:22