1 | <?php |
||
39 | class TranslatedBehavior extends BaseTranslatedBehavior |
||
40 | { |
||
41 | /** |
||
42 | * @var string the translations relation name |
||
43 | */ |
||
44 | public $translateRelation; |
||
45 | /** |
||
46 | * @var string the translations model language attribute name |
||
47 | */ |
||
48 | public $languageAttribute = 'lang_id'; |
||
49 | |||
50 | /** |
||
51 | * @inheritdoc |
||
52 | */ |
||
53 | 17 | public function events() |
|
54 | { |
||
55 | return [ |
||
56 | 17 | ActiveRecord::EVENT_BEFORE_DELETE => 'eventBeforeDelete', |
|
57 | 17 | ActiveRecord::EVENT_AFTER_INSERT => 'eventAfterSave', |
|
58 | 17 | ActiveRecord::EVENT_AFTER_UPDATE => 'eventAfterSave', |
|
59 | ]; |
||
60 | } |
||
61 | |||
62 | 4 | public function eventAfterSave() |
|
63 | { |
||
64 | 4 | $this->owner->link($this->translateRelation, $this->getTranslation()); |
|
65 | 4 | } |
|
66 | |||
67 | 1 | public function eventBeforeDelete() |
|
68 | { |
||
69 | 1 | $this->owner->unlinkAll($this->translateRelation, true); |
|
70 | 1 | } |
|
71 | |||
72 | /** |
||
73 | * Returns the translation model for the specified language. |
||
74 | * @param string|null $language |
||
75 | * @return ActiveRecord |
||
76 | */ |
||
77 | 11 | public function getTranslation($language = null) |
|
78 | { |
||
79 | 11 | $language = $language ?: $this->getLanguage(); |
|
80 | |||
81 | 11 | $translations = $this->getTranslateRelations(); |
|
82 | 11 | if (isset($translations[$language])) { |
|
83 | 11 | return $translations[$language]; |
|
84 | } |
||
85 | |||
86 | 7 | $sourceLanguage = $this->getSourceLanguage(); |
|
87 | 7 | $attributes = isset($translations[$sourceLanguage]) ? |
|
88 | 7 | ArrayHelper::toArray($translations[$sourceLanguage]) : []; |
|
89 | |||
90 | 7 | $translations[$language] = $this->createTranslation($language, $attributes); |
|
91 | |||
92 | 7 | $this->setTranslateRelations($translations); |
|
93 | |||
94 | 7 | return $translations[$language]; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * @return ActiveRecord[] |
||
99 | */ |
||
100 | 11 | protected function getTranslateRelations() |
|
101 | { |
||
102 | 11 | if ($this->owner->isRelationPopulated($this->translateRelation)) { |
|
103 | 11 | $translations = ArrayHelper::index($this->owner->__get($this->translateRelation), $this->languageAttribute); |
|
104 | 11 | $this->setTranslateRelations($translations); |
|
105 | } else { |
||
106 | 10 | $this->setTranslateRelations($this->owner['currentTranslate']); |
|
107 | } |
||
108 | 11 | return $this->owner->__get($this->translateRelation); |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param ActiveRecord[] $models |
||
113 | */ |
||
114 | 11 | protected function setTranslateRelations($models) |
|
118 | |||
119 | /** |
||
120 | * @param string $language |
||
121 | * @param array $attributes |
||
122 | * @return ActiveRecord |
||
123 | */ |
||
124 | 7 | protected function createTranslation($language, $attributes = []) |
|
133 | |||
134 | /** |
||
135 | * @return \yii\db\ActiveQuery |
||
136 | */ |
||
137 | 14 | protected function getRelation() |
|
138 | { |
||
146 | |||
147 | /** |
||
148 | * @inheritdoc |
||
149 | */ |
||
150 | 14 | public function canGetProperty($name, $checkVars = true) |
|
156 | |||
157 | /** |
||
158 | * @inheritdoc |
||
159 | */ |
||
160 | 9 | public function canSetProperty($name, $checkVars = true) |
|
166 | |||
167 | /** |
||
168 | * @inheritdoc |
||
169 | */ |
||
170 | 14 | public function __get($name) |
|
179 | |||
180 | /** |
||
181 | * @inheritdoc |
||
182 | */ |
||
183 | 17 | public function __set($name, $value) |
|
193 | |||
194 | /** |
||
195 | * Returns a value indicating whether a method is defined. |
||
196 | * |
||
197 | * The default implementation is a call to php function `method_exists()`. |
||
198 | * You may override this method when you implemented the php magic method `__call()`. |
||
199 | * @param string $name the method name |
||
200 | * @return boolean whether the method is defined |
||
201 | */ |
||
202 | 10 | public function hasMethod($name) |
|
207 | |||
208 | /** |
||
209 | * Calls the named method which is not a class method. |
||
210 | * |
||
211 | * Do not call this method directly as it is a PHP magic method that |
||
212 | * will be implicitly called when an unknown method is being invoked. |
||
213 | * @param string $name the method name |
||
214 | * @param array $params method parameters |
||
215 | * @return mixed the method return value |
||
216 | */ |
||
217 | 1 | public function __call($name, $params) |
|
221 | |||
222 | /** |
||
223 | * @return bool |
||
224 | */ |
||
225 | 1 | public function isTranslated() |
|
229 | |||
230 | /** |
||
231 | * This read only relations designed for method $this->hasTranslate() |
||
232 | * @return \yii\db\ActiveQuery |
||
233 | */ |
||
234 | 3 | public function getHasTranslate() |
|
245 | |||
246 | /** |
||
247 | * @return \yii\db\ActiveQuery |
||
248 | */ |
||
249 | 12 | public function getCurrentTranslate() |
|
261 | } |
||
262 |