1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace execut\import\models; |
4
|
|
|
use execut\crudFields\Behavior; |
5
|
|
|
use execut\crudFields\BehaviorStub; |
6
|
|
|
use execut\crudFields\fields\DropDown; |
7
|
|
|
use execut\crudFields\fields\HasManyMultipleInput; |
8
|
|
|
use execut\crudFields\fields\Hidden; |
9
|
|
|
use execut\crudFields\fields\NumberField; |
10
|
|
|
use execut\crudFields\ModelsHelperTrait; |
11
|
|
|
use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior; |
12
|
|
|
use yii\behaviors\TimestampBehavior; |
13
|
|
|
use yii\db\Expression; |
14
|
|
|
use yii\helpers\ArrayHelper; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This is the model class for table "import_settings_sets". |
18
|
|
|
*/ |
19
|
|
|
class SettingsSet extends base\SettingsSet |
20
|
|
|
{ |
21
|
|
|
use BehaviorStub, ModelsHelperTrait; |
22
|
|
|
|
23
|
|
|
public static function find() |
24
|
|
|
{ |
25
|
|
|
return new queries\SettingsSet(static::class); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function behaviors() |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
|
|
'relationsSaver' => [ |
32
|
|
|
'class' => SaveRelationsBehavior::class, |
33
|
|
|
'relations' => [ |
34
|
|
|
'settingsValues' |
35
|
|
|
], |
36
|
|
|
], |
37
|
|
|
'fields' => [ |
38
|
|
|
'class' => Behavior::class, |
39
|
|
|
'module' => 'import', |
40
|
|
|
'fields' => $this->getStandardFields(['visible', 'name'], [ |
41
|
|
|
'import_settings_sheet_id' => [ |
42
|
|
|
'class' => Hidden::class, |
43
|
|
|
'attribute' => 'import_settings_sheet_id', |
44
|
|
|
], |
45
|
|
|
'type' => [ |
46
|
|
|
'class' => DropDown::class, |
47
|
|
|
'attribute' => 'type', |
48
|
|
|
'data' => self::getAttributesSetsTypesList(), |
49
|
|
|
], |
50
|
|
|
'settingsValues' => [ |
51
|
|
|
'class' => HasManyMultipleInput::class, |
52
|
|
|
'nameAttribute' => null, |
53
|
|
|
'attribute' => 'settingsValues', |
54
|
|
|
'relation' => 'settingsValues', |
55
|
|
|
], |
56
|
|
|
]), |
57
|
|
|
// 'plugins' => \yii::$app->getModule('import')->getSettingsSetsCrudFieldsPlugins(), |
58
|
|
|
], |
59
|
|
|
'date' => [ |
60
|
|
|
'class' => TimestampBehavior::class, |
61
|
|
|
'createdAtAttribute' => 'created', |
62
|
|
|
'updatedAtAttribute' => 'updated', |
63
|
|
|
'value' => new Expression('NOW()'), |
64
|
|
|
], |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function validateRequiredFields() |
69
|
|
|
{ |
70
|
|
|
if (!empty($this->type)) { |
71
|
|
|
$requiredTypes = $this->getRequiredAttributesByTypes()[$this->type]; |
72
|
|
|
foreach ($this->settingsValues as $value) { |
73
|
|
|
if (!empty($value->type)) { |
74
|
|
|
if (($key = array_search($value->type, $requiredTypes)) !== false) { |
75
|
|
|
unset($requiredTypes[$key]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!empty($requiredTypes)) { |
81
|
|
|
$labels = \yii::$app->getModule('import')->getAttributesValuesTypesList(); |
82
|
|
|
$attributes = []; |
83
|
|
|
foreach ($requiredTypes as $type) { |
84
|
|
|
$attributes[$type] = $labels[$type]; |
85
|
|
|
} |
86
|
|
|
$this->addError('type', 'Attributes ' . implode(', ', $attributes) . ' is required'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function rules() |
92
|
|
|
{ |
93
|
|
|
$rules = $this->getBehavior('fields')->rules(); |
94
|
|
|
|
95
|
|
|
return ArrayHelper::merge([ |
96
|
|
|
[['settingsValues'], 'validateRequiredFields'], |
97
|
|
|
// [['type'], 'validateValuesFields'], |
98
|
|
|
], $rules); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getRequiredAttributesByTypes() { |
102
|
|
|
return \yii::$app->getModule('import')->getRequiredAttributesByTypes(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public static function getAttributesSetsTypesList() { |
106
|
|
|
return \yii::$app->getModule('import')->getAttributesSetsTypesList(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function delete() |
110
|
|
|
{ |
111
|
|
|
foreach ($this->settingsValues as $value) { |
112
|
|
|
$value->delete(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return parent::delete(); // TODO: Change the autogenerated stub |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|