1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\collections; |
4
|
|
|
|
5
|
|
|
use hipanel\modules\finance\models\Price; |
6
|
|
|
use hipanel\modules\finance\models\factories\PriceModelFactory; |
7
|
|
|
use hiqdev\hiart\Collection; |
8
|
|
|
use Yii; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class PricesCollection overrides loading behavior of parent class in order to: |
12
|
|
|
* make it possible to load all the models, specified in [[knownForms]] at once |
13
|
|
|
* |
14
|
|
|
* @author Dmytro Naumenko <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class PricesCollection extends Collection |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \hipanel\modules\finance\models\factories\PriceModelFactory |
20
|
|
|
*/ |
21
|
|
|
private $priceModelFactory; |
22
|
|
|
|
23
|
|
|
public function __construct(PriceModelFactory $priceModelFactory, array $config = []) |
24
|
|
|
{ |
25
|
|
|
parent::__construct($config); |
26
|
|
|
$this->priceModelFactory = $priceModelFactory; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function load($data = null) |
33
|
|
|
{ |
34
|
|
|
if ($data === null && $this->dataToBeLoadedExistsInPostRequest()) { |
35
|
|
|
$data = $this->loadDifferentModelsFromPostRequest(); |
36
|
|
|
$this->checkConsistency = false; |
37
|
|
|
|
38
|
|
|
return $this->set($data); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return parent::load($data); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function dataToBeLoadedExistsInPostRequest() |
45
|
|
|
{ |
46
|
|
|
$request = Yii::$app->request->post(); |
47
|
|
|
|
48
|
|
|
$map = $this->priceModelFactory->getMap(); |
49
|
|
|
foreach ($map as $formName => $className) { |
50
|
|
|
if (is_array($map[$formName])) { |
51
|
|
|
foreach ($map[$formName] as $type => $class) { |
52
|
|
|
$class = (new \ReflectionClass($class))->getShortName(); |
53
|
|
|
if (isset($request[$class])) { |
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} elseif (isset($request[$formName])) { |
58
|
|
|
return true; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function loadDifferentModelsFromPostRequest() |
66
|
|
|
{ |
67
|
|
|
/** @var Price[] $result */ |
68
|
|
|
$result = []; |
69
|
|
|
$request = Yii::$app->request->post(); |
70
|
|
|
|
71
|
|
|
$iter = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->priceModelFactory->getMap())); |
72
|
|
|
foreach ($iter as $className) { |
73
|
|
|
if (is_array($className)) { |
74
|
|
|
continue; |
75
|
|
|
} |
76
|
|
|
$formName = (new \ReflectionClass($className))->getShortName(); |
77
|
|
|
if (empty($request[$formName])) { |
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** @var Price[] $models */ |
82
|
|
|
$models = []; |
83
|
|
|
/** @var array $modelsData */ |
84
|
|
|
$modelsData = []; |
85
|
|
|
/** @var Price $modelPrototype */ |
86
|
|
|
$modelPrototype = $this->priceModelFactory->instantiate($className); |
87
|
|
|
$modelPrototype->setAttributes($this->modelOptions); |
88
|
|
|
$modelPrototype->scenario = $this->getScenario(); |
89
|
|
|
|
90
|
|
|
$data = $request[$formName]; |
91
|
|
|
foreach ($data as $key => $modelData) { |
92
|
|
|
$models[$key] = clone $modelPrototype; |
93
|
|
|
$modelsData[$modelPrototype->formName()][$key] = $modelData; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$modelPrototype->loadMultiple($models, $modelsData); |
97
|
|
|
$result = array_merge($result, $models); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $result; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|