-
Notifications
You must be signed in to change notification settings - Fork 3
/
Multilanguage.php
103 lines (93 loc) · 2.56 KB
/
Multilanguage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace abcms\multilanguage;
use Yii;
use abcms\multilanguage\models\Translation;
use yii\helpers\ArrayHelper;
/**
* MultiLanguage Component Class
*/
class Multilanguage extends MultilanguageBase
{
/**
* @var array Key is the language code and value is the name
*/
protected $_languages = [];
/**
* {@inheritdoc}
*/
public function getLanguages()
{
return $this->_languages;
}
/**
* Set the languages attribute
* @param array $languages
*/
public function setLanguages($languages)
{
$this->_languages = $languages;
}
/**
* {@inheritdoc}
*/
public function translation($model, $lang)
{
$modelId = $model->returnModelId();
$pk = $model->id;
$condition = [
'modelId' => $modelId,
'pk' => $pk,
'lang' => $lang,
];
$models = Translation::find()->andWhere($condition)->all();
$array = [];
foreach($models as $model) {
$array[$model->attribute] = $model->translation;
}
return $array;
}
/**
* {@inheritdoc}
*/
public function translationMultiple($models, $lang)
{
if(isset($models[0])) {
$modelId = $models[0]->returnModelId();
}
else {
return $models;
}
$ids = ArrayHelper::getColumn($models, 'id');
$condition = [
'modelId' => $modelId,
'lang' => $lang,
'pk' => $ids,
];
$translations = Translation::find()->andWhere($condition)->all();
$array = [];
foreach($translations as $translation) {
$array[$translation->pk][$translation->attribute] = $translation->translation;
}
return $array;
}
/**
* {@inheritdoc}
*/
public function saveTranslation($model, $data)
{
$languages = $this->getTranslationLanguages();
$fields = $model->getTranslationFields();
$modelId = $model->returnModelId();
$pk = $model->id;
foreach($fields as $attribute => $field)
{
foreach($languages as $lang => $langName)
{
$inputName = $model->getTranslationInputName($attribute, $lang);
if(isset($data[$inputName])){
Translation::commit($modelId, $pk, $attribute, $lang, $data[$inputName]);
}
}
}
}
}