|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Finance module for HiPanel |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
|
6
|
|
|
* @package hipanel-module-finance |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\collections; |
|
12
|
|
|
|
|
13
|
|
|
use Closure; |
|
14
|
|
|
use hipanel\modules\finance\models\factories\PriceModelFactory; |
|
15
|
|
|
use hipanel\modules\finance\models\Price; |
|
16
|
|
|
use hiqdev\hiart\Collection; |
|
17
|
|
|
use Yii; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class PricesCollection overrides loading behavior of parent class in order to: |
|
21
|
|
|
* make it possible to load all the models, specified in [[knownForms]] at once. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Dmytro Naumenko <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class PricesCollection extends Collection |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var \hipanel\modules\finance\models\factories\PriceModelFactory |
|
29
|
|
|
*/ |
|
30
|
|
|
private $priceModelFactory; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(PriceModelFactory $priceModelFactory, array $config = []) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct($config); |
|
35
|
|
|
$this->priceModelFactory = $priceModelFactory; |
|
36
|
|
|
// Prevent default behavior in Collection::collectData() in order to allow all attributes for different models |
|
37
|
|
|
$this->dataCollector = fn(Price $model) => [$model->getPrimaryKey(), $model->toArray()]; |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function load($data = null) |
|
44
|
|
|
{ |
|
45
|
|
|
if ($data === null && $this->dataToBeLoadedExistsInPostRequest()) { |
|
46
|
|
|
$data = $this->loadDifferentModelsFromPostRequest(); |
|
47
|
|
|
$this->checkConsistency = false; |
|
48
|
|
|
|
|
49
|
|
|
return $this->set($data); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return parent::load($data); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function collectData($attributes = null) |
|
59
|
|
|
{ |
|
60
|
|
|
$data = []; |
|
61
|
|
|
foreach ($this->models as $model) { |
|
62
|
|
|
if ($this->dataCollector instanceof Closure) { |
|
63
|
|
|
[$key, $row] = call_user_func($this->dataCollector, $model, $this); |
|
64
|
|
|
} else { |
|
65
|
|
|
$key = $model->getPrimaryKey(); |
|
66
|
|
|
$row = $model->getAttributes($this->isConsistent() ? $attributes : $model->activeAttributes()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($key) { |
|
70
|
|
|
$data[$key] = $row; |
|
71
|
|
|
} else { |
|
72
|
|
|
$data[] = $row; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $data; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function dataToBeLoadedExistsInPostRequest() |
|
80
|
|
|
{ |
|
81
|
|
|
$request = Yii::$app->request->post(); |
|
82
|
|
|
|
|
83
|
|
|
$map = $this->priceModelFactory->getMap(); |
|
84
|
|
|
foreach ($map as $formName => $className) { |
|
85
|
|
|
if (is_array($map[$formName])) { |
|
86
|
|
|
foreach ($map[$formName] as $type => $class) { |
|
87
|
|
|
$class = (new \ReflectionClass($class))->getShortName(); |
|
88
|
|
|
if (isset($request[$class])) { |
|
89
|
|
|
return true; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} elseif (isset($request[$formName])) { |
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function loadDifferentModelsFromPostRequest() |
|
101
|
|
|
{ |
|
102
|
|
|
/** @var Price[] $result */ |
|
103
|
|
|
$result = []; |
|
104
|
|
|
$request = Yii::$app->request->post(); |
|
105
|
|
|
$usedClasses = []; |
|
106
|
|
|
|
|
107
|
|
|
$iter = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->priceModelFactory->getMap())); |
|
108
|
|
|
foreach ($iter as $className) { |
|
109
|
|
|
if (is_array($className) || isset($usedClasses[$className])) { |
|
110
|
|
|
continue; |
|
111
|
|
|
} |
|
112
|
|
|
$formName = (new \ReflectionClass($className))->getShortName(); |
|
113
|
|
|
if (empty($request[$formName])) { |
|
114
|
|
|
continue; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$usedClasses[$className] = true; |
|
118
|
|
|
/** @var Price[] $models */ |
|
119
|
|
|
$models = []; |
|
120
|
|
|
/** @var array $modelsData */ |
|
121
|
|
|
$modelsData = []; |
|
122
|
|
|
/** @var Price $modelPrototype */ |
|
123
|
|
|
$modelPrototype = $this->priceModelFactory->instantiate($className); |
|
124
|
|
|
$modelPrototype->setAttributes($this->modelOptions); |
|
125
|
|
|
$modelPrototype->scenario = $this->getScenario(); |
|
126
|
|
|
|
|
127
|
|
|
$data = $request[$formName]; |
|
128
|
|
|
foreach ($data as $key => $modelData) { |
|
129
|
|
|
if (empty(array_filter($modelData))) { |
|
130
|
|
|
continue; |
|
131
|
|
|
} |
|
132
|
|
|
$models[$key] = clone $modelPrototype; |
|
133
|
|
|
$modelsData[$modelPrototype->formName()][$key] = $modelData; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$modelPrototype->loadMultiple($models, $modelsData); |
|
137
|
|
|
$result = array_merge($result, $models); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $result; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|