1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package frontend.components.collection
|
8
|
|
|
*
|
9
|
|
|
* @property FActiveRecord $owner
|
10
|
|
|
* @property integer $sum
|
11
|
|
|
* @property integer $collectionAmount
|
12
|
|
|
* @property integer $collectionIndex
|
13
|
|
|
* @property integer $collectionItems
|
14
|
|
|
*/
|
15
|
|
|
class FCollectionElementBehavior extends CBehavior
|
16
|
|
|
{
|
17
|
|
|
/**
|
18
|
|
|
* @var FCollectionElement
|
19
|
|
|
*/
|
20
|
|
|
protected $collectionElement;
|
21
|
|
|
|
22
|
|
|
/**
|
23
|
|
|
* @param string|null $index
|
24
|
|
|
*
|
25
|
|
|
* @return FCollection|null
|
26
|
|
|
*/
|
27
|
|
|
public function getCollectionItems($index = null)
|
28
|
|
|
{
|
29
|
|
|
if( is_null($this->collectionElement) )
|
30
|
|
|
return null;
|
31
|
|
|
|
32
|
|
|
if( is_null($index) )
|
33
|
|
|
return $this->collectionElement->items;
|
34
|
|
|
else
|
35
|
|
|
{
|
36
|
|
|
return !is_null($this->collectionElement->items) && isset($this->collectionElement->items[$index]) ? $this->collectionElement->items[$index] : null;
|
37
|
|
|
}
|
38
|
|
|
}
|
39
|
|
|
|
40
|
|
|
public function getSum()
|
41
|
|
|
{
|
42
|
|
|
try
|
43
|
|
|
{
|
44
|
|
|
$price = $this->owner->getPrice();
|
45
|
|
|
}
|
46
|
|
|
catch(Exception $e)
|
47
|
|
|
{
|
48
|
|
|
$price = $this->owner->price;
|
49
|
|
|
}
|
50
|
|
|
|
51
|
|
|
return $price * $this->collectionAmount;
|
52
|
|
|
}
|
53
|
|
|
|
54
|
|
|
public function getSumTotal()
|
55
|
|
|
{
|
56
|
|
|
$price = 0;
|
57
|
|
|
|
58
|
|
|
$collectionItemsForSum = $this->collectionElement->collectionParent->root->collectionItemsForSum;
|
59
|
|
|
|
60
|
|
|
if( $collectionItemsForSum == FCollection::COLLECTION_ITEMS_ROOT )
|
61
|
|
|
{
|
62
|
|
|
$price += $this->getCollectionItemSum();
|
63
|
|
|
}
|
64
|
|
|
else if( is_array($collectionItemsForSum) )
|
65
|
|
|
{
|
66
|
|
|
foreach($collectionItemsForSum as $key)
|
67
|
|
|
{
|
68
|
|
|
$price += $this->getCollectionItemSum($key);
|
69
|
|
|
}
|
70
|
|
|
}
|
71
|
|
|
|
72
|
|
|
return $this->owner->getSum() + $price * $this->collectionAmount;
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
public function getCollectionItemSum($index = null)
|
76
|
|
|
{
|
77
|
|
|
$sum = 0;
|
78
|
|
|
|
79
|
|
|
if( $items = $this->getCollectionItems($index) )
|
80
|
|
|
{
|
81
|
|
|
if( !($items instanceof FCollection) )
|
82
|
|
|
$items = array($items);
|
83
|
|
|
|
84
|
|
|
foreach($items as $item)
|
85
|
|
|
{
|
86
|
|
|
if( $item instanceof FCollection )
|
87
|
|
|
{
|
88
|
|
|
foreach($item as $innerItem)
|
89
|
|
|
$sum += $innerItem->getSum();
|
90
|
|
|
}
|
91
|
|
|
else if( !empty($item) )
|
92
|
|
|
{
|
93
|
|
|
$sum += $item->getSum();
|
94
|
|
|
}
|
95
|
|
|
}
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
return $sum;
|
99
|
|
|
}
|
100
|
|
|
|
101
|
|
|
/**
|
102
|
|
|
* @param $index
|
103
|
|
|
* @param $key
|
104
|
|
|
* @param $value
|
105
|
|
|
*
|
106
|
|
|
* @return array
|
107
|
|
|
*/
|
108
|
|
|
public function collectionItemsListData($index, $key, $value)
|
109
|
|
|
{
|
110
|
|
|
if( $items = $this->getCollectionItems($index) )
|
111
|
|
|
return CHtml::listData($items, $key, $value);
|
112
|
|
|
|
113
|
|
|
return array();
|
114
|
|
|
}
|
115
|
|
|
|
116
|
|
|
/**
|
117
|
|
|
* Метод вызывается после создания обекта колекции
|
118
|
|
|
*/
|
119
|
|
|
public function afterCreateCollection()
|
120
|
|
|
{
|
121
|
|
|
|
122
|
|
|
}
|
123
|
|
|
|
124
|
|
|
/**
|
125
|
|
|
* @return array
|
126
|
|
|
*/
|
127
|
|
|
public function defaultCollectionItems()
|
128
|
|
|
{
|
129
|
|
|
return array();
|
130
|
|
|
}
|
131
|
|
|
|
132
|
|
|
/**
|
133
|
|
|
* Внутренние параметры заказа, не проходящие через коллекцию (те которые нальзя изменять)
|
134
|
|
|
*
|
135
|
|
|
* @return array|FCollectionCustomParameter[]
|
136
|
|
|
*/
|
137
|
|
|
public function innerCollectionItems()
|
138
|
|
|
{
|
139
|
|
|
return array();
|
140
|
|
|
}
|
141
|
|
|
|
142
|
|
|
public function toArray()
|
143
|
|
|
{
|
144
|
|
|
return array(
|
145
|
|
|
'id' => $this->owner->primaryKey,
|
146
|
|
|
'type' => Utils::toSnakeCase(get_class($this->owner))
|
147
|
|
|
);
|
148
|
|
|
}
|
149
|
|
|
|
150
|
|
|
public function getOrderItemType()
|
151
|
|
|
{
|
152
|
|
|
return get_class($this->owner);
|
153
|
|
|
}
|
154
|
|
|
|
155
|
|
|
public function getPrimaryKey()
|
156
|
|
|
{
|
157
|
|
|
return $this->owner->getPrimaryKey();
|
158
|
|
|
}
|
159
|
|
|
|
160
|
|
|
public function getOrderItemName()
|
161
|
|
|
{
|
162
|
|
|
return 'Не задано';
|
163
|
|
|
}
|
164
|
|
|
|
165
|
|
|
public function getOrderItemValue()
|
166
|
|
|
{
|
167
|
|
|
return (string)$this->owner;
|
168
|
|
|
}
|
169
|
|
|
|
170
|
|
|
public function getOrderItemAmount()
|
171
|
|
|
{
|
172
|
|
|
return 0;
|
173
|
|
|
}
|
174
|
|
|
|
175
|
|
|
public function getOrderItemPrice()
|
176
|
|
|
{
|
177
|
|
|
return 0;
|
178
|
|
|
}
|
179
|
|
|
|
180
|
|
|
protected function getCollectionIndex()
|
181
|
|
|
{
|
182
|
|
|
return isset($this->collectionElement) ? $this->collectionElement->index : null;
|
183
|
|
|
}
|
184
|
|
|
|
185
|
|
|
protected function getCollectionAmount()
|
186
|
|
|
{
|
187
|
|
|
return isset($this->collectionElement) ? $this->collectionElement->amount : 1;
|
188
|
|
|
}
|
189
|
|
|
|
190
|
|
|
protected function setCollectionElement($link)
|
191
|
|
|
{
|
192
|
|
|
$this->collectionElement = $link;
|
193
|
|
|
}
|
194
|
|
|
|
195
|
|
|
protected function getCollectionElement()
|
196
|
|
|
{
|
197
|
|
|
return $this->collectionElement;
|
198
|
|
|
}
|
199
|
|
|
} |