|
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
|
|
|
* 'modificationBehavior' => array('class' => 'ModificationBehavior'),
|
|
9
|
|
|
*/
|
|
10
|
|
|
|
|
11
|
|
|
/**
|
|
12
|
|
|
* Class ModificationBehavior
|
|
13
|
|
|
* @property Product $owner
|
|
14
|
|
|
* @property Product[] $modifications
|
|
15
|
|
|
* @property Product $parentModel
|
|
16
|
|
|
*/
|
|
17
|
|
|
class ModificationBehavior extends SActiveRecordBehavior
|
|
18
|
|
|
{
|
|
19
|
|
|
private $child;
|
|
20
|
|
|
|
|
21
|
|
View Code Duplication |
public function init()
|
|
|
|
|
|
|
22
|
|
|
{
|
|
23
|
|
|
parent::init();
|
|
24
|
|
|
|
|
25
|
|
|
$this->owner->getMetaData()->addRelation('modifications', array(
|
|
26
|
|
|
FActiveRecord::HAS_MANY, 'Product', array('parent' => 'id'),
|
|
27
|
|
|
));
|
|
28
|
|
|
|
|
29
|
|
|
$this->owner->getMetaData()->addRelation('parentModel', array(
|
|
30
|
|
|
FActiveRecord::HAS_ONE, 'Product', array('id' => 'parent'),
|
|
31
|
|
|
));
|
|
32
|
|
|
}
|
|
33
|
|
|
|
|
34
|
|
|
/**
|
|
35
|
|
|
* @return bool
|
|
36
|
|
|
*/
|
|
37
|
|
|
public function isModification()
|
|
38
|
|
|
{
|
|
39
|
|
|
return !empty($this->owner->parent);
|
|
40
|
|
|
}
|
|
41
|
|
|
|
|
42
|
|
|
/**
|
|
43
|
|
|
* @return Product
|
|
44
|
|
|
*/
|
|
45
|
|
|
public function getParentModel()
|
|
46
|
|
|
{
|
|
47
|
|
|
return $this->owner->parentModel;
|
|
48
|
|
|
}
|
|
49
|
|
|
|
|
50
|
|
|
/**
|
|
51
|
|
|
* @return Product[]
|
|
52
|
|
|
*/
|
|
53
|
|
|
public function getModifications()
|
|
54
|
|
|
{
|
|
55
|
|
|
return $this->owner->modifications;
|
|
56
|
|
|
}
|
|
57
|
|
|
|
|
58
|
|
|
/**
|
|
59
|
|
|
* @param $id
|
|
60
|
|
|
*
|
|
61
|
|
|
* @return Product|null
|
|
62
|
|
|
*/
|
|
63
|
|
|
public function getChild($id)
|
|
64
|
|
|
{
|
|
65
|
|
|
if( !isset($this->child[$id]) )
|
|
66
|
|
|
$this->child[$id] = Product::model()->findByAttributes(array('id' => $id, 'visible' => 1));
|
|
67
|
|
|
|
|
68
|
|
|
return $this->child[$id];
|
|
69
|
|
|
}
|
|
70
|
|
|
|
|
71
|
|
|
/**
|
|
72
|
|
|
* @return bool
|
|
73
|
|
|
*/
|
|
74
|
|
|
public function isParent()
|
|
75
|
|
|
{
|
|
76
|
|
|
return count($this->getModifications()) > 0;
|
|
77
|
|
|
}
|
|
78
|
|
|
|
|
79
|
|
|
/**
|
|
80
|
|
|
* @return Product|null
|
|
81
|
|
|
*/
|
|
82
|
|
|
public function getFirstModification()
|
|
83
|
|
|
{
|
|
84
|
|
|
return Arr::reset($this->getModifications());
|
|
85
|
|
|
}
|
|
86
|
|
|
|
|
87
|
|
|
/**
|
|
88
|
|
|
* Если у модификации нет изображения, метод вернет изображение родителя
|
|
89
|
|
|
* @param string $type
|
|
90
|
|
|
*
|
|
91
|
|
|
* @return FActiveImage
|
|
92
|
|
|
*/
|
|
93
|
|
|
public function getImageModification($type = 'main')
|
|
94
|
|
|
{
|
|
95
|
|
|
if( $this->isModification() )
|
|
96
|
|
|
{
|
|
97
|
|
|
if( empty($this->owner->asa('imagesBehavior')->getImage($type)->name) )
|
|
98
|
|
|
{
|
|
99
|
|
|
return $this->getParentModel()->asa('imagesBehavior')->getImage($type);
|
|
100
|
|
|
}
|
|
101
|
|
|
}
|
|
102
|
|
View Code Duplication |
else if( $this->isParent() )
|
|
|
|
|
|
|
103
|
|
|
{
|
|
104
|
|
|
$modification = $this->getFirstModification();
|
|
105
|
|
|
if( !empty($modification->asa('imagesBehavior')->getImage($type)->name) )
|
|
106
|
|
|
return $modification->asa('imagesBehavior')->getImage($type);
|
|
107
|
|
|
}
|
|
108
|
|
|
|
|
109
|
|
|
return $this->owner->asa('imagesBehavior')->getImage($type);
|
|
110
|
|
|
}
|
|
111
|
|
|
|
|
112
|
|
|
/**
|
|
113
|
|
|
* @param string $type
|
|
114
|
|
|
*
|
|
115
|
|
|
* @return FActiveImage
|
|
116
|
|
|
*/
|
|
117
|
|
|
public function getImagesModification($type = 'main')
|
|
118
|
|
|
{
|
|
119
|
|
|
if( $this->isModification() )
|
|
120
|
|
|
{
|
|
121
|
|
|
$images = $this->owner->asa('imagesBehavior')->getImages($type);
|
|
122
|
|
|
if( empty($images) )
|
|
123
|
|
|
{
|
|
124
|
|
|
return $images;
|
|
125
|
|
|
}
|
|
126
|
|
|
}
|
|
127
|
|
View Code Duplication |
else if( $this->isParent() )
|
|
|
|
|
|
|
128
|
|
|
{
|
|
129
|
|
|
$modification = $this->getFirstModification();
|
|
130
|
|
|
$images = $modification->asa('imagesBehavior')->getImages($type);
|
|
131
|
|
|
if( !empty($images) )
|
|
132
|
|
|
return $images;
|
|
133
|
|
|
}
|
|
134
|
|
|
|
|
135
|
|
|
return $this->owner->asa('imagesBehavior')->getImages($type);
|
|
136
|
|
|
}
|
|
137
|
|
|
|
|
138
|
|
|
/**
|
|
139
|
|
|
* @param $property
|
|
140
|
|
|
*
|
|
141
|
|
|
* @return string
|
|
142
|
|
|
*/
|
|
143
|
|
|
public function getNotEmptyProperty($property)
|
|
144
|
|
|
{
|
|
145
|
|
|
if( $this->isModification() && empty($this->owner->{$property}) )
|
|
146
|
|
|
{
|
|
147
|
|
|
return $this->getParentModel()->{$property};
|
|
148
|
|
|
}
|
|
149
|
|
|
|
|
150
|
|
|
return $this->owner->{$property};
|
|
151
|
|
|
}
|
|
152
|
|
|
|
|
153
|
|
|
public function getModificationUrl($absolute = false)
|
|
154
|
|
|
{
|
|
155
|
|
|
|
|
156
|
|
|
//$this->getPa
|
|
157
|
|
|
return $absolute ? Yii::app()->createAbsoluteUrl('product/one', array('url' => $this->url)) : Yii::app()->createUrl('product/one', array('url' => $this->url));
|
|
158
|
|
|
}
|
|
159
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.