1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2015 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
*
|
8
|
|
|
* Пример:
|
9
|
|
|
* 'modificationBehavior' => array('class' => 'BModificationBehavior')
|
10
|
|
|
*/
|
11
|
|
|
/**
|
12
|
|
|
* Class BModificationBehavior
|
13
|
|
|
*
|
14
|
|
|
* @property BProduct $owner
|
15
|
|
|
* @property BProduct[] $modifications
|
16
|
|
|
* @property BProduct|null $parentModel
|
17
|
|
|
*/
|
18
|
|
|
class BModificationBehavior extends SActiveRecordBehavior
|
19
|
|
|
{
|
20
|
|
|
const SCENARIO_MODIFICATION = 'modification';
|
21
|
|
|
|
22
|
|
|
public $gridTableRowHtmlOptions = array('class' => 'modification');
|
23
|
|
|
|
24
|
|
|
public function init()
|
25
|
|
|
{
|
26
|
|
|
if( $this->owner->isNewRecord )
|
27
|
|
|
{
|
28
|
|
|
$this->owner->parent = Yii::app()->request->getParam('modificationParent');
|
29
|
|
|
}
|
30
|
|
|
|
31
|
|
|
$this->attachRelations();
|
32
|
|
|
|
33
|
|
|
$this->owner->attachEventHandler('onBeforeSearch', array($this, 'beforeSearch'));
|
34
|
|
|
$this->attachEventHandler('onAfterRenderTableRow', array($this, 'onAfterRenderTableRow'));
|
35
|
|
|
|
36
|
|
|
//to do: добавить параметр блокирующей отключение одного значения
|
37
|
|
|
$this->owner->attachBehavior('radioToggleBehavior', array(
|
38
|
|
|
'class' => 'RadioToggleBehavior',
|
39
|
|
|
'conditionAttribute' => 'parent',
|
40
|
|
|
'toggleAttribute' => 'default_modification'
|
41
|
|
|
));
|
42
|
|
|
|
43
|
|
|
$this->owner->enableBehavior('radioToggleBehavior');
|
44
|
|
|
}
|
45
|
|
|
|
46
|
|
|
public function beforeSearch(CEvent $event)
|
47
|
|
|
{
|
48
|
|
|
/**
|
49
|
|
|
* @var CDbCriteria $criteria
|
50
|
|
|
*/
|
51
|
|
|
$criteria = $event->params['criteria'];
|
52
|
|
|
|
53
|
|
|
$criteria->addCondition('t.parent IS NULL');
|
54
|
|
|
|
55
|
|
|
return $criteria;
|
56
|
|
|
}
|
57
|
|
|
|
58
|
|
|
public function beforeValidate($event)
|
59
|
|
|
{
|
60
|
|
|
if( $this->isModification() )
|
61
|
|
|
$this->owner->scenario = self::SCENARIO_MODIFICATION;
|
62
|
|
|
|
63
|
|
|
return parent::beforeValidate($event);
|
64
|
|
|
}
|
65
|
|
|
|
66
|
|
|
public function beforeSave($event)
|
67
|
|
|
{
|
68
|
|
|
if( $this->isModification() )
|
69
|
|
|
{
|
70
|
|
|
$this->owner->detachEventHandler('onAfterSave', array(Yii::app()->controller, 'saveProductAssignment'));
|
71
|
|
|
|
72
|
|
|
$model = BProductAssignment::model();
|
73
|
|
|
|
74
|
|
|
$assignments = array();
|
75
|
|
|
foreach($model->getFields() as $field)
|
76
|
|
|
{
|
77
|
|
|
$attribute = $field->name;
|
78
|
|
|
$assignments[$attribute] = $this->getParentModel()->{$attribute};
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
$model->saveAssignments($this->owner, $assignments);
|
82
|
|
|
}
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
/**
|
86
|
|
|
* @return BProduct
|
87
|
|
|
*/
|
88
|
|
|
public function getParentModel()
|
89
|
|
|
{
|
90
|
|
|
return $this->owner->parentModel;
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
/**
|
94
|
|
|
* @return bool
|
95
|
|
|
*/
|
96
|
|
|
public function isModification()
|
97
|
|
|
{
|
98
|
|
|
return !empty($this->owner->parent);
|
99
|
|
|
}
|
100
|
|
|
|
101
|
|
|
/**
|
102
|
|
|
* @return bool
|
103
|
|
|
*/
|
104
|
|
|
public function isParent()
|
105
|
|
|
{
|
106
|
|
|
return count($this->getModifications()) > 0;
|
107
|
|
|
}
|
108
|
|
|
|
109
|
|
|
/**
|
110
|
|
|
* @return Product[]
|
111
|
|
|
*/
|
112
|
|
|
public function getModifications()
|
113
|
|
|
{
|
114
|
|
|
return $this->owner->modifications;
|
115
|
|
|
}
|
116
|
|
|
|
117
|
|
|
public function getFacetProductIdList()
|
118
|
|
|
{
|
119
|
|
|
if( !$this->isParent() && !$this->isModification() )
|
120
|
|
|
return array($this->owner->id);
|
121
|
|
|
else if( $this->isParent() )
|
122
|
|
|
return CHtml::listData($this->getModifications(), 'id', 'id');
|
123
|
|
|
else if( $this->isModification() )
|
124
|
|
|
{
|
125
|
|
|
$parent = $this->getParentModel();
|
126
|
|
|
return CHtml::listData($parent->getModifications(), 'id', 'id');
|
127
|
|
|
}
|
128
|
|
|
}
|
129
|
|
|
|
130
|
|
|
public function getParentId()
|
131
|
|
|
{
|
132
|
|
|
if( $this->isParent() )
|
133
|
|
|
return $this->owner->id;
|
134
|
|
|
else if( $this->isModification() )
|
135
|
|
|
return $this->owner->getParentModel()->id;
|
136
|
|
|
|
137
|
|
|
return null;
|
138
|
|
|
}
|
139
|
|
|
|
140
|
|
|
private function attachRelations()
|
141
|
|
|
{
|
142
|
|
|
$this->owner->getMetaData()->addRelation('modifications', array(
|
143
|
|
|
BActiveRecord::HAS_MANY, 'BProduct', array('parent' => 'id'), 'order' => 'modifications.dump DESC, IF(modifications.price=0, 1, 0), modifications.price ASC'
|
144
|
|
|
));
|
145
|
|
|
|
146
|
|
|
$this->owner->getMetaData()->addRelation('parentModel', array(
|
147
|
|
|
BActiveRecord::HAS_ONE, 'BProduct', array('id' => 'parent'),
|
148
|
|
|
));
|
149
|
|
|
}
|
150
|
|
|
|
151
|
|
|
protected function onAfterRenderTableRow(CEvent $event)
|
152
|
|
|
{
|
153
|
|
|
if( empty($this->owner->modifications) || Yii::app()->controller->popup)
|
154
|
|
|
return;
|
155
|
|
|
|
156
|
|
|
/**
|
157
|
|
|
* @var BGridView $grid
|
158
|
|
|
*/
|
159
|
|
|
$grid = $event->sender;
|
160
|
|
|
$oldDataProvider = $grid->dataProvider;
|
161
|
|
|
$oldRowCssClassExpression = $grid->rowCssClassExpression;
|
162
|
|
|
|
163
|
|
|
$dataProvider = new CArrayDataProvider($this->owner->modifications, array('pagination' => false));
|
164
|
|
|
$grid->dataProvider = $dataProvider;
|
165
|
|
|
$grid->rowCssClassExpression = '"modification"';
|
166
|
|
|
|
167
|
|
|
foreach($grid->dataProvider->getData() as $row => $data)
|
168
|
|
|
{
|
169
|
|
|
$grid->renderTableRow($row);
|
170
|
|
|
}
|
171
|
|
|
|
172
|
|
|
$grid->dataProvider = $oldDataProvider;
|
173
|
|
|
$grid->rowCssClassExpression = $oldRowCssClassExpression;
|
174
|
|
|
}
|
175
|
|
|
} |