LanguageBehavior.php
11.3 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
namespace artbox\core\behaviors;
use artbox\core\models\Language;
use yii\base\Behavior;
use yii\base\Event;
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\db\Transaction;
use yii\web\Request;
/**
* Class LanguageBehavior
*
* @property ActiveRecord $owner
* @property string $ownerKey
* @property string $langKey
* @property ActiveRecord[] $langs
* @property ActiveRecord $lang
*/
class LanguageBehavior extends Behavior
{
/**
* @var ActiveRecord $objectLang Empty language model for $owner
*/
public $objectLang;
/**
* @var ActiveRecord[] $modelLangs
*/
public $modelLangs = [];
/**
* Key which is used to link to Language model
*
* @var string $ownerKey
*/
private $ownerKey;
/**
* Key which is used in Language model to link parent record
*
* @var string $langKey
*/
private $langKey;
/**
* @var Transaction $transaction
*/
private $transaction;
/**
* @var bool $transactionStatus
*/
private $transactionStatus = false;
/**
* @inheritdoc
*/
public function events()
{
return [
ActiveRecord::EVENT_BEFORE_INSERT => 'beforeSave',
ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeSave',
ActiveRecord::EVENT_AFTER_INSERT => 'afterSave',
ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave',
];
}
/**
* Get $owner primary key to link language model
*
* @return string
*/
public function getOwnerKey(): string
{
if (!empty($this->ownerKey)) {
return $this->ownerKey;
} else {
return $this->owner->primaryKey()[ 0 ];
}
}
/**
* Set which attribute to use as $owner primary key to link language model
*
* @param string $value
*/
public function setOwnerKey(string $value)
{
$this->ownerKey = $value;
}
/**
* Get language model attribute that is used as foreign key to $owner
*
* @return string
*/
public function getLangKey(): string
{
if (!empty($this->langKey)) {
return $this->langKey;
} else {
$owner = $this->owner;
return $owner::getTableSchema()->name . '_id';
}
}
/**
* Set which attribute to use as language model foreign key to $owner
*
* @param $value
*/
public function setLangKey(string $value)
{
$this->langKey = $value;
}
/**
* Additional checks to attach this behavior
*
* @param ActiveRecord $owner
*
* @throws InvalidConfigException
*/
public function attach($owner)
{
if (empty($this->objectLang)) {
$this->objectLang = $owner::className() . 'Lang';
} elseif (!is_string($this->objectLang)) {
throw new InvalidConfigException('Object lang must be fully classified namespaced classname');
}
try {
$this->objectLang = \Yii::createObject($this->objectLang);
} catch (\ReflectionException $exception) {
throw new InvalidConfigException('Object lang must be fully classified namespaced classname');
}
if (( !$owner instanceof ActiveRecord ) || ( !$this->objectLang instanceof ActiveRecord )) {
throw new InvalidConfigException('Object lang must be fully classified namespaced classname');
}
parent::attach($owner);
}
/**
* Get query to get all language models for $owner indexed by language_id
*
* @return ActiveQuery
*/
public function getLangs()
{
$objectLang = $this->objectLang;
$owner = $this->owner;
return $owner->hasMany($objectLang::className(), [ $this->getLangKey() => $this->getOwnerKey() ])
->indexBy('language_id');
}
/**
* Get query to get language model for $owner for language_id, default to
* Language::getCurrent()
*
* @param int $language_id
* @param bool $and_null
*
* @return \yii\db\ActiveQuery
*/
public function getLang(int $language_id = null, bool $and_null = true)
{
if (empty($language_id)) {
$language_id = Language::getCurrent()->id;
}
$objectLang = $this->objectLang;
$table_name = $objectLang::getTableSchema()->name;
$owner = $this->owner;
$query = $owner->hasOne($objectLang::className(), [ $this->getLangKey() => $this->getOwnerKey() ])
->where([ $table_name . '.language_id' => $language_id ]);
if ($and_null) {
$query->orWhere([ $table_name . '.language_id' => null ]);
}
return $query;
}
/**
* Generate language models for $owner for active languages. If $owner not new and language
* models already inserted, models will be filled with them.
*
* @return void
*/
public function generateLangs()
{
$owner = $this->owner;
$languages = Language::find()
->where([ 'status' => true ])
->orderBy([ 'id' => SORT_ASC ])
->asArray()
->column();
$objectLang = $this->objectLang;
$owner_key = $this->getOwnerKey();
$langs = [];
if (!$owner->isNewRecord) {
$langs = $this->getLangs()
->andFilterWhere([ 'language_id' => $languages ])
->orderBy([ 'language_id' => SORT_ASC ])
->all();
}
foreach ($languages as $language) {
if (!array_key_exists($language, $langs)) {
$langs[ $language ] = \Yii::createObject(
[
'class' => $objectLang::className(),
'language_id' => $language,
$this->getLangKey() => ( $owner->isNewRecord ? null : $owner->$owner_key ),
]
);
}
}
$this->modelLangs = $langs;
}
/**
* Load language models with post data.
*
* @param Request $request
*/
public function loadLangs(Request $request)
{
foreach ($request->post($this->objectLang->formName(), []) as $lang => $value) {
if (!empty($this->modelLangs[ $lang ])) {
$this->modelLangs[ $lang ]->attributes = $value;
$this->modelLangs[ $lang ]->language_id = $lang;
}
}
}
/**
* Link language models with $owner by setting language model language key to owner key of
* owner
*
* @return bool If $owner is new record then return false else true
*/
public function linkLangs()
{
$owner = $this->owner;
// if($owner->isNewRecord) {
// return false;
// }
$lang_key = $this->getLangKey();
$owner_key = $this->getOwnerKey();
$modelLangs = $this->modelLangs;
foreach ($modelLangs as $model_lang) {
$model_lang->$lang_key = $owner->$owner_key;
}
return true;
}
/**
* Try to save all language models to the db. Validation function is run for all models.
*
* @return bool Whether all models are valid
*/
public function saveLangs()
{
$success = true;
$modelLangs = $this->modelLangs;
foreach ($modelLangs as $model_lang) {
if ($model_lang->save() === false) {
$success = false;
}
}
return $success;
}
/**
* Starts transaction and generates remote_id
*
* @param Event $event
*/
public function beforeSave($event)
{
/**
* @var ActiveRecord $owner
*/
$owner = $this->owner;
$db = $owner::getDb();
$this->transaction = $db->beginTransaction();
if ($owner->hasAttribute('remote_id') && empty($owner->remote_id)) {
$owner->setAttribute('remote_id', strval(microtime(true) * 10000));
}
}
/**
* Links Language models to parent model, commits transaction and write its status to $tansactionStatus
*
* @param Event $event
*/
public function afterSave($event)
{
if (!empty($this->modelLangs)) {
if ($this->linkLangs() && $this->saveLangs()) {
$this->transaction->commit();
$this->transactionStatus = true;
} else {
$this->transaction->rollBack();
$this->transactionStatus = false;
}
} else {
$this->transaction->commit();
$this->transactionStatus = true;
}
}
/**
* Whether transaction successful or not
*
* @return bool
*/
public function getTransactionStatus(): bool
{
return $this->transactionStatus;
}
/**
* @return \yii\db\ActiveQuery
*/
public function getLangInteractive()
{
$objectLang = $this->objectLang;
$owner = $this->owner;
return $owner->hasOne($objectLang::className(), [ $this->getLangKey() => $this->getOwnerKey() ]);
}
/**
* Load model and langs with data
*
* @see Model::load()
* @see LanguageBehavior::loadLangs()
*
* @param \yii\web\Request $request
*
* @return bool
*/
public function loadWithLangs(Request $request)
{
$this->loadLangs($request);
return $this->owner->load($request->post());
}
/**
* Save model and langs
*
* @see ActiveRecord::save()
* @see LanguageBehavior::getTransactionStatus()
* @return bool
*/
public function saveWithLangs()
{
return $this->owner->save() && $this->transactionStatus;
}
}