Overhauled language import form
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / LanguageImportForm.class.php
1 <?php
2 namespace wcf\acp\form;
3 use wcf\data\language\Language;
4 use wcf\data\language\LanguageEditor;
5 use wcf\form\AbstractForm;
6 use wcf\system\exception\SystemException;
7 use wcf\system\exception\UserInputException;
8 use wcf\system\language\LanguageFactory;
9 use wcf\system\WCF;
10 use wcf\system\WCFACP;
11 use wcf\util\XML;
12
13 /**
14 * Shows the language import form.
15 *
16 * @author Marcel Werk
17 * @copyright 2001-2016 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package com.woltlab.wcf
20 * @subpackage acp.form
21 * @category Community Framework
22 */
23 class LanguageImportForm extends AbstractForm {
24 /**
25 * @inheritDoc
26 */
27 public $activeMenuItem = 'wcf.acp.menu.link.language.import';
28
29 /**
30 * @inheritDoc
31 */
32 public $neededPermissions = ['admin.language.canManageLanguage'];
33
34 /**
35 * file name
36 * @var string
37 */
38 public $filename = '';
39
40 /**
41 * language object
42 * @var Language
43 */
44 public $language;
45
46 /**
47 * list of available languages
48 * @var Language[]
49 */
50 public $languages = [];
51
52 /**
53 * source language object
54 * @var Language
55 */
56 public $sourceLanguage;
57
58 /**
59 * source language id
60 * @var integer
61 */
62 public $sourceLanguageID = 0;
63
64 /**
65 * @inheritDoc
66 */
67 public function readFormParameters() {
68 parent::readFormParameters();
69
70 if (isset($_FILES['languageUpload']) && !empty($_FILES['languageUpload']['tmp_name'])) {
71 $this->filename = $_FILES['languageUpload']['tmp_name'];
72 }
73 if (isset($_POST['sourceLanguageID'])) $this->sourceLanguageID = intval($_POST['sourceLanguageID']);
74 }
75
76 /**
77 * @inheritDoc
78 */
79 public function validate() {
80 parent::validate();
81
82 // check file
83 if (!file_exists($this->filename)) {
84 throw new UserInputException('languageUpload');
85 }
86
87 if (empty($this->sourceLanguageID)) {
88 throw new UserInputException('sourceLanguageID');
89 }
90
91 // get language
92 $this->sourceLanguage = LanguageFactory::getInstance()->getLanguage($this->sourceLanguageID);
93 if (!$this->sourceLanguage->languageID) {
94 throw new UserInputException('sourceLanguageID');
95 }
96
97 // try to import
98 try {
99 // open xml document
100 $xml = new XML();
101 $xml->load($this->filename);
102
103 // import xml document
104 $this->language = LanguageEditor::importFromXML($xml, -1, $this->sourceLanguage);
105 }
106 catch (SystemException $e) {
107 throw new UserInputException('languageUpload', $e->getMessage());
108 }
109 }
110
111 /**
112 * @inheritDoc
113 */
114 public function save() {
115 parent::save();
116
117 LanguageFactory::getInstance()->clearCache();
118 LanguageFactory::getInstance()->deleteLanguageCache();
119 $this->saved();
120
121 // show success message
122 WCF::getTPL()->assign('success', true);
123 }
124
125 /**
126 * @inheritDoc
127 */
128 public function readData() {
129 parent::readData();
130
131 $this->languages = LanguageFactory::getInstance()->getLanguages();
132 }
133
134 /**
135 * @inheritDoc
136 */
137 public function assignVariables() {
138 parent::assignVariables();
139
140 WCF::getTPL()->assign([
141 'languages' => $this->languages,
142 'sourceLanguageID' => $this->sourceLanguageID
143 ]);
144 }
145
146 /**
147 * @inheritDoc
148 */
149 public function show() {
150 // check master password
151 WCFACP::checkMasterPassword();
152
153 parent::show();
154 }
155 }