1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\data\models; |
4
|
|
|
|
5
|
|
|
use app\modules\data\DataModule; |
6
|
|
|
use Yii; |
7
|
|
|
use yii\base\Model; |
8
|
|
|
use yii\helpers\Json; |
9
|
|
|
use yii\web\UploadedFile; |
10
|
|
|
|
11
|
|
|
class ImportModel extends Model implements \Serializable |
12
|
|
|
{ |
13
|
|
|
const EVENT_BEFORE_SERIALIZE = 'beforeSerialize'; |
14
|
|
|
const EVENT_AFTER_SERIALIZE = 'afterSerialize'; |
15
|
|
|
const EVENT_BEFORE_LOAD = 'beforeLoad'; |
16
|
|
|
const EVENT_AFTER_LOAD = 'afterLoad'; |
17
|
|
|
const EVENT_BEFORE_UNSERIALIZE = 'beforeUnserialize'; |
18
|
|
|
const EVENT_AFTER_UNSERIALIZE = 'afterUnserialize'; |
19
|
|
|
|
20
|
|
|
public $object; |
21
|
|
|
private $user = 0; |
22
|
|
|
/** |
23
|
|
|
* @var UploadedFile|null file attribute |
24
|
|
|
*/ |
25
|
|
|
public $file; |
26
|
|
|
public $fields; |
27
|
|
|
public $type; |
28
|
|
|
|
29
|
|
|
public $filterByCategory; |
30
|
|
|
public $filterByProperties; |
31
|
|
|
public $filterByFields; |
32
|
|
|
|
33
|
|
|
public $conditions = []; |
34
|
|
|
|
35
|
|
|
public $_serializeArray = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Array of PropertyGroup's ids to add to each record |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
public $addPropertyGroups = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Should we create new record if supplied 'internal_id' doesn't exist |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
public $createIfNotExists = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Delimiter for multiple values of field which were supplied in one field |
51
|
|
|
* Can be regexp starting with a slash - then preg_split used |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
public $multipleValuesDelimiter = '|'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Array of additional fields to process |
58
|
|
|
* (see ExportableInterface::exportableAdditionalFields for format) |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
public $additionalFields = []; |
62
|
|
|
|
63
|
|
|
public function behaviors() |
64
|
|
|
{ |
65
|
|
|
return array_merge(parent::behaviors(),[ |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getFilename($prefix = '') |
70
|
|
|
{ |
71
|
|
|
if (trim($prefix)) { |
72
|
|
|
$prefix = "{$prefix}_"; |
73
|
|
|
} else { |
74
|
|
|
$prefix = ''; |
75
|
|
|
} |
76
|
|
|
return "{$prefix}{$this->object}_{$this->getUser()}." . $this->getExtension($this->type); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getUser() |
80
|
|
|
{ |
81
|
|
|
if ($this->user <= 0) { |
82
|
|
|
$this->user = Yii::$app->user->id; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->user; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function load($data, $formName = null) |
89
|
|
|
{ |
90
|
|
|
$this->trigger(self::EVENT_BEFORE_LOAD,Yii::createObject([ |
91
|
|
|
'class' => '\app\modules\data\components\ImportModelLoadEvent', |
92
|
|
|
'_data'=>$data, |
93
|
|
|
'_formName'=>$formName |
94
|
|
|
])); |
95
|
|
|
|
96
|
|
View Code Duplication |
if (isset($data['ImportModel']) && |
97
|
|
|
isset($data['ImportModel']['fields']) && |
98
|
|
|
isset($data['ImportModel']['fields']['property']) && |
99
|
|
|
$data['ImportModel']['fields']['property'] |
100
|
|
|
) { |
101
|
|
|
foreach ($data['ImportModel']['fields']['property'] as $key => $property) { |
102
|
|
|
if (!isset($property['enabled']) || !$property['enabled']) { |
103
|
|
|
unset($data['ImportModel']['fields']['property'][$key]); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
View Code Duplication |
if (isset($data['ImportModel']) && |
108
|
|
|
isset($data['ImportModel']['fields']) && |
109
|
|
|
isset($data['ImportModel']['fields']['additionalFields']) && |
110
|
|
|
$data['ImportModel']['fields']['additionalFields'] |
111
|
|
|
) { |
112
|
|
|
foreach ($data['ImportModel']['fields']['additionalFields'] as $key => $additionalFields) { |
113
|
|
|
if (!isset($additionalFields['enabled']) || !$additionalFields['enabled']) { |
114
|
|
|
unset($data['ImportModel']['fields']['additionalFields'][$key]); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->trigger(self::EVENT_AFTER_LOAD,Yii::createObject([ |
120
|
|
|
'class' => '\app\modules\data\components\ImportModelLoadEvent', |
121
|
|
|
'_data'=>$data, |
122
|
|
|
'_formName'=>$formName |
123
|
|
|
])); |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
return parent::load($data, $formName); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return array the validation rules. |
|
|
|
|
131
|
|
|
*/ |
132
|
|
|
public function rules() |
133
|
|
|
{ |
134
|
|
|
return [ |
135
|
|
|
[['file'], 'file', 'extensions' => 'csv, xls, xlsx'], |
136
|
|
|
[['object'], 'integer'], |
137
|
|
|
[['object'], 'required'], |
138
|
|
|
[['type'] ,'in', 'range'=> array_keys(self::knownTypes())], |
139
|
|
|
[['type'] ,'default', 'value'=> Yii::$app->modules['data']->defaultType ], |
140
|
|
|
[['fields', 'conditions' ,'type'], 'safe'], |
141
|
|
|
[['addPropertyGroups', 'multipleValuesDelimiter', 'additionalFields'], 'safe'], |
142
|
|
|
[['createIfNotExists'], 'boolean'], |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public static function knownTypes() |
147
|
|
|
{ |
148
|
|
|
return [ |
149
|
|
|
'csv' => 'CSV', |
150
|
|
|
'excelCsv' => 'Excel CSV', |
151
|
|
|
'xls' => 'Excel XLS', |
152
|
|
|
'xlsx' => 'Excel XLSX', |
153
|
|
|
]; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function getExtension($type) |
157
|
|
|
{ |
158
|
|
|
$extensions = [ |
159
|
|
|
'excelCsv' => 'csv', |
160
|
|
|
'csv' => 'csv', |
161
|
|
|
'xls' => 'xls', |
162
|
|
|
'xlsx' => 'xlsx', |
163
|
|
|
]; |
164
|
|
|
if (!isset($extensions[$type])) { |
165
|
|
|
return 'unknown'; |
166
|
|
|
} |
167
|
|
|
return $extensions[$type]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @inheritdoc |
172
|
|
|
*/ |
173
|
|
|
public function attributeLabels() |
174
|
|
|
{ |
175
|
|
|
return [ |
176
|
|
|
'file' => \Yii::t('app', 'File'), |
177
|
|
|
'object' => \Yii::t('app', 'Object'), |
178
|
|
|
'fields' => \Yii::t('app', 'Fields'), |
179
|
|
|
'type' => \Yii::t('app', 'Type'), |
180
|
|
|
'createIfNotExists' => \Yii::t('app', 'Create record with supplied internal_id as ID if not exists.'), |
181
|
|
|
'multipleValuesDelimiter' => \Yii::t('app', 'Multiple values delimiter'), |
182
|
|
|
]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function serialize() |
186
|
|
|
{ |
187
|
|
|
|
188
|
|
|
$this->_serializeArray = [ |
189
|
|
|
'object' => $this->object, |
190
|
|
|
'fields' => $this->fields, |
191
|
|
|
'conditions' => $this->conditions, |
192
|
|
|
'type' => $this->type, |
193
|
|
|
'user' => Yii::$app->user->id, |
194
|
|
|
'addPropertyGroups' => is_array($this->addPropertyGroups) ? $this->addPropertyGroups : [], |
195
|
|
|
'createIfNotExists' => $this->createIfNotExists, |
196
|
|
|
'multipleValuesDelimiter' => $this->multipleValuesDelimiter, |
197
|
|
|
'additionalFields' => $this->additionalFields, |
198
|
|
|
]; |
199
|
|
|
|
200
|
|
|
$this->trigger(self::EVENT_BEFORE_SERIALIZE); |
201
|
|
|
|
202
|
|
|
$ret = Json::encode($this->_serializeArray); |
203
|
|
|
|
204
|
|
|
$this->trigger(self::EVENT_AFTER_SERIALIZE); |
205
|
|
|
|
206
|
|
|
return $ret; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function unserialize($serialized) |
210
|
|
|
{ |
211
|
|
|
$this->trigger(self::EVENT_BEFORE_UNSERIALIZE,Yii::createObject([ |
212
|
|
|
'class' => '\app\modules\data\components\ImportModelBUnserializeEvent', |
213
|
|
|
'serialized' => $serialized |
214
|
|
|
])); |
215
|
|
|
|
216
|
|
|
$fields = Json::decode($serialized); |
217
|
|
|
|
218
|
|
|
$this->object = $fields['object']; |
219
|
|
|
$this->fields = $fields['fields']; |
220
|
|
|
$this->conditions = isset($fields['conditions']) ? $fields['conditions'] : []; |
221
|
|
|
$this->type = $fields['type']; |
222
|
|
|
$this->user = $fields['user']; |
223
|
|
|
$this->addPropertyGroups = $fields['addPropertyGroups']; |
224
|
|
|
$this->createIfNotExists = $fields['createIfNotExists']; |
225
|
|
|
$this->multipleValuesDelimiter = $fields['multipleValuesDelimiter']; |
226
|
|
|
$this->additionalFields = isset($fields['fields']['additionalFields']) ? $fields['fields']['additionalFields'] : []; |
227
|
|
|
|
228
|
|
|
$this->trigger(self::EVENT_AFTER_UNSERIALIZE,Yii::createObject([ |
229
|
|
|
'class' => '\app\modules\data\components\ImportModelAUnserializeEvent', |
230
|
|
|
'fields' => $fields |
231
|
|
|
])); |
232
|
|
|
|
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.