1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Sergey Glagolev <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package frontend.models.behaviors
|
8
|
|
|
*/
|
9
|
|
|
|
10
|
|
|
/**
|
11
|
|
|
* Behavior realised methods for get/set/cache FActiveImage objects of FActiveRecord model
|
12
|
|
|
*
|
13
|
|
|
* Example:
|
14
|
|
|
*
|
15
|
|
|
* <pre>
|
16
|
|
|
* public function behaviors()
|
17
|
|
|
* {
|
18
|
|
|
* return array(
|
19
|
|
|
* 'imagesBehavior' => array('class' => 'ActiveImageBehavior', 'imageClass' => 'ProductImage'),
|
20
|
|
|
* );
|
21
|
|
|
* }
|
22
|
|
|
* </pre>
|
23
|
|
|
*
|
24
|
|
|
* Class ActiveImageBehavior
|
25
|
|
|
*
|
26
|
|
|
* @property FActiveRecord $owner
|
27
|
|
|
* @property FActiveImage image;
|
28
|
|
|
* @property FActiveImage images;
|
29
|
|
|
*/
|
30
|
|
|
class ActiveImageBehavior extends SBehavior
|
31
|
|
|
{
|
32
|
|
|
public $imageClass;
|
33
|
|
|
|
34
|
|
|
private $images;
|
35
|
|
|
|
36
|
38 |
|
public function init()
|
37
|
|
|
{
|
38
|
38 |
|
if( !isset($this->imageClass) )
|
39
|
38 |
|
{
|
40
|
|
|
throw new CException('Can not attach ActiveImageBehavior without imageClass property');
|
41
|
|
|
}
|
42
|
38 |
|
}
|
43
|
|
|
|
44
|
|
|
/**
|
45
|
|
|
* @param string $type
|
46
|
|
|
*
|
47
|
|
|
* @return FActiveImage
|
48
|
|
|
*/
|
49
|
|
|
public function getImage($type = 'main')
|
50
|
|
|
{
|
51
|
|
|
if( $image = Arr::reset($this->getImages($type)) )
|
52
|
|
|
return $image;
|
53
|
|
|
|
54
|
|
|
return Yii::createComponent(['class' => $this->imageClass]);
|
55
|
|
|
}
|
56
|
|
|
|
57
|
|
|
/**
|
58
|
|
|
* @param string $type
|
59
|
|
|
*
|
60
|
|
|
* @return FActiveImage[]
|
61
|
|
|
*/
|
62
|
|
|
public function getImages($type = 'main')
|
63
|
|
|
{
|
64
|
|
|
if( !isset($this->images) )
|
65
|
|
|
{
|
66
|
|
|
$criteria = $this->getColorCriteria();
|
67
|
|
|
$criteria->compare('parent', $this->owner->getPrimaryKey());
|
68
|
|
|
/**
|
69
|
|
|
* @var FActiveRecord $model
|
70
|
|
|
*/
|
71
|
|
|
$model = new $this->imageClass;
|
72
|
|
|
$images = $model->findAll($criteria);
|
73
|
|
|
|
74
|
|
|
$this->setImages($images, $type);
|
75
|
|
|
}
|
76
|
|
|
|
77
|
|
|
return isset($this->images[$type]) ? $this->images[$type] : array();
|
78
|
|
|
}
|
79
|
|
|
|
80
|
|
|
/**
|
81
|
|
|
* @param array $images
|
82
|
|
|
* @param string $type
|
83
|
|
|
*
|
84
|
|
|
* @return void
|
85
|
|
|
*/
|
86
|
2 |
|
public function setImages($images, $type)
|
87
|
|
|
{
|
88
|
2 |
|
if( !isset($this->images) )
|
89
|
2 |
|
$this->images = array();
|
90
|
|
|
|
91
|
2 |
|
if( !isset($this->images[$type]) )
|
92
|
2 |
|
$this->images[$type] = array();
|
93
|
|
|
|
94
|
2 |
|
foreach($images as $image)
|
95
|
2 |
|
$this->images[$image['type']][] = $image;
|
96
|
2 |
|
}
|
97
|
|
|
|
98
|
|
|
/**
|
99
|
|
|
* ��������� ������ �����������
|
100
|
|
|
*
|
101
|
|
|
* @param FActiveImage|null $excludeImage - ������ �����������
|
102
|
|
|
* @param array $types - �������� ���
|
103
|
|
|
*
|
104
|
|
|
* @return FActiveImage[]
|
105
|
|
|
*/
|
106
|
|
|
public function getImagesGallery(FActiveImage $excludeImage = null, $types = array('main', 'gallery'))
|
107
|
|
|
{
|
108
|
|
|
$galleryImages = array();
|
109
|
|
|
|
110
|
|
|
foreach($types as $type)
|
111
|
|
|
{
|
112
|
|
|
foreach($this->getImages($type) as $image)
|
113
|
|
|
{
|
114
|
|
|
if( $image->id != $excludeImage->id )
|
115
|
|
|
$galleryImages[] = $image;
|
116
|
|
|
}
|
117
|
|
|
}
|
118
|
|
|
|
119
|
|
|
return $galleryImages;
|
120
|
|
|
}
|
121
|
|
|
|
122
|
2 |
|
public function getColorCriteria()
|
123
|
|
|
{
|
124
|
2 |
|
$criteria = new CDbCriteria();
|
125
|
2 |
|
$criteria->order = 'IF(position = 0, 1, 0), position';
|
126
|
|
|
|
127
|
2 |
|
if( !Yii::app()->controller->asa('productFilterBehavior') )
|
128
|
2 |
|
return $criteria;
|
129
|
|
|
|
130
|
|
|
/**
|
131
|
|
|
* @var ProductFilterBehavior $controller
|
132
|
|
|
*/
|
133
|
|
|
$controller = Yii::app()->controller;
|
134
|
|
|
if( $filter = $controller->getFilter(false) )
|
135
|
|
|
{
|
136
|
|
|
if( $color = $filter->getElementByKey(ProductFilterBehavior::FILTER_COLOR) )
|
137
|
|
|
{
|
138
|
|
|
/**
|
139
|
|
|
* @var FilterElementItem $selectedItem
|
140
|
|
|
*/
|
141
|
|
|
if( $selectedItem = Arr::reset($color->getSelectedItems()) )
|
142
|
|
|
{
|
143
|
|
|
$criteria->order = 'IF(notice = '.intval($selectedItem->id).', 0, 1), IF(position = 0, 1, 0), position';
|
144
|
|
|
}
|
145
|
|
|
}
|
146
|
|
|
}
|
147
|
|
|
|
148
|
|
|
return $criteria;
|
149
|
|
|
}
|
150
|
|
|
} |