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
|
24 |
|
public function init()
|
25
|
|
|
{
|
26
|
24 |
|
if( $this->owner->isNewRecord )
|
27
|
24 |
|
{
|
28
|
13 |
|
$this->owner->parent = Yii::app()->request->getParam('modificationParent');
|
29
|
13 |
|
$this->owner->parentModel = BProduct::model()->findByPk($this->owner->parent);
|
30
|
13 |
|
}
|
31
|
|
|
|
32
|
24 |
|
$this->attachRelations();
|
33
|
|
|
|
34
|
24 |
|
$this->owner->attachEventHandler('onBeforeSearch', array($this, 'beforeSearch'));
|
35
|
24 |
|
$this->attachEventHandler('onAfterRenderTableRow', array($this, 'onAfterRenderTableRow'));
|
36
|
24 |
|
}
|
37
|
|
|
|
38
|
5 |
|
public function beforeSearch(CEvent $event)
|
39
|
|
|
{
|
40
|
|
|
/**
|
41
|
|
|
* @var CDbCriteria $criteria
|
42
|
|
|
*/
|
43
|
5 |
|
$criteria = $event->params['criteria'];
|
44
|
|
|
|
45
|
5 |
|
$criteria->addCondition('t.parent IS NULL');
|
46
|
|
|
|
47
|
5 |
|
return $criteria;
|
48
|
|
|
}
|
49
|
|
|
|
50
|
5 |
|
public function beforeValidate($event)
|
51
|
|
|
{
|
52
|
5 |
|
if( $this->isModification() )
|
53
|
5 |
|
$this->owner->scenario = self::SCENARIO_MODIFICATION;
|
54
|
|
|
|
55
|
5 |
|
return parent::beforeValidate($event);
|
56
|
|
|
}
|
57
|
|
|
|
58
|
4 |
|
public function beforeSave($event)
|
59
|
|
|
{
|
60
|
4 |
|
if( $this->isModification() )
|
61
|
4 |
|
{
|
62
|
|
|
$this->owner->detachEventHandler('onAfterSave', array(Yii::app()->controller, 'saveProductAssignment'));
|
63
|
|
|
|
64
|
|
|
$model = BProductAssignment::model();
|
65
|
|
|
|
66
|
|
|
$assignments = array();
|
67
|
|
|
foreach($model->getFields() as $field)
|
68
|
|
|
{
|
69
|
|
|
$attribute = $field->name;
|
70
|
|
|
$assignments[$attribute] = $this->getParentModel()->{$attribute};
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
$model->saveAssignments($this->owner, $assignments);
|
74
|
|
|
}
|
75
|
4 |
|
}
|
76
|
|
|
|
77
|
|
|
/**
|
78
|
|
|
* @return BProduct
|
79
|
|
|
*/
|
80
|
|
|
public function getParentModel()
|
81
|
|
|
{
|
82
|
|
|
return $this->owner->parentModel;
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
/**
|
86
|
|
|
* @return bool
|
87
|
|
|
*/
|
88
|
9 |
|
public function isModification()
|
89
|
|
|
{
|
90
|
9 |
|
return !empty($this->owner->parent);
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
/**
|
94
|
|
|
* @return bool
|
95
|
|
|
*/
|
96
|
4 |
|
public function isParent()
|
97
|
|
|
{
|
98
|
4 |
|
return count($this->getModifications()) > 0;
|
99
|
|
|
}
|
100
|
|
|
|
101
|
|
|
/**
|
102
|
|
|
* @return Product[]
|
103
|
|
|
*/
|
104
|
4 |
|
public function getModifications()
|
105
|
|
|
{
|
106
|
4 |
|
return $this->owner->modifications;
|
107
|
|
|
}
|
108
|
|
|
|
109
|
4 |
|
public function getFacetProductIdList()
|
110
|
|
|
{
|
111
|
4 |
|
if( !$this->isParent() && !$this->isModification() )
|
112
|
4 |
|
return array($this->owner->id);
|
113
|
|
|
else if( $this->isParent() )
|
114
|
|
|
return CHtml::listData($this->getModifications(), 'id', 'id');
|
115
|
|
|
else if( $this->isModification() )
|
116
|
|
|
{
|
117
|
|
|
$parent = $this->getParentModel();
|
118
|
|
|
return CHtml::listData($parent->getModifications(), 'id', 'id');
|
119
|
|
|
}
|
120
|
|
|
}
|
121
|
|
|
|
122
|
4 |
|
public function getParentId()
|
123
|
|
|
{
|
124
|
4 |
|
if( $this->isParent() )
|
125
|
4 |
|
return $this->owner->id;
|
126
|
4 |
|
else if( $this->isModification() )
|
127
|
4 |
|
return $this->owner->getParentModel()->id;
|
128
|
|
|
|
129
|
4 |
|
return null;
|
130
|
|
|
}
|
131
|
|
|
|
132
|
24 |
|
private function attachRelations()
|
133
|
|
|
{
|
134
|
24 |
|
$this->owner->getMetaData()->addRelation('modifications', array(
|
135
|
24 |
|
BActiveRecord::HAS_MANY, 'BProduct', array('parent' => 'id'), 'order' => 'modifications.dump DESC, IF(modifications.price=0, 1, 0), modifications.price ASC'
|
136
|
24 |
|
));
|
137
|
|
|
|
138
|
24 |
|
$this->owner->getMetaData()->addRelation('parentModel', array(
|
139
|
24 |
|
BActiveRecord::HAS_ONE, 'BProduct', array('id' => 'parent'),
|
140
|
24 |
|
));
|
141
|
24 |
|
}
|
142
|
|
|
|
143
|
|
|
protected function onAfterRenderTableRow(CEvent $event)
|
144
|
|
|
{
|
145
|
|
|
if( empty($this->owner->modifications) || Yii::app()->controller->popup)
|
146
|
|
|
return;
|
147
|
|
|
|
148
|
|
|
/**
|
149
|
|
|
* @var BGridView $grid
|
150
|
|
|
*/
|
151
|
|
|
$grid = $event->sender;
|
152
|
|
|
$oldDataProvider = $grid->dataProvider;
|
153
|
|
|
$oldRowCssClassExpression = $grid->rowCssClassExpression;
|
154
|
|
|
|
155
|
|
|
$dataProvider = new CArrayDataProvider($this->owner->modifications, array('pagination' => false));
|
156
|
|
|
$grid->dataProvider = $dataProvider;
|
157
|
|
|
$grid->rowCssClassExpression = '"modification"';
|
158
|
|
|
|
159
|
|
|
foreach($grid->dataProvider->getData() as $row => $data)
|
160
|
|
|
{
|
161
|
|
|
$grid->renderTableRow($row);
|
162
|
|
|
}
|
163
|
|
|
|
164
|
|
|
$grid->dataProvider = $oldDataProvider;
|
165
|
|
|
$grid->rowCssClassExpression = $oldRowCssClassExpression;
|
166
|
|
|
}
|
167
|
|
|
} |