Commit | Line | Data |
---|---|---|
bd70fec3 | 1 | <?php |
a9229942 | 2 | |
bd70fec3 | 3 | namespace wcf\acp\form; |
a9229942 | 4 | |
7a23a706 | 5 | use wcf\data\language\Language; |
bd70fec3 | 6 | use wcf\data\language\LanguageEditor; |
66aa26cc TD |
7 | use wcf\data\package\Package; |
8 | use wcf\data\package\PackageCache; | |
b4ae02cc | 9 | use wcf\event\language\LanguageImported; |
bd70fec3 | 10 | use wcf\form\AbstractForm; |
c0683dd1 | 11 | use wcf\system\event\EventHandler; |
bd70fec3 MW |
12 | use wcf\system\exception\SystemException; |
13 | use wcf\system\exception\UserInputException; | |
14 | use wcf\system\language\LanguageFactory; | |
15 | use wcf\system\WCF; | |
bd70fec3 MW |
16 | use wcf\util\XML; |
17 | ||
18 | /** | |
19 | * Shows the language import form. | |
a9229942 TD |
20 | * |
21 | * @author Marcel Werk | |
2a7aea52 | 22 | * @copyright 2001-2021 WoltLab GmbH |
a9229942 | 23 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
bd70fec3 | 24 | */ |
a9229942 TD |
25 | class LanguageImportForm extends AbstractForm |
26 | { | |
27 | /** | |
28 | * @inheritDoc | |
29 | */ | |
30 | public $activeMenuItem = 'wcf.acp.menu.link.language.import'; | |
31 | ||
32 | /** | |
33 | * @inheritDoc | |
34 | */ | |
35 | public $neededPermissions = ['admin.language.canManageLanguage']; | |
36 | ||
37 | /** | |
38 | * file name | |
39 | * @var string | |
40 | */ | |
41 | public $filename = ''; | |
42 | ||
43 | /** | |
44 | * language object | |
45 | * @var Language | |
46 | */ | |
47 | public $language; | |
48 | ||
49 | /** | |
50 | * list of available languages | |
51 | * @var Language[] | |
52 | */ | |
53 | public $languages = []; | |
54 | ||
55 | /** | |
56 | * source language object | |
57 | * @var Language | |
58 | */ | |
59 | public $sourceLanguage; | |
60 | ||
61 | /** | |
62 | * source language id | |
63 | * @var int | |
64 | */ | |
65 | public $sourceLanguageID = 0; | |
66 | ||
66aa26cc TD |
67 | /** |
68 | * @var int | |
2a7aea52 | 69 | * @since 5.4 |
66aa26cc TD |
70 | */ |
71 | public $packageID = 0; | |
72 | ||
a9229942 TD |
73 | /** |
74 | * @inheritDoc | |
75 | */ | |
76 | public function readParameters() | |
77 | { | |
78 | parent::readParameters(); | |
79 | ||
80 | $this->languages = LanguageFactory::getInstance()->getLanguages(); | |
81 | } | |
82 | ||
83 | /** | |
84 | * @inheritDoc | |
85 | */ | |
86 | public function readFormParameters() | |
87 | { | |
88 | parent::readFormParameters(); | |
89 | ||
90 | if (isset($_FILES['languageUpload']) && !empty($_FILES['languageUpload']['tmp_name'])) { | |
91 | $this->filename = $_FILES['languageUpload']['tmp_name']; | |
92 | } | |
93 | if (isset($_POST['sourceLanguageID'])) { | |
94 | $this->sourceLanguageID = \intval($_POST['sourceLanguageID']); | |
95 | } | |
66aa26cc TD |
96 | if (isset($_POST['packageID'])) { |
97 | $this->packageID = \intval($_POST['packageID']); | |
98 | } | |
a9229942 TD |
99 | } |
100 | ||
101 | /** | |
102 | * @inheritDoc | |
103 | */ | |
104 | public function validate() | |
105 | { | |
106 | parent::validate(); | |
107 | ||
108 | // check file | |
109 | if (!\file_exists($this->filename)) { | |
110 | throw new UserInputException('languageUpload'); | |
111 | } | |
112 | ||
113 | if (empty($this->sourceLanguageID)) { | |
114 | throw new UserInputException('sourceLanguageID'); | |
115 | } | |
116 | ||
117 | // get language | |
118 | $this->sourceLanguage = LanguageFactory::getInstance()->getLanguage($this->sourceLanguageID); | |
119 | if (!$this->sourceLanguage->languageID) { | |
120 | throw new UserInputException('sourceLanguageID'); | |
121 | } | |
122 | ||
66aa26cc TD |
123 | if (!PackageCache::getInstance()->getPackage($this->packageID)) { |
124 | throw new UserInputException('packageID'); | |
125 | } | |
126 | ||
a9229942 TD |
127 | // try to import |
128 | try { | |
129 | // open xml document | |
130 | $xml = new XML(); | |
131 | $xml->load($this->filename); | |
132 | ||
133 | // import xml document | |
66aa26cc | 134 | $this->language = LanguageEditor::importFromXML($xml, $this->packageID, $this->sourceLanguage); |
a9229942 TD |
135 | |
136 | // copy content | |
137 | if (!isset($this->languages[$this->language->languageID])) { | |
138 | LanguageEditor::copyLanguageContent($this->sourceLanguage->languageID, $this->language->languageID); | |
139 | } | |
140 | } catch (SystemException $e) { | |
141 | throw new UserInputException('languageUpload', $e->getMessage()); | |
142 | } catch (\InvalidArgumentException $e) { | |
143 | throw new UserInputException('languageUpload', $e->getMessage()); | |
144 | } | |
145 | } | |
146 | ||
147 | /** | |
148 | * @inheritDoc | |
149 | */ | |
150 | public function save() | |
151 | { | |
152 | parent::save(); | |
153 | ||
154 | LanguageFactory::getInstance()->clearCache(); | |
155 | LanguageFactory::getInstance()->deleteLanguageCache(); | |
c0683dd1 | 156 | |
310fc0b6 | 157 | EventHandler::getInstance()->fire(new LanguageImported(new Language($this->language->languageID))); |
c0683dd1 | 158 | |
a9229942 TD |
159 | $this->saved(); |
160 | ||
c1181ad9 MS |
161 | // reset fields |
162 | $this->sourceLanguageID = 0; | |
2a7aea52 | 163 | $this->packageID = 0; |
c1181ad9 | 164 | |
a9229942 TD |
165 | // show success message |
166 | WCF::getTPL()->assign('success', true); | |
167 | } | |
168 | ||
169 | /** | |
170 | * @inheritDoc | |
171 | */ | |
172 | public function assignVariables() | |
173 | { | |
174 | parent::assignVariables(); | |
175 | ||
66aa26cc | 176 | $packages = PackageCache::getInstance()->getPackages(); |
5f952b89 MW |
177 | $collator = new \Collator(WCF::getLanguage()->getLocale()); |
178 | \usort( | |
179 | $packages, | |
180 | static fn (Package $a, Package $b) => $collator->compare($a->getName(), $b->getName()) | |
181 | ); | |
66aa26cc | 182 | |
a9229942 TD |
183 | WCF::getTPL()->assign([ |
184 | 'languages' => $this->languages, | |
185 | 'sourceLanguageID' => $this->sourceLanguageID, | |
66aa26cc TD |
186 | 'packages' => $packages, |
187 | 'packageID' => $this->packageID, | |
a9229942 TD |
188 | ]); |
189 | } | |
bd70fec3 | 190 | } |