8df80521
Alexander Karnovsky
not fixed commite
|
1
2
3
4
5
|
<?php
namespace common\modules\rubrication\behaviors;
use common\modules\rubrication\models\TaxValueString;
|
0b8441f2
Karnovsky A
Karnovsky-02052016
|
6
|
use dosamigos\transliterator\TransliteratorHelper;
|
8df80521
Alexander Karnovsky
not fixed commite
|
7
8
9
10
11
|
use yii\base\Behavior;
use yii\base\Exception;
use yii\db\ActiveRecord;
use Yii;
use yii\helpers\ArrayHelper;
|
0b8441f2
Karnovsky A
Karnovsky-02052016
|
12
|
use yii\helpers\Inflector;
|
8df80521
Alexander Karnovsky
not fixed commite
|
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
use yii\web\NotFoundHttpException;
class ArtboxSynonymBehavior extends Behavior {
public $keyNameId;
public $keyNameValue = 'default_value';
/** @var ActiveRecord $valueModel */
public $valueModel = '\common\modules\rubrication\models\TaxValueString';
public $valuePKey;
public $valueOptionId = 'tax_option_id';
public $valueFields = [];
public $values = [];
|
0b8441f2
Karnovsky A
Karnovsky-02052016
|
28
29
|
public $slug = null;
|
8df80521
Alexander Karnovsky
not fixed commite
|
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
|
/**
* @param ActiveRecord $owner
* @throws Exception
*/
public function attach($owner)
{
parent::attach($owner);
if ($this->keyNameId === null) {
$primaryKey = $owner->primaryKey();
if (!isset($primaryKey[0])) {
throw new Exception('"' . $owner->className() . '" must have a primary key.');
}
$this->keyNameId = $primaryKey[0];
}
}
/*
* Inicialize behavior (read and prepare params)
*/
public function init() {
if (empty($this->valueModel)) {
$this->valueModel = TaxValueString::className();
}
if (empty($this->valuePKey)) {
$vm = $this->valueModel;
$primaryKey = $vm::primaryKey();
if (!isset($primaryKey[0])) {
throw new Exception('"' . $this->valueModel . '" must have a primary key.');
}
if (count($primaryKey) > 1) {
throw new Exception('"' . $this->valueModel . '" is contains multiple keys.');
}
$this->valuePKey = $primaryKey[0];
}
}
/*
* Events for auto-drive default value
*/
public function events() {
return [
ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
ActiveRecord::EVENT_AFTER_UPDATE => 'beforeUpdate',
ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete',
];
}
public function getAutoValue() {
return $this->owner->hasOne($this->valueModel, [$this->valuePKey => $this->keyNameValue]);
}
public function afterInsert() {
/** @var ActiveRecord $valueModel */
$valueModel = new $this->valueModel;
foreach ($this->valueFields as $key => $field) {
$valueModel->setAttribute($field, $this->$key);
}
$valueModel->setAttribute($this->valueOptionId, $this->owner->getAttribute($this->keyNameId));
$valueModel->save();
$this->owner->setAttribute($this->keyNameValue, $valueModel->getAttribute($this->valuePKey));
|
0b8441f2
Karnovsky A
Karnovsky-02052016
|
91
|
$this->owner->{$this->slug['slugKeyName']} = $this->slugify($valueModel->{$this->slug['valueKeyName']});
|
8df80521
Alexander Karnovsky
not fixed commite
|
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
|
$this->owner->save(false);
}
public function beforeUpdate() {
if ($this->owner->getIsNewRecord()) {
throw new NotSupportedException('Method "' . $this->owner->className() . '::insert" is not supported for inserting new entitys.');
}
if (!$this->owner->getAttribute($this->keyNameValue)) {
return;
}
/** @var ActiveRecord $valueModel */
$valueModelName = $this->valueModel;
$valueModel = $valueModelName::findOne($this->owner->getAttribute($this->keyNameValue));
$isave = false;
foreach ($this->valueFields as $key => $field) {
if ($valueModel->getAttribute($field) == $this->$key) {
continue;
}
$isave = true;
$valueModel->setAttribute($field, $this->$key);
}
if ($isave) {
$valueModel->setAttribute($this->valueOptionId, $this->owner->getAttribute($this->keyNameId));
$valueModel->save();
|
1f90c5f4
Karnovsky A
ArtboxImage resizer
|
119
|
if (!empty($this->slug) && empty($this->owner->{$this->slug['slugKeyName']})) {
|
0b8441f2
Karnovsky A
Karnovsky-02052016
|
120
121
|
$this->owner->{$this->slug['slugKeyName']} = $this->slugify($valueModel->{$this->slug['valueKeyName']});
}
|
8df80521
Alexander Karnovsky
not fixed commite
|
122
123
124
125
126
127
128
129
130
131
132
|
}
}
public function beforeDelete() {
/** @var ActiveRecord $valueModel */
$valueModelName = $this->valueModel;
$valueModelName::deleteAll([
$this->valueOptionId => $this->owner->getAttribute($this->keyNameId)
]);
}
|
0b8441f2
Karnovsky A
Karnovsky-02052016
|
133
134
135
136
137
|
private function slugify( $slug )
{
return Inflector::slug( TransliteratorHelper::process( $slug ), '-', true );
}
|
8df80521
Alexander Karnovsky
not fixed commite
|
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
|
/**
* @inheritdoc
*/
public function canSetProperty($key, $checkVars = true)
{
return array_key_exists($key, $this->valueFields) ?
true : parent::canSetProperty($key, $checkVars = true);
}
/**
* @inheritdoc
*/
public function __set($key, $value) {
if (isset($this->valueFields[$key])) {
$this->values[$key] = $value;
}
}
/**
* @inheritdoc
*/
public function __get($key) {
return $this->_getValue($key);
}
public function _getValue($key) {
if (isset($this->values[$key])) {
return $this->values[$key];
}
}
}
|