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
|
|
|
foreach ($this->priceModelFactory->getMap() as $formName => $className) { |
49
|
|
|
if (isset($request[$formName])) { |
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function loadDifferentModelsFromPostRequest() |
58
|
|
|
{ |
59
|
|
|
/** @var Price[] $result */ |
60
|
|
|
$result = []; |
61
|
|
|
$request = Yii::$app->request->post(); |
62
|
|
|
|
63
|
|
|
foreach ($this->priceModelFactory->getMap() as $formName => $className) { |
64
|
|
|
if (empty($request[$formName])) { |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @var Price[] $models */ |
69
|
|
|
$models = []; |
70
|
|
|
/** @var array $modelsData */ |
71
|
|
|
$modelsData = []; |
72
|
|
|
/** @var Price $modelPrototype */ |
73
|
|
|
$modelPrototype = $this->priceModelFactory->build($formName); |
74
|
|
|
$modelPrototype->setAttributes($this->modelOptions); |
75
|
|
|
|
76
|
|
|
$data = $request[$formName]; |
77
|
|
|
foreach ($data as $key => $modelData) { |
78
|
|
|
$models[$key] = clone $modelPrototype; |
79
|
|
|
$modelsData[$modelPrototype->formName()][$key] = $modelData; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$modelPrototype->loadMultiple($models, $modelsData); |
83
|
|
|
$result += $models; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $result; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|