1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\backend\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\data\ArrayDataProvider; |
7
|
|
|
use yii\filters\AccessControl; |
8
|
|
|
use yii\helpers\Json; |
9
|
|
|
use yii\i18n\PhpMessageSource; |
10
|
|
|
use yii\web\Controller; |
11
|
|
|
use yii\web\Cookie; |
12
|
|
|
use yii\web\NotFoundHttpException; |
13
|
|
|
|
14
|
|
|
class I18nController extends Controller |
15
|
|
|
{ |
16
|
|
|
private $aliases; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @return array |
20
|
|
|
* @throws \yii\base\InvalidConfigException |
21
|
|
|
*/ |
22
|
|
|
private function getAliases() |
23
|
|
|
{ |
24
|
|
|
if ($this->aliases === null) { |
25
|
|
|
$this->aliases = []; |
26
|
|
|
foreach (Yii::$app->i18n->translations as $name => $translation) { |
27
|
|
|
if (is_array($translation)) { |
28
|
|
|
$translation = Yii::createObject($translation); |
29
|
|
|
} |
30
|
|
|
if (!($translation instanceof PhpMessageSource) || $name == 'yii') { |
31
|
|
|
continue; |
32
|
|
|
} |
33
|
|
|
$basePath = Yii::getAlias($translation->basePath); |
34
|
|
|
$rdi = new \RecursiveDirectoryIterator( |
35
|
|
|
$basePath, |
36
|
|
|
\RecursiveDirectoryIterator::SKIP_DOTS |
37
|
|
|
); |
38
|
|
|
foreach (new \RecursiveIteratorIterator($rdi, \RecursiveIteratorIterator::CHILD_FIRST) as $file) { |
39
|
|
|
$fileName = $file->getRealpath(); |
40
|
|
|
if (pathinfo($fileName, PATHINFO_EXTENSION) == 'php') { |
41
|
|
|
$alias = $translation->basePath . substr($fileName, strlen($basePath)); |
42
|
|
|
$this->aliases[$alias] = $fileName; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
return $this->aliases; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public function behaviors() |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
'access' => [ |
54
|
|
|
'class' => AccessControl::className(), |
|
|
|
|
55
|
|
|
'rules' => [ |
56
|
|
|
[ |
57
|
|
|
'allow' => true, |
58
|
|
|
'roles' => ['setting manage'], |
59
|
|
|
], |
60
|
|
|
], |
61
|
|
|
], |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function actionIndex() |
66
|
|
|
{ |
67
|
|
|
$aliases = $this->getAliases(); |
68
|
|
|
$dataProvider = new ArrayDataProvider( |
69
|
|
|
[ |
70
|
|
|
'allModels' => $aliases, |
71
|
|
|
] |
72
|
|
|
); |
73
|
|
|
return $this->render( |
74
|
|
|
'index', |
75
|
|
|
[ |
76
|
|
|
'dataProvider' => $dataProvider, |
77
|
|
|
] |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function actionUpdate($id) |
82
|
|
|
{ |
83
|
|
|
$aliases = $this->getAliases(); |
84
|
|
|
if (!isset($aliases[$id])) { |
85
|
|
|
throw new NotFoundHttpException; |
86
|
|
|
} |
87
|
|
|
if (!is_writable($aliases[$id])) { |
88
|
|
|
Yii::$app->session->setFlash( |
89
|
|
|
'warning', |
90
|
|
|
Yii::t( |
91
|
|
|
'app', |
92
|
|
|
'File "{file}" is not writable', |
93
|
|
|
['file' => $aliases[$id]] |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
if (Yii::$app->request->isPost && !is_null(Yii::$app->request->post('messages'))) { |
98
|
|
|
Yii::$app->response->cookies->add( |
99
|
|
|
new Cookie( |
100
|
|
|
[ |
101
|
|
|
'name' => 'sortMessages', |
102
|
|
|
'value' => Yii::$app->request->post('ksort') == 1, |
103
|
|
|
] |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
$messages = Yii::$app->request->post('messages'); |
107
|
|
|
$data = Json::decode($messages); |
108
|
|
|
$hasErrors = false; |
109
|
|
|
foreach ((array) $data as $message => $translation) { |
110
|
|
|
if (!is_string($translation)) { |
111
|
|
|
$hasErrors = true; |
112
|
|
|
break; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
if (!$hasErrors) { |
116
|
|
|
try { |
117
|
|
|
if (Yii::$app->request->post('ksort') == 1) { |
118
|
|
|
ksort($data, SORT_NATURAL | SORT_FLAG_CASE); |
119
|
|
|
} |
120
|
|
|
file_put_contents($aliases[$id], "<?php\n\nreturn " . var_export($data, true) . ";\n"); |
121
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'Messages has been saved')); |
122
|
|
|
$this->refresh(); |
123
|
|
|
Yii::$app->end(); |
124
|
|
|
} catch (\Exception $e) { |
125
|
|
|
Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot save messages')); |
126
|
|
|
} |
127
|
|
|
} else { |
128
|
|
|
Yii::$app->session->setFlash('error', Yii::t('app', 'Wrong data')); |
129
|
|
|
} |
130
|
|
|
} else { |
131
|
|
|
try { |
132
|
|
|
$messages = Json::encode(include $aliases[$id]); |
133
|
|
|
} catch (\Exception $e) { |
134
|
|
|
$messages = '{}'; |
135
|
|
|
Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot read messages')); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
return $this->render( |
139
|
|
|
'update', |
140
|
|
|
[ |
141
|
|
|
'alias' => $id, |
142
|
|
|
'file' => $aliases[$id], |
143
|
|
|
'messages' => $messages, |
144
|
|
|
] |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.